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.setPosition(xCoordinate, gameRef.blockSize * level);
if (gameRef.isTooNearOtherObstacles(bug.sprite.toRect())) {
return false;
}
bugs[level].add(bug);
gameRef.add(bug.sprite);
if (platform != null) {

View File

@ -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;

View File

@ -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) {

View File

@ -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<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
void render(Canvas canvas) {
gameState.render(canvas);