Guest User

Untitled

a guest
Jan 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = {};
  2.  
  3. var game = {
  4.  
  5.   //Current level played
  6.   gameLevel : 0,
  7.  
  8.   enemies : [],
  9.   enemiesToCome : 1,
  10.   secondsBetweenEnemies : 0,
  11.   timeLastEnemy : Math.round(new Date().getTime() / 1000),
  12.  
  13.   timeLastFrame : Math.round(new Date().getTime() / 1000),
  14.  
  15.   tileSize : 32,
  16.  
  17.   init : function () {
  18.     this.canvasDOMelm = document.getElementById('gameCanvas');
  19.     canvas = new CanvasTools(this.canvasDOMelm.getContext('2d'), this.canvasDOMelm.width, this.canvasDOMelm.height);
  20.    
  21.     this.start();
  22.   },
  23.  
  24.   start : function () {
  25.     this.map = maps[this.gameLevel].map;
  26.     this.mapParser = new MapParser(this.gameLevel, this.tileSize);
  27.     this.mapParser.doParse();
  28.  
  29.     this.loop();
  30.   },
  31.  
  32.   loop : function () {
  33.     if(!this.gameOver) {
  34.       this.update();
  35.       this.draw();
  36.      
  37.       window.setTimeout('game.loop()', 10);
  38.     }
  39.   },
  40.  
  41.   update : function () {
  42.     var time = Math.round(new Date().getTime() / 1000);
  43.     var secondsSinceLastEnemy = time-this.timeLastEnemy;
  44.     //If we need to set in a new enemy
  45.     if(this.enemiesToCome>0 && secondsSinceLastEnemy>=this.secondsBetweenEnemies) {
  46.       var enemy = new Enemy();
  47.       enemy.init(this.mapParser.getPathPositions(), this.mapParser.getStartPosition(), 2, 10);
  48.       this.enemies.push(enemy);
  49.       this.enemiesToCome--;
  50.       this.timeLastEnemy = time;
  51.     }
  52.    
  53.     for(var i in this.enemies) {
  54.       this.enemies[i].update();
  55.     }
  56.    
  57.     this.timeLastFrame = time;
  58.   },
  59.  
  60.   draw : function () {
  61.     canvas.clear();
  62.     this.drawMap();
  63.     for(var i in this.enemies) {
  64.       this.enemies[i].draw();
  65.     }
  66.   },
  67.  
  68.   drawMap : function () {
  69.    
  70.     //Draw start position
  71.     var start = this.mapParser.getStartCell();
  72.     canvas.fillStyle("green");
  73.     canvas.rect(
  74.       start[0]*this.tileSize,
  75.       start[1]*this.tileSize,
  76.       this.tileSize,
  77.       this.tileSize
  78.     );
  79.  
  80.     //Drawing path
  81.     var path = this.mapParser.getPathCells();
  82.     canvas.fillStyle("black");
  83.     for(var i in path) {
  84.       canvas.rect(
  85.         path[i][0]*this.tileSize,
  86.         path[i][1]*this.tileSize,
  87.         this.tileSize,
  88.         this.tileSize
  89.       );
  90.     }
  91.     //Draw end position
  92.     var end = this.mapParser.getEndCell();
  93.     canvas.fillStyle("red");
  94.     canvas.rect(
  95.       end[0]*this.tileSize,
  96.       end[1]*this.tileSize,
  97.       this.tileSize,
  98.       this.tileSize
  99.     );
  100.   }
  101. }
  102.  
  103. window.onload = function () {
  104.   game.init();
  105. }
Add Comment
Please, Sign In to add comment