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

Commit

Permalink
feat: add birds (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliePugh authored Mar 12, 2024
1 parent c761ca8 commit 8982a35
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/trashy_road/lib/gen/assets.gen.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class TrashyRoadWorld extends PositionComponent {
final obstaclesLayer = tileMap.getObjectGroup(_TiledLayer.obstacles.name);
await addAll(obstaclesLayer.objects.map(Obstacle.fromTiledObject));

await addAll(Bird.randomAmount());

await add(_TiledFloor());

size = Vector2(tileMap.width.toDouble(), tileMap.height.toDouble());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'bird_flying_behavior.dart';
export 'bird_random_spawning_behavior.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:flame/components.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:trashy_road/src/game/game.dart';

class BirdFlyingBehavior extends Behavior<Bird>
with HasGameReference<TrashyRoadGame> {
/// The speed multiplier for the bird.
static const birdSpeedMultiplier = 30;

@override
void update(double dt) {
super.update(dt);

final distanceCovered = birdSpeedMultiplier * dt * parent.speed;

final direction = parent.isFlyingRight ? 1 : -1;
parent.position.x += distanceCovered * direction;

final isWithinBound = game.bounds!.isPointInside(parent.position);

// has exited the screen
if (!isWithinBound) {
parent.position.setAll(0);
if (parent.isFlyingRight) {
parent.position.x = 0;
} else {
parent.position.x = game.bounds!.bottomRight.x;
}

parent.position.y = Bird.random.nextDouble() * game.bounds!.bottomRight.y;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:flame/components.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:trashy_road/src/game/game.dart';

class BirdRandomSpawningBehavior extends Behavior<Bird>
with HasGameReference<TrashyRoadGame> {
@override
void onLoad() {
if (!parent.isFlyingRight) {
parent.flipHorizontally();
}

final x = Bird.random.nextDouble() * game.bounds!.bottomRight.x;
final y = Bird.random.nextDouble() * game.bounds!.bottomRight.y;
parent.position = Vector2(x, y);
}
}
63 changes: 63 additions & 0 deletions packages/trashy_road/lib/src/game/entities/bird/bird.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:math';

import 'package:flame/components.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:trashy_road/gen/assets.gen.dart';
import 'package:trashy_road/src/game/game.dart';

export 'behaviors/behaviors.dart';

class Bird extends PositionedEntity with ZIndex {
Bird._({required this.isFlyingRight, required this.speed})
: super(
scale: Vector2.all(0.5),
behaviors: [
BirdFlyingBehavior(),
BirdRandomSpawningBehavior(),
PausingBehavior<Bird>(
selector: (bird) => bird.findBehaviors<BirdFlyingBehavior>(),
),
],
children: [
GameSpriteAnimationComponent.fromPath(
spritePath: Assets.images.sprites.birdFlying.path,
animationData: SpriteAnimationData.sequenced(
amount: 25,
amountPerRow: 5,
textureSize: Vector2.all(128),
stepTime: 1 / 24,
),
),
],
);

factory Bird.randomize() {
final isFlyingRight = random.nextBool();
final speed = minSpeed + random.nextDouble() * (maxSpeed - minSpeed);

return Bird._(isFlyingRight: isFlyingRight, speed: speed);
}

static List<Bird> randomAmount() {
final amountOfBirds = random.nextInt(maxAmountOfBirds + 1);

return List.generate(amountOfBirds, (_) => Bird.randomize());
}

static final random = Random();

/// The maximum amount of birds that are loaded into a map.
static const maxAmountOfBirds = 5;

static const maxSpeed = 3.0;
static const minSpeed = 1.0;

/// Whether the bird is flying right.
final bool isFlyingRight;

/// Speed Multiplier
final double speed;

@override
int get zIndex => 100000;
}
1 change: 1 addition & 0 deletions packages/trashy_road/lib/src/game/entities/entities.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export 'behaviors/behaviors.dart';
export 'bird/bird.dart';
export 'bus/bus.dart';
export 'car/car.dart';
export 'map_edge/map_edge.dart';
Expand Down

0 comments on commit 8982a35

Please sign in to comment.