JuiceBoxx

gamestate.hx

May 27th, 2016
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 5.02 KB | None | 0 0
  1. package;
  2.  
  3. import effects.Ripple;
  4. import effects.Tile;
  5. import flixel.group.FlxGroup;
  6. import flixel.math.FlxPoint;
  7. import flixel.math.FlxRect;
  8. import openfl.display.Tileset;
  9. import registry.REG;
  10. import flixel.FlxG;
  11. import flixel.FlxSprite;
  12. import flixel.FlxState;
  13. import flixel.text.FlxText;
  14. import flixel.util.FlxColor;
  15. import flixel.math.FlxMath;
  16. import flixel.tile.FlxTilemap;
  17. import flixel.addons.editors.ogmo.FlxOgmoLoader;
  18. import flixel.FlxObject;
  19. import flixel.util.FlxSort;
  20. import flixel.math.FlxMath;
  21.  
  22. class GameState extends FlxState
  23. {
  24.     //Map
  25.     public var tileMap:FlxOgmoLoader;
  26.     public var floor:FlxTilemap;
  27.     public var maxAng:Float = 8;
  28.     public var tiles:FlxTypedGroup<Tile>;
  29.    
  30.     //Background
  31.     public var baseBG:FlxSprite;
  32.     public var layer1:FlxTypedGroup<FlxSprite>;
  33.     public var layer2:FlxTypedGroup<FlxSprite>;
  34.     public var layer3:FlxTypedGroup<FlxSprite>;
  35.    
  36.     //Details
  37.     public var ripples:FlxTypedGroup<Ripple>;
  38.     public var shadows:FlxTypedGroup<FlxSprite>;
  39.    
  40.     //Entities
  41.     public var entities:FlxTypedGroup<Entity>;
  42.    
  43.     public var player:Player;
  44.    
  45.     override public function create():Void
  46.     {
  47.         super.create();
  48.        
  49.         REG.applyOptions();
  50.        
  51.         //Tilemap
  52.         tileMap = new FlxOgmoLoader('assets/maps/' + REG.levelType + '_' + Std.string(REG.levelID) + '.oel');
  53.        
  54.         floor = tileMap.loadTilemap('assets/maps/tiles/' + REG.levelType + '.png', 96, 96, "tiles");
  55.        
  56.         tiles = new FlxTypedGroup<Tile>();
  57.        
  58.         //Backgrounds
  59.         baseBG = new FlxSprite(0, 0, 'assets/sprites/backgrounds/base.png');
  60.         layer1 = new FlxTypedGroup<FlxSprite>();
  61.         layer2 = new FlxTypedGroup<FlxSprite>();
  62.         layer3 = new FlxTypedGroup<FlxSprite>();
  63.         ripples = new FlxTypedGroup<Ripple>();
  64.        
  65.         /*for (i in 0 ... 16)
  66.         {
  67.             var r:Ripple = new Ripple(FlxG.random.float(106.5, FlxG.width - 106.5), FlxG.random.float(80, FlxG.height - 80), 2, false);
  68.             ripples.add(r);
  69.         }*/
  70.        
  71.         getTBG(1, layer1);
  72.         getTBG(2, layer2);
  73.         getTBG(3, layer3);
  74.        
  75.         addObjects();
  76.     }
  77.    
  78.     //Get Tiled Backdrops
  79.     function getTBG(LayerID:Int, Layer:FlxTypedGroup<FlxSprite>)
  80.     {
  81.         var tS:FlxPoint = new FlxPoint(200, 182); //Tile Size
  82.         var T:FlxPoint = new FlxPoint(FlxG.width / tS.x, FlxG.height / tS.y);
  83.        
  84.         for (i in 0 ... Math.round(T.y + 1))
  85.         {
  86.             for (b in 0 ... Math.round(T.x + 1))
  87.             {
  88.                 var d:FlxSprite = new FlxSprite(b * tS.x, i * tS.y, 'assets/sprites/backgrounds/layer' + Std.string(LayerID) + '.png');
  89.                
  90.                 switch(LayerID)
  91.                 {
  92.                     case 1:
  93.                         d.velocity.y = -10;
  94.                         d.alpha = 0.2;
  95.                     case 2:
  96.                         d.alpha = 0.2;
  97.                     case 3:
  98.                         d.velocity.y = -20;
  99.                         d.alpha = 0.3;
  100.                        
  101.                 }
  102.                
  103.                 if (LayerID == 2)
  104.                 {
  105.                     d.loadGraphic('assets/sprites/backgrounds/layer2.png', true, 200, 182);
  106.                     d.animation.add('water', [0, 1, 2, 3, 4, 5, 6, 7, 8], 8, true);
  107.                     d.animation.play('water', true);
  108.                 }
  109.                
  110.                 Layer.add(d);
  111.             }
  112.         }
  113.     }
  114.    
  115.     //Convert tiles to sprites
  116.     function convertTiles():Void
  117.     {
  118.         var mS:FlxPoint = new FlxPoint(floor.widthInTiles, floor.heightInTiles);
  119.        
  120.         for (i in 0 ... Std.int(mS.x))
  121.         {
  122.             for (b in 0 ... Std.int(mS.y))
  123.             {
  124.                 if (floor.getTile(i, b) != 0)
  125.                 {
  126.                     var sprite:FlxSprite;
  127.                     sprite = floor.tileToSprite(i, b, 0);
  128.                     sprite.angle = FlxG.random.float( -maxAng, maxAng);
  129.                     sprite.alive = false; //For seeing if it's pushed down or not
  130.                     tiles.add(new Tile(0, 0, this, sprite));
  131.                     //Maybe do some more dynamic stuff here later, like if the tile is overlapping a beacon
  132.                 }
  133.             }
  134.         }
  135.     }
  136.    
  137.     //Get entities
  138.     function loadEntities(name:String, data:Xml):Void
  139.     {
  140.         var x:Int = Std.parseInt(data.get("x"));
  141.         var y:Int = Std.parseInt(data.get("y"));
  142.        
  143.         switch (name)
  144.         {
  145.             case 'player':
  146.                 player.x = x - 32;
  147.                 player.y = y - 32;
  148.         }
  149.     }
  150.    
  151.     //Add Objects
  152.    
  153.     function addObjects()
  154.     {
  155.         //Load Objects
  156.         entities = new FlxTypedGroup<Entity>();
  157.         shadows = new FlxTypedGroup<FlxSprite>();
  158.        
  159.         player = new Player(0, 0, this);
  160.        
  161.         //Load Entities
  162.         tileMap.loadEntities(loadEntities, 'entities');
  163.         convertTiles();
  164.         tiles.sort(FlxSort.byY);
  165.        
  166.         //BG
  167.         add(baseBG);
  168.         add(layer1);
  169.         add(layer2);
  170.         add(layer3);
  171.         add(ripples);
  172.        
  173.         //Tilemap
  174.         add(tiles);
  175.        
  176.         //Entities
  177.         entities.add(player);
  178.        
  179.         //Add Groups
  180.         add(shadows);
  181.         add(entities);
  182.     }
  183.  
  184.     function setTileProperties(Layer:FlxTilemap, T:Array<Int>)
  185.     {
  186.         var tmap:FlxTilemap = Layer;
  187.        
  188.         for (i in 1 ... T.length)
  189.         {
  190.             tmap.setTileProperties(i, T[i-1]);
  191.         }
  192.        
  193.         return tmap;
  194.     }
  195.    
  196.     override public function update(elapsed:Float):Void
  197.     {
  198.         super.update(elapsed);
  199.        
  200.         layer1.forEach(bgTile);
  201.         layer2.forEach(bgTile);
  202.         layer3.forEach(bgTile);
  203.        
  204.         entities.sort(FlxSort.byY);
  205.        
  206.         FlxG.overlap(tiles, entities, sinkTiles);
  207.        
  208.         tiles.forEachAlive(function (T:Tile)
  209.         {
  210.             if (!FlxG.overlap(entities, T))
  211.                 T.floatUp();
  212.         });
  213.        
  214.     }
  215.    
  216.     function sinkTiles(T:Tile, E:Entity)
  217.     {
  218.         if (T.alive == false)
  219.             T.startSink();
  220.     }
  221.    
  222.     function bgTile(S:FlxSprite)
  223.     {
  224.         if (S.y <= -182)
  225.             S.y = FlxG.height+12;
  226.     }
  227.    
  228. }
Add Comment
Please, Sign In to add comment