Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var canvas = {};
- var game = {
- //Current level played
- gameLevel : 0,
- enemies : [],
- enemiesToCome : 1,
- secondsBetweenEnemies : 0,
- timeLastEnemy : Math.round(new Date().getTime() / 1000),
- timeLastFrame : Math.round(new Date().getTime() / 1000),
- tileSize : 32,
- init : function () {
- this.canvasDOMelm = document.getElementById('gameCanvas');
- canvas = new CanvasTools(this.canvasDOMelm.getContext('2d'), this.canvasDOMelm.width, this.canvasDOMelm.height);
- this.start();
- },
- start : function () {
- this.map = maps[this.gameLevel].map;
- this.mapParser = new MapParser(this.gameLevel, this.tileSize);
- this.mapParser.doParse();
- this.loop();
- },
- loop : function () {
- if(!this.gameOver) {
- this.update();
- this.draw();
- window.setTimeout('game.loop()', 10);
- }
- },
- update : function () {
- var time = Math.round(new Date().getTime() / 1000);
- var secondsSinceLastEnemy = time-this.timeLastEnemy;
- //If we need to set in a new enemy
- if(this.enemiesToCome>0 && secondsSinceLastEnemy>=this.secondsBetweenEnemies) {
- var enemy = new Enemy();
- enemy.init(this.mapParser.getPathPositions(), this.mapParser.getStartPosition(), 2, 10);
- this.enemies.push(enemy);
- this.enemiesToCome--;
- this.timeLastEnemy = time;
- }
- for(var i in this.enemies) {
- this.enemies[i].update();
- }
- this.timeLastFrame = time;
- },
- draw : function () {
- canvas.clear();
- this.drawMap();
- for(var i in this.enemies) {
- this.enemies[i].draw();
- }
- },
- drawMap : function () {
- //Draw start position
- var start = this.mapParser.getStartCell();
- canvas.fillStyle("green");
- canvas.rect(
- start[0]*this.tileSize,
- start[1]*this.tileSize,
- this.tileSize,
- this.tileSize
- );
- //Drawing path
- var path = this.mapParser.getPathCells();
- canvas.fillStyle("black");
- for(var i in path) {
- canvas.rect(
- path[i][0]*this.tileSize,
- path[i][1]*this.tileSize,
- this.tileSize,
- this.tileSize
- );
- }
- //Draw end position
- var end = this.mapParser.getEndCell();
- canvas.fillStyle("red");
- canvas.rect(
- end[0]*this.tileSize,
- end[1]*this.tileSize,
- this.tileSize,
- this.tileSize
- );
- }
- }
- window.onload = function () {
- game.init();
- }
Add Comment
Please, Sign In to add comment