///////////////////////////////////////// // // Vec2D // var Vec2D = { Scale: function(vecIn, scale) { return { X: vecIn.X * scale, Y: vecIn.Y * scale }; }, Normalize: function(vecIn){ var len = Math.sqrt( (vecIn.X * vecIn.X) + (vecIn.Y * vecIn.Y)); return { X: vecIn.X / len, Y: vecIn.Y / len }; } }; ///////////////////////////////////////// // // 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 ); }, InsideScreen: function(){ return ( (this.Position.X+(this.Size.X/2))>0 && (this.Position.Y+(this.Size.Y/2))>0 && (this.Position.X-(this.Size.X/2))