Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import flixel.FlxCamera;
- import flixel.FlxG;
- import flixel.FlxObject;
- import flixel.FlxState;
- import flixel.FlxSprite;
- import flixel.group.FlxGroup;
- import flixel.math.FlxPoint;
- import flixel.text.FlxText;
- import flixel.util.FlxColor;
- import flixel.util.FlxTimer;
- import flixel.util.FlxSpriteUtil;
- import flixel.addons.effects.FlxTrail;
- import flixel.effects.particles.FlxEmitter;
- import flixel.effects.particles.FlxParticle;
- class PlayState extends FlxState
- {
- public var level:TiledLevel;
- private var ground:FlxSprite;
- public var player:Player;
- private var trail:FlxTrail;
- // Handles Particle Generation
- public var _emitter:FlxEmitter;
- public var enemyGroup:EnemyGroup;
- private var enemySpawnTimer:FlxTimer;
- private var vacume:FlxSprite;
- private var scoreTxt:FlxText;
- override public function create():Void
- {
- super.create();
- Reg.debug();
- setProperties();
- initVacume();
- initPlayer();
- initEffectsEmitter();
- initEnemies();
- initGround();
- initText();
- initTiledLevel(AssetPaths.level__tmx, this);
- }
- private function setProperties():Void
- {
- #if debug
- FlxG.log.redirectTraces = true;
- #end
- FlxG.camera.fade(Reg.BGColor, 3.3, true);
- FlxG.mouse.visible = false;
- FlxG.cameras.bgColor = Reg.BGColor;
- FlxG.sound.playMusic(AssetPaths.Gloomy_battle_theme__mp3, Reg.musicVolume, true);
- }
- private function initPlayer():Void
- {
- player = new Player(this);
- initPlayerTrail();
- add(player);
- }
- private function initPlayerTrail():Void
- {
- trail = new FlxTrail(player, null, 10, 0);
- add(trail);
- }
- private function initEffectsEmitter():Void
- {
- var particles:Int = 1000;
- var colors:Array<Int> = [Reg.mainColor, Reg.BGColor];
- _emitter = new FlxEmitter(200, FlxG.height / 2, particles);
- _emitter.speed.set(750);
- _emitter.acceleration.start.min.y = -50;
- _emitter.acceleration.start.max.y = -100;
- _emitter.acceleration.end.min.y = -700;
- _emitter.acceleration.end.max.y = -900;
- var particle:FlxParticle;
- for (i in 0...particles)
- {
- particle = new FlxParticle();
- particle.makeGraphic(3, 3, FlxG.random.getObject(colors));
- particle.exists = false;
- _emitter.add(particle);
- }
- add(_emitter);
- }
- private function initEnemies():Void
- {
- enemyGroup = new EnemyGroup(35);
- add(enemyGroup);
- }
- var ticks:Float = 0;
- private function emitEnemies(elapsed:Float):Void
- {
- var rand = FlxG.random.getObject([0.3, 1, 2, 3, 4], [10 + vacume.y, 20, 30, 35, 40]);
- if (ticks >= rand)
- {
- enemyGroup.emit();
- rand = FlxG.random.getObject([0.3, 1, 2, 3, 4], [10 + vacume.y, 20, 30, 35, 40]);
- ticks -= ticks;
- }
- else
- {
- ticks += elapsed;
- }
- }
- private function initGround():Void
- {
- ground = new FlxSprite(-40, 416).makeGraphic(FlxG.width + 70, 40, FlxColor.TRANSPARENT);
- ground.immovable = true;
- add(ground);
- }
- private function initVacume():Void
- {
- var h = Std.int(FlxG.height / 4);
- vacume = new FlxSprite(0, 0);
- vacume.loadGraphic(AssetPaths.vacume__png);
- vacume.scale.scale(4);
- vacume.offset.y += 64;
- vacume.screenCenter(X);
- add(vacume);
- FlxG.watch.add(vacume, "y");
- FlxG.watch.add(Reg, "vacumeTimer");
- FlxG.watch.add(Reg, "difficulty");
- }
- private function initText():Void
- {
- scoreTxt = new FlxText(30);
- scoreTxt.text = "Bunnies Stomped: " + Reg.bunnyCounter;
- scoreTxt.setFormat(Reg.universalFont, 40, FlxColor.WHITE, CENTER, OUTLINE_FAST, FlxColor.BLACK);
- scoreTxt.y += (scoreTxt.height + (scoreTxt.height / 4));
- add(scoreTxt);
- }
- private function initTiledLevel(levelPath:String, state:PlayState):Void
- {
- level = new TiledLevel(levelPath, state);
- // Add backgrounds
- add(level.backgroundLayer);
- // Add static images
- add(level.imagesLayer);
- // Load player objects
- add(level.objectsLayer);
- // Add foreground tiles after adding level objects, so these tiles render on top of player
- add(level.foregroundTiles);
- }
- override public function update(elapsed:Float):Void
- {
- super.update(elapsed);
- updatePolish();
- updateVars(elapsed);
- updateTxt();
- updateCollisions();
- emitEnemies(elapsed);
- handleInput();
- }
- private function updatePolish():Void
- {
- FlxSpriteUtil.screenWrap(player, true, true);
- }
- private function updateVars(elapsed:Float):Void
- {
- Reg.difficulty += (elapsed / 4);
- Reg.vacumeTimer += (elapsed * Reg.difficulty);
- vacume.y = Reg.vacumeTimer;
- if (vacume.y >= (416 - 32))
- FlxG.switchState(new GameOver());
- }
- private function updateTxt():Void
- {
- scoreTxt.text = "Bunnies Stomped: " + Reg.bunnyCounter;
- }
- private function updateCollisions():Void
- {
- // Collide with foreground tile layer
- level.collideWithLevel(player);
- FlxG.collide(player, ground);
- FlxG.overlap(player, enemyGroup, checkCollision);
- }
- private function checkCollision(obj1:Player, obj2:Enemy):Void
- {
- if (obj1.fsm.stateClass == Player.DropKick)
- {
- Reg.vacumeTimer -= 5;
- Reg.bunnyCounter++;
- FlxG.sound.play(AssetPaths.hit__mp3, Reg.sfxVolume);
- FlxG.camera.shake(0.005, 0.10);
- _emitter.setPosition(obj2.x + (obj2.width / 2), obj2.y + obj2.height);
- _emitter.start(true, 0, 150);
- obj2.kill();
- }
- }
- private function handleInput():Void
- {
- if (FlxG.keys.justReleased.R)
- {
- FlxG.camera.flash(FlxColor.BLACK, 1, FlxG.resetState);
- }
- }
- }
Add Comment
Please, Sign In to add comment