Advertisement
XArnonX

map

Feb 16th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Map(game) {
  2.   // constants
  3.   var MAP_NAME = 'Map',
  4.       TILE_SIZE = 100,
  5.       NUM_COLUMNS = 11,
  6.       NUM_ROWS = 11,
  7.       //SAVE_TILE = 13,
  8.       //PLAYERSTART = {1,1},
  9.       Tilesets = {
  10.         TILES: 'HintergrundKacheln',
  11.         COLLISION: 'Collision Tileset'
  12.       },
  13.       AssetsPaths = {
  14.         TILEMAP: 'assets/tilemaps/map.json',
  15.         TILES: 'assets/images/tilesets/Hintergrund_Kacheln.png',
  16.         COLLISION: 'assets/images/tilesets/collision2.png'
  17.       };
  18.       Colors = {
  19.         RED: 'rgba(240, 0, 0, 0.333)',
  20.         GREEN: 'rgba(0, 240, 0, 0.333)',
  21.         BLUE: 'rgba(0, 0, 240, 0.333)'
  22.       };
  23.  
  24.  
  25.   // vars
  26.   var _game = game, // game reference
  27.       _map = null, // map object
  28.  
  29.       // tilesets
  30.       _mainTileset = null,
  31.       _collisionTileset = null,
  32.  
  33.       // layers
  34.       _tilesLayer = null,
  35.       _collisionLayer = null,
  36.  
  37.       //
  38.       _surroundingsToDraw = [];
  39.  
  40.  
  41.   // public api
  42.   var _class = {};
  43.       // methods
  44.       _class.preload = preload;
  45.       _class.init = init;
  46.       _class.drawRectAt = drawRectAt;
  47.       _class.drawRedRectAt = drawRedRectAt;
  48.       _class.drawGreenRectAt = drawGreenRectAt;
  49.       _class.drawBlueRectAt = drawBlueRectAt;
  50.       _class.drawSurroundingCollisions = drawSurroundingCollisions;
  51.       // getters
  52.       _class.getTilesLayer = getTilesLayer;
  53.       _class.getCollisionAt = getCollisionAt;
  54.       _class.setCollisionAt = setCollisionAt;
  55.       _class.getSurroundingCollisionsAt = getSurroundingCollisionsAt;
  56.       //_class.getRandomSaveTiles = getRandomSaveTiles;
  57.       // properties
  58.       _class.collisionTiles = [];
  59.  
  60.  
  61.   // private methods
  62.   function preload() {
  63.     _game.load.tilemap(
  64.       MAP_NAME,
  65.       AssetsPaths.TILEMAP,
  66.       null,
  67.       Phaser.Tilemap.TILED_JSON
  68.     );
  69.     _game.load.image(Tilesets.TILES, AssetsPaths.TILES);
  70.     _game.load.image(Tilesets.COLLISION, AssetsPaths.COLLISION);
  71.   }
  72.  
  73.   function init() {
  74.     // create a map with the map name given in tile editor
  75.     _map = _game.add.tilemap(MAP_NAME);
  76.  
  77.     // add tileset image with name given in tile editor
  78.     _map.addTilesetImage(Tilesets.TILES);
  79.     _map.addTilesetImage(Tilesets.COLLISION);
  80.  
  81.     // create layers, assign them an index
  82.     _tilesLayer = _map.createLayer(0);
  83.     _collisionLayer = _map.createLayer(1);
  84.     _collisionLayer.alpha = 0;
  85.  
  86.     // manually set directional collision
  87.     var i = 0, j = 0,
  88.         collisionTile,
  89.         targetTile,
  90.         hasCollision,
  91.         collisionLog = '';
  92.     for(i = 0; i < NUM_ROWS; i++) {
  93.       for(j = 0; j < NUM_COLUMNS; j++) {
  94.         // get collision data from collision layer
  95.         collisionTile = _collisionLayer.layer.data[i][j];
  96.         hasCollision = (collisionTile.index > 13); // TODO: figure how to make dynamic?
  97.  
  98.         // set collision uniformely
  99.         // (no cloud tile for now)
  100.         targetTile = _tilesLayer.layer.data[i][j];
  101.         targetTile.collideDown = hasCollision;
  102.         targetTile.collideLeft = hasCollision;
  103.         targetTile.collideRight = hasCollision;
  104.         targetTile.collideUp = hasCollision;
  105.  
  106.         collisionLog += hasCollision ? '×' : ' ';
  107.       }
  108.       collisionLog += '\n';
  109.     }
  110.     // console.log(collisionLog);
  111.  
  112.     _tilesLayer.resizeWorld();
  113.     _collisionLayer.resizeWorld();
  114.   }
  115.  
  116.   function getTilesLayer() {
  117.     return _tilesLayer;
  118.   }
  119.  
  120.   function getCollisionLayer() {
  121.     return _collisionLayer;
  122.   }
  123.  
  124.   function getCollisionAt(tile) {
  125.     var tileData = _tilesLayer.layer.data[tile.y][tile.x];
  126.     var hasCollision = (tileData.collideUp && tileData.collideRight && tileData.collideDown && tileData.collideLeft);
  127.     // console.log('hasCollision: ', hasCollision);
  128.     return hasCollision;
  129.   }
  130.  
  131.   function setCollisionAt(tile, hasCollision) {
  132.     var tileData = _tilesLayer.layer.data[tile.y][tile.x];
  133.     tileData.collideUp = hasCollision;
  134.     tileData.collideRight = hasCollision;
  135.     tileData.collideDown = hasCollision;
  136.     tileData.collideLeft = hasCollision;
  137.   }
  138.  
  139.   function resetSurroundingsToDraw() {
  140.     _surroundingsToDraw.length = 0;
  141.     _surroundingsToDraw = [];
  142.   }
  143.  
  144.   function getSurroundingCollisionsAt(tile, setsSurroundingsToDraw) {
  145.     if(setsSurroundingsToDraw) {
  146.       resetSurroundingsToDraw();
  147.     }
  148.  
  149.     var surroundings = {
  150.       up: false,
  151.       right: false,
  152.       down: false,
  153.       left: false
  154.     };
  155.  
  156.     // check tile up
  157.     var tileUp = {
  158.       x: tile.x,
  159.       y: tile.y - 1
  160.     };
  161.     if(tileUp.y < 0) {
  162.       surroundings.up = true;
  163.     } else {
  164.       surroundings.up = getCollisionAt(tileUp);
  165.       if(setsSurroundingsToDraw) {
  166.         _surroundingsToDraw.push({tile: tileUp, collision: surroundings.up});
  167.       }
  168.     }
  169.  
  170.     // check tile right
  171.     var tileRight = {
  172.       x: tile.x + 1,
  173.       y: tile.y
  174.     };
  175.     if(tileRight.x >= NUM_COLUMNS) {
  176.       surroundings.right = true;
  177.     } else {
  178.       surroundings.right = getCollisionAt(tileRight);
  179.       if(setsSurroundingsToDraw) {
  180.         _surroundingsToDraw.push({tile: tileRight, collision: surroundings.right});
  181.       }
  182.     }
  183.  
  184.     // check tile down
  185.     var tileDown = {
  186.       x: tile.x,
  187.       y: tile.y + 1
  188.     };
  189.     if(tileDown.y >= NUM_ROWS) {
  190.       surroundings.down = true;
  191.     } else {
  192.       surroundings.down = getCollisionAt(tileDown);
  193.       if(setsSurroundingsToDraw) {
  194.         _surroundingsToDraw.push({tile: tileDown, collision: surroundings.down});
  195.       }
  196.     }
  197.  
  198.     // check tile left
  199.     var tileLeft = {
  200.       x: tile.x - 1,
  201.       y: tile.y
  202.     };
  203.     if(tileLeft.x < 0) {
  204.       surroundings.left = true;
  205.     } else {
  206.       surroundings.left = getCollisionAt(tileLeft);
  207.       if(setsSurroundingsToDraw) {
  208.         _surroundingsToDraw.push({tile: tileLeft, collision: surroundings.left});
  209.       }
  210.     }
  211.  
  212.     return surroundings;
  213.   }
  214.  
  215.   function drawRectAt(tile, color) {
  216.     if(!tile) {
  217.       tile = {x: 0, y: 0};
  218.     }
  219.     var rect = new Phaser.Rectangle(
  220.       tile.x * TILE_SIZE,
  221.       tile.y * TILE_SIZE,
  222.       TILE_SIZE, TILE_SIZE
  223.     );
  224.     _game.debug.geom(rect, color, true);
  225.   }
  226.  
  227.   function drawRedRectAt(tile) {
  228.     drawRectAt(tile, Colors.RED);
  229.   }
  230.  
  231.   function drawGreenRectAt(tile) {
  232.     drawRectAt(tile, Colors.GREEN);
  233.   }
  234.  
  235.   function drawBlueRectAt(tile) {
  236.     drawRectAt(tile, Colors.BLUE);
  237.   }
  238.  
  239.   function drawSurroundingCollisions() {
  240.     var i = 0,
  241.         numLoops = _surroundingsToDraw.length,
  242.         tile, collision;
  243.     for(i; i < numLoops; i++) {
  244.       tile = _surroundingsToDraw[i].tile;
  245.       collision = _surroundingsToDraw[i].collision;
  246.       if(collision) {
  247.         drawRedRectAt(tile);
  248.       } else {
  249.         drawGreenRectAt(tile);
  250.       }
  251.     }
  252.   }
  253.  
  254.  
  255.   return _class;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement