prevent crash if incorrect zone setup

This commit is contained in:
2024-04-19 22:42:35 +00:00
parent fac8c2002c
commit ff6a78b9af
2 changed files with 13 additions and 3 deletions

View File

@@ -27,7 +27,11 @@ export default class Game {
} }
this.initLastSentLocations(); this.initLastSentLocations();
this.zone.reset() this.zone.reset()
this.zone.start() //If the zone cannot be setup, reset everything
if(!this.zone.start()) {
this.setState(GameState.SETUP);
return;
}
} }
if (newState != GameState.PLAYING) { if (newState != GameState.PLAYING) {
this.zone.reset(); this.zone.reset();

View File

@@ -108,7 +108,8 @@ export class ZoneManager {
this.nextZone = JSON.parse(JSON.stringify(this.zoneSettings.max)); this.nextZone = JSON.parse(JSON.stringify(this.zoneSettings.max));
this.currentStartZone = JSON.parse(JSON.stringify(this.zoneSettings.max)); this.currentStartZone = JSON.parse(JSON.stringify(this.zoneSettings.max));
this.currentZone = JSON.parse(JSON.stringify(this.zoneSettings.max)); this.currentZone = JSON.parse(JSON.stringify(this.zoneSettings.max));
this.setNextZone(); return this.setNextZone();
} }
/** /**
@@ -121,11 +122,16 @@ export class ZoneManager {
getRandomNextCenter(newRadius) { getRandomNextCenter(newRadius) {
let ok = false; let ok = false;
let res = null let res = null
let tries = 0;
const MAX_TRIES = 1000
//take a random point satisfying both conditions //take a random point satisfying both conditions
while (!ok) { while (tries++<MAX_TRIES) {
res = randomCirclePoint({ latitude: this.currentZone.center.lat, longitude: this.currentZone.center.lng }, this.currentZone.radius - newRadius); res = randomCirclePoint({ latitude: this.currentZone.center.lat, longitude: this.currentZone.center.lng }, this.currentZone.radius - newRadius);
ok = (isInCircle({ lat: res.latitude, lng: res.longitude }, this.zoneSettings.min.center, newRadius - this.zoneSettings.min.radius)) ok = (isInCircle({ lat: res.latitude, lng: res.longitude }, this.zoneSettings.min.center, newRadius - this.zoneSettings.min.radius))
} }
if(tries>=MAX_TRIES) {
return false;
}
return { return {
lat: res.latitude, lat: res.latitude,
lng: res.longitude lng: res.longitude