Advertisement
ShadowTzu

Superpowers: arcadeBody2D from tilemap

May 3rd, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var UnitFactor = 64.0 / 64.0; //pixelGrid / PixelbyUnit;
  2.  
  3. class behavior_Map extends Sup.Behavior {
  4.  
  5.   layer: number = 0; // collision layer
  6.  
  7.   awake() {
  8.    
  9.     var tilemap: Sup.TileMap = this.actor.tileMapRenderer.getTileMap();
  10.     var position: Sup.Math.Vector3 = this.actor.getPosition();
  11.     var scale: Sup.Math.Vector3 = this.actor.getLocalScale();
  12.     var c;
  13.     var prev_id = -1;
  14.     var width_count = 0;
  15.     var TileID: number;
  16.    
  17.     for ( var y = 0; y < tilemap.getHeight(); y++ ) {
  18.       for ( var x = -1; x <= tilemap.getWidth(); x++ ) {  // x value need to be from -1 to (width + 1) for capture rectangle (TileID return -1 if we are outside of bound)
  19.        
  20.         TileID = tilemap.getTileAt( this.layer, x, y);
  21.        
  22.         width_count = width_count + 1;
  23.         if (TileID == prev_id) {
  24.           continue;
  25.         }
  26.        
  27.         if (prev_id != -1) {
  28.           //build previous rectangle found
  29.           c = {
  30.             actor: new Sup.Actor("physic_body"),
  31.             body: null,
  32.             origin: new Sup.Math.Vector3(((x - (width_count * 0.5 ) ) * UnitFactor  * scale.x + position.x), (y  * UnitFactor * scale.y + position.y), 0)
  33.           }
  34.           c.actor.setParent(this.actor);      
  35.           c.actor.setLocalPosition(c.origin);      
  36.           c.body = new Sup.ArcadePhysics2D.Body(c.actor, {width:1 * width_count * UnitFactor * scale.x, height:1 * UnitFactor * scale.y});
  37.           c.body.setMovable(false);
  38.          
  39.       //if you want store body collision:
  40.           //Collision.Grounds_Body.push(c.body);
  41.         }
  42.        
  43.         width_count = 0;
  44.         prev_id = TileID;
  45.        
  46.       }
  47.     }
  48.     //this.actor.tileMapRenderer.setLayerOpacity(0, 0);
  49.    
  50.    
  51.   }
  52.  
  53.   update() {
  54.    
  55.   }
  56.  
  57. }
  58.  
  59. Sup.registerBehavior(behavior_Map);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement