game_manager.js 3.6 KB

1
  1. function GameManager(e,t,n,r){this.size=e,this.inputManager=new t,this.storageManager=new r,this.actuator=new n,this.startTiles=2,this.inputManager.on("move",this.move.bind(this)),this.inputManager.on("restart",this.restart.bind(this)),this.inputManager.on("keepPlaying",this.keepPlaying.bind(this)),this.setup()}GameManager.prototype.restart=function(){this.storageManager.clearGameState(),this.actuator.continueGame(),this.setup()},GameManager.prototype.keepPlaying=function(){this.keepPlaying=!0,this.actuator.continueGame()},GameManager.prototype.isGameTerminated=function(){return this.over||this.won&&!this.keepPlaying?!0:!1},GameManager.prototype.setup=function(){var e=this.storageManager.getGameState();e?(this.grid=new Grid(e.grid.size,e.grid.cells),this.score=e.score,this.over=e.over,this.won=e.won,this.keepPlaying=e.keepPlaying):(this.grid=new Grid(this.size),this.score=0,this.over=!1,this.won=!1,this.keepPlaying=!1,this.addStartTiles()),this.actuate()},GameManager.prototype.addStartTiles=function(){for(var e=0;e<this.startTiles;e++)this.addRandomTile()},GameManager.prototype.addRandomTile=function(){if(this.grid.cellsAvailable()){var e=Math.random()<.9?2:4,t=new Tile(this.grid.randomAvailableCell(),e);this.grid.insertTile(t)}},GameManager.prototype.actuate=function(){this.storageManager.getBestScore()<this.score&&this.storageManager.setBestScore(this.score),this.over||this.won?this.storageManager.clearGameState():this.storageManager.setGameState(this.serialize()),this.actuator.actuate(this.grid,{score:this.score,over:this.over,won:this.won,bestScore:this.storageManager.getBestScore(),terminated:this.isGameTerminated()})},GameManager.prototype.serialize=function(){return{grid:this.grid.serialize(),score:this.score,over:this.over,won:this.won,keepPlaying:this.keepPlaying}},GameManager.prototype.prepareTiles=function(){this.grid.eachCell(function(e,t,n){n&&(n.mergedFrom=null,n.savePosition())})},GameManager.prototype.moveTile=function(e,t){this.grid.cells[e.x][e.y]=null,this.grid.cells[t.x][t.y]=e,e.updatePosition(t)},GameManager.prototype.move=function(e){var t=this;if(this.isGameTerminated())return;var n,r,i=this.getVector(e),s=this.buildTraversals(i),o=!1;this.prepareTiles(),s.x.forEach(function(e){s.y.forEach(function(s){n={x:e,y:s},r=t.grid.cellContent(n);if(r){var u=t.findFarthestPosition(n,i),a=t.grid.cellContent(u.next);if(a&&a.value===r.value&&!a.mergedFrom){var f=new Tile(u.next,r.value*2);f.mergedFrom=[r,a],t.grid.insertTile(f),t.grid.removeTile(r),r.updatePosition(u.next),t.score+=f.value,f.value==256&&(t.won=!0)}else t.moveTile(r,u.farthest);t.positionsEqual(n,r)||(o=!0)}})}),o&&(this.addRandomTile(),this.movesAvailable()||(this.over=!0),this.actuate())},GameManager.prototype.getVector=function(e){var t={0:{x:0,y:-1},1:{x:1,y:0},2:{x:0,y:1},3:{x:-1,y:0}};return t[e]},GameManager.prototype.buildTraversals=function(e){var t={x:[],y:[]};for(var n=0;n<this.size;n++)t.x.push(n),t.y.push(n);return e.x===1&&(t.x=t.x.reverse()),e.y===1&&(t.y=t.y.reverse()),t},GameManager.prototype.findFarthestPosition=function(e,t){var n;do n=e,e={x:n.x+t.x,y:n.y+t.y};while(this.grid.withinBounds(e)&&this.grid.cellAvailable(e));return{farthest:n,next:e}},GameManager.prototype.movesAvailable=function(){return this.grid.cellsAvailable()||this.tileMatchesAvailable()},GameManager.prototype.tileMatchesAvailable=function(){var e=this,t;for(var n=0;n<this.size;n++)for(var r=0;r<this.size;r++){t=this.grid.cellContent({x:n,y:r});if(t)for(var i=0;i<4;i++){var s=e.getVector(i),o={x:n+s.x,y:r+s.y},u=e.grid.cellContent(o);if(u&&u.value===t.value)return!0}}return!1},GameManager.prototype.positionsEqual=function(e,t){return e.x===t.x&&e.y===t.y}