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:
parent
513d2d08c0
commit
2926907f9c
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user