Skip to content

Commit

Permalink
feat: animate new slimes
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Aug 9, 2023
1 parent 4743b48 commit 57585bc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
37 changes: 34 additions & 3 deletions lib/flame/components/slime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Slime extends BodyComponent with ContactCallbacks {
_animation.givesPlayerABullet = value;
}

/// Whether the body has been created yet.
/// This is used to prevent the body from being created multiple times,
/// since the body is created before [onLoad] is called.
bool bodyCreated = false;

Slime({
required this.position,
required this.maxHp,
Expand Down Expand Up @@ -92,20 +97,45 @@ class Slime extends BodyComponent with ContactCallbacks {
}
}

/// Moves a new slime in from the top of the screen
void moveInFromTop(Duration duration) {
assert(position.y == 0, 'Slime must be at the top of the screen');
_startMovement(_SlimeMovement(
startingPosition: position.clone()..y = size.y * -0.5,
targetPosition: position.clone(),
totalSeconds: duration.inMilliseconds / 1000,
));
}

/// Moves the slime down to the next row
void moveDown(Duration duration) {
_movement = _SlimeMovement(
_startMovement(_SlimeMovement(
startingPosition: body.position.clone(),
targetPosition: body.position + Vector2(0, size.y / 2),
totalSeconds: duration.inMilliseconds / 1000,
);
));
}

void _startMovement(_SlimeMovement movement) {
_movement = movement;
_animation.walking = true;

// Create body if it hasn't been created yet,
// and set its starting position
body = createBody();
position.setFrom(movement.startingPosition);
body.position.setFrom(movement.startingPosition);

// Set the body's velocity
body.setType(BodyType.kinematic);
body.linearVelocity = _movement!.velocity;
body.linearVelocity = movement.velocity;
}

@override
Body createBody() {
if (bodyCreated) return body;
bodyCreated = true;

final shape = PolygonShape()
..set([
Vector2(9, 15),
Expand Down Expand Up @@ -196,6 +226,7 @@ class _SlimeAnimation extends SpriteAnimationGroupComponent<SlimeState>
Future<void> onLoad() async {
animations = await getAnimations();
current = SlimeState.idle;
walking = _walking;
await super.onLoad();

width = 32;
Expand Down
30 changes: 18 additions & 12 deletions lib/flame/ricochlime_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,35 @@ class RicochlimeGame extends Forge2DGame with PanDetector {

/// Moves the existing slimes down and spawns new ones at the top
Future<void> spawnNewSlimes() async {
const moveDownDuration = Duration(seconds: 1);
const slimeMoveDuration = Duration(seconds: 1);

// remove slimes that have been killed
slimes.removeWhere((slime) => slime.parent == null);
if (slimes.isNotEmpty) {
for (final slime in slimes) {
slime.moveDown(moveDownDuration);
}
await Future.delayed(moveDownDuration);

// move existing slimes down
for (final slime in slimes) {
slime.moveDown(slimeMoveDuration);
}

// spawn new slimes at the top
score.value++;
final row = createNewRow(
random: random,
slimeHp: score.value,
);
for (final component in row) {
if (component == null) {
continue;
}
slimes.add(component);
add(component);
for (final slime in row) {
if (slime == null) continue;

slimes.add(slime);
add(slime);

// trigger the slime's animation
slime.moveInFromTop(slimeMoveDuration);
}

// wait for the slimes to move
await Future.delayed(slimeMoveDuration);

await saveGame();
}

Expand Down

0 comments on commit 57585bc

Please sign in to comment.