Last active
August 4, 2016 20:49
-
-
Save rBurgett/d190ed37166646ddb27df8780d90be31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Here is another way to do the Game constructor. This is the traditional JS way of doing constructor functions | |
* The benefits are that this is the fastest way to construct and run a function. The downside is that all methods | |
* and properties are publicly available, which is a gross way to design classes. Also, you are still forced to | |
* often use .bind() in order to make the context is correct. But, regardless, if you look at many JS libraries, | |
* this is how you will find their classes structured. | |
* | |
* To run the constructor and instantiate the class: | |
* var game = new Game({ | |
* levels: levels, | |
* ctx : canvas.getContext("2d"), | |
* tileSize : 16 | |
* }); | |
**/ | |
var EventEmitter = require("events").EventEmitter; | |
function Game(options) { | |
this.defaultGrid = options.levels; | |
this.grid = this.cloneLevels(options.levels); | |
this.canvas = options.canvas; | |
this.ctx = options.ctx; | |
this.tileSize = options.tileSize; | |
this.events = new EventEmitter(); | |
} | |
Game.prototype.frozen = false; | |
Game.prototype.currentLevel = 0; | |
Game.prototype.charX = 1; | |
Game.prototype.charY = 1; | |
Game.prototype.moveQueue = []; | |
Game.prototype.tiles = { | |
0: "black", | |
1: "yellow", | |
2: "blue", | |
3: "red" | |
}; | |
Game.prototype.draw = function() { | |
if(!this.frozen && this.moveQueue.length > 0) { | |
this.moveQueue[0](); | |
} | |
this.moveQueue = []; | |
this.make(); | |
}; | |
Game.prototype.make = function() { | |
var level = this.currentLevel; | |
this.grid[level][this.charX][this.charY] = 2; | |
var ctx = this.ctx; | |
var tileSize = this.tileSize; | |
var row, col; | |
for (var i = 0; i < this.grid[level].length; i++) { | |
row = tileSize * i; | |
for (var j = 0; j < this.grid[level][0].length; j++) { | |
col = tileSize * j; | |
ctx.beginPath(); | |
ctx.rect(row, col, tileSize, tileSize); | |
ctx.fillStyle = this.tiles[this.grid[level][i][j]]; | |
ctx.fill(); | |
ctx.closePath(); | |
} | |
} | |
}; | |
Game.prototype.moveRight = function() { | |
var level = this.currentLevel; | |
if(this.charX < 19) { | |
if (this.grid[level][this.charX + 1][this.charY] === 0) { | |
this.grid[level][this.charX][this.charY] = 0; | |
this.charX += 1; | |
this.grid[level][this.charX][this.charY] = 2; | |
} else if (this.grid[level][this.charX + 1][this.charY] === 3) { | |
this.levelUp(); | |
} | |
} | |
}; | |
Game.prototype.moveLeft = function() { | |
var level = this.currentLevel; | |
if(this.charX > 0) { | |
if (this.grid[level][this.charX - 1][this.charY] === 0) { | |
this.grid[level][this.charX][this.charY] = 0; | |
this.charX -= 1; | |
this.grid[level][this.charX][this.charY] = 2; | |
} else if (this.grid[level][this.charX - 1][this.charY] === 3) { | |
this.levelUp(); | |
} | |
} | |
}; | |
Game.prototype.moveUp = function() { | |
var level = this.currentLevel; | |
if(this.charY > 0) { | |
if (this.grid[level][this.charX][this.charY -1] === 0) { | |
this.grid[level][this.charX][this.charY] = 0; | |
this.charY -= 1; | |
this.grid[level][this.charX][this.charY] = 2; | |
} else if (this.grid[level][this.charX][this.charY -1] === 3) { | |
this.levelUp(); | |
} | |
} | |
}; | |
Game.prototype.moveDown = function() { | |
var level = this.currentLevel; | |
if(this.charY < 19) { | |
if (this.grid[level][this.charX][this.charY + 1] === 0) { | |
this.grid[level][this.charX][this.charY] = 0; | |
this.charY += 1; | |
this.grid[level][this.charX][this.charY] = 2; | |
} else if (this.grid[level][this.charX][this.charY + 1] === 3) { | |
this.levelUp(); | |
} | |
} | |
}; | |
Game.prototype.levelUp = function() { | |
this.moveQueue = []; | |
if (this.currentLevel === this.grid.length - 1) { | |
this.frozen = true; | |
this.events.emit("gameOver"); | |
} else { | |
this.frozen = true; | |
this.currentLevel++; | |
this.events.emit("leveledUp", {currentLevel: this.currentLevel + 1}); | |
this.charX = 1; | |
this.charY = 1; | |
this.frozen = false; | |
} | |
}; | |
Game.prototype.restart = function() { | |
this.events.emit("restart"); | |
this.grid = this.cloneLevels(this.defaultGrid); | |
this.charX = 1; | |
this.charY = 1; | |
this.currentLevel = 0; | |
this.frozen = false; | |
this.moveQueue = 0; | |
this.make(); | |
}; | |
Game.prototype.keyDownHandler = function(e) { | |
switch(e.keyCode) { | |
case 39: | |
this.moveQueue.push(this.moveRight.bind(this)); | |
break; | |
case 37: | |
this.moveQueue.push(this.moveLeft.bind(this)); | |
break; | |
case 38: | |
this.moveQueue.push(this.moveUp.bind(this)); | |
break; | |
case 40: | |
this.moveQueue.push(this.moveDown.bind(this)); | |
break; | |
} | |
}; | |
Game.prototype.cloneLevels = function(levels) { | |
return levels.map(function(level) { | |
return level.map(function(row) { | |
return row.concat(); | |
}); | |
}); | |
}; | |
Game.prototype.start = function() { | |
document.addEventListener("keydown", this.keyDownHandler.bind(this)); | |
this.interval = setInterval((function() { | |
this.draw(); | |
}).bind(this), 90); | |
return this; | |
}; | |
module.exports = Game; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment