forked from marco/firo_runner
Added Firo coins and collisions with Firo coins.
This commit is contained in:
parent
d1144e9ff2
commit
f5be43202d
45
lib/Coin.dart
Normal file
45
lib/Coin.dart
Normal file
@ -0,0 +1,45 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:firo_runner/MovingObject.dart';
|
||||
import 'package:firo_runner/main.dart';
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
enum CoinState { normal }
|
||||
|
||||
class Coin extends MovingObject {
|
||||
Coin(MyGame gameRef) : super(gameRef) {
|
||||
var coin = gameRef.coinHolder.getCoin();
|
||||
SpriteAnimation normal = SpriteAnimation.fromFrameData(
|
||||
coin,
|
||||
SpriteAnimationData.sequenced(
|
||||
amount: 10,
|
||||
stepTime: 0.1,
|
||||
textureSize: Vector2(512, 512),
|
||||
),
|
||||
);
|
||||
|
||||
sprite = SpriteAnimationGroupComponent(
|
||||
animations: {
|
||||
CoinState.normal: normal,
|
||||
},
|
||||
current: CoinState.normal,
|
||||
);
|
||||
|
||||
var platform = gameRef.platformHolder.getPlatform(0);
|
||||
|
||||
setSize(
|
||||
gameRef.blockSize * (platform!.width / platform!.height / 14),
|
||||
gameRef.blockSize * (platform!.width / platform!.height / 14),
|
||||
);
|
||||
}
|
||||
|
||||
double getRightEnd() {
|
||||
return sprite.position.x + sprite.width;
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas c) {
|
||||
getSprite().render(c, position: sprite.position, size: sprite.size);
|
||||
}
|
||||
}
|
83
lib/CoinHolder.dart
Normal file
83
lib/CoinHolder.dart
Normal file
@ -0,0 +1,83 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'Coin.dart';
|
||||
import 'main.dart';
|
||||
|
||||
class Coinholder {
|
||||
var coin;
|
||||
Random random = Random();
|
||||
|
||||
late List<List<Coin>> coins = [];
|
||||
|
||||
Future loadCoins() async {
|
||||
coin = await Flame.images.load("coin-frames.png");
|
||||
for (int i = 0; i < 9; i++) {
|
||||
coins.add([]);
|
||||
}
|
||||
}
|
||||
|
||||
getCoin() {
|
||||
return coin;
|
||||
}
|
||||
|
||||
bool generateCoin(MyGame gameRef, int level, bool force) {
|
||||
if (totalCoins() > 5) {
|
||||
return false;
|
||||
}
|
||||
double xCordinate = gameRef.platformHolder.getFlushX();
|
||||
// if (coins[level].isNotEmpty) {
|
||||
// xCordinate = coins[level].last.getRightEnd();
|
||||
// }
|
||||
xCordinate = xCordinate +
|
||||
gameRef.blockSize * random.nextInt(5) +
|
||||
gameRef.blockSize * 20;
|
||||
|
||||
if (xCordinate < gameRef.size.x && random.nextInt(1000000) > 99999) {
|
||||
return true;
|
||||
} else {
|
||||
Coin coin = Coin(gameRef);
|
||||
coin.setPosition(xCordinate, gameRef.blockSize * level);
|
||||
coins[level].add(coin);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int totalCoins() {
|
||||
int total = 0;
|
||||
for (List<Coin> levelCoins in coins) {
|
||||
total += levelCoins.length;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
void render(Canvas canvas) {
|
||||
for (List<Coin> coinLevel in coins) {
|
||||
for (Coin p in coinLevel) {
|
||||
p.render(canvas);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void update(double dt) {
|
||||
for (List<Coin> coinLevel in coins) {
|
||||
for (Coin p in coinLevel) {
|
||||
p.update(dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void removePast(MyGame gameRef) {
|
||||
for (List<Coin> coinLevel in coins) {
|
||||
for (int i = 0; i < coinLevel.length;) {
|
||||
if (coinLevel[i].sprite.x + coinLevel[i].sprite.width < 0) {
|
||||
coinLevel.removeAt(i);
|
||||
continue;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -87,4 +87,15 @@ class PlatformHolder {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double getFlushX() {
|
||||
Platform platform =
|
||||
platforms[2].firstWhere((element) => element.sprite.x > 0, orElse: () {
|
||||
return platforms[5].firstWhere((element) => element.sprite.x > 0,
|
||||
orElse: () {
|
||||
return platforms[8].firstWhere((element) => element.sprite.x > 0);
|
||||
});
|
||||
});
|
||||
return platform.sprite.x;
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
import 'package:firo_runner/Coin.dart';
|
||||
import 'package:firo_runner/main.dart';
|
||||
import 'package:flame/effects.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@ -232,6 +233,18 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
}
|
||||
}
|
||||
|
||||
for (List<Coin> coinLevel in gameRef.coinHolder.coins) {
|
||||
for (int i = 0; i < coinLevel.length;) {
|
||||
if (coinLevel[i].intersect(runnerRect) != "none") {
|
||||
gameRef.gameState.numCoins++;
|
||||
coinLevel.removeAt(i);
|
||||
print(gameRef.gameState.numCoins);
|
||||
continue;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!onTopOfPlatform &&
|
||||
(runnerState == "run" ||
|
||||
runnerState == "kick" ||
|
||||
|
@ -1,7 +1,10 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:firo_runner/CoinHolder.dart';
|
||||
import 'package:firo_runner/GameState.dart';
|
||||
import 'package:firo_runner/MovingObject.dart';
|
||||
import 'package:firo_runner/Platform.dart';
|
||||
import 'package:firo_runner/PlatformLoader.dart';
|
||||
import 'package:firo_runner/PlatformHolder.dart';
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
@ -34,6 +37,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
|
||||
);
|
||||
|
||||
late PlatformHolder platformHolder;
|
||||
late Coinholder coinHolder;
|
||||
Random random = Random();
|
||||
|
||||
late Sprite background1;
|
||||
late Sprite background2;
|
||||
@ -72,6 +77,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
|
||||
|
||||
platformHolder = PlatformHolder();
|
||||
await platformHolder.loadPlatforms();
|
||||
coinHolder = Coinholder();
|
||||
await coinHolder.loadCoins();
|
||||
|
||||
gameState = GameState();
|
||||
await gameState.load(size);
|
||||
@ -97,6 +104,10 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
|
||||
for (int i = 2; i < 9; i = i + 3) {
|
||||
while (!platformHolder.generatePlatform(this, i, false));
|
||||
}
|
||||
int choseCoinLevel = random.nextInt(9);
|
||||
if (choseCoinLevel % 3 != 2) {
|
||||
coinHolder.generateCoin(this, choseCoinLevel, false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@ -109,6 +120,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
|
||||
);
|
||||
super.render(canvas);
|
||||
platformHolder.render(canvas);
|
||||
coinHolder.render(canvas);
|
||||
final fpsCount = fps(1);
|
||||
textPaint.render(
|
||||
canvas,
|
||||
@ -120,10 +132,12 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
|
||||
@override
|
||||
void update(double dt) {
|
||||
platformHolder.removePast(this);
|
||||
coinHolder.removePast(this);
|
||||
fillScreen();
|
||||
super.update(dt);
|
||||
gameState.update(dt);
|
||||
platformHolder.update(dt);
|
||||
coinHolder.update(dt);
|
||||
}
|
||||
|
||||
@override
|
||||
|
Loading…
Reference in New Issue
Block a user