Perform rover destruction during the main server tick
This commit is contained in:
parent
f9b3ce3edb
commit
cd97220a11
2 changed files with 29 additions and 22 deletions
|
@ -63,15 +63,11 @@ func NewWorld(chunkSize int) *World {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SpawnRover adds an rover to the game
|
// SpawnRover adds an rover to the game (without lock)
|
||||||
func (w *World) SpawnRover(account string) (string, error) {
|
func (w *World) SpawnRover(account string) (string, error) {
|
||||||
w.worldMutex.Lock()
|
w.worldMutex.Lock()
|
||||||
defer w.worldMutex.Unlock()
|
defer w.worldMutex.Unlock()
|
||||||
return w.spawnRover(account)
|
|
||||||
}
|
|
||||||
|
|
||||||
// spawnRover adds an rover to the game (without lock)
|
|
||||||
func (w *World) spawnRover(account string) (string, error) {
|
|
||||||
// Initialise the rover
|
// Initialise the rover
|
||||||
rover := DefaultRover()
|
rover := DefaultRover()
|
||||||
|
|
||||||
|
@ -180,8 +176,11 @@ func (w *World) RoverBroadcast(rover string, message []byte) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// destroyRover Removes an rover from the game
|
// DestroyRover Removes an rover from the game
|
||||||
func (w *World) destroyRover(rover string) error {
|
func (w *World) DestroyRover(rover string) error {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
r, ok := w.Rovers[rover]
|
r, ok := w.Rovers[rover]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("no rover matching id")
|
return fmt.Errorf("no rover matching id")
|
||||||
|
@ -291,20 +290,6 @@ func (w *World) TryMoveRover(rover string, b roveapi.Bearing) (maths.Vector, err
|
||||||
i.AddLogEntryf("tried to move %s to %+v", b.String(), newPos)
|
i.AddLogEntryf("tried to move %s to %+v", b.String(), newPos)
|
||||||
i.Integrity = i.Integrity - 1
|
i.Integrity = i.Integrity - 1
|
||||||
i.AddLogEntryf("had a collision, new integrity %d", i.Integrity)
|
i.AddLogEntryf("had a collision, new integrity %d", i.Integrity)
|
||||||
|
|
||||||
if i.Integrity == 0 {
|
|
||||||
// The rover has died destroy it
|
|
||||||
err := w.destroyRover(rover)
|
|
||||||
if err != nil {
|
|
||||||
return maths.Vector{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Spawn a new one for this account
|
|
||||||
_, err = w.spawnRover(i.Owner)
|
|
||||||
if err != nil {
|
|
||||||
return maths.Vector{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.Pos, nil
|
return i.Pos, nil
|
||||||
|
@ -695,6 +680,25 @@ func (w *World) Tick() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check all rover integrities
|
||||||
|
for _, r := range w.Rovers {
|
||||||
|
if r.Integrity <= 0 {
|
||||||
|
// The rover has died destroy it
|
||||||
|
err := w.DestroyRover(r.Name)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO: Report this error somehow
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spawn a new one for this account
|
||||||
|
_, err = w.SpawnRover(r.Owner)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO: Report this error somehow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Increment the current tick count
|
// Increment the current tick count
|
||||||
w.CurrentTicks++
|
w.CurrentTicks++
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ func TestWorld_DestroyRover(t *testing.T) {
|
||||||
b, err := world.SpawnRover("")
|
b, err := world.SpawnRover("")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
err = world.destroyRover(a)
|
err = world.DestroyRover(a)
|
||||||
assert.NoError(t, err, "Error returned from rover destroy")
|
assert.NoError(t, err, "Error returned from rover destroy")
|
||||||
|
|
||||||
// Basic duplicate check
|
// Basic duplicate check
|
||||||
|
@ -165,6 +165,9 @@ func TestWorld_RoverDamage(t *testing.T) {
|
||||||
assert.Equal(t, pos, vec, "Rover managed to move into large rock")
|
assert.Equal(t, pos, vec, "Rover managed to move into large rock")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tick the world to check for rover deaths
|
||||||
|
world.Tick()
|
||||||
|
|
||||||
// Rover should have been destroyed now
|
// Rover should have been destroyed now
|
||||||
_, err = world.GetRover(a)
|
_, err = world.GetRover(a)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
Loading…
Add table
Reference in a new issue