diff --git a/code/GameLib.js b/code/GameLib.js index 036babe..e762931 100644 --- a/code/GameLib.js +++ b/code/GameLib.js @@ -3,7 +3,7 @@ // // GameScreen // -var GameScreen = function(idScreen, funcInit, funcProc, funcEnd){ +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}; @@ -13,16 +13,29 @@ var GameScreen = function(idScreen, funcInit, funcProc, funcEnd){ 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; var self = this; this.Tick = function(){ - if(self.FuncProc){ - self.FuncProc(self); + while(self.AccTickTime>=self.TickTime){ + self.Update(); + if(self.FuncProc){ + self.FuncProc(self); + } + self.CleanDead(); + self.InsertAdded(); + self.AccTickTime -= self.TickTime; } - self.CleanDead(); - self.InsertAdded(); - self.Update(); - self.Draw(); + 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{ @@ -60,12 +73,12 @@ GameScreen.prototype = { } } }, - Draw: function(){ + Draw: function(factor){ this.Ctx.clearRect(0, 0, this.Size.X, this.Size.Y); for(var i=0,n=this.Entities.length;i