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-03 18:12:08 +01:00
|
|
|
// Instances is a map of all the instances in the game
|
2020-06-02 17:51:54 +01:00
|
|
|
Instances map[uuid.UUID]Instance `json:"instances"`
|
2020-05-30 23:42:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instance describes a single entity or instance of an entity in the world
|
|
|
|
type Instance struct {
|
2020-06-02 17:44:39 +01:00
|
|
|
// id is a unique ID for this instance
|
2020-05-31 00:06:14 +01:00
|
|
|
id uuid.UUID
|
2020-06-02 17:44:39 +01:00
|
|
|
|
|
|
|
// pos represents where this instance is in the world
|
2020-06-03 18:12:08 +01:00
|
|
|
pos Vector
|
2020-05-30 23:42:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:51:54 +01:00
|
|
|
const kWorldFileName = "rove-world.json"
|
|
|
|
|
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-02 17:51:54 +01:00
|
|
|
Instances: make(map[uuid.UUID]Instance),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:12:08 +01:00
|
|
|
// Spawn adds an instance to the game
|
|
|
|
func (w *World) Spawn(id uuid.UUID) error {
|
|
|
|
if _, ok := w.Instances[id]; ok {
|
|
|
|
return fmt.Errorf("instance with id %s already exists in world", id)
|
|
|
|
}
|
2020-05-30 23:42:01 +01:00
|
|
|
|
|
|
|
// Initialise the instance
|
|
|
|
instance := Instance{
|
|
|
|
id: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the instance to the list
|
2020-06-02 17:51:54 +01:00
|
|
|
w.Instances[id] = instance
|
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-03 12:31:52 +01:00
|
|
|
// Removes an instance from the game
|
|
|
|
func (w *World) DestroyInstance(id uuid.UUID) error {
|
|
|
|
if _, ok := w.Instances[id]; ok {
|
|
|
|
delete(w.Instances, id)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("no instance matching id")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:44:39 +01:00
|
|
|
// GetPosition returns the position of a given instance
|
2020-06-03 18:12:08 +01:00
|
|
|
func (w World) GetPosition(id uuid.UUID) (Vector, error) {
|
|
|
|
if i, ok := w.Instances[id]; ok {
|
|
|
|
return i.pos, nil
|
|
|
|
} else {
|
|
|
|
return Vector{}, fmt.Errorf("no instance matching id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPosition sets an instances position
|
|
|
|
func (w *World) SetPosition(id uuid.UUID, pos Vector) error {
|
2020-06-02 17:51:54 +01:00
|
|
|
if i, ok := w.Instances[id]; ok {
|
2020-06-03 18:12:08 +01:00
|
|
|
i.pos = pos
|
|
|
|
w.Instances[id] = i
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("no instance matching id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPosition sets an instances position
|
|
|
|
func (w *World) MovePosition(id uuid.UUID, vec Vector) (Vector, error) {
|
|
|
|
if i, ok := w.Instances[id]; ok {
|
|
|
|
i.pos.Add(vec)
|
|
|
|
w.Instances[id] = i
|
2020-06-02 17:44:39 +01:00
|
|
|
return i.pos, nil
|
|
|
|
} else {
|
2020-06-03 18:12:08 +01:00
|
|
|
return Vector{}, fmt.Errorf("no instance 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
|
|
|
|
}
|