Prepare to refactor movement to be based on attributes

This commit is contained in:
Marc Di Luzio 2020-06-04 18:54:33 +01:00
parent d0a5b91de7
commit 33f25a7414
8 changed files with 45 additions and 47 deletions

View file

@ -6,11 +6,12 @@ import "github.com/google/uuid"
type Command func() error
// CommandMove will move the instance in question
func (w *World) CommandMove(id uuid.UUID, vec Vector) Command {
func (w *World) CommandMove(id uuid.UUID, bearing float64, duration int64) Command {
return func() error {
// Move the instance
_, err := w.MovePosition(id, vec)
return err
// TODO: Calculate the move itself
//_, err := w.MovePosition(id, vec)
return nil
}
}

View file

@ -16,7 +16,7 @@ func TestCommand_Spawn(t *testing.T) {
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")
assert.Equal(t, a, instance.Id, "New instance has incorrect id")
}
func TestCommand_Move(t *testing.T) {
@ -27,24 +27,20 @@ func TestCommand_Move(t *testing.T) {
pos := Vector{
X: 1.0,
Y: 2.0,
Z: 3.0,
}
err := world.SetPosition(a, pos)
assert.NoError(t, err, "Failed to set position for instance")
move := Vector{
X: 3.0,
Y: 2.0,
Z: 1.0,
}
// TODO: Test the bearing/duration movement
/*
// Try the move command
moveCommand := world.CommandMove(a, move)
assert.NoError(t, world.Execute(moveCommand), "Failed to execute move command")
// Try the move command
moveCommand := world.CommandMove(a, move)
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")
pos.Add(move)
assert.Equal(t, pos, newpos, "Failed to correctly set position for instance")
newpos, err := world.GetPosition(a)
assert.NoError(t, err, "Failed to set position for instance")
pos.Add(move)
assert.Equal(t, pos, newpos, "Failed to correctly set position for instance")
*/
}

View file

@ -4,12 +4,10 @@ package game
type Vector struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
}
// Add adds one vector to another
func (v *Vector) Add(v2 Vector) {
v.X += v2.X
v.Y += v2.Y
v.Z += v2.Z
}

View file

@ -14,15 +14,19 @@ type World struct {
// 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
// Id is a unique ID for this instance
Id uuid.UUID `json:"id"`
// pos represents where this instance is in the world
pos Vector
// Pos represents where this instance is in the world
Pos Vector `json:"pos"`
// Speed represents the Speed that the instance will move per second
Speed float64 `json:"speed"`
// Sight represents the distance the unit can see
Sight float64 `json:"sight"`
}
const kWorldFileName = "rove-world.json"
// NewWorld creates a new world object
func NewWorld() *World {
return &World{
@ -38,7 +42,7 @@ func (w *World) Spawn(id uuid.UUID) error {
// Initialise the instance
instance := Instance{
id: id,
Id: id,
}
// Append the instance to the list
@ -60,7 +64,7 @@ func (w *World) DestroyInstance(id uuid.UUID) error {
// GetPosition returns the position of a given instance
func (w World) GetPosition(id uuid.UUID) (Vector, error) {
if i, ok := w.Instances[id]; ok {
return i.pos, nil
return i.Pos, nil
} else {
return Vector{}, fmt.Errorf("no instance matching id")
}
@ -69,7 +73,7 @@ func (w World) GetPosition(id uuid.UUID) (Vector, error) {
// 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
i.Pos = pos
w.Instances[id] = i
return nil
} else {
@ -80,9 +84,9 @@ func (w *World) SetPosition(id uuid.UUID, pos Vector) error {
// 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)
i.Pos.Add(vec)
w.Instances[id] = i
return i.pos, nil
return i.Pos, nil
} else {
return Vector{}, fmt.Errorf("no instance matching id")
}

View file

@ -56,7 +56,6 @@ func TestWorld_GetSetMovePosition(t *testing.T) {
pos := Vector{
X: 1.0,
Y: 2.0,
Z: 3.0,
}
err := world.SetPosition(a, pos)