Skip to content

Commit

Permalink
refactor: using newer flame api
Browse files Browse the repository at this point in the history
  • Loading branch information
an920107 committed Nov 12, 2024
1 parent 969334c commit 00e6879
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 113 deletions.
3 changes: 1 addition & 2 deletions lib/asset_manager/sprite_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ abstract class SpriteManager {

static Future<List<Sprite>> _generateSpritesList(int length, Future<Sprite> Function(int index) generator) async {
final List<Sprite?> spriteList = List.filled(length, null);
await Future.wait(
List.generate(length, (index) => generator(index).then((sprite) => spriteList[index] = sprite)));
await Future.wait(List.generate(length, (index) => generator(index).then((sprite) => spriteList[index] = sprite)));
return List.unmodifiable(spriteList);
}
}
Expand Down
12 changes: 9 additions & 3 deletions lib/controllers/playing/obstacle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'dart:async';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:ncu_biking/controllers/scalable_sprite.dart';

class Obstacle extends ScalableSprite {
Expand All @@ -16,8 +18,12 @@ class Obstacle extends ScalableSprite {

@override
FutureOr<void> onLoad() {
// _hitbox.debugColor = Colors.green;
// _hitbox.debugMode = true;
if (kDebugMode) {
_hitbox.debugColor = Colors.green;
_hitbox.debugMode = true;
_hitbox.isSolid = true;
}

add(PositionComponent(
anchor: Anchor.center,
children: [_hitbox],
Expand All @@ -27,7 +33,7 @@ class Obstacle extends ScalableSprite {

@override
void onMount() {
onGameResize(gameRef.size);
onGameResize(game.size);
super.onMount();
}

Expand Down
18 changes: 9 additions & 9 deletions lib/controllers/playing/obstacle/bird.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Bird extends Obstacle {
@override
FutureOr<void> onLoad() async {
for (int i = 0; i < 4; i++) {
_sprites.add(gameRef.spriteManager.birds[i]);
_sprites.add(game.spriteManager.birds[i]);
}
sprite = _sprites.first;
anchor = Anchor.center;
Expand All @@ -42,20 +42,20 @@ class Bird extends Obstacle {
@override
void onMount() {
position = Vector2(
gameRef.size.x / 2 + (_rightForward ? -1 : 1) * 520 * gameRef.scale,
size.y + Random().nextInt((gameRef.size.y / 3 - size.y).toInt()),
game.size.x / 2 + (_rightForward ? -1 : 1) * 520 * game.scale,
size.y + Random().nextInt((game.size.y / 3 - size.y).toInt()),
);
super.onMount();
}

@override
void update(double dt) {
if (gameRef.isPlaying) {
position.y += (gameRef.baseSpeed + _speed) * dt * gameRef.scale;
position.x += (_rightForward ? 1 : -1) * _speed * dt * gameRef.scale;
if (position.y > gameRef.size.y + size.y ||
position.x < gameRef.size.x / 2 - 550 * gameRef.scale ||
position.x > gameRef.size.x / 2 + 550 * gameRef.scale) {
if (game.isPlaying) {
position.y += (game.baseSpeed + _speed) * dt * game.scale;
position.x += (_rightForward ? 1 : -1) * _speed * dt * game.scale;
if (position.y > game.size.y + size.y ||
position.x < game.size.x / 2 - 550 * game.scale ||
position.x > game.size.x / 2 + 550 * game.scale) {
removeFromParent();
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/controllers/playing/obstacle/bus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Bus extends Obstacle {
@override
FutureOr<void> onLoad() async {
for (int i = 0; i < 6; i++) {
_sprites.add(gameRef.spriteManager.buses[i]);
_sprites.add(game.spriteManager.buses[i]);
}
sprite = _sprites.first;
anchor = Anchor.center;
Expand All @@ -40,16 +40,16 @@ class Bus extends Obstacle {
@override
void onMount() {
position = Vector2(
gameRef.size.x / 2,
gameRef.size.y + size.y / 2,
game.size.x / 2,
game.size.y + size.y / 2,
);
super.onMount();
}

@override
void update(double dt) {
if (gameRef.isPlaying) {
position.y -= gameRef.baseSpeed * dt * gameRef.scale;
if (game.isPlaying) {
position.y -= game.baseSpeed * dt * game.scale;
if (position.y < -size.y) {
removeFromParent();
}
Expand Down
10 changes: 5 additions & 5 deletions lib/controllers/playing/obstacle/car.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Car extends Obstacle {

@override
FutureOr<void> onLoad() async {
sprite = gameRef.spriteManager.cars[Random().nextInt(3)];
sprite = game.spriteManager.cars[Random().nextInt(3)];
anchor = Anchor.center;
angle = pi / 180 * 90;
return super.onLoad();
Expand All @@ -21,17 +21,17 @@ class Car extends Obstacle {
@override
void onMount() {
position = Vector2(
gameRef.size.x / 2 + (Random().nextBool() ? 1 : -1) * 400 * gameRef.scale,
game.size.x / 2 + (Random().nextBool() ? 1 : -1) * 400 * game.scale,
-size.y / 2,
);
super.onMount();
}

@override
void update(double dt) {
if (gameRef.isPlaying) {
position.y += gameRef.baseSpeed * dt * gameRef.scale;
if (position.y > gameRef.size.y + size.y) {
if (game.isPlaying) {
position.y += game.baseSpeed * dt * game.scale;
if (position.y > game.size.y + size.y) {
removeFromParent();
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/controllers/playing/obstacle/person.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Person extends Obstacle {
@override
FutureOr<void> onLoad() async {
for (int i = 0; i < 4; i++) {
_sprites.add(gameRef.spriteManager.persons[i]);
_sprites.add(game.spriteManager.persons[i]);
}
sprite = _sprites.first;
anchor = Anchor.center;
Expand All @@ -41,20 +41,20 @@ class Person extends Obstacle {
@override
void onMount() {
position = Vector2(
gameRef.size.x / 2 + (_rightForward ? -1 : 1) * 520 * gameRef.scale,
size.y + Random().nextInt((gameRef.size.y / 2 - size.y).toInt()),
game.size.x / 2 + (_rightForward ? -1 : 1) * 520 * game.scale,
size.y + Random().nextInt((game.size.y / 2 - size.y).toInt()),
);
super.onMount();
}

@override
void update(double dt) {
if (gameRef.isPlaying) {
position.y += gameRef.baseSpeed * dt * gameRef.scale;
position.x += (_rightForward ? 1 : -1) * _speed * dt * gameRef.scale;
if (position.y > gameRef.size.y + size.y ||
position.x < gameRef.size.x / 2 - 550 * gameRef.scale ||
position.x > gameRef.size.x / 2 + 550 * gameRef.scale) {
if (game.isPlaying) {
position.y += game.baseSpeed * dt * game.scale;
position.x += (_rightForward ? 1 : -1) * _speed * dt * game.scale;
if (position.y > game.size.y + size.y ||
position.x < game.size.x / 2 - 550 * game.scale ||
position.x > game.size.x / 2 + 550 * game.scale) {
removeFromParent();
}
}
Expand Down
66 changes: 34 additions & 32 deletions lib/controllers/playing/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:collection';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ncu_biking/controllers/playing/obstacle.dart';
import 'package:ncu_biking/controllers/scalable_sprite.dart';
Expand All @@ -11,8 +13,8 @@ import 'package:ncu_biking/main.dart';
class Player extends ScalableSprite with CollisionCallbacks, KeyboardHandler {
Player({super.coefficient = 0.12});

late final double _forwardSpeed = gameRef.baseSpeed + 300;
late final double _backwardSpeed = gameRef.baseSpeed;
late final double _forwardSpeed = game.baseSpeed + 300;
late final double _backwardSpeed = game.baseSpeed;
final double _offsetSpeed = 5;
final _hitbox = RectangleHitbox();
final _sprites = Queue<Sprite>();
Expand All @@ -24,13 +26,16 @@ class Player extends ScalableSprite with CollisionCallbacks, KeyboardHandler {
@override
FutureOr<void> onLoad() async {
for (int i = 0; i < 3; i++) {
_sprites.add(gameRef.spriteManager.players[i]);
_sprites.add(game.spriteManager.players[i]);
}
sprite = _sprites.first;
anchor = Anchor.center;

// _hitbox.debugColor = Colors.red;
// _hitbox.debugMode = true;
if (kDebugMode) {
_hitbox.debugColor = Colors.red;
_hitbox.debugMode = true;
_hitbox.isSolid = true;
}

animationTimer = Timer(
0.2,
Expand All @@ -52,39 +57,39 @@ class Player extends ScalableSprite with CollisionCallbacks, KeyboardHandler {
_offset = 0;
_offsetDest = 0;
position = _getDefaultPosition();
onGameResize(gameRef.size);
onGameResize(game.size);
super.onMount();
}

@override
void update(double dt) {
position.x = gameRef.size.x / 2 + _offset * 330 * gameRef.scale;
position.x = game.size.x / 2 + _offset * 330 * game.scale;

if (!gameRef.isPlaying) {
if (!game.isPlaying) {
super.update(dt);
return;
}

if (_isMovingForward && position.y > gameRef.size.y * 0.15) {
position.y -= _forwardSpeed * dt * gameRef.scale;
gameRef.milage += dt * _forwardSpeed;
if (_isMovingForward && position.y > game.size.y * 0.15) {
position.y -= _forwardSpeed * dt * game.scale;
game.milage += dt * _forwardSpeed;
milageChangeNotifier.notify();
} else if (position.y < _getDefaultPosition().y) {
position.y += _backwardSpeed * dt * gameRef.scale;
position.y += _backwardSpeed * dt * game.scale;
} else {
position.y = _getDefaultPosition().y;
gameRef.milage += dt * _backwardSpeed;
game.milage += dt * _backwardSpeed;
milageChangeNotifier.notify();
}

if (_offsetDest != _offset) {
if (_offsetDest < _offset) {
_offset -= _offsetSpeed * dt * gameRef.scale;
_offset -= _offsetSpeed * dt * game.scale;
if (_offsetDest >= _offset) {
_offset = _offsetDest.toDouble();
}
} else {
_offset += _offsetSpeed * dt * gameRef.scale;
_offset += _offsetSpeed * dt * game.scale;
if (_offsetDest <= _offset) {
_offset = _offsetDest.toDouble();
}
Expand All @@ -107,7 +112,7 @@ class Player extends ScalableSprite with CollisionCallbacks, KeyboardHandler {

@override
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
if (gameRef.isPlaying) {
if (game.isPlaying) {
if (event is KeyDownEvent) {
switch (event.logicalKey) {
case LogicalKeyboardKey.arrowLeft:
Expand Down Expand Up @@ -140,15 +145,14 @@ class Player extends ScalableSprite with CollisionCallbacks, KeyboardHandler {
}

@override
void onCollisionStart(
Set<Vector2> intersectionPoints, PositionComponent other) {
void onCollisionStart(Set<Vector2> intersectionPoints, PositionComponent other) {
try {
gameRef.crashed = other.parent as Obstacle;
game.crashed = other.parent as Obstacle;
} catch (e) {
gameRef.crashed = null;
game.crashed = null;
}
gameRef.overlays.add("game_over");
gameRef.isPlaying = false;
game.overlays.add("game_over");
game.isPlaying = false;
super.onCollisionStart(intersectionPoints, other);
}

Expand All @@ -169,7 +173,7 @@ class Player extends ScalableSprite with CollisionCallbacks, KeyboardHandler {
}

Vector2 _getDefaultPosition() {
return Vector2(gameRef.size.x / 2, gameRef.size.y * 0.9);
return Vector2(game.size.x / 2, game.size.y * 0.9);
}
}

Expand All @@ -181,26 +185,24 @@ class _PositionHitbox extends PositionComponent with CollisionCallbacks {

@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
if (parent is CollisionCallbacks) {
(parent as CollisionCallbacks).onCollision(intersectionPoints, other);
if (parent case final CollisionCallbacks parent) {
parent.onCollision(intersectionPoints, other);
}
super.onCollision(intersectionPoints, other);
}

@override
void onCollisionStart(
Set<Vector2> intersectionPoints, PositionComponent other) {
if (parent is CollisionCallbacks) {
(parent as CollisionCallbacks)
.onCollisionStart(intersectionPoints, other);
void onCollisionStart(Set<Vector2> intersectionPoints, PositionComponent other) {
if (parent case final CollisionCallbacks parent) {
parent.onCollisionStart(intersectionPoints, other);
}
super.onCollisionStart(intersectionPoints, other);
}

@override
void onCollisionEnd(PositionComponent other) {
if (parent is CollisionCallbacks) {
(parent as CollisionCallbacks).onCollisionEnd(other);
if (parent case final CollisionCallbacks parent) {
parent.onCollisionEnd(other);
}
super.onCollisionEnd(other);
}
Expand Down
12 changes: 6 additions & 6 deletions lib/controllers/playing/road.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ class Road extends ScalableSprite {
final Function()? _onArrived;
Road? linked;

late final double _speed = gameRef.baseSpeed;
late final double _speed = game.baseSpeed;

@override
void update(double dt) {
if (gameRef.isPlaying) {
if (game.isPlaying) {
if (linked == null) {
position.y += dt * _speed * gameRef.scale;
if (position.y > gameRef.size.y + size.y) {
position.y += dt * _speed * game.scale;
if (position.y > game.size.y + size.y) {
if (_onArrived != null) _onArrived!.call();
}
} else {
position.y = linked!.y - linked!.size.y + dt * _speed * gameRef.scale;
position.y = linked!.y - linked!.size.y + dt * _speed * game.scale;
}
}
super.update(dt);
}

@override
void onGameResize(Vector2 size) {
position.x = gameRef.size.x / 2;
position.x = game.size.x / 2;
super.onGameResize(size);
}
}
Loading

0 comments on commit 00e6879

Please sign in to comment.