Compare v01 to v02
Produced: 13/1/17 5:32:12 pm
   
Mode:  All  
Left file: /Users/peetj/github/cs-js-beg-platformer/versions/game-01.js  
Right file: /Users/peetj/github/cs-js-beg-platformer/versions/game-02.js  
  1 const TILE_WIDTH = 80;
  2 const TILE_HEIGHT = 60;
  3  
  4 var tileMap = [
  5   [[0,1],[8,1],[0,13],[4,1],[0,8],[8,1],[0,5],[4,1],[0,7],[8,1],[0,6],[8,1],[0,2]],
  6   [[0,8],[4,1],[0,3],[4,1],[0,6],[8,1],[0,28]],
  7   [[0,8],[0,24],[4,1],[0,3],[4,1],[0,6],[8,1],[0,4]],
  8   [[0,11],[4,1],[0,15],[4,1],[0,13],[4,1],[0,6]],
  9   [[0,16],[8,1],[0,19],[8,1],[0,11]],
  10   [[0,9],[4,1],[0,38]],
  11   [[0,48]],
  12   [[1,48]]
  13 ];
  14  
1 15 Crafty.init(800, 600, document.getElementById('gamecanvas'));
2 16  
  17 var assets = {
  18   'tiles': ['img/tile-1.png', 'img/platform.png', 'img/platformx2.png']
  19 };
3 20 initialiseGame();
4 21  
5 22 function initialiseGame () {
6     loadBackground();
  23   Crafty.load(assets, function(){
  24     loadBackground();
  25     generateMap();
  26   });
7 27 }
8 28  
9 29 function loadBackground () {
10 30   Crafty.background('#3BB9FF');
11     Crafty.background('#FFFFFF url(img/bg.png) repeat-x center center');
  31   //Crafty.background('#FFFFFF url(img/bg.png) repeat-x center center');
12 32 }
  33  
  34 function generateMap () {
  35   const Y_OFFSET = 600 - (tileMap.length * TILE_HEIGHT);
  36   tileMap.map(function (tileRow, rowIdx) {
  37     var xPos = 0;
  38     var yPos = 0;
  39     tileRow.map(function (tile, tileIdx) {
  40       yPos = Y_OFFSET + (rowIdx * 60);
  41       var tileType = tile[0];
  42       var tileNum = tile[1];
  43       if (tileType === 0){
  44         xPos += (tileNum * 80);
  45       }
  46       if (tileType === 1) {
  47         for(var i=0; i < tileNum; i++){
  48           Crafty.e('FloorTile, 2D, DOM, Image, Collision')
  49             .attr({ x: xPos, y: yPos, w: TILE_WIDTH, h: TILE_HEIGHT })
  50             .image(Crafty.assets['img/tile-' + tileType + '.png'].src);
  51           xPos += 80;
  52         }
  53       }
  54       else{
  55         if (tileType === 4) {
  56           Crafty.e('Platform')
  57             .setImage('img/platform.png')
  58             .setPlatform(xPos, yPos, 1)
  59             .addCoins(Crafty.math.randomInt(1,2));
  60         }
  61         else if (tileType === 8) {
  62           Crafty.e('Platform')
  63             .setImage('img/platformx2.png')
  64             .setPlatform(xPos, yPos, 2)
  65             .addCoins(Crafty.math.randomInt(1,2));
  66         }
  67         xPos += 80;
  68       }
  69     });
  70   });
  71 }