2020-05-31 11:18:26 +01:00
|
|
|
package game
|
2020-05-30 23:42:01 +01:00
|
|
|
|
2020-06-02 17:44:39 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
2020-05-31 00:06:14 +01:00
|
|
|
|
2020-05-30 23:42:01 +01:00
|
|
|
// World describes a self contained universe and everything in it
|
|
|
|
type World struct {
|
2020-06-04 21:17:43 +01:00
|
|
|
// Rovers is a id->data map of all the rovers in the game
|
|
|
|
Rovers map[uuid.UUID]Rover `json:"rovers"`
|
2020-05-30 23:42:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Rover describes a single rover in the world
|
|
|
|
type Rover struct {
|
|
|
|
// Id is a unique ID for this rover
|
2020-06-04 18:54:33 +01:00
|
|
|
Id uuid.UUID `json:"id"`
|
2020-06-02 17:44:39 +01:00
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Pos represents where this rover is in the world
|
2020-06-04 18:54:33 +01:00
|
|
|
Pos Vector `json:"pos"`
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Speed represents the Speed that the rover will move per second
|
2020-06-04 18:54:33 +01:00
|
|
|
Speed float64 `json:"speed"`
|
2020-05-30 23:42:01 +01:00
|
|
|
|
2020-06-04 18:54:33 +01:00
|
|
|
// Sight represents the distance the unit can see
|
|
|
|
Sight float64 `json:"sight"`
|
|
|
|
}
|
2020-06-02 17:51:54 +01:00
|
|
|
|
2020-05-30 23:42:01 +01:00
|
|
|
// NewWorld creates a new world object
|
2020-06-02 19:16:02 +01:00
|
|
|
func NewWorld() *World {
|
2020-06-02 17:44:39 +01:00
|
|
|
return &World{
|
2020-06-04 21:17:43 +01:00
|
|
|
Rovers: make(map[uuid.UUID]Rover),
|
2020-06-02 17:51:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// SpawnRover adds an rover to the game
|
|
|
|
func (w *World) SpawnRover(id uuid.UUID) error {
|
|
|
|
if _, ok := w.Rovers[id]; ok {
|
|
|
|
return fmt.Errorf("rover with id %s already exists in world", id)
|
2020-06-03 18:12:08 +01:00
|
|
|
}
|
2020-05-30 23:42:01 +01:00
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Initialise the rover
|
|
|
|
rover := Rover{
|
2020-06-04 18:54:33 +01:00
|
|
|
Id: id,
|
2020-05-30 23:42:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Append the rover to the list
|
|
|
|
w.Rovers[id] = rover
|
2020-05-30 23:42:01 +01:00
|
|
|
|
2020-06-03 18:12:08 +01:00
|
|
|
return nil
|
2020-05-30 23:42:01 +01:00
|
|
|
}
|
2020-06-02 17:44:39 +01:00
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Removes an rover from the game
|
|
|
|
func (w *World) DestroyRover(id uuid.UUID) error {
|
|
|
|
if _, ok := w.Rovers[id]; ok {
|
|
|
|
delete(w.Rovers, id)
|
2020-06-03 12:31:52 +01:00
|
|
|
} else {
|
2020-06-04 21:17:43 +01:00
|
|
|
return fmt.Errorf("no rover matching id")
|
2020-06-03 12:31:52 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// GetPosition returns the position of a given rover
|
2020-06-03 18:12:08 +01:00
|
|
|
func (w World) GetPosition(id uuid.UUID) (Vector, error) {
|
2020-06-04 21:17:43 +01:00
|
|
|
if i, ok := w.Rovers[id]; ok {
|
2020-06-04 18:54:33 +01:00
|
|
|
return i.Pos, nil
|
2020-06-03 18:12:08 +01:00
|
|
|
} else {
|
2020-06-04 21:17:43 +01:00
|
|
|
return Vector{}, fmt.Errorf("no rover matching id")
|
2020-06-03 18:12:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// SetPosition sets an rovers position
|
2020-06-03 18:12:08 +01:00
|
|
|
func (w *World) SetPosition(id uuid.UUID, pos Vector) error {
|
2020-06-04 21:17:43 +01:00
|
|
|
if i, ok := w.Rovers[id]; ok {
|
2020-06-04 18:54:33 +01:00
|
|
|
i.Pos = pos
|
2020-06-04 21:17:43 +01:00
|
|
|
w.Rovers[id] = i
|
2020-06-03 18:12:08 +01:00
|
|
|
return nil
|
|
|
|
} else {
|
2020-06-04 21:17:43 +01:00
|
|
|
return fmt.Errorf("no rover matching id")
|
2020-06-03 18:12:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// SetPosition sets an rovers position
|
2020-06-03 18:12:08 +01:00
|
|
|
func (w *World) MovePosition(id uuid.UUID, vec Vector) (Vector, error) {
|
2020-06-04 21:17:43 +01:00
|
|
|
if i, ok := w.Rovers[id]; ok {
|
2020-06-04 18:54:33 +01:00
|
|
|
i.Pos.Add(vec)
|
2020-06-04 21:17:43 +01:00
|
|
|
w.Rovers[id] = i
|
2020-06-04 18:54:33 +01:00
|
|
|
return i.Pos, nil
|
2020-06-02 17:44:39 +01:00
|
|
|
} else {
|
2020-06-04 21:17:43 +01:00
|
|
|
return Vector{}, fmt.Errorf("no rover matching id")
|
2020-06-02 17:44:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-03 18:12:08 +01:00
|
|
|
|
|
|
|
// Execute will run the commands given
|
|
|
|
func (w *World) Execute(commands ...Command) error {
|
|
|
|
for _, c := range commands {
|
|
|
|
if err := c(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|