Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into alestiago/readme
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago authored Mar 12, 2024
2 parents 0d0ab80 + bb1032f commit 0cb70ba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ import 'package:flame/components.dart';
/// Marks a component as untraversable.
///
/// Untraversable components are not traversable by the `Player`.
mixin Untraversable on PositionComponent {}
mixin Untraversable on PositionComponent {
bool untraversable = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import 'package:trashy_road/src/game/game.dart';
/// This behavior is meant to be used in conjunction with
/// [PlayerMovingBehavior].
class PlayerDragMovingBehavior extends Behavior<Player> {
static const _swipeThreshold = 50;

late final PlayerMovingBehavior _playerMovingBehavior;

Offset _startPosition = Offset.zero;
Expand Down Expand Up @@ -46,15 +44,15 @@ class PlayerDragMovingBehavior extends Behavior<Player> {
final isVertical = verticalDelta.abs() > horizontalDelta.abs();

if (isVertical) {
final isUpwardsSwipe = verticalDelta < -_swipeThreshold;
final isUpwardsSwipe = verticalDelta < 0;

if (isUpwardsSwipe) {
_playerMovingBehavior.move(Direction.up);
} else {
_playerMovingBehavior.move(Direction.down);
}
} else {
final isRightSwipe = horizontalDelta > _swipeThreshold;
final isRightSwipe = horizontalDelta > 0;

if (isRightSwipe) {
_playerMovingBehavior.move(Direction.right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class PlayerObstacleBehavior extends CollisionBehavior<Untraversable, Player> {
Untraversable other,
) {
super.onCollisionStart(intersectionPoints, other);
parent.findBehavior<PlayerMovingBehavior>().bounceBack();
if (other.untraversable) {
parent.findBehavior<PlayerMovingBehavior>().bounceBack();
}
}
}
22 changes: 16 additions & 6 deletions packages/trashy_road/lib/src/game/entities/trash/trash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:meta/meta.dart';
import 'package:tiled/tiled.dart';
import 'package:trashy_road/game_settings.dart';
Expand Down Expand Up @@ -36,7 +37,7 @@ enum TrashType {
/// Trash is usually scattered around the road and the player has to pick it up
/// to keep the map clean.
class Trash extends PositionedEntity
with HasGameReference<TrashyRoadGame>, ZIndex {
with HasGameReference<TrashyRoadGame>, ZIndex, Untraversable {
Trash._({
required Vector2 position,
required this.trashType,
Expand All @@ -62,6 +63,7 @@ class Trash extends PositionedEntity
],
) {
zIndex = position.y.floor();
untraversable = false;
}

Trash._plastic({
Expand Down Expand Up @@ -120,14 +122,22 @@ class Trash extends PositionedEntity
/// Collects the trash.
void collect() {
game.audioBloc.playEffect(GameSoundEffects.trashCollected);
findBehavior<PropagatingCollisionBehavior>()
.children
.whereType<RectangleHitbox>()
.first
.collisionType = CollisionType.inactive;
removeFromParent();
}

@override
Future<void> onLoad() async {
await super.onLoad();
add(
FlameBlocListener<GameBloc, GameState>(
onNewState: _toggleTraversable,
),
);
}

void _toggleTraversable(GameState state) =>
untraversable = state.inventory.items.length == Inventory.size;

final TrashType trashType;
}

Expand Down

0 comments on commit 0cb70ba

Please sign in to comment.