Added the moving circuit background. Probably need a new background that is more seamless.

This commit is contained in:
Marco Salazar 2021-09-04 19:05:08 -06:00
parent 2926907f9c
commit c75935db19
2 changed files with 74 additions and 31 deletions

View File

@ -0,0 +1,52 @@
import 'package:firo_runner/MovingObject.dart';
import 'package:firo_runner/main.dart';
import 'package:flame/components.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart';
class CircuitBackground extends MovingObject {
late var background;
late Sprite background1;
late Sprite background2;
Vector2 background1Size = Vector2(0, 0);
Vector2 background2Size = Vector2(0, 0);
Vector2 background1Position = Vector2(0, 0);
Vector2 background2Position = Vector2(0, 0);
CircuitBackground(MyGame gameRef) : super(gameRef);
Future load() async {
background = await Flame.images.load("bg.png");
background1 = Sprite(background);
background2 = Sprite(background);
background1Size = Vector2(
gameRef.size.y * (background!.width / background!.height),
gameRef.size.y);
background2Size = Vector2(
gameRef.size.y * (background!.width / background!.height),
gameRef.size.y);
}
@override
void update(double dt) {
if (background1Position.x + background1Size.x < 0) {
double newPosition = background2Position.x + background2Size.x;
background1Position = Vector2(newPosition - 1, 0);
} else if (background2Position.x + background2Size.x < 0) {
double newPosition = background1Position.x + background1Size.x;
background2Position = Vector2(newPosition - 1, 0);
}
double velocity = gameRef.gameState.getVelocity() / 10.0;
background1Position = background1Position - Vector2(velocity * dt, 0);
background2Position = background2Position - Vector2(velocity * dt, 0);
}
void render(Canvas canvas) {
background1.render(canvas,
size: background1Size, position: background1Position);
background2.render(canvas,
size: background2Size, position: background2Position);
}
}

View File

@ -1,6 +1,7 @@
import 'dart:math'; import 'dart:math';
import 'package:firo_runner/BugHolder.dart'; import 'package:firo_runner/BugHolder.dart';
import 'package:firo_runner/CircuitBackground.dart';
import 'package:firo_runner/CoinHolder.dart'; import 'package:firo_runner/CoinHolder.dart';
import 'package:firo_runner/GameState.dart'; import 'package:firo_runner/GameState.dart';
import 'package:firo_runner/PlatformHolder.dart'; import 'package:firo_runner/PlatformHolder.dart';
@ -40,23 +41,24 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
config: TextPaintConfig(fontSize: 48.0), config: TextPaintConfig(fontSize: 48.0),
); );
late CircuitBackground circuitBackground;
late PlatformHolder platformHolder; late PlatformHolder platformHolder;
late CoinHolder coinHolder; late CoinHolder coinHolder;
late WireHolder wireHolder; late WireHolder wireHolder;
late BugHolder bugHolder; late BugHolder bugHolder;
Random random = Random(); Random random = Random();
late Sprite background1; // late Sprite background1;
late Sprite background2; // late Sprite background2;
late Runner runner; late Runner runner;
late GameState gameState; late GameState gameState;
var background; // var background;
var runnerPosition = Vector2(0, 0); var runnerPosition = Vector2(0, 0);
var runnerSize; var runnerSize;
var backgroundSize; // var backgroundSize;
var background1Position; // var background1Position;
var background2Position; // var background2Position;
late double blockSize; late double blockSize;
bool loaded = false; bool loaded = false;
@ -66,10 +68,12 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
Future<void> onLoad() async { Future<void> onLoad() async {
// debugMode = true; // debugMode = true;
FlameAudio.bgm.initialize(); FlameAudio.bgm.initialize();
background = await Flame.images.load('bg.png'); // background = await Flame.images.load('bg.png');
background1 = Sprite(background); // background1 = Sprite(background);
background2 = Sprite(background); // background2 = Sprite(background);
circuitBackground = CircuitBackground(this);
await circuitBackground.load();
platformHolder = PlatformHolder(); platformHolder = PlatformHolder();
await platformHolder.loadPlatforms(); await platformHolder.loadPlatforms();
coinHolder = CoinHolder(); coinHolder = CoinHolder();
@ -155,11 +159,12 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
gameState.render(canvas); gameState.render(canvas);
background1.render( circuitBackground.render(canvas);
canvas, // background1.render(
position: Vector2(0, 0), // canvas,
size: Vector2(size.y * (background!.width / background!.height), size.y), // position: Vector2(0, 0),
); // size: Vector2(size.y * (background!.width / background!.height), size.y),
// );
super.render(canvas); super.render(canvas);
final fpsCount = fps(1); final fpsCount = fps(1);
textPaint.render( textPaint.render(
@ -177,6 +182,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
bugHolder.removePast(this); bugHolder.removePast(this);
fillScreen(); fillScreen();
super.update(dt); super.update(dt);
circuitBackground.update(dt);
gameState.update(dt); gameState.update(dt);
platformHolder.update(dt); platformHolder.update(dt);
coinHolder.update(dt); coinHolder.update(dt);
@ -194,8 +200,8 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
); );
if (loaded) { if (loaded) {
backgroundSize = // backgroundSize =
Vector2(size.y * (background!.width / background!.height), size.y); // Vector2(size.y * (background!.width / background!.height), size.y);
gameState.setSize(size); gameState.setSize(size);
} }
} }
@ -277,18 +283,3 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
} }
} }
} }
class Background extends Component {
static final Paint _paint = Paint()..color = COLOR;
final size;
Background(this.size);
@override
void render(Canvas c) {
c.drawRect(Rect.fromLTWH(0.0, 0.0, size.x, size.y), _paint);
}
@override
void update(double t);
}