This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/trashy_road/lib/src/game/entities/bird/behaviors/behaviors.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
33 changes: 33 additions & 0 deletions
33
packages/trashy_road/lib/src/game/entities/bird/behaviors/bird_flying_behavior.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/trashy_road/lib/src/game/entities/bird/behaviors/bird_random_spawning_behavior.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters