Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix jump after superjump pickup, add groundpound pickup #323

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 68 additions & 16 deletions Features/FlxFSM/source/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,35 @@ import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.tile.FlxTilemap;
import flixel.tweens.FlxEase;
import flixel.tweens.FlxTween;
import flixel.util.FlxColor;

using StringTools;

class PlayState extends FlxState
{
static inline var BASE_INFO:String = "LEFT & RIGHT to move, UP to jump";
static inline var GROUND_POUND_INFO:String = "DOWN (in the air) to ground-pound";
static inline var RESET_INFO:String = "R to Reset\n\nCurrent State: {STATE}";

var _map:FlxTilemap;
var _slime:Slime;
var _powerup:FlxSprite;
var _superJump:FlxSprite;
var _groundPound:FlxSprite;

var _info:String = "LEFT & RIGHT to move, UP to jump\nDOWN (in the air) " + "to ground-pound.\nR to Reset\n\nCurrent State: {STATE}";
var _info:String = BASE_INFO + "\n" + RESET_INFO;
var _txtInfo:FlxText;

override public function create():Void
{
bgColor = 0xff661166;
super.create();

_map = new FlxTilemap();
_map.loadMapFromArray([
final J = 99;
final G = 100;
final columns = 20;
final data = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
Expand All @@ -35,19 +44,35 @@ class PlayState extends FlxState
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, G, 0, 1,
1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 0, 0, J, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
], 20, 15, "assets/tiles.png", 16, 16);
];

// remove powerups from map, store index so we can place them
final superJumpIndex = data.indexOf(J);
data[superJumpIndex] = 0;

final groundPoundIndex = data.indexOf(G);
data[groundPoundIndex] = 0;

final tileSize = 16;
_map = new FlxTilemap();
_map.loadMapFromArray(data, 20, 15, "assets/tiles.png", tileSize, tileSize);
add(_map);

_slime = new Slime(192, 128);
add(_slime);

_powerup = new FlxSprite(48, 208, "assets/powerup.png");
add(_powerup);
_superJump = new FlxSprite((superJumpIndex % columns) * tileSize, Std.int(superJumpIndex / columns) * tileSize, "assets/powerup.png");
add(_superJump);

_groundPound = new FlxSprite((groundPoundIndex % columns) * tileSize, Std.int(groundPoundIndex / columns) * tileSize, "assets/powerup.png");
_groundPound.flipY = true;
_groundPound.y += 4;
add(_groundPound);

_txtInfo = new FlxText(16, 16, -1, _info);
add(_txtInfo);
Expand All @@ -58,7 +83,19 @@ class PlayState extends FlxState
super.update(elapsed);

FlxG.collide(_map, _slime);
FlxG.overlap(_slime, _powerup, getPowerup);

FlxG.overlap(_slime, _superJump, function (_, _)
{
_slime.addSuperJump();
_superJump.kill();
});

FlxG.overlap(_slime, _groundPound, function (_, _)
{
_slime.addGroundPound();
_groundPound.kill();
startGroundPoundInfoTween();
});

_txtInfo.text = _info.replace("{STATE}", Type.getClassName(_slime.fsm.stateClass));

Expand All @@ -67,12 +104,27 @@ class PlayState extends FlxState
FlxG.camera.flash(FlxColor.BLACK, .1, FlxG.resetState);
}
}

function getPowerup(slime:Slime, particle:FlxSprite):Void
function startGroundPoundInfoTween()
{
slime.fsm.transitions.replace(Slime.Jump, Slime.SuperJump);
slime.fsm.transitions.add(Slime.Jump, Slime.Idle, Slime.Conditions.grounded);

particle.kill();
final duration = 2.0;
final flash_period = 0.15 / duration;
final oldColor = _txtInfo.color;
final ease = FlxEase.linear;
FlxTween.num(0, 1, duration,
{
onComplete:function (_)
{
_txtInfo.color = oldColor;
_info = BASE_INFO + "\n" + GROUND_POUND_INFO + "\n" + RESET_INFO;
}
},
function(n)
{
_txtInfo.color = ((n / flash_period) % 1 > 0.5) ? FlxColor.YELLOW : oldColor;
final chars = Std.int(ease(n) * GROUND_POUND_INFO.length);
_info = BASE_INFO + "\n" + GROUND_POUND_INFO.substr(0, chars) + "\n" + RESET_INFO;
}
);
}
}
71 changes: 45 additions & 26 deletions Features/FlxFSM/source/Slime.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Slime extends FlxSprite
{
public static inline var GRAVITY:Float = 600;

public var fsm:FlxFSM<FlxSprite>;
public var fsm:FlxFSM<Slime>;

public function new(X:Float = 0, Y:Float = 0)
{
Expand All @@ -28,22 +28,41 @@ class Slime extends FlxSprite
acceleration.y = GRAVITY;
maxVelocity.set(100, GRAVITY);

fsm = new FlxFSM<FlxSprite>(this);
fsm.transitions.add(Idle, Jump, Conditions.jump)
.add(Jump, Idle, Conditions.grounded)
.add(Jump, GroundPound, Conditions.groundSlam)
.add(GroundPound, GroundPoundFinish, Conditions.grounded)
.add(GroundPoundFinish, Idle, Conditions.animationFinished)
.start(Idle);
fsm = new FlxFSM(this);
fsm.transitions.add(Idle, Jump, Conditions.jump);
fsm.transitions.add(Jump, Idle, Conditions.grounded);
fsm.transitions.start(Idle);
}

public function addSuperJump()
{
// remove regular jump now
fsm.transitions.remove(Idle, Jump, Conditions.jump, true);
fsm.transitions.add(Idle, SuperJump, Conditions.jump);
// replace the rest when it's safe
fsm.transitions.replace(Jump, SuperJump, false);
}

public function addGroundPound()
{
fsm.transitions.add(GroundPound, GroundPoundFinish, Conditions.grounded);
fsm.transitions.add(GroundPoundFinish, Idle, Conditions.animationFinished);

if (fsm.transitions.hasTransition(Idle, Jump))
fsm.transitions.add(Jump, GroundPound, Conditions.groundSlam);

if (fsm.transitions.hasTransition(Idle, SuperJump))
fsm.transitions.add(SuperJump, GroundPound, Conditions.groundSlam);

}

override public function update(elapsed:Float):Void
override function update(elapsed:Float):Void
{
fsm.update(elapsed);
super.update(elapsed);
}

override public function destroy():Void
override function destroy():Void
{
fsm.destroy();
fsm = null;
Expand Down Expand Up @@ -74,14 +93,14 @@ class Conditions
}
}

class Idle extends FlxFSMState<FlxSprite>
class Idle extends FlxFSMState<Slime>
{
override public function enter(owner:FlxSprite, fsm:FlxFSM<FlxSprite>):Void
override function enter(owner:Slime, fsm:FlxFSM<Slime>):Void
{
owner.animation.play("standing");
}

override public function update(elapsed:Float, owner:FlxSprite, fsm:FlxFSM<FlxSprite>):Void
override function update(elapsed:Float, owner:Slime, fsm:FlxFSM<Slime>):Void
{
owner.acceleration.x = 0;
if (FlxG.keys.pressed.LEFT || FlxG.keys.pressed.RIGHT)
Expand All @@ -98,15 +117,15 @@ class Idle extends FlxFSMState<FlxSprite>
}
}

class Jump extends FlxFSMState<FlxSprite>
class Jump extends FlxFSMState<Slime>
{
override public function enter(owner:FlxSprite, fsm:FlxFSM<FlxSprite>):Void
override function enter(owner:Slime, fsm:FlxFSM<Slime>):Void
{
owner.animation.play("jumping");
owner.velocity.y = -200;
}

override public function update(elapsed:Float, owner:FlxSprite, fsm:FlxFSM<FlxSprite>):Void
override function update(elapsed:Float, owner:Slime, fsm:FlxFSM<Slime>):Void
{
owner.acceleration.x = 0;
if (FlxG.keys.pressed.LEFT || FlxG.keys.pressed.RIGHT)
Expand All @@ -118,29 +137,29 @@ class Jump extends FlxFSMState<FlxSprite>

class SuperJump extends Jump
{
override public function enter(owner:FlxSprite, fsm:FlxFSM<FlxSprite>):Void
override function enter(owner:Slime, fsm:FlxFSM<Slime>):Void
{
owner.animation.play("jumping");
owner.velocity.y = -300;
}
}

class GroundPound extends FlxFSMState<FlxSprite>
class GroundPound extends FlxFSMState<Slime>
{
var _ticks:Float;
var time:Float;

override public function enter(owner:FlxSprite, fsm:FlxFSM<FlxSprite>):Void
override function enter(owner:Slime, fsm:FlxFSM<Slime>):Void
{
owner.animation.play("pound");
owner.velocity.x = 0;
owner.acceleration.x = 0;
_ticks = 0;
time = 0;
}

override public function update(elapsed:Float, owner:FlxSprite, fsm:FlxFSM<FlxSprite>):Void
override function update(elapsed:Float, owner:Slime, fsm:FlxFSM<Slime>):Void
{
_ticks++;
if (_ticks < 15)
time += elapsed;
if (time < 0.25)
{
owner.velocity.y = 0;
}
Expand All @@ -151,9 +170,9 @@ class GroundPound extends FlxFSMState<FlxSprite>
}
}

class GroundPoundFinish extends FlxFSMState<FlxSprite>
class GroundPoundFinish extends FlxFSMState<Slime>
{
override public function enter(owner:FlxSprite, fsm:FlxFSM<FlxSprite>):Void
override function enter(owner:Slime, fsm:FlxFSM<Slime>):Void
{
owner.animation.play("landing");
FlxG.camera.shake(0.025, 0.25);
Expand Down
Loading