From 5e48cb0444c93fc0f5635a7a76ef0cd6b78c927f Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 16 Sep 2021 11:38:46 -0600 Subject: [PATCH] Fixed #1 such that resizing the window still keeps every item where it should be, and maintains collision on all platforms. --- lib/bug_holder.dart | 10 ++++++++++ lib/circuit_background.dart | 17 +++++++++++++++-- lib/coin_holder.dart | 28 ++++++++-------------------- lib/firework.dart | 12 ++++++++++++ lib/main.dart | 12 ++++++++++++ lib/moving_object.dart | 7 +++++++ lib/platform_holder.dart | 10 ++++++++++ lib/runner.dart | 30 +++++++++++++----------------- lib/wire_holder.dart | 8 ++++++++ 9 files changed, 95 insertions(+), 39 deletions(-) diff --git a/lib/bug_holder.dart b/lib/bug_holder.dart index c79d49c..5172f30 100644 --- a/lib/bug_holder.dart +++ b/lib/bug_holder.dart @@ -117,4 +117,14 @@ class BugHolder { } } } + + void resize(Vector2 newSize, double xRatio, double yRatio) { + for (List 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; + } + } + } } diff --git a/lib/circuit_background.dart b/lib/circuit_background.dart index b8ba9fc..ef9382b 100644 --- a/lib/circuit_background.dart +++ b/lib/circuit_background.dart @@ -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; + } } diff --git a/lib/coin_holder.dart b/lib/coin_holder.dart index fca928f..d12fac2 100644 --- a/lib/coin_holder.dart +++ b/lib/coin_holder.dart @@ -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 coinLevel in coins) { + for (Coin p in coinLevel) { + p.resize(newSize, xRatio, yRatio); + } + } + } } diff --git a/lib/firework.dart b/lib/firework.dart index 543fdcc..d3900c3 100644 --- a/lib/firework.dart +++ b/lib/firework.dart @@ -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; + } } diff --git a/lib/main.dart b/lib/main.dart index c0b0c41..e25eb1e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 diff --git a/lib/moving_object.dart b/lib/moving_object.dart index fb40968..479de1a 100644 --- a/lib/moving_object.dart +++ b/lib/moving_object.dart @@ -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; + } } diff --git a/lib/platform_holder.dart b/lib/platform_holder.dart index 644dfab..4d3703d 100644 --- a/lib/platform_holder.dart +++ b/lib/platform_holder.dart @@ -196,4 +196,14 @@ class PlatformHolder { } return null; } + + void resize(Vector2 newSize, double xRatio, double yRatio) { + for (List 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; + } + } + } } diff --git a/lib/runner.dart b/lib/runner.dart index c84e1a9..2093791 100644 --- a/lib/runner.dart +++ b/lib/runner.dart @@ -28,6 +28,7 @@ class Runner extends Component with HasGameRef { 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 { 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 { return; } Rect runnerRect = sprite.toRect(); - bool onTopOfPlatform = false; - for (List 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 coinLevel in gameRef.coinHolder.coins) { for (int i = 0; i < coinLevel.length;) { @@ -493,4 +479,14 @@ class Runner extends Component with HasGameRef { 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!(); + } + } } diff --git a/lib/wire_holder.dart b/lib/wire_holder.dart index 5e94415..e43a54e 100644 --- a/lib/wire_holder.dart +++ b/lib/wire_holder.dart @@ -118,4 +118,12 @@ class WireHolder { } } } + + void resize(Vector2 newSize, double xRatio, double yRatio) { + for (List platformLevel in wires) { + for (Wire p in platformLevel) { + p.resize(newSize, xRatio, yRatio); + } + } + } }