rove/pkg/game/world.go

104 lines
2.2 KiB
Go
Raw Normal View History

package game
2020-05-30 23:42:01 +01:00
import (
"fmt"
"github.com/google/uuid"
)
2020-05-30 23:42:01 +01:00
// World describes a self contained universe and everything in it
type World struct {
// 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
}
// Rover describes a single rover in the world
type Rover struct {
// Id is a unique ID for this rover
Id uuid.UUID `json:"id"`
// Pos represents where this rover is in the world
Pos Vector `json:"pos"`
// Speed represents the Speed that the rover will move per second
Speed float64 `json:"speed"`
2020-05-30 23:42:01 +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
func NewWorld() *World {
return &World{
Rovers: make(map[uuid.UUID]Rover),
2020-06-02 17:51:54 +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-05-30 23:42:01 +01:00
// Initialise the rover
rover := Rover{
Id: id,
2020-05-30 23:42:01 +01:00
}
// Append the rover to the list
w.Rovers[id] = rover
2020-05-30 23:42:01 +01:00
return nil
2020-05-30 23:42:01 +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)
} else {
return fmt.Errorf("no rover matching id")
}
return nil
}
// GetPosition returns the position of a given rover
func (w World) GetPosition(id uuid.UUID) (Vector, error) {
if i, ok := w.Rovers[id]; ok {
return i.Pos, nil
} else {
return Vector{}, fmt.Errorf("no rover matching id")
}
}
// SetPosition sets an rovers position
func (w *World) SetPosition(id uuid.UUID, pos Vector) error {
if i, ok := w.Rovers[id]; ok {
i.Pos = pos
w.Rovers[id] = i
return nil
} else {
return fmt.Errorf("no rover matching id")
}
}
// SetPosition sets an rovers position
func (w *World) MovePosition(id uuid.UUID, vec Vector) (Vector, error) {
if i, ok := w.Rovers[id]; ok {
i.Pos.Add(vec)
w.Rovers[id] = i
return i.Pos, nil
} else {
return Vector{}, fmt.Errorf("no rover matching id")
}
}
// 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
}