Created
July 11, 2014 16:43
-
-
Save davidvgus/5588dcb8cb44d19137e0 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
/*jslint latedef:false*/ | |
/*global Phaser*/ | |
/*global Keyboard*/ | |
//font size | |
var FONT = 32; | |
//map dimensions | |
var ROWS = 10; | |
var COLS = 15; | |
// number of actors per level including player; | |
var ACTORS = 10; | |
// the structure of the map | |
var map; | |
// the ascii display, as a 2d array of characters | |
var screen; | |
//initialize phaser, call create() once done | |
var game = new Phaser.Game(COLS * FONT * 0.6, ROWS * FONT, Phaser.AUTO, null, { | |
create: create | |
}); | |
function drawMap(){ | |
for (var y = 0; y < ROWS; y = y + 1) { | |
for (var x = 0; x < COLS; x = x + 1) { | |
screen[y][x].text = map[y][x]; | |
} | |
} | |
} | |
function create() { | |
//init keyboard commands | |
game.input.keyboard.addCallbacks(null, null, onKeyUp); | |
initMap(); | |
screen = []; | |
for (var y = 0; y < ROWS; y = y + 1) { | |
var newRow = []; | |
screen.push(newRow); | |
for (var x = 0; x < COLS; x = x + 1) { | |
newRow.push( initCell('', x, y)); | |
} | |
} | |
drawMap(); | |
} | |
function initCell( chr, x, y ){ | |
// add a single cell in a given position to the ascii display | |
var style = { font: FONT + "px monospace", fill:"#fff" }; | |
return game.add.text(FONT * 0.6 * x, FONT * y, chr, style); | |
} | |
function initMap(){ | |
map = []; | |
for (var y = 0; y < ROWS; y = y + 1){ | |
var newRow = []; | |
for (var x = 0; x < COLS; x = x +1){ | |
if (Math.random() > 0.8) { | |
newRow.push('#'); | |
} else { | |
newRow.push('.'); | |
} | |
} | |
map.push(newRow); | |
} | |
} | |
function onKeyUp(event) { | |
switch (event.keyCode) { | |
case Keyboard.LEFT: | |
case Keyboard.RIGHT: | |
case Keyboard.UP: | |
case Keyboard.DOWN: | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment