updated 内部属性修改

This commit is contained in:
郑浩乐 2016-01-05 18:00:22 +08:00
parent 0634570c60
commit 7e8d45f058
2 changed files with 30 additions and 15 deletions

29
game.js
View File

@ -28,15 +28,16 @@ function Game(id,options){
y:0, //纵坐标 y:0, //纵坐标
width:20, //宽 width:20, //宽
height:20, //高 height:20, //高
type:1, //对象类型 type:0, //对象类型,0表示普通对象(不与地图绑定),1表示玩家控制对象,2表示程序控制对象
status:1, //对象状态,1表示正常,0表示隐藏 status:1, //对象状态,1表示正常,0表示隐藏
orientation:0, //当前定位方向,0表示上,1表示右,2表示下,3表示左 orientation:0, //当前定位方向,0表示上,1表示右,2表示下,3表示左
vector:{ //目标坐标 vector:{}, //目标坐标
x:0, coord:{}, //如果对象与地图绑定,获得坐标值
y:0
},
speed:1, //速度等级,内部计算器times多少帧变化一次 speed:1, //速度等级,内部计算器times多少帧变化一次
control:{}, //控制缓存,到达定位点时处理 control:{}, //控制缓存,到达定位点时处理
path:[], //NPC自动行走的路径
index:0, //对象索引
stage:null, //绑定对象与所属布景绑定
update:function(){}, //更新参数信息 update:function(){}, //更新参数信息
draw:function(){} //绘制 draw:function(){} //绘制
}; };
@ -107,7 +108,7 @@ function Game(id,options){
//寻址算法 //寻址算法
Map.prototype.finder = function(param){ Map.prototype.finder = function(param){
var defaults = { var defaults = {
map :this.data, map:this.data,
start:[0,0], start:[0,0],
end:[0,0] end:[0,0]
}; };
@ -131,7 +132,7 @@ function Game(id,options){
steps[x][y] = 0; steps[x][y] = 0;
} }
} }
(function(list){ var _render = function(list){
var new_list = []; var new_list = [];
var next = function(from,to){ var next = function(from,to){
var x = to[0],y = to[1]; var x = to[0],y = to[1];
@ -154,9 +155,10 @@ function Game(id,options){
next(current,[x,y-1]); next(current,[x,y-1]);
} }
if(!finded&&new_list.length){ if(!finded&&new_list.length){
arguments.callee(new_list); _render(new_list);
} }
})([options.start]); };
_render([options.start]);
if(finded){ if(finded){
var current=options.end; var current=options.end;
while(current[0]!=options.start[0]||current[1]!=options.start[1]){ while(current[0]!=options.start[0]||current[1]!=options.start[1]){
@ -198,6 +200,9 @@ function Game(id,options){
if(!(f%item.speed)){ if(!(f%item.speed)){
item.frames = f/item.speed; //计数器 item.frames = f/item.speed; //计数器
} }
if(stage.map&&item.type){
item.coord = stage.map.position2coord(item.x,item.y);
}
item.update(); item.update();
} }
item.draw(_context); item.draw(_context);
@ -216,9 +221,9 @@ function Game(id,options){
//添加对象 //添加对象
Stage.prototype.createItem = function(options){ Stage.prototype.createItem = function(options){
var item = new Item(options); var item = new Item(options);
//对象动态属性 //动态属性
item.stage = this; //绑定对象与所属布景绑定 item.stage = this;
item.index = this.items.length; //对象层级 item.index = this.items.length;
this.items.push(item); this.items.push(item);
return item; return item;
}; };

View File

@ -218,10 +218,11 @@
y:pos.y, y:pos.y,
width:30, width:30,
height:30, height:30,
type:1,
orientation:3, orientation:3,
speed:10, speed:10,
update:function(){ update:function(){
var coord = map.position2coord(this.x,this.y); var coord = this.coord;
var steps = 2; var steps = 2;
if(!coord.offset){ if(!coord.offset){
if(typeof this.control.orientation!='undefined'){ if(typeof this.control.orientation!='undefined'){
@ -338,11 +339,20 @@
context.fill(); context.fill();
} }
}); });
var pos = map.coord2position(12,14);
stage.createItem({ stage.createItem({
x:650, x:pos.x,
y:100, y:pos.y,
width:30, width:30,
height:30, height:30,
type:2,
speed:10,
update:function(){
this.path = map.finder({
start:[this.coord.y,this.coord.x],
end:[player.coord.y,player.coord.x]
});
},
draw:function(context){ draw:function(context){
context.fillStyle = '#F00'; context.fillStyle = '#F00';
context.beginPath(); context.beginPath();