Objects are now placed much farther from each other so they do not overlap, and so the user has a chance to avoid the obstacles.

This commit is contained in:
Marco Salazar 2021-09-04 18:36:14 -06:00
parent 513d2d08c0
commit 2926907f9c
4 changed files with 50 additions and 0 deletions

View File

@ -60,6 +60,11 @@ class BugHolder {
Bug bug = Bug(gameRef); Bug bug = Bug(gameRef);
bug.setPosition(xCoordinate, gameRef.blockSize * level); bug.setPosition(xCoordinate, gameRef.blockSize * level);
if (gameRef.isTooNearOtherObstacles(bug.sprite.toRect())) {
return false;
}
bugs[level].add(bug); bugs[level].add(bug);
gameRef.add(bug.sprite); gameRef.add(bug.sprite);
if (platform != null) { if (platform != null) {

View File

@ -36,6 +36,11 @@ class CoinHolder {
} else { } else {
Coin coin = Coin(gameRef); Coin coin = Coin(gameRef);
coin.setPosition(xCoordinate, gameRef.blockSize * level); coin.setPosition(xCoordinate, gameRef.blockSize * level);
if (gameRef.isTooNearOtherObstacles(coin.sprite.toRect())) {
return false;
}
coins[level].add(coin); coins[level].add(coin);
gameRef.add(coin.sprite); gameRef.add(coin.sprite);
return false; return false;

View File

@ -60,6 +60,11 @@ class WireHolder {
wire.setPosition( wire.setPosition(
xCoordinate, gameRef.blockSize * level + gameRef.blockSize / 10); xCoordinate, gameRef.blockSize * level + gameRef.blockSize / 10);
} }
if (gameRef.isTooNearOtherObstacles(wire.sprite.toRect())) {
return false;
}
wires[level].add(wire); wires[level].add(wire);
gameRef.add(wire.sprite); gameRef.add(wire.sprite);
if (platform != null) { if (platform != null) {

View File

@ -15,6 +15,8 @@ import 'package:flame/keyboard.dart';
import 'package:flame_audio/flame_audio.dart'; import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'Bug.dart';
import 'Coin.dart';
import 'Runner.dart'; import 'Runner.dart';
const COLOR = const Color(0xFFDDC0A3); 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<Wire> wireLevel in wireHolder.wires) {
for (Wire wire in wireLevel) {
if (wire.intersect(obstacleBounds) != "none") {
return true;
}
}
}
for (List<Coin> coinLevel in coinHolder.coins) {
for (Coin coin in coinLevel) {
if (coin.intersect(obstacleBounds) != "none") {
return true;
}
}
}
for (List<Bug> bugLevel in bugHolder.bugs) {
for (Bug bug in bugLevel) {
if (bug.intersect(obstacleBounds) != "none") {
return true;
}
}
}
return false;
}
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
gameState.render(canvas); gameState.render(canvas);