Advertisement
Guest User

Game of Life with TypeScript in Phaser

a guest
Jun 3rd, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <reference path="Phaser/Game.ts" />
  2. /// <reference path="Phaser/system/input/Mouse.ts" />
  3.  
  4. // instructions on getting TypeScript working in Visual Studio 2010:
  5. // http://stackoverflow.com/questions/14510511/how-can-i-install-typescript-with-visual-studio-2010-and-vista
  6.  
  7. // tutorial on how to get Phaser framework working with Visual Studio:
  8. // http://www.html5gamedevs.com/topic/463-need-a-simple-tutorial-on-using-the-phaser-framework-with-vs-express-2012/
  9.  
  10. (function () {
  11.  
  12.     var NUM_ROWS: number = 25;
  13.     var NUM_COLS: number = 35;
  14.  
  15.     var myGame = new Phaser.Game(this, 'game', 640, 480, init, create, update, render);
  16.  
  17.     var allCells: Phaser.Sprite[];
  18.     var cellStatuses: bool[];
  19.     var neighbors: Phaser.Point[];
  20.     var elapsedTime: number = 0;
  21.  
  22.     function init()
  23.     {
  24.         myGame.loader.addImageFile("cell", "assets/img/cell.png");
  25.         myGame.loader.load();
  26.     }
  27.  
  28.     function create()
  29.     {
  30.         neighbors = [];
  31.         neighbors.push(new Phaser.Point(-1, -1));
  32.         neighbors.push(new Phaser.Point(0, -1));
  33.         neighbors.push(new Phaser.Point(1, -1));
  34.         neighbors.push(new Phaser.Point(1, 0));
  35.         neighbors.push(new Phaser.Point(1, 1));
  36.         neighbors.push(new Phaser.Point(0, 1));
  37.         neighbors.push(new Phaser.Point(-1, 1));
  38.         neighbors.push(new Phaser.Point(-1, 0));
  39.  
  40.         allCells = [];
  41.         cellStatuses = [];
  42.  
  43.         for (var y = 0; y < NUM_ROWS; y++)
  44.         {
  45.             for (var x = 0; x < NUM_COLS; x++)
  46.             {
  47.                 var cell: Phaser.Sprite = myGame.createSprite(10 + x * 14, 75 + y * 14, "cell");
  48.                 cell.visible = (Math.random() > 0.80);
  49.  
  50.                 allCells.push(cell);
  51.  
  52.                 cellStatuses.push(cell.visible);
  53.             }
  54.         }
  55.     }
  56.  
  57.     function update()
  58.     {
  59.         elapsedTime += myGame.time.elapsed;
  60.  
  61.         if (elapsedTime >= 0.2)
  62.         {
  63.             elapsedTime -= 0.2;
  64.  
  65.             runGeneration();
  66.         }
  67.     }
  68.  
  69.     function runGeneration()
  70.     {
  71.         // make a copy of the cell statuses that we can read from
  72.         var cellStatusesRead: bool[] = cellStatuses.slice(0, NUM_ROWS * NUM_COLS);
  73.         var cellAlive: bool = false;
  74.         var numAliveNeighbors: number = 0;
  75.         var cell: Phaser.Sprite;
  76.  
  77.         for (var y = 0; y < NUM_ROWS; y++)
  78.         {
  79.             for (var x = 0; x < NUM_COLS; x++)
  80.             {
  81.                 cell = allCells[y * NUM_COLS + x];
  82.                 cellAlive = cellStatusesRead[y * NUM_COLS + x];
  83.                 numAliveNeighbors = getNumAliveNeighbors(cellStatusesRead, x, y);
  84.  
  85.                 if (cellAlive)
  86.                 {
  87.                     if (numAliveNeighbors < 2 || numAliveNeighbors > 3)
  88.                     {
  89.                         cell.visible = false;
  90.                     }
  91.                     else
  92.                     {
  93.                         cell.visible = true;
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     if (numAliveNeighbors == 3)
  99.                     {
  100.                         cell.visible = true;
  101.                     }
  102.                 }
  103.  
  104.                 cellStatuses[y * NUM_COLS + x] = cell.visible;
  105.             }
  106.         }
  107.     }
  108.  
  109.     function getNumAliveNeighbors(cells: bool[], x: number, y: number): number
  110.     {
  111.         var numAliveNeighbors: number = 0;
  112.  
  113.         for (var i = 0; i < neighbors.length; i++)
  114.         {
  115.             var point: Phaser.Point = neighbors[i];
  116.             var xx: number = x + point.x;
  117.             var yy: number = y + point.y;
  118.  
  119.             if (xx < 1 || xx >= NUM_COLS - 1)
  120.             {
  121.                 continue;
  122.             }
  123.  
  124.             if (yy < 1 || yy >= NUM_ROWS - 1)
  125.             {
  126.                 continue;
  127.             }
  128.  
  129.             if (cells[yy * NUM_COLS + xx])
  130.             {
  131.                 numAliveNeighbors++;
  132.             }
  133.         }
  134.  
  135.         return numAliveNeighbors;
  136.     }
  137.  
  138.     function render()
  139.     {
  140.         myGame.input.renderDebugInfo(32, 32, 'rgb(255,255,255)');
  141.     }
  142.  
  143. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement