Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ns.catchme;
- import flixel.FlxState;
- import flixel.FlxSprite;
- import flixel.FlxG;
- import flixel.effects.particles.FlxEmitter;
- import flixel.group.FlxGroup.FlxTypedGroup;
- import flixel.math.FlxPoint;
- import flixel.math.FlxRect;
- import flixel.text.FlxText;
- import flixel.util.FlxTimer;
- import flixel.util.FlxColor;
- class PlayState extends FlxState
- {
- private static inline var SPEED: Int = 250;
- private static inline var RESPAWN_TIME: Float = 1;
- private var _bg: FlxSprite;
- private var _platform: Platform;
- private var _ground: FlxSprite;
- private var _groundHt: FlxRect;
- private var _balls: FlxTypedGroup<Ball>;
- private var _ballVelocity: Int = 400;
- private var _spawnTimer : FlxTimer;
- private var _stat: GameStat;
- private var _scoreText: FlxText;
- private var _livesHud: LivesHud;
- private var _emitter: FlxEmitter;
- private var _emitterHelper: EmitterHelper;
- private var _platformHitbox: FlxRect;
- private var _groundHitbox: FlxRect;
- override public function create():Void
- {
- super.create();
- _bg = new FlxSprite(0, 0, AssetPaths.bg__png);
- _ground = new FlxSprite(0, FlxG.height - 64, AssetPaths.bottom__png);
- _groundHt = new FlxRect(0, FlxG.height - 96, FlxG.width, 32);
- _platform = new Platform(FlxG.width / 2, FlxG.height - 128);
- _balls = new FlxTypedGroup<Ball>();
- for (i in 1...16)
- {
- var ball = new Ball();
- ball.kill();
- _balls.add(ball);
- }
- _spawnTimer = new FlxTimer();
- _spawnTimer.start(RESPAWN_TIME, onSpawn);
- _livesHud = new LivesHud();
- _livesHud.setPosition(FlxG.width - 128, 0);
- _stat = new GameStat();
- _stat.onScoreUpdated.add(function ()
- {
- _scoreText.text = _stat.scoreText;
- });
- _stat.onLivesUpdated.add(function ()
- {
- _livesHud.lives = _stat.lives;
- });
- _scoreText = new FlxText();
- _scoreText.size = 40;
- _scoreText.text = _stat.scoreText;
- _stat.reset();
- initEmitter();
- initHitboxes();
- // ОКРУЖЕНИЕ
- add(_bg);
- add(_ground);
- // GAME OBJECTS
- add(_balls);
- add(_platform);
- add(_emitter);
- // HUD
- add(_scoreText);
- add(_livesHud);
- }
- private function initEmitter()
- {
- _emitter = new FlxEmitter(0, 0, 256);
- _emitter.makeParticles(4, 4, FlxColor.WHITE, 256);
- _emitter.lifespan.max = 1.5;
- _emitter.speed.set(300, 600, 20, 100);
- _emitter.acceleration.set(0, 1000);
- _emitter.launchAngle.set( -105, -75);
- _emitterHelper = new EmitterHelper(_emitter);
- }
- private function initHitboxes()
- {
- _platformHitbox = new FlxRect();
- var p = new FlxPoint(_platform.width + 60, 32);
- _platformHitbox.fromTwoPoints(new FlxPoint(0, 0), p);
- _groundHitbox = new FlxRect(0, FlxG.height - 96, FlxG.width, 32);
- }
- override public function update(elapsed:Float):Void
- {
- super.update(elapsed);
- _platformHitbox.setPosition(_platform.x, _platform.y);
- _platformHitbox.offset(-32, -32);
- var ch = checkBall;
- _balls.forEachAlive(ch);
- if (_stat.lives < 0)
- {
- FlxG.switchState(new MenuState());
- }
- }
- private function checkBall(ball: Ball)
- {
- var p = ball.getMidpoint();
- if (_platformHitbox.containsPoint(p))
- {
- _emitterHelper.emitOnPoint(p.x, p.y + 32, 64);
- ball.kill();
- onCaught();
- }
- if (_groundHitbox.containsPoint(p))
- {
- _emitterHelper.emitOnPoint(p.x, p.y + 32, 64);
- ball.kill();
- onMiss();
- }
- }
- private function onSpawn(timer: FlxTimer)
- {
- var ball = _balls.getFirstDead();
- if (ball != null)
- {
- var by = ball.getHitbox().height;
- var bx = FlxG.random.float(0, FlxG.width - ball.getHitbox().width);
- ball.setPosition(bx, -by);
- ball.velocity.y = _ballVelocity;
- ball.revive();
- }
- timer.reset();
- }
- private function onCaught()
- {
- _stat.score += 100;
- _stat.caught++;
- }
- private function onMiss()
- {
- _stat.lives--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement