Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <reference path="Phaser/Game.ts" />
- /// <reference path="Phaser/system/input/Mouse.ts" />
- // instructions on getting TypeScript working in Visual Studio 2010:
- // http://stackoverflow.com/questions/14510511/how-can-i-install-typescript-with-visual-studio-2010-and-vista
- // tutorial on how to get Phaser framework working with Visual Studio:
- // http://www.html5gamedevs.com/topic/463-need-a-simple-tutorial-on-using-the-phaser-framework-with-vs-express-2012/
- (function () {
- var NUM_ROWS: number = 25;
- var NUM_COLS: number = 35;
- var myGame = new Phaser.Game(this, 'game', 640, 480, init, create, update, render);
- var allCells: Phaser.Sprite[];
- var cellStatuses: bool[];
- var neighbors: Phaser.Point[];
- var elapsedTime: number = 0;
- function init()
- {
- myGame.loader.addImageFile("cell", "assets/img/cell.png");
- myGame.loader.load();
- }
- function create()
- {
- neighbors = [];
- neighbors.push(new Phaser.Point(-1, -1));
- neighbors.push(new Phaser.Point(0, -1));
- neighbors.push(new Phaser.Point(1, -1));
- neighbors.push(new Phaser.Point(1, 0));
- neighbors.push(new Phaser.Point(1, 1));
- neighbors.push(new Phaser.Point(0, 1));
- neighbors.push(new Phaser.Point(-1, 1));
- neighbors.push(new Phaser.Point(-1, 0));
- allCells = [];
- cellStatuses = [];
- for (var y = 0; y < NUM_ROWS; y++)
- {
- for (var x = 0; x < NUM_COLS; x++)
- {
- var cell: Phaser.Sprite = myGame.createSprite(10 + x * 14, 75 + y * 14, "cell");
- cell.visible = (Math.random() > 0.80);
- allCells.push(cell);
- cellStatuses.push(cell.visible);
- }
- }
- }
- function update()
- {
- elapsedTime += myGame.time.elapsed;
- if (elapsedTime >= 0.2)
- {
- elapsedTime -= 0.2;
- runGeneration();
- }
- }
- function runGeneration()
- {
- // make a copy of the cell statuses that we can read from
- var cellStatusesRead: bool[] = cellStatuses.slice(0, NUM_ROWS * NUM_COLS);
- var cellAlive: bool = false;
- var numAliveNeighbors: number = 0;
- var cell: Phaser.Sprite;
- for (var y = 0; y < NUM_ROWS; y++)
- {
- for (var x = 0; x < NUM_COLS; x++)
- {
- cell = allCells[y * NUM_COLS + x];
- cellAlive = cellStatusesRead[y * NUM_COLS + x];
- numAliveNeighbors = getNumAliveNeighbors(cellStatusesRead, x, y);
- if (cellAlive)
- {
- if (numAliveNeighbors < 2 || numAliveNeighbors > 3)
- {
- cell.visible = false;
- }
- else
- {
- cell.visible = true;
- }
- }
- else
- {
- if (numAliveNeighbors == 3)
- {
- cell.visible = true;
- }
- }
- cellStatuses[y * NUM_COLS + x] = cell.visible;
- }
- }
- }
- function getNumAliveNeighbors(cells: bool[], x: number, y: number): number
- {
- var numAliveNeighbors: number = 0;
- for (var i = 0; i < neighbors.length; i++)
- {
- var point: Phaser.Point = neighbors[i];
- var xx: number = x + point.x;
- var yy: number = y + point.y;
- if (xx < 1 || xx >= NUM_COLS - 1)
- {
- continue;
- }
- if (yy < 1 || yy >= NUM_ROWS - 1)
- {
- continue;
- }
- if (cells[yy * NUM_COLS + xx])
- {
- numAliveNeighbors++;
- }
- }
- return numAliveNeighbors;
- }
- function render()
- {
- myGame.input.renderDebugInfo(32, 32, 'rgb(255,255,255)');
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement