diff --git a/packages/trashy_road/assets/images/sprites/bird_flying.png b/packages/trashy_road/assets/images/sprites/bird_flying.png new file mode 100644 index 00000000..b8f84f61 Binary files /dev/null and b/packages/trashy_road/assets/images/sprites/bird_flying.png differ diff --git a/packages/trashy_road/lib/gen/assets.gen.dart b/packages/trashy_road/lib/gen/assets.gen.dart index b9a76ddf..7e8a9608 100644 --- a/packages/trashy_road/lib/gen/assets.gen.dart +++ b/packages/trashy_road/lib/gen/assets.gen.dart @@ -511,6 +511,10 @@ class $AssetsImagesSpritesGen { AssetGenImage get bench => const AssetGenImage('assets/images/sprites/bench.png'); + /// File path: assets/images/sprites/bird_flying.png + AssetGenImage get birdFlying => + const AssetGenImage('assets/images/sprites/bird_flying.png'); + /// File path: assets/images/sprites/building-shadow.png AssetGenImage get buildingShadow => const AssetGenImage('assets/images/sprites/building-shadow.png'); @@ -832,6 +836,7 @@ class $AssetsImagesSpritesGen { arrowWest, banana, bench, + birdFlying, buildingShadow, building2, building3, diff --git a/packages/trashy_road/lib/src/game/components/trashy_road_world/trashy_road_world.dart b/packages/trashy_road/lib/src/game/components/trashy_road_world/trashy_road_world.dart index 9dc00f0a..fa2eab64 100644 --- a/packages/trashy_road/lib/src/game/components/trashy_road_world/trashy_road_world.dart +++ b/packages/trashy_road/lib/src/game/components/trashy_road_world/trashy_road_world.dart @@ -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()); diff --git a/packages/trashy_road/lib/src/game/entities/bird/behaviors/behaviors.dart b/packages/trashy_road/lib/src/game/entities/bird/behaviors/behaviors.dart new file mode 100644 index 00000000..53ff2e38 --- /dev/null +++ b/packages/trashy_road/lib/src/game/entities/bird/behaviors/behaviors.dart @@ -0,0 +1,2 @@ +export 'bird_flying_behavior.dart'; +export 'bird_random_spawning_behavior.dart'; diff --git a/packages/trashy_road/lib/src/game/entities/bird/behaviors/bird_flying_behavior.dart b/packages/trashy_road/lib/src/game/entities/bird/behaviors/bird_flying_behavior.dart new file mode 100644 index 00000000..eb0a4c33 --- /dev/null +++ b/packages/trashy_road/lib/src/game/entities/bird/behaviors/bird_flying_behavior.dart @@ -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 + with HasGameReference { + /// 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; + } + } +} diff --git a/packages/trashy_road/lib/src/game/entities/bird/behaviors/bird_random_spawning_behavior.dart b/packages/trashy_road/lib/src/game/entities/bird/behaviors/bird_random_spawning_behavior.dart new file mode 100644 index 00000000..4ef10833 --- /dev/null +++ b/packages/trashy_road/lib/src/game/entities/bird/behaviors/bird_random_spawning_behavior.dart @@ -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 + with HasGameReference { + @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); + } +} diff --git a/packages/trashy_road/lib/src/game/entities/bird/bird.dart b/packages/trashy_road/lib/src/game/entities/bird/bird.dart new file mode 100644 index 00000000..9f08cb7e --- /dev/null +++ b/packages/trashy_road/lib/src/game/entities/bird/bird.dart @@ -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( + selector: (bird) => bird.findBehaviors(), + ), + ], + 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 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; +} diff --git a/packages/trashy_road/lib/src/game/entities/entities.dart b/packages/trashy_road/lib/src/game/entities/entities.dart index fb54330a..ef6135e7 100644 --- a/packages/trashy_road/lib/src/game/entities/entities.dart +++ b/packages/trashy_road/lib/src/game/entities/entities.dart @@ -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';