firo_runner/lib/main.dart

260 lines
6.3 KiB
Dart
Raw Normal View History

import 'dart:math';
import 'package:firo_runner/BugHolder.dart';
import 'package:firo_runner/CoinHolder.dart';
import 'package:firo_runner/GameState.dart';
import 'package:firo_runner/PlatformHolder.dart';
import 'package:firo_runner/Wire.dart';
import 'package:firo_runner/WireHolder.dart';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flame/gestures.dart';
import 'package:flame/keyboard.dart';
import 'package:flame_audio/flame_audio.dart';
2021-08-31 16:46:17 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'Runner.dart';
2021-08-31 16:46:17 +00:00
const COLOR = const Color(0xFFDDC0A3);
const RUNNER_PRIORITY = 100;
const PLATFORM_PRIORITY = 50;
const WIRE_PRIORITY = 25;
const COIN_PRIORITY = 70;
const BUG_PRIORITY = 75;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Flame.device.fullScreen();
await Flame.device.setLandscape();
final myGame = MyGame();
runApp(GameWidget(game: myGame));
2021-08-31 16:46:17 +00:00
}
class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
TextPaint textPaint = TextPaint(
config: TextPaintConfig(fontSize: 48.0),
);
late PlatformHolder platformHolder;
late CoinHolder coinHolder;
late WireHolder wireHolder;
late BugHolder bugHolder;
Random random = Random();
late Sprite background1;
late Sprite background2;
late Runner runner;
late GameState gameState;
var background;
var runnerPosition = Vector2(0, 0);
var runnerSize;
var backgroundSize;
var background1Position;
var background2Position;
late double blockSize;
bool loaded = false;
late Wire wire;
@override
Future<void> onLoad() async {
// debugMode = true;
FlameAudio.bgm.initialize();
background = await Flame.images.load('bg.png');
background1 = Sprite(background);
background2 = Sprite(background);
platformHolder = PlatformHolder();
await platformHolder.loadPlatforms();
coinHolder = CoinHolder();
await coinHolder.loadCoins();
wireHolder = WireHolder();
await wireHolder.loadWires();
bugHolder = BugHolder();
await bugHolder.loadBugs();
gameState = GameState();
await gameState.load(size);
runner = Runner();
await runner.load(loadSpriteAnimation);
runner.setSize(runnerSize, blockSize);
runnerPosition = Vector2(blockSize, blockSize * 1);
runner.setPosition(runnerPosition);
add(runner);
// Generate the first 4 Platforms that will always be there at the start.
for (int i = 0; i < 4; i++) {
platformHolder.generatePlatform(this, 8, true);
}
fillScreen();
FlameAudio.bgm.play('Infinite_Spankage_M.mp3');
loaded = true;
}
void fillScreen() {
for (int i = 2; i < 9; i = i + 3) {
while (!platformHolder.generatePlatform(this, i, false)) {}
}
int wireChosenRegion = random.nextInt(8) + 1;
if (wireChosenRegion % 3 != 2) {
wireHolder.generateWire(this, wireChosenRegion, false);
}
int bugChosenRegion = random.nextInt(8) + 1;
if (bugChosenRegion % 3 != 2) {
bugHolder.generateBug(this, bugChosenRegion, false);
}
int choseCoinLevel = random.nextInt(8) + 1;
if (choseCoinLevel % 3 != 2) {
coinHolder.generateCoin(this, choseCoinLevel, false);
}
}
2021-08-31 16:46:17 +00:00
@override
void render(Canvas canvas) {
gameState.render(canvas);
background1.render(
canvas,
position: Vector2(0, 0),
size: Vector2(size.y * (background!.width / background!.height), size.y),
2021-08-31 16:46:17 +00:00
);
super.render(canvas);
final fpsCount = fps(1);
textPaint.render(
canvas,
fpsCount.toString(),
Vector2(0, 0),
);
2021-08-31 16:46:17 +00:00
}
@override
void update(double dt) {
platformHolder.removePast(this);
coinHolder.removePast(this);
wireHolder.removePast(this);
bugHolder.removePast(this);
fillScreen();
super.update(dt);
gameState.update(dt);
platformHolder.update(dt);
coinHolder.update(dt);
wireHolder.update(dt);
bugHolder.update(dt);
}
@override
void onResize(Vector2 size) {
super.onResize(size);
blockSize = size.y / 9;
runnerSize = Vector2(
size.y / 9,
size.y / 9,
);
if (loaded) {
backgroundSize =
Vector2(size.y * (background!.width / background!.height), size.y);
gameState.setSize(size);
}
}
2021-08-31 16:46:17 +00:00
// Mobile controls
late List<double> xDeltas;
late List<double> yDeltas;
@override
void onPanStart(DragStartInfo info) {
xDeltas = List.empty(growable: true);
yDeltas = List.empty(growable: true);
}
2021-08-31 16:46:17 +00:00
@override
void onPanUpdate(DragUpdateInfo info) {
xDeltas.add(info.delta.game.x);
yDeltas.add(info.delta.game.y);
}
2021-08-31 16:46:17 +00:00
@override
void onPanEnd(DragEndInfo info) {
double xDelta = xDeltas.isEmpty
? 0
: xDeltas.reduce((value, element) => value + element);
double yDelta = yDeltas.isEmpty
? 0
: yDeltas.reduce((value, element) => value + element);
if (xDelta.abs() > yDelta.abs()) {
if (xDelta > 0) {
runner.control("right");
} else {
runner.control("left");
}
} else if (xDelta.abs() < yDelta.abs()) {
if (yDelta > 0) {
runner.control("down");
} else {
runner.control("up");
}
}
}
2021-08-31 16:46:17 +00:00
@override
void onTap() {
runner.control("center");
}
2021-08-31 16:46:17 +00:00
// Keyboard controls.
var keyboardKey;
@override
void onKeyEvent(RawKeyEvent event) {
if (event is RawKeyUpEvent) {
keyboardKey = null;
switch (event.data.keyLabel) {
case "w":
runner.control("up");
break;
case "a":
runner.control("left");
break;
case "s":
runner.control("down");
break;
case "d":
runner.control("right");
break;
default:
if (event.data.logicalKey.keyId == 32) {
runner.control("down");
}
break;
}
}
if (event is RawKeyDownEvent && event.data.logicalKey.keyId == 32) {
if (keyboardKey == null) {
runner.control("center");
}
keyboardKey = "spacebar";
}
2021-08-31 16:46:17 +00:00
}
}
class Background extends Component {
static final Paint _paint = Paint()..color = COLOR;
final size;
Background(this.size);
2021-08-31 16:46:17 +00:00
@override
void render(Canvas c) {
c.drawRect(Rect.fromLTWH(0.0, 0.0, size.x, size.y), _paint);
2021-08-31 16:46:17 +00:00
}
@override
void update(double t);
2021-08-31 16:46:17 +00:00
}