Advertisement
Guest User

PlayState

a guest
Mar 1st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 3.85 KB | None | 0 0
  1. package ns.catchme;
  2.  
  3. import flixel.FlxState;
  4. import flixel.FlxSprite;
  5. import flixel.FlxG;
  6. import flixel.effects.particles.FlxEmitter;
  7. import flixel.group.FlxGroup.FlxTypedGroup;
  8. import flixel.math.FlxPoint;
  9. import flixel.math.FlxRect;
  10. import flixel.text.FlxText;
  11. import flixel.util.FlxTimer;
  12. import flixel.util.FlxColor;
  13.  
  14. class PlayState extends FlxState
  15. {
  16.     private static inline var SPEED: Int = 250;
  17.     private static inline var RESPAWN_TIME: Float = 1;
  18.    
  19.     private var _bg: FlxSprite;
  20.     private var _platform: Platform;
  21.     private var _ground: FlxSprite;
  22.     private var _groundHt: FlxRect;
  23.    
  24.     private var _balls: FlxTypedGroup<Ball>;
  25.     private var _ballVelocity: Int = 400;
  26.     private var _spawnTimer : FlxTimer;
  27.    
  28.     private var _stat: GameStat;
  29.     private var _scoreText: FlxText;
  30.     private var _livesHud: LivesHud;
  31.    
  32.     private var _emitter: FlxEmitter;
  33.     private var _emitterHelper: EmitterHelper;
  34.    
  35.     private var _platformHitbox: FlxRect;
  36.     private var _groundHitbox: FlxRect;
  37.    
  38.     override public function create():Void
  39.     {
  40.         super.create();
  41.        
  42.         _bg = new FlxSprite(0, 0, AssetPaths.bg__png);
  43.         _ground = new FlxSprite(0, FlxG.height - 64, AssetPaths.bottom__png);
  44.         _groundHt = new FlxRect(0, FlxG.height - 96, FlxG.width, 32);
  45.         _platform = new Platform(FlxG.width / 2, FlxG.height - 128);
  46.        
  47.         _balls = new FlxTypedGroup<Ball>();
  48.         for (i in 1...16)
  49.         {
  50.             var ball = new Ball();
  51.             ball.kill();
  52.             _balls.add(ball);
  53.         }
  54.        
  55.         _spawnTimer = new FlxTimer();
  56.         _spawnTimer.start(RESPAWN_TIME, onSpawn);
  57.        
  58.         _livesHud = new LivesHud();
  59.         _livesHud.setPosition(FlxG.width - 128, 0);
  60.            
  61.         _stat = new GameStat();
  62.         _stat.onScoreUpdated.add(function ()
  63.         {
  64.             _scoreText.text = _stat.scoreText;
  65.         });
  66.         _stat.onLivesUpdated.add(function ()
  67.         {
  68.             _livesHud.lives = _stat.lives;
  69.         });
  70.        
  71.         _scoreText = new FlxText();
  72.         _scoreText.size = 40;
  73.         _scoreText.text = _stat.scoreText;
  74.        
  75.         _stat.reset();
  76.        
  77.         initEmitter();
  78.         initHitboxes();
  79.        
  80.         // ОКРУЖЕНИЕ
  81.         add(_bg);
  82.         add(_ground);
  83.        
  84.         // GAME OBJECTS
  85.         add(_balls);
  86.         add(_platform);
  87.         add(_emitter);
  88.        
  89.         // HUD
  90.         add(_scoreText);
  91.         add(_livesHud);
  92.     }
  93.  
  94.     private function initEmitter()
  95.     {
  96.         _emitter = new FlxEmitter(0, 0, 256);
  97.        
  98.         _emitter.makeParticles(4, 4, FlxColor.WHITE, 256);
  99.         _emitter.lifespan.max = 1.5;
  100.         _emitter.speed.set(300, 600, 20, 100);
  101.         _emitter.acceleration.set(0, 1000);
  102.         _emitter.launchAngle.set( -105, -75);
  103.        
  104.         _emitterHelper = new EmitterHelper(_emitter);
  105.     }
  106.    
  107.     private function initHitboxes()
  108.     {
  109.         _platformHitbox = new FlxRect();
  110.         var p = new FlxPoint(_platform.width + 60, 32);
  111.         _platformHitbox.fromTwoPoints(new FlxPoint(0, 0), p);
  112.        
  113.         _groundHitbox = new FlxRect(0, FlxG.height - 96, FlxG.width, 32);
  114.     }
  115.    
  116.     override public function update(elapsed:Float):Void
  117.     {
  118.         super.update(elapsed);
  119.        
  120.         _platformHitbox.setPosition(_platform.x, _platform.y);
  121.         _platformHitbox.offset(-32, -32);
  122.         var ch = checkBall;
  123.         _balls.forEachAlive(ch);
  124.        
  125.         if (_stat.lives < 0)
  126.         {
  127.             FlxG.switchState(new MenuState());
  128.         }
  129.     }
  130.    
  131.     private function checkBall(ball: Ball)
  132.     {
  133.         var p = ball.getMidpoint();
  134.         if (_platformHitbox.containsPoint(p))
  135.         {
  136.             _emitterHelper.emitOnPoint(p.x, p.y + 32, 64);
  137.             ball.kill();
  138.             onCaught();
  139.         }
  140.         if (_groundHitbox.containsPoint(p))
  141.         {
  142.             _emitterHelper.emitOnPoint(p.x, p.y + 32, 64);
  143.             ball.kill();
  144.             onMiss();
  145.         }
  146.     }
  147.    
  148.     private function onSpawn(timer: FlxTimer)
  149.     {
  150.         var ball = _balls.getFirstDead();
  151.         if (ball != null)
  152.         {
  153.             var by = ball.getHitbox().height;
  154.             var bx = FlxG.random.float(0, FlxG.width - ball.getHitbox().width);
  155.             ball.setPosition(bx, -by);
  156.             ball.velocity.y = _ballVelocity;
  157.             ball.revive();
  158.         }
  159.         timer.reset();
  160.     }
  161.    
  162.     private function onCaught()
  163.     {
  164.         _stat.score += 100;
  165.         _stat.caught++;
  166.     }
  167.    
  168.     private function onMiss()
  169.     {
  170.         _stat.lives--;
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement