rove/pkg/game/world.go

68 lines
1.4 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 {
2020-06-02 17:51:54 +01:00
Instances map[uuid.UUID]Instance `json:"instances"`
// dataPath is the location for the data to be stored
dataPath string
2020-05-30 23:42:01 +01:00
}
// Instance describes a single entity or instance of an entity in the world
type Instance struct {
// id is a unique ID for this instance
id uuid.UUID
// pos represents where this instance is in the world
pos Position
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
func NewWorld() *World {
return &World{
2020-06-02 17:51:54 +01:00
Instances: make(map[uuid.UUID]Instance),
}
}
2020-05-30 23:42:01 +01:00
// Adds an instance to the game
func (w *World) CreateInstance() uuid.UUID {
id := uuid.New()
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
return id
}
// 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
}
// GetPosition returns the position of a given instance
func (w World) GetPosition(id uuid.UUID) (Position, error) {
2020-06-02 17:51:54 +01:00
if i, ok := w.Instances[id]; ok {
return i.pos, nil
} else {
return Position{}, fmt.Errorf("no instance matching id")
}
}