JuiceBoxx

PlayState

Sep 12th, 2015
158
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package;
  2.  
  3. import flixel.FlxG;
  4. import flixel.FlxSprite;
  5. import flixel.FlxState;
  6. import flixel.text.FlxText;
  7. import flixel.ui.FlxButton;
  8. import flixel.util.FlxMath;
  9. import flixel.addons.editors.ogmo.FlxOgmoLoader;
  10. import flixel.tile.FlxTilemap;
  11. import flixel.FlxObject;
  12. import flixel.FlxCamera;
  13. import flixel.group.FlxTypedGroup;
  14. import flixel.group.FlxGroup;
  15. import flixel.util.FlxColor;
  16. import flixel.util.FlxSort;
  17. import flixel.util.FlxPath;
  18. import flixel.util.FlxPoint;
  19.  
  20. /**
  21.  * A FlxState which can be used for the actual gameplay.
  22.  */
  23. class PlayState extends FlxState
  24. {
  25.     /**
  26.     /Define Variables
  27.     */
  28.  
  29.     public var _player:Player;
  30.     private var _map:FlxOgmoLoader;
  31.     public var _mWalls:FlxTilemap;
  32.     private var _mWallTops:FlxTilemap;
  33.     private var _hud:HUD;
  34.     public var _money:Int = 0;
  35.     public var _health:Int = 3;
  36.     public var _maxHealth:Int = 3;
  37.     private var path:FlxPath;
  38.     private var pathPoints:Array<FlxPoint>;
  39.     //Groups
  40.     private var _grpCoins:FlxTypedGroup<Coin>;
  41.     private var _grpEnemies:FlxTypedGroup<Enemy>;
  42.     private var _grpSprites:FlxTypedGroup<AnimClass>;
  43.     //Points
  44.     private var _playMidPos:FlxPoint;
  45.     private var _newPos:FlxPoint;
  46.  
  47.     /**
  48.      * Function that is called up when to state is created to set it up.
  49.      */
  50.     override public function create():Void
  51.     {
  52.  
  53.         //Load Map
  54.         _map = new FlxOgmoLoader('assets/data/room-002.oel');
  55.         _mWalls = _map.loadTilemap('assets/data/tiles.png', 64,64, "walls");
  56.         _mWallTops = _map.loadTilemap('assets/data/tiles.png', 64,64, "wall_tops");
  57.         //Set Wall Layer Props
  58.         _mWalls.setTileProperties(1, FlxObject.NONE);
  59.         _mWalls.setTileProperties(2, FlxObject.ANY);
  60.         _mWalls.setTileProperties(3, FlxObject.ANY);
  61.         //Set WallTop Layer Props
  62.         _mWalls.setTileProperties(4, FlxObject.NONE);
  63.         //Add Wall Layer
  64.         add(_mWalls);
  65.         //Initialize Pickups
  66.         _grpCoins = new FlxTypedGroup<Coin>();
  67.         //Initialize Enemies
  68.         _grpEnemies = new FlxTypedGroup<Enemy>();
  69.         //Initialize Player
  70.         _player = new Player();
  71.         _playMidPos = new FlxPoint(_player.x,_player.y);
  72.         _newPos = new FlxPoint(_player.x,_player.y);
  73.         //Init Sprites
  74.         _grpSprites = new FlxTypedGroup<AnimClass>();
  75.         //Load Stuff
  76.         _map.loadEntities(placeEntities, "entities");
  77.         add(_grpSprites);
  78.         add(_player);
  79.         add(_grpEnemies);
  80.         add(_grpCoins);
  81.         //Load Wall Tops
  82.         add(_mWallTops);
  83.         //Create HUD
  84.         _hud = new HUD();
  85.         add(_hud);
  86.  
  87.         //Set Camera
  88.         FlxG.camera.follow(_player, FlxCamera.STYLE_TOPDOWN,0);
  89.  
  90.         super.create();
  91.     }
  92.    
  93.     /**
  94.      * Function that is called when this state is destroyed - you might want to
  95.      * consider setting all objects this state uses to null to help garbage collection.
  96.      */
  97.     override public function destroy():Void
  98.     {
  99.         super.destroy();
  100.     }
  101.  
  102.     /**
  103.      * Function that is called once every frame.
  104.      */
  105.     override public function update():Void
  106.     {
  107.         super.update();
  108.         //Collisions!
  109.         FlxG.collide(_player, _mWalls);
  110.         FlxG.overlap(_player, _grpCoins, playerTouchCoin);
  111.         FlxG.collide(_grpEnemies, _mWalls);
  112.         FlxG.overlap(_player, _grpEnemies, playerHurt);
  113.         _grpEnemies.forEachAlive(checkEnemyVision);
  114.         //Get Player Midpoint
  115.         _playMidPos = new FlxPoint(_player.x+32,_player.y+32);
  116.         _newPos = new FlxPoint(_player.x+32,_player.y);
  117.         //Max health
  118.         if (_health > _maxHealth)
  119.             _health = _maxHealth;
  120.  
  121.         _grpSprites.sort(FlxSort.byY);
  122.     }
  123.  
  124.     //Annual Vision Test
  125.     private function checkEnemyVision(e:Enemy):Void
  126.     {
  127.         if (_mWalls.ray(e.getMidpoint(), _player.getMidpoint()))
  128.         {
  129.             e.pathPoints = _mWalls.findPath(e.getMidpoint(),_player.getMidpoint());
  130.             e.seesPlayer = true;
  131.             e.playerPos.copyFrom(_playMidPos);
  132.         }
  133.         else
  134.             e.seesPlayer = false;
  135.     }
  136.  
  137.     /*
  138.     -----------------------------Place Entities Function-----------------------
  139.     */
  140.     private function placeEntities(entityName:String, entityData:Xml):Void
  141.     {
  142.         var x:Int = Std.parseInt(entityData.get("x"));
  143.         var y:Int = Std.parseInt(entityData.get("y"));
  144.         if (entityName == "player")
  145.         {
  146.             _grpSprites.add(new AnimClass(_player.x,_player.y,'Player',_player,null,0));
  147.             _player.x = x;
  148.             _player.y = y;
  149.         }
  150.         else if (entityName == "coin")
  151.         {
  152.             _grpCoins.add(new Coin(x + 16, y + 16,_player,this));
  153.         }
  154.         else if (entityName == "enemy")
  155.         {
  156.             _grpEnemies.add(new Enemy(x, y, Std.parseInt(entityData.get("etype")),this,_mWalls));
  157.         }
  158.     }
  159.     /*
  160.     ----------------------------Set Animations for Enemy---------------------------
  161.     */
  162.     public function setEnemyAnim(E:Enemy,T:Int,X:Float,Y:Float):Void
  163.     {
  164.         _grpSprites.add(new AnimClass(X,Y,'Enemy',null,E,T));
  165.     }
  166.     public function setCoinAnim(C:Coin):Void
  167.     {
  168.         _grpSprites.add(new AnimClass(C.x,C.y,'Coin',C));
  169.     }
  170.  
  171.     //Collect Coins
  172.     private function playerTouchCoin(P:Player, C:Coin):Void
  173.     {
  174.         if (P.alive && P.exists && C.alive && C.exists && C._pickedUp == false)
  175.         {
  176.             FlxG.camera.flash(FlxColor.WHITE, 0.3, true);
  177.             _money++;
  178.             _hud.updateHUD(_health,_maxHealth,_money);
  179.             C.pickUp();
  180.         }
  181.     }
  182.     //Get Hurt
  183.     private function playerHurt(P:Player, E:Enemy):Void
  184.     {
  185.         if (E.etype == 0)
  186.             FlxG.camera.shake(0.01,0.1);
  187.         if (E.etype == 1)
  188.             FlxG.camera.shake(0.02,0.1);
  189.     }  
  190. }
RAW Paste Data