diff --git a/lib/BugHolder.dart b/lib/BugHolder.dart index 18b2a9a..6e29eaa 100644 --- a/lib/BugHolder.dart +++ b/lib/BugHolder.dart @@ -60,6 +60,11 @@ class BugHolder { Bug bug = Bug(gameRef); bug.setPosition(xCoordinate, gameRef.blockSize * level); + + if (gameRef.isTooNearOtherObstacles(bug.sprite.toRect())) { + return false; + } + bugs[level].add(bug); gameRef.add(bug.sprite); if (platform != null) { diff --git a/lib/CoinHolder.dart b/lib/CoinHolder.dart index 0a5e468..5d556b8 100644 --- a/lib/CoinHolder.dart +++ b/lib/CoinHolder.dart @@ -36,6 +36,11 @@ class CoinHolder { } 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; diff --git a/lib/WireHolder.dart b/lib/WireHolder.dart index eead570..3d71488 100644 --- a/lib/WireHolder.dart +++ b/lib/WireHolder.dart @@ -60,6 +60,11 @@ class WireHolder { wire.setPosition( xCoordinate, gameRef.blockSize * level + gameRef.blockSize / 10); } + + if (gameRef.isTooNearOtherObstacles(wire.sprite.toRect())) { + return false; + } + wires[level].add(wire); gameRef.add(wire.sprite); if (platform != null) { diff --git a/lib/main.dart b/lib/main.dart index b3dbca5..200baef 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -15,6 +15,8 @@ import 'package:flame/keyboard.dart'; import 'package:flame_audio/flame_audio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'Bug.dart'; +import 'Coin.dart'; import 'Runner.dart'; const COLOR = const Color(0xFFDDC0A3); @@ -117,6 +119,39 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents { } } + bool isTooNearOtherObstacles(Rect rect) { + Rect obstacleBounds = Rect.fromLTRB( + 2 * rect.left - rect.right - 1, + 2 * rect.top - rect.bottom - 1, + 2 * rect.right - rect.left + 1, + 2 * rect.bottom - rect.top + 1); + for (List wireLevel in wireHolder.wires) { + for (Wire wire in wireLevel) { + if (wire.intersect(obstacleBounds) != "none") { + return true; + } + } + } + + for (List coinLevel in coinHolder.coins) { + for (Coin coin in coinLevel) { + if (coin.intersect(obstacleBounds) != "none") { + return true; + } + } + } + + for (List bugLevel in bugHolder.bugs) { + for (Bug bug in bugLevel) { + if (bug.intersect(obstacleBounds) != "none") { + return true; + } + } + } + + return false; + } + @override void render(Canvas canvas) { gameState.render(canvas);