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

Commit

Permalink
feat: shaking trash effect
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Mar 10, 2024
1 parent b3031e3 commit a243d4e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:flame_bloc/flame_bloc.dart';
Expand All @@ -18,6 +19,7 @@ class PlayerCollectingTrashBehavior extends CollisionBehavior<Trash, Player>
}

bloc.add(GameCollectedTrashEvent(item: other.trashType));
other.removeFromParent();

other.collect();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'trash_shaking_behavior.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:async';
import 'dart:math';

import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:flame_noise/flame_noise.dart';
import 'package:trashy_road/src/game/game.dart';

final _random = Random(0);

/// Shakes the [Trash] around every now and then.
///
/// This is to catch the player's attention so they know that the [Trash] is
/// interactable and needs to be collected.
class TrashShakingBehavior extends Behavior<Trash> {
final _shakeDistance = Vector2(10, 4);

@override
FutureOr<void> onLoad() async {
await super.onLoad();

add(
TimerComponent(
repeat: true,
period: 2 + _random.nextDouble() * 2,
onTick: () {
parent.add(
MoveEffect.by(
_shakeDistance,
NoiseEffectController(
noise: WhiteNoise(
seed: _random.nextInt(100),
frequency: 5,
),
duration: 0.5,
),
),
);
},
),
);
}
}
10 changes: 6 additions & 4 deletions packages/trashy_road/lib/src/game/entities/trash/trash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:trashy_road/gen/assets.gen.dart';
import 'package:trashy_road/src/audio/audio.dart';
import 'package:trashy_road/src/game/game.dart';

export 'behaviors/behaviors.dart';

/// The different types of [Trash].
enum TrashType {
plastic._('plastic'),
Expand Down Expand Up @@ -49,6 +51,7 @@ class Trash extends PositionedEntity
drop: Vector2(0, -45),
minDuration: 0.1,
),
TrashShakingBehavior(),
PropagatingCollisionBehavior(
RectangleHitbox(
isSolid: true,
Expand Down Expand Up @@ -114,16 +117,15 @@ class Trash extends PositionedEntity
}
}

@override
void removeFromParent() {
/// Collects the trash.
void collect() {
game.audioBloc.playEffect(GameSoundEffects.plasticTrash);

findBehavior<PropagatingCollisionBehavior>()
.children
.whereType<RectangleHitbox>()
.first
.collisionType = CollisionType.inactive;
super.removeFromParent();
removeFromParent();
}

final TrashType trashType;
Expand Down
16 changes: 16 additions & 0 deletions packages/trashy_road/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
fast_noise:
dependency: transitive
description:
name: fast_noise
sha256: "2aa7bbde5599d2b6769750ee23536ec8a0096606fc9d10d29a3855e80715e1c6"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
ffi:
dependency: transitive
description:
Expand Down Expand Up @@ -376,6 +384,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.11.0"
flame_noise:
dependency: "direct main"
description:
name: flame_noise
sha256: e75afe58380156298deeca0f2b40b9e077fd4dea7b84a9b5c6e4536bf94a35e8
url: "https://pub.dev"
source: hosted
version: "0.3.0"
flame_test:
dependency: "direct dev"
description:
Expand Down
1 change: 1 addition & 0 deletions packages/trashy_road/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
flame_audio: ^2.10.0
flame_behaviors: ^1.1.0
flame_bloc: ^1.11.0
flame_noise: ^0.3.0
flame_tiled: ^1.19.0
flutter:
sdk: flutter
Expand Down

0 comments on commit a243d4e

Please sign in to comment.