Skip to content

Commit

Permalink
fix!: moving some entity logic to updateAfter
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtome committed Aug 12, 2024
1 parent 85180b1 commit fd86777
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
9 changes: 3 additions & 6 deletions examples/standard_platformer/lib/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ class Player extends JumperCharacter<ExamplePlatformerLeapGame> {
}

@override
void update(double dt) {
super.update(dt);

final wasAlive = isAlive;
final wasJumping = jumping;

void updateAfter(double dt) {
updateHandleInput(dt);

updateCollisionInteractions(dt);
Expand All @@ -71,6 +66,8 @@ class Player extends JumperCharacter<ExamplePlatformerLeapGame> {
if (!wasJumping && jumping) {
FlameAudio.play('jump.wav');
}

super.updateAfter(dt);
}

void resetPosition() {
Expand Down
13 changes: 13 additions & 0 deletions packages/leap/lib/src/characters/jumper_character.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/foundation.dart';
import 'package:leap/src/characters/jumper_behavior.dart';
import 'package:leap/src/entities/entities.dart';
import 'package:leap/src/leap_game.dart';

/// An example base class for a jumping player character
class JumperCharacter<TGame extends LeapGame> extends Character<TGame> {
JumperCharacter({super.removeOnDeath, super.health})
: super(behaviors: [JumperBehavior()]);
Expand All @@ -13,6 +15,9 @@ class JumperCharacter<TGame extends LeapGame> extends Character<TGame> {
/// Typically this means the jump button is being held down.
bool jumping = false;

/// Was [jumping] in previous game tick
bool wasJumping = false;

/// When true moves at [walkSpeed] in the direction the
/// character is facing.
bool walking = false;
Expand Down Expand Up @@ -40,6 +45,7 @@ class JumperCharacter<TGame extends LeapGame> extends Character<TGame> {
bool get isOnGround => collisionInfo.down;

@override
@mustCallSuper
void update(double dt) {
super.update(dt);

Expand All @@ -51,4 +57,11 @@ class JumperCharacter<TGame extends LeapGame> extends Character<TGame> {
}
}
}

@override
@mustCallSuper
void updateAfter(double dt) {
wasJumping = jumping;
super.updateAfter(dt);
}
}
16 changes: 5 additions & 11 deletions packages/leap/lib/src/entities/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Character<T extends LeapGame> extends PhysicalEntity<T> {
/// Whether or not this is "dead" (or destroyed) in the game.
bool get isDead => !isAlive;

/// Indicates that this was alive on the previous [update] loop
/// Indicates that this was alive on the previous game tick
bool wasAlive = true;

CharacterAnimation? _characterAnimation;
Expand Down Expand Up @@ -68,16 +68,7 @@ class Character<T extends LeapGame> extends PhysicalEntity<T> {

@override
@mustCallSuper
void updateTree(double dt) {
super.updateTree(dt);
wasAlive = isAlive;
}

@override
@mustCallSuper
void update(double dt) {
super.update(dt);

void updateAfter(double dt) {
if (isDead && wasAlive) {
if (removeOnDeath) {
_removingFromDeath = true;
Expand All @@ -91,5 +82,8 @@ class Character<T extends LeapGame> extends PhysicalEntity<T> {
_removingFromDeath = false;
removeFromParent();
}

wasAlive = isAlive;
super.updateAfter(dt);
}
}
13 changes: 5 additions & 8 deletions packages/leap/lib/src/entities/moving_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,12 @@ abstract class MovingPlatform<T extends LeapGame> extends PhysicalEntity<T> {

@override
@mustCallSuper
void update(double dt) {
super.update(dt);

void updateAfter(double dt) {
if (!_stopped) {
final prevX = x;
final prevY = y;

_updatePositionAndLoop(dt);

final deltaX = x - prevX;
final deltaY = y - prevY;
final deltaX = x - prevPosition.x;
final deltaY = y - prevPosition.y;
// Update the position of anything on top of this platform. Ideally
// this happens before the other entity's collision logic
world.physicals
Expand All @@ -92,6 +87,8 @@ abstract class MovingPlatform<T extends LeapGame> extends PhysicalEntity<T> {
element.y += deltaY;
});
}

super.updateAfter(dt);
}

void _updatePositionAndLoop(double dt) {
Expand Down
14 changes: 12 additions & 2 deletions packages/leap/lib/src/entities/physical_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import 'package:leap/leap.dart';
///
/// [static] components can be collided with but never move and have a much
/// smaller performance impact on the game loop.
///
/// See [update] for informatoin on ordering your logic, you may want to
/// use [updateAfter] or a [Behavior]
abstract class PhysicalEntity<TGame extends LeapGame> extends PositionedEntity
with HasGameRef<TGame> {
/// Position object to store the x/y components.
Expand Down Expand Up @@ -85,16 +88,23 @@ abstract class PhysicalEntity<TGame extends LeapGame> extends PositionedEntity
// `super.updateTree` calls `this.update` and then updates all children,
// which is where collision detection and position updates happen
super.updateTree(dt);

prevPosition.setFrom(position);
// after [this.update] and children are updated
updateAfter(dt);
}

/// Note that this is called BEFORE behaviors and other child components,
/// therefore you may prefer to use [updateAfter] or add a [Behavior]
@override
@mustCallSuper
void update(double dt) {
super.update(dt);
}

/// after [update] and children are updated
@mustCallSuper
void updateAfter(double dt) {
_updateDebugHitbox();
prevPosition.setFrom(position);
}

void _updateDebugHitbox() {
Expand Down

0 comments on commit fd86777

Please sign in to comment.