Skip to content

Commit

Permalink
Merge pull request #2 from marcossevilla/fix/camera
Browse files Browse the repository at this point in the history
fix: camera and input sequences
  • Loading branch information
marcossevilla authored Sep 14, 2023
2 parents 5a7ba98 + 1c1c1ae commit cd324ed
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/standard_platformer/lib/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class Player extends JumperCharacter<ExamplePlatformerLeapGame> {
velocity.y = -minJumpImpulse;
}

for (final other in collisionInfo.otherCollisions) {
for (final other in collisionInfo.otherCollisions ?? const []) {
if (other is Coin) {
other.removeFromParent();
coins++;
Expand Down
10 changes: 8 additions & 2 deletions packages/leap/lib/src/input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ class SimpleTapInput extends PositionComponent
bool get isPressed => downEvent != null && upEvent == null;

bool get isPressedLeft {
print(upEvent);
final upEventInfo = upEvent?.asInfo(game);
return isPressed &&
upEventInfo!.eventPosition.global.x < gameRef.canvasSize.x / 2;
if (upEventInfo != null) {
return isPressed &&
upEventInfo.eventPosition.global.x < gameRef.canvasSize.x / 2;
}
return false;
}

bool get isPressedRight => isPressed && !isPressedLeft;
Expand All @@ -90,6 +94,7 @@ class SimpleTapInput extends PositionComponent

@override
bool onTapUp(TapUpEvent event) {
print(event);
upEvent = event;
return true;
}
Expand All @@ -108,6 +113,7 @@ class SimpleTapInput extends PositionComponent
}

void reset() {
print('reset');
downEvent = null;
upEvent = null;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/leap/lib/src/leap_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class LeapGame extends FlameGame with HasTrackedComponents {
height: tileSize * map.height,
);

await addAll([map, world, cameraComponent]);
world.add(map);

await addAll([world, cameraComponent]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CollisionDetectionBehavior extends PhysicalBehavior {
world.physicals.where((p) => p.collisionType == CollisionType.standard);
for (final other in nonMapCollidables) {
if (intersects(other)) {
collisionInfo.otherCollisions.add(other);
collisionInfo.otherCollisions?.add(other);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CollisionInfo {
this.downCollision,
this.leftCollision,
this.rightCollision,
this.otherCollisions = const [],
this.otherCollisions,
});

/// Component that this is colliding with on top.
Expand All @@ -23,7 +23,7 @@ class CollisionInfo {
LeapMapGroundTile? rightCollision;

/// Non-map collisions.
final List<PhysicalEntity> otherCollisions;
List<PhysicalEntity>? otherCollisions;

/// Is currently colliding on top
bool get up => upCollision != null;
Expand Down Expand Up @@ -53,7 +53,7 @@ class CollisionInfo {
downCollision = null;
leftCollision = null;
rightCollision = null;
otherCollisions.clear();
otherCollisions = null;
}

void copyFrom(CollisionInfo collisionInfo) {
Expand All @@ -62,7 +62,7 @@ class CollisionInfo {
leftCollision = collisionInfo.leftCollision;
rightCollision = collisionInfo.rightCollision;

otherCollisions.clear();
otherCollisions.addAll(collisionInfo.otherCollisions);
otherCollisions?.clear();
otherCollisions?.addAll(collisionInfo.otherCollisions ?? []);
}
}

0 comments on commit cd324ed

Please sign in to comment.