Add the concept of commands to the world and executing them
This commit is contained in:
parent
013a69fa63
commit
e5d5d123a6
6 changed files with 163 additions and 19 deletions
pkg/game
|
@ -8,10 +8,8 @@ import (
|
|||
|
||||
// World describes a self contained universe and everything in it
|
||||
type World struct {
|
||||
// Instances is a map of all the instances in the game
|
||||
Instances map[uuid.UUID]Instance `json:"instances"`
|
||||
|
||||
// dataPath is the location for the data to be stored
|
||||
dataPath string
|
||||
}
|
||||
|
||||
// Instance describes a single entity or instance of an entity in the world
|
||||
|
@ -20,7 +18,7 @@ type Instance struct {
|
|||
id uuid.UUID
|
||||
|
||||
// pos represents where this instance is in the world
|
||||
pos Position
|
||||
pos Vector
|
||||
}
|
||||
|
||||
const kWorldFileName = "rove-world.json"
|
||||
|
@ -32,9 +30,11 @@ func NewWorld() *World {
|
|||
}
|
||||
}
|
||||
|
||||
// Adds an instance to the game
|
||||
func (w *World) CreateInstance() uuid.UUID {
|
||||
id := uuid.New()
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Initialise the instance
|
||||
instance := Instance{
|
||||
|
@ -44,7 +44,7 @@ func (w *World) CreateInstance() uuid.UUID {
|
|||
// Append the instance to the list
|
||||
w.Instances[id] = instance
|
||||
|
||||
return id
|
||||
return nil
|
||||
}
|
||||
|
||||
// Removes an instance from the game
|
||||
|
@ -58,10 +58,42 @@ func (w *World) DestroyInstance(id uuid.UUID) error {
|
|||
}
|
||||
|
||||
// GetPosition returns the position of a given instance
|
||||
func (w World) GetPosition(id uuid.UUID) (Position, error) {
|
||||
func (w World) GetPosition(id uuid.UUID) (Vector, error) {
|
||||
if i, ok := w.Instances[id]; ok {
|
||||
return i.pos, nil
|
||||
} else {
|
||||
return Position{}, fmt.Errorf("no instance matching id")
|
||||
return Vector{}, fmt.Errorf("no instance matching id")
|
||||
}
|
||||
}
|
||||
|
||||
// SetPosition sets an instances position
|
||||
func (w *World) SetPosition(id uuid.UUID, pos Vector) error {
|
||||
if i, ok := w.Instances[id]; ok {
|
||||
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
|
||||
return i.pos, nil
|
||||
} else {
|
||||
return Vector{}, fmt.Errorf("no instance 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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue