'use strict'; /* * 小型游戏引擎 */ function Game(id,options){ var _ = this; options = options||{}; var settings = { width:960, //画布宽度 height:640 //画布高度 }; for(var i in settings){ this[i] = options[i]||settings[i]; } var $canvas = document.getElementById(id); $canvas.width = _.width; $canvas.height = _.height; var _context = $canvas.getContext('2d'); //画布上下文环境 var _stages = []; //布景对象队列 var _events = {}; //事件集合 var _index, //当前布景索引 _hander; //帧动画控制 //活动对象构造 var Item = function(options){ options = options||{}; var settings = { x:0, //位置坐标:横坐标 y:0, //位置坐标:纵坐标 width:20, //宽 height:20, //高 type:0, //对象类型,0表示普通对象(不与地图绑定),1表示玩家控制对象,2表示程序控制对象 color:'#F00', //标识颜色 status:1, //对象状态,0表示隐藏,1表示正常,2表示暂停 orientation:0, //当前定位方向,0表示右,1表示下,2表示左,3表示上 speed:0, //移动速度 //地图相关 location:null, //定位地图,Map对象 coord:null, //如果对象与地图绑定,需设置地图坐标;若不绑定,则设置位置坐标 path:[], //NPC自动行走的路径 vector:null, //目标坐标 //布局相关 stage:null, //绑定对象与所属布景绑定 index:0, //对象索引 frames:1, //速度等级,内部计算器times多少帧变化一次 times:0, //刷新画布计数(用于循环动画状态判断) timeout:0, //倒计时(用于过程动画状态判断) control:{}, //控制缓存,到达定位点时处理 update:function(){}, //更新参数信息 draw:function(){} //绘制 }; for(var i in settings){ this[i] = options[i]||settings[i]; } }; Item.prototype.bind = function(eventType,callback){ if(!_events[eventType]){ _events[eventType] = {}; $canvas.addEventListener(eventType,function(e){ var position = _.getPosition(e); _stages[_index].items.forEach(function(item){ if(Math.abs(position.x-item.x)