Skip to content

Commit

Permalink
feat: removing faceLeft in favor of direction enums (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtome authored Oct 7, 2024
1 parent ced8074 commit cce48df
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
20 changes: 12 additions & 8 deletions examples/standard_platformer/lib/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Player extends JumperCharacter
velocity.x = 0;
velocity.y = 0;
airXVelocity = 0;
faceLeft = false;
walkDirection = HorizontalDirection.left;
jumping = false;

FlameAudio.play('spawn.wav');
Expand Down Expand Up @@ -165,7 +165,11 @@ class PlayerInputBehavior extends PhysicalBehavior<Player> {
parent.airXVelocity = parent.walkSpeed;
parent.isWalking = true;
// Make sure the player exits the ladder facing the direction jumped
parent.faceLeft = parent._input.isPressedLeft;
if (parent._input.isPressedLeft) {
parent.walkDirection = HorizontalDirection.left;
} else {
parent.walkDirection = HorizontalDirection.right;
}
}
}
}
Expand All @@ -175,7 +179,7 @@ class PlayerInputBehavior extends PhysicalBehavior<Player> {
if (parent._input.justPressed && parent._input.isPressedLeft) {
// Tapped left.
if (parent.isWalking) {
if (parent.faceLeft) {
if (parent.walkDirection == HorizontalDirection.left) {
// Already moving left.
if (parent.collisionInfo.down) {
parent.jumping = true;
Expand All @@ -185,20 +189,20 @@ class PlayerInputBehavior extends PhysicalBehavior<Player> {
if (parent.collisionInfo.down) {
parent.isWalking = false;
}
parent.faceLeft = true;
parent.walkDirection = HorizontalDirection.left;
}
} else {
// Standing still.
parent.isWalking = true;
parent.faceLeft = true;
parent.walkDirection = HorizontalDirection.left;
if (parent.collisionInfo.down) {
parent.airXVelocity = parent.walkSpeed;
}
}
} else if (parent._input.justPressed && parent._input.isPressedRight) {
// Tapped right.
if (parent.isWalking) {
if (!parent.faceLeft) {
if (parent.walkDirection == HorizontalDirection.right) {
// Already moving right.
if (parent.collisionInfo.down) {
parent.jumping = true;
Expand All @@ -208,12 +212,12 @@ class PlayerInputBehavior extends PhysicalBehavior<Player> {
if (parent.collisionInfo.down) {
parent.isWalking = false;
}
parent.faceLeft = false;
parent.walkDirection = HorizontalDirection.right;
}
} else {
// Standing still.
parent.isWalking = true;
parent.faceLeft = false;
parent.walkDirection = HorizontalDirection.right;
if (parent.collisionInfo.down) {
parent.airXVelocity = parent.walkSpeed;
}
Expand Down
9 changes: 4 additions & 5 deletions examples/standard_platformer/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,10 @@ packages:
flame_tiled:
dependency: "direct main"
description:
name: flame_tiled
sha256: bb00888e0893413ffeb083e24cee3d5f7c08cbae5d8df551f14a564fdf7c5282
url: "https://pub.dev"
source: hosted
version: "1.20.3"
path: "../../../flame/packages/flame_tiled"
relative: true
source: path
version: "1.18.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import 'package:leap/src/characters/jumper_character.dart';
import 'package:leap/src/entities/behaviors/physical_behavior.dart';
import 'package:leap/src/entities/ladder.dart';
import 'package:leap/src/utils/direction.dart';

/// Updates [velocity] for jumper's movement.
class JumperAccelerationBehavior extends PhysicalBehavior<JumperCharacter> {
Expand Down Expand Up @@ -44,7 +45,7 @@ class JumperAccelerationBehavior extends PhysicalBehavior<JumperCharacter> {
// Only apply walking acceleration when on ground
if (parent.collisionInfo.down) {
if (parent.isWalking) {
if (parent.faceLeft) {
if (parent.walkDirection == HorizontalDirection.left) {
velocity.x = -parent.walkSpeed;
} else {
velocity.x = parent.walkSpeed;
Expand All @@ -55,7 +56,7 @@ class JumperAccelerationBehavior extends PhysicalBehavior<JumperCharacter> {
parent.airXVelocity = velocity.x.abs();
} else {
// in the air
if (parent.faceLeft) {
if (parent.walkDirection == HorizontalDirection.left) {
velocity.x = -parent.airXVelocity;
} else {
velocity.x = parent.airXVelocity;
Expand Down
2 changes: 1 addition & 1 deletion packages/leap/lib/src/characters/jumper_character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import 'package:leap/src/leap_game.dart';
/// entity class with similar mixins and add the appropriate
/// behaviors.
class JumperCharacter<TGame extends LeapGame> extends PhysicalEntity
with HasJumps, HasWalkSpeed, HasFaceLeft, HasHealth {
with HasJumps, HasWalkSpeed, HasHealth {
// No behavior, just mixins and state
}
3 changes: 0 additions & 3 deletions packages/leap/lib/src/entities/mixins/has_face_left.dart

This file was deleted.

4 changes: 2 additions & 2 deletions packages/leap/lib/src/entities/mixins/has_walk_speed.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:leap/src/entities/physical_entity.dart';
import 'package:leap/leap.dart';

mixin HasWalkSpeed on PhysicalEntity {
/// Wether or not this is currently facing left
bool faceLeft = false;
HorizontalDirection walkDirection = HorizontalDirection.left;

/// Wether or not walk speed should be applied
bool isWalking = false;
Expand Down
1 change: 0 additions & 1 deletion packages/leap/lib/src/entities/mixins/mixins.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export 'has_animation_group.dart';
export 'has_death_animation.dart';
export 'has_face_left.dart';
export 'has_health.dart';
export 'has_jumps.dart';
export 'has_walk_speed.dart';

0 comments on commit cce48df

Please sign in to comment.