///////////////////////////////////////// // // GameScreen // var GameScreen = function(idScreen, funcInit, funcProc, funcEnd, tps){ this.Screen = document.getElementById(idScreen); this.Ctx = this.Screen.getContext('2d'); this.Size = {X: this.Screen.width, Y: this.Screen.height}; this.Entities = []; this.NewEntities = []; this.Running = false; this.FuncInit = funcInit; this.FuncProc = funcProc; this.FuncEnd = funcEnd; this.TPS = tps || 10; this.TickTime = 1000/this.TPS; this.AccTickTime = this.TickTime; this.PreviousTime = 0; this.Mouse = new Mouse(this.Screen, this.Size); var self = this; this.Tick = function(){ while(self.AccTickTime>=self.TickTime){ self.Update(); if(self.FuncProc){ self.FuncProc(self); } self.Mouse.Update(); self.CleanDead(); self.InsertAdded(); self.AccTickTime -= self.TickTime; } self.Draw(self.AccTickTime/self.TickTime); var timeNow = performance.now(); self.AccTickTime += timeNow - self.PreviousTime; self.PreviousTime = timeNow; if(self.Running){ window.requestAnimationFrame(self.Tick); }else{ if(self.FuncEnd){ self.FuncEnd(self); } } } }; GameScreen.prototype = { // For internal use CleanDead: function(){ var i = this.Entities.length-1; while(i>0){ if(this.Entities[i].GameEntity.Deleted){ this.Entities.splice(i,1); } i--; } }, InsertAdded: function(){ for(var i=0,n=this.NewEntities.length;i (this.PositionDest.X - (this.Size.X/2)) && point.Y < (this.PositionDest.Y + (this.Size.Y/2)) && point.Y > (this.PositionDest.Y - (this.Size.Y/2)) && true ); }, Debug: false }; ///////////////////////////////////////// // // Mouse // var Mouse = function(screen, size){ this.Screen = screen; this.Size = size || {X: screen.width, Y: screen.height}; this.Down = false; this.StartPosition = {X: 0, Y: 0}; this.EndPosition = {X: 0, Y: 0}; this.Screen.addEventListener("mousedown", this.OnMouseDown.bind(this), false); this.Screen.addEventListener("mousemove", this.OnMouseMove.bind(this), false); this.Screen.addEventListener("mouseup", this.OnMouseUp.bind(this), false); this.Screen.addEventListener("mouseleave", this.OnMouseLeave.bind(this), false); this.Screen.addEventListener("touchstart", this.OnTouchStart.bind(this), false); this.Screen.addEventListener("touchmove", this.OnTouchMove.bind(this), false); this.Screen.addEventListener("touchend", this.OnTouchEnd.bind(this), false); this.Screen.addEventListener("touchcancel", this.OnTouchEnd.bind(this), false); }; Mouse.prototype = { GetEventPoistion: function(positionEvent){ var position = {X: positionEvent.X, Y: positionEvent.Y}; var element = this.Screen; while(element){ position.X -= element.offsetLeft; position.Y -= element.offsetTop; element = element.offsetParent; } position.X = (position.X / this.Screen.offsetWidth) * this.Size.X; position.Y = (position.Y / this.Screen.offsetHeight) * this.Size.Y; return position; }, OnMouseDown: function(event){ var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY}); this.RealDown = true; this.Down = true; this.StartPosition.X = position.X; this.StartPosition.Y = position.Y; this.EndPosition.X = position.X; this.EndPosition.Y = position.Y; }, OnMouseMove: function(event){ if(this.RealDown == false){ return; } var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY}); this.RealDown = true; this.Down = true; this.EndPosition.X = position.X; this.EndPosition.Y = position.Y; }, OnMouseUp: function(event){ var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY}); this.RealDown = false; this.EndPosition.X = position.X; this.EndPosition.Y = position.Y; }, OnMouseLeave: function(){ this.RealDown = false; this.Down = false; }, OnTouchStart: function(event){ var position = this.GetEventPoistion({X: event.touches[0].clientX, Y: event.touches[0].clientY}); this.RealDown = true; this.Down = true; this.StartPosition.X = position.X; this.StartPosition.Y = position.Y; this.EndPosition.X = position.X; this.EndPosition.Y = position.Y; }, OnTouchMove: function(event){ if(this.RealDown == false){ return; } var position = this.GetEventPoistion({X: event.touches[0].clientX, Y: event.touches[0].clientY}); this.RealDown = true; this.Down = true; this.EndPosition.X = position.X; this.EndPosition.Y = position.Y; }, OnTouchEnd: function(event){ this.RealDown = false; }, Update: function(){ if(this.RealDown == false){ this.Down = false; } }, Cancel: function(){ this.RealDown = false; this.Down = false; }, Debug: false }; ///////////////////////////////////////// // // ImageLoader // var ImageLoader = function(imageList, funcOnLoad){ this.Images = {}; }; ImageLoader.prototype = { IsImageOk: function(img){ if (!img.complete) { return false; } if (typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0) { return false; } return true; }, LoadImages: function(imageList, funcOnLoad){ this.ImageCount = imageList.length; this.FuncOnLoad = funcOnLoad; var i,n; for(i=0,n=imageList.length;i