Fixed #1 such that resizing the window still keeps every item where it should be, and maintains collision on all platforms.

This commit is contained in:
Marco Salazar 2021-09-16 11:38:46 -06:00
parent ee87a47672
commit 5e48cb0444
9 changed files with 95 additions and 39 deletions

View File

@ -117,4 +117,14 @@ class BugHolder {
}
}
}
void resize(Vector2 newSize, double xRatio, double yRatio) {
for (List<Bug> platformLevel in bugs) {
for (Bug p in platformLevel) {
p.resize(newSize, xRatio, yRatio);
p.sprite.y = (p.sprite.position.y / p.gameRef.blockSize).round() *
p.gameRef.blockSize;
}
}
}
}

View File

@ -140,8 +140,6 @@ class CircuitBackground extends MovingObject {
void setUp() {
windowA.current = WindowState.first;
windowB.current = WindowState.first;
// gameRef.add(windowA);
// gameRef.add(windowB);
background1Position = Vector2(0, 0);
background1Size = Vector2(
gameRef.viewport.canvasSize.y * (background.width / background.height),
@ -219,4 +217,19 @@ class CircuitBackground extends MovingObject {
windowB.render(canvas);
canvas.restore();
}
@override
void resize(Vector2 newSize, double xRatio, double yRatio) {
background1Size =
Vector2(newSize.y * (background.width / background.height), newSize.y);
windowA.position = background1Position;
windowA.size = background1Size;
background2Position =
Vector2(background1Position.x + background1Size.x - 1, 0);
background2Size =
Vector2(newSize.y * (background.width / background.height), newSize.y);
windowB.position = background2Position;
windowB.size = background2Size;
}
}

View File

@ -70,26 +70,6 @@ class CoinHolder {
gameRef.add(coin.sprite);
}
return false;
// double xCoordinate = gameRef.platformHolder.getFlushX();
// xCoordinate = xCoordinate +
// gameRef.blockSize * random.nextInt(5) +
// gameRef.blockSize * 20;
//
// if (xCoordinate < gameRef.size.x || random.nextInt(100) > 25) {
// return true;
// } else {
// Coin coin = Coin(gameRef);
// coin.setPosition(xCoordinate, gameRef.blockSize * level);
//
// if (gameRef.isTooNearOtherObstacles(coin.sprite.toRect())) {
// return false;
// }
//
// coins[level].add(coin);
// gameRef.add(coin.sprite);
// return false;
// }
}
int totalCoins() {
@ -125,4 +105,12 @@ class CoinHolder {
}
}
}
void resize(Vector2 newSize, double xRatio, double yRatio) {
for (List<Coin> coinLevel in coins) {
for (Coin p in coinLevel) {
p.resize(newSize, xRatio, yRatio);
}
}
}
}

View File

@ -97,4 +97,16 @@ class Firework extends Component {
sprite1.animation!.reset();
sprite2.animation!.reset();
}
void resize(Vector2 newSize, double xRatio, double yRatio) {
sprite1.x *= xRatio;
sprite1.y *= yRatio;
sprite1.width *= xRatio;
sprite1.height *= yRatio;
sprite2.x *= xRatio;
sprite2.y *= yRatio;
sprite2.width *= xRatio;
sprite2.height *= yRatio;
}
}

View File

@ -243,8 +243,20 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
@override
void onResize(Vector2 canvasSize) {
Vector2 oldSize = viewport.canvasSize;
super.onResize(canvasSize);
blockSize = canvasSize.y / 9;
if (loaded) {
double xRatio = canvasSize.x / oldSize.x;
double yRatio = canvasSize.y / oldSize.y;
circuitBackground.resize(canvasSize, xRatio, yRatio);
runner.resize(canvasSize, xRatio, yRatio);
platformHolder.resize(canvasSize, xRatio, yRatio);
coinHolder.resize(canvasSize, xRatio, yRatio);
wireHolder.resize(canvasSize, xRatio, yRatio);
bugHolder.resize(canvasSize, xRatio, yRatio);
fireworks.resize(canvasSize, xRatio, yRatio);
}
}
// Mobile controls

View File

@ -47,4 +47,11 @@ class MovingObject {
}
return "none";
}
void resize(Vector2 newSize, double xRatio, double yRatio) {
sprite.x *= xRatio;
sprite.y *= yRatio;
sprite.width *= xRatio;
sprite.height *= yRatio;
}
}

View File

@ -196,4 +196,14 @@ class PlatformHolder {
}
return null;
}
void resize(Vector2 newSize, double xRatio, double yRatio) {
for (List<Platform> platformLevel in platforms) {
for (Platform p in platformLevel) {
p.resize(newSize, xRatio, yRatio);
p.sprite.y = (p.sprite.position.y / p.gameRef.blockSize).round() *
p.gameRef.blockSize;
}
}
}
}

View File

@ -28,6 +28,7 @@ class Runner extends Component with HasGameRef<MyGame> {
String previousState = "run";
var runnerPosition = Vector2(0, 0);
late Vector2 runnerSize;
// late Rect runnerRect;
bool dead = false;
void setUp() {
@ -104,7 +105,7 @@ class Runner extends Component with HasGameRef<MyGame> {
path: [
Vector2(sprite.x, (level - 2) * gameRef.blockSize),
],
speed: 150,
duration: 0.5,
curve: Curves.ease,
onComplete: () {
updateLevel();
@ -315,22 +316,7 @@ class Runner extends Component with HasGameRef<MyGame> {
return;
}
Rect runnerRect = sprite.toRect();
bool onTopOfPlatform = false;
for (List<Platform> platformLevel in gameRef.platformHolder.platforms) {
for (Platform p in platformLevel) {
String side = p.intersect(runnerRect);
if (side == "none") {
Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top,
runnerRect.right, runnerRect.bottom + 1);
if (p.intersect(belowRunner) != "none") {
onTopOfPlatform = true;
}
} else if (side == "bottom") {
// event("die");
return;
}
}
}
bool onTopOfPlatform = this.onTopOfPlatform();
for (List<Coin> coinLevel in gameRef.coinHolder.coins) {
for (int i = 0; i < coinLevel.length;) {
@ -493,4 +479,14 @@ class Runner extends Component with HasGameRef<MyGame> {
changePriorityWithoutResorting(RUNNER_PRIORITY);
}
void resize(Vector2 newSize, double xRatio, double yRatio) {
sprite.x = gameRef.blockSize * 2;
sprite.y = gameRef.blockSize * level;
sprite.size.x = gameRef.blockSize;
sprite.size.y = gameRef.blockSize;
if (sprite.effects.isNotEmpty) {
sprite.effects.first.onComplete!();
}
}
}

View File

@ -118,4 +118,12 @@ class WireHolder {
}
}
}
void resize(Vector2 newSize, double xRatio, double yRatio) {
for (List<Wire> platformLevel in wires) {
for (Wire p in platformLevel) {
p.resize(newSize, xRatio, yRatio);
}
}
}
}