Advertisement
Guest User

Untitled

a guest
May 16th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /**
  2.      * Takes the collision data defined in the collision editor of Tiled and applies it to
  3.      * and exisiting tilemap for use with P2 physics bodies.
  4.      *
  5.      * Tilemap json data MUST be loaded in the Preload state/function using game.load.json() before this function is called
  6.      * map must only have ONE tileset
  7.      * each tile can only have ONE polyline set for it's collision
  8.      * the polyline MUST be a complete shape (the last point is indentical to the first)
  9.      *
  10.      * @param {Phaser.Tilemap} map - this is the map which you want polylines added to
  11.      * @param {string} key - this is the key for the raw json tilemap data loaded in the preload state
  12.      * @param {bool} [roundValues=true] - Will round the x,y coordinates for the polylines to nearest integer
  13.      */
  14.     addPolylineCollision: function (map, key, roundValues) {
  15.  
  16.         if (roundValues === undefined) { roundValues = true; }
  17.  
  18.         // json with the collision data that was omitted by phaser
  19.         let data = this.game.cache.getJSON(key);
  20.         //console.log(data);
  21.         let collisionData = data.tilesets[0].tiles;
  22.         // array of the collision polygons that will get added to the tilemap
  23.         var polygons = [];
  24.         // tiles in the Collision Layer that will help define the polygons
  25.         let mapData = map.layers[map.getLayer('Collision Layer')].data;
  26.         for (let row in mapData) {
  27.             for (let col in mapData[row]) {
  28.                 var wall = collisionData[mapData[row][col].index - 1];
  29.                 if (wall !== undefined) {
  30.                     for (let i in wall.objectgroup.objects) {
  31.                         let poly = {
  32.                             height: 0,
  33.                             name: "",
  34.                             polyline: [],
  35.                             properties: undefined,
  36.                             type: "",
  37.                             visible: true,
  38.                             width: 0,
  39.                             x: mapData[row][col].x * map.tileWidth,
  40.                             y: mapData[row][col].y * map.tileHeight
  41.                         };
  42.                         for (let j in wall.objectgroup.objects[i].polyline) {
  43.  
  44.                             var coords = [wall.objectgroup.objects[i].polyline[j].x,
  45.                                           wall.objectgroup.objects[i].polyline[j].y];
  46.                             if (roundValues) {
  47.                                 coords[0] = Math.round(coords[0]);
  48.                                 coords[1] = Math.round(coords[1]);
  49.                             }
  50.                             poly.polyline.push(coords);
  51.                         }
  52.                         polygons.push(poly);
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.         console.log(polygons);
  58.         map.collision['Collision Layer'] = polygons;
  59.     }
  60. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement