Huge Instance -> Rover refactor, for clarification
This commit is contained in:
parent
33f25a7414
commit
0fbad15c01
10 changed files with 96 additions and 96 deletions
|
@ -2,10 +2,10 @@ package game
|
|||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
// A command is simply a function that acts on the a given instance in the world
|
||||
// A command is simply a function that acts on the a given rover in the world
|
||||
type Command func() error
|
||||
|
||||
// CommandMove will move the instance in question
|
||||
// CommandMove will move the rover in question
|
||||
func (w *World) CommandMove(id uuid.UUID, bearing float64, duration int64) Command {
|
||||
return func() error {
|
||||
// TODO: Calculate the move itself
|
||||
|
@ -19,6 +19,6 @@ func (w *World) CommandMove(id uuid.UUID, bearing float64, duration int64) Comma
|
|||
// TODO: Two spawn commands with the same id could trigger a fail later on, we should prevent that somehow
|
||||
func (w *World) CommandSpawn(id uuid.UUID) Command {
|
||||
return func() error {
|
||||
return w.Spawn(id)
|
||||
return w.SpawnRover(id)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,15 +14,15 @@ func TestCommand_Spawn(t *testing.T) {
|
|||
spawnCommand := world.CommandSpawn(a)
|
||||
assert.NoError(t, world.Execute(spawnCommand), "Failed to execute spawn command")
|
||||
|
||||
instance, ok := world.Instances[a]
|
||||
assert.True(t, ok, "No new instance in world")
|
||||
assert.Equal(t, a, instance.Id, "New instance has incorrect id")
|
||||
rover, ok := world.Rovers[a]
|
||||
assert.True(t, ok, "No new rover in world")
|
||||
assert.Equal(t, a, rover.Id, "New rover has incorrect id")
|
||||
}
|
||||
|
||||
func TestCommand_Move(t *testing.T) {
|
||||
world := NewWorld()
|
||||
a := uuid.New()
|
||||
assert.NoError(t, world.Spawn(a), "Failed to spawn")
|
||||
assert.NoError(t, world.SpawnRover(a), "Failed to spawn")
|
||||
|
||||
pos := Vector{
|
||||
X: 1.0,
|
||||
|
@ -30,7 +30,7 @@ func TestCommand_Move(t *testing.T) {
|
|||
}
|
||||
|
||||
err := world.SetPosition(a, pos)
|
||||
assert.NoError(t, err, "Failed to set position for instance")
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
||||
// TODO: Test the bearing/duration movement
|
||||
/*
|
||||
|
@ -39,8 +39,8 @@ func TestCommand_Move(t *testing.T) {
|
|||
assert.NoError(t, world.Execute(moveCommand), "Failed to execute move command")
|
||||
|
||||
newpos, err := world.GetPosition(a)
|
||||
assert.NoError(t, err, "Failed to set position for instance")
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
pos.Add(move)
|
||||
assert.Equal(t, pos, newpos, "Failed to correctly set position for instance")
|
||||
assert.Equal(t, pos, newpos, "Failed to correctly set position for rover")
|
||||
*/
|
||||
}
|
||||
|
|
|
@ -8,19 +8,19 @@ 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"`
|
||||
// Rovers is a id->data map of all the rovers in the game
|
||||
Rovers map[uuid.UUID]Rover `json:"rovers"`
|
||||
}
|
||||
|
||||
// Instance describes a single entity or instance of an entity in the world
|
||||
type Instance struct {
|
||||
// Id is a unique ID for this instance
|
||||
// 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 instance is in the world
|
||||
// Pos represents where this rover is in the world
|
||||
Pos Vector `json:"pos"`
|
||||
|
||||
// Speed represents the Speed that the instance will move per second
|
||||
// Speed represents the Speed that the rover will move per second
|
||||
Speed float64 `json:"speed"`
|
||||
|
||||
// Sight represents the distance the unit can see
|
||||
|
@ -30,65 +30,65 @@ type Instance struct {
|
|||
// NewWorld creates a new world object
|
||||
func NewWorld() *World {
|
||||
return &World{
|
||||
Instances: make(map[uuid.UUID]Instance),
|
||||
Rovers: make(map[uuid.UUID]Rover),
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Initialise the instance
|
||||
instance := Instance{
|
||||
// Initialise the rover
|
||||
rover := Rover{
|
||||
Id: id,
|
||||
}
|
||||
|
||||
// Append the instance to the list
|
||||
w.Instances[id] = instance
|
||||
// Append the rover to the list
|
||||
w.Rovers[id] = rover
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Removes an instance from the game
|
||||
func (w *World) DestroyInstance(id uuid.UUID) error {
|
||||
if _, ok := w.Instances[id]; ok {
|
||||
delete(w.Instances, id)
|
||||
// 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 instance matching id")
|
||||
return fmt.Errorf("no rover matching id")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPosition returns the position of a given instance
|
||||
// GetPosition returns the position of a given rover
|
||||
func (w World) GetPosition(id uuid.UUID) (Vector, error) {
|
||||
if i, ok := w.Instances[id]; ok {
|
||||
if i, ok := w.Rovers[id]; ok {
|
||||
return i.Pos, nil
|
||||
} else {
|
||||
return Vector{}, fmt.Errorf("no instance matching id")
|
||||
return Vector{}, fmt.Errorf("no rover matching id")
|
||||
}
|
||||
}
|
||||
|
||||
// SetPosition sets an instances position
|
||||
// SetPosition sets an rovers position
|
||||
func (w *World) SetPosition(id uuid.UUID, pos Vector) error {
|
||||
if i, ok := w.Instances[id]; ok {
|
||||
if i, ok := w.Rovers[id]; ok {
|
||||
i.Pos = pos
|
||||
w.Instances[id] = i
|
||||
w.Rovers[id] = i
|
||||
return nil
|
||||
} else {
|
||||
return fmt.Errorf("no instance matching id")
|
||||
return fmt.Errorf("no rover matching id")
|
||||
}
|
||||
}
|
||||
|
||||
// SetPosition sets an instances position
|
||||
// SetPosition sets an rovers position
|
||||
func (w *World) MovePosition(id uuid.UUID, vec Vector) (Vector, error) {
|
||||
if i, ok := w.Instances[id]; ok {
|
||||
if i, ok := w.Rovers[id]; ok {
|
||||
i.Pos.Add(vec)
|
||||
w.Instances[id] = i
|
||||
w.Rovers[id] = i
|
||||
return i.Pos, nil
|
||||
} else {
|
||||
return Vector{}, fmt.Errorf("no instance matching id")
|
||||
return Vector{}, fmt.Errorf("no rover matching id")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,43 +15,43 @@ func TestNewWorld(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestWorld_CreateInstance(t *testing.T) {
|
||||
func TestWorld_CreateRover(t *testing.T) {
|
||||
world := NewWorld()
|
||||
a := uuid.New()
|
||||
b := uuid.New()
|
||||
assert.NoError(t, world.Spawn(a), "Failed to spawn")
|
||||
assert.NoError(t, world.Spawn(b), "Failed to spawn")
|
||||
assert.NoError(t, world.SpawnRover(a), "Failed to spawn")
|
||||
assert.NoError(t, world.SpawnRover(b), "Failed to spawn")
|
||||
|
||||
// Basic duplicate check
|
||||
if a == b {
|
||||
t.Errorf("Created identical instances")
|
||||
} else if len(world.Instances) != 2 {
|
||||
t.Errorf("Incorrect number of instances created")
|
||||
t.Errorf("Created identical rovers")
|
||||
} else if len(world.Rovers) != 2 {
|
||||
t.Errorf("Incorrect number of rovers created")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorld_DestroyInstance(t *testing.T) {
|
||||
func TestWorld_DestroyRover(t *testing.T) {
|
||||
world := NewWorld()
|
||||
a := uuid.New()
|
||||
b := uuid.New()
|
||||
assert.NoError(t, world.Spawn(a), "Failed to spawn")
|
||||
assert.NoError(t, world.Spawn(b), "Failed to spawn")
|
||||
assert.NoError(t, world.SpawnRover(a), "Failed to spawn")
|
||||
assert.NoError(t, world.SpawnRover(b), "Failed to spawn")
|
||||
|
||||
err := world.DestroyInstance(a)
|
||||
assert.NoError(t, err, "Error returned from instance destroy")
|
||||
err := world.DestroyRover(a)
|
||||
assert.NoError(t, err, "Error returned from rover destroy")
|
||||
|
||||
// Basic duplicate check
|
||||
if len(world.Instances) != 1 {
|
||||
t.Error("Too many instances left in world")
|
||||
} else if _, ok := world.Instances[b]; !ok {
|
||||
t.Error("Remaining instance is incorrect")
|
||||
if len(world.Rovers) != 1 {
|
||||
t.Error("Too many rovers left in world")
|
||||
} else if _, ok := world.Rovers[b]; !ok {
|
||||
t.Error("Remaining rover is incorrect")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorld_GetSetMovePosition(t *testing.T) {
|
||||
world := NewWorld()
|
||||
a := uuid.New()
|
||||
assert.NoError(t, world.Spawn(a), "Failed to spawn")
|
||||
assert.NoError(t, world.SpawnRover(a), "Failed to spawn")
|
||||
|
||||
pos := Vector{
|
||||
X: 1.0,
|
||||
|
@ -59,14 +59,14 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
|
|||
}
|
||||
|
||||
err := world.SetPosition(a, pos)
|
||||
assert.NoError(t, err, "Failed to set position for instance")
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
||||
newpos, err := world.GetPosition(a)
|
||||
assert.NoError(t, err, "Failed to set position for instance")
|
||||
assert.Equal(t, pos, newpos, "Failed to correctly set position for instance")
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
assert.Equal(t, pos, newpos, "Failed to correctly set position for rover")
|
||||
|
||||
newpos, err = world.MovePosition(a, pos)
|
||||
assert.NoError(t, err, "Failed to set position for instance")
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
pos.Add(pos)
|
||||
assert.Equal(t, pos, newpos, "Failed to correctly move position for instance")
|
||||
assert.Equal(t, pos, newpos, "Failed to correctly move position for rover")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue