Prepare to refactor movement to be based on attributes
This commit is contained in:
parent
d0a5b91de7
commit
33f25a7414
8 changed files with 45 additions and 47 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
*/
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package server
|
||||
|
||||
import "github.com/mdiluz/rove/pkg/game"
|
||||
|
||||
// ==============================
|
||||
// API: /status method: GET
|
||||
// Queries the status of the server
|
||||
|
@ -45,7 +43,9 @@ type SpawnResponse struct {
|
|||
Success bool `json:"success"`
|
||||
Error string `json:"error"`
|
||||
|
||||
Position game.Vector `json:"position"`
|
||||
// The location of the spawned entity
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
}
|
||||
|
||||
// ==============================
|
||||
|
@ -76,7 +76,8 @@ type Command struct {
|
|||
Command string `json:"command"`
|
||||
|
||||
// Used for CommandMove
|
||||
Vector game.Vector `json:"vector"`
|
||||
Bearing float64 `json:"bearing"` // The direction to move in degrees
|
||||
Duration int64 `json:"duration"` // The duration of the move in seconds
|
||||
}
|
||||
|
||||
// ================
|
||||
|
|
|
@ -135,7 +135,8 @@ func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error {
|
|||
response.Error = err.Error()
|
||||
} else {
|
||||
response.Success = true
|
||||
response.Position = pos
|
||||
response.X = pos.X
|
||||
response.Y = pos.Y
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -179,7 +180,7 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
|
|||
for _, c := range data.Commands {
|
||||
switch c.Command {
|
||||
case CommandMove:
|
||||
cmds = append(cmds, s.world.CommandMove(inst, c.Vector))
|
||||
cmds = append(cmds, s.world.CommandMove(inst, c.Bearing, c.Duration))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/game"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -81,14 +80,13 @@ func TestHandleCommands(t *testing.T) {
|
|||
// Spawn the primary instance for the account
|
||||
_, inst, err := s.SpawnPrimaryForAccount(a.Id)
|
||||
|
||||
move := game.Vector{X: 1, Y: 2, Z: 3}
|
||||
|
||||
data := CommandsData{
|
||||
Id: a.Id.String(),
|
||||
Commands: []Command{
|
||||
{
|
||||
Command: CommandMove,
|
||||
Vector: move,
|
||||
Command: CommandMove,
|
||||
Bearing: 0.0,
|
||||
Duration: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -108,9 +106,9 @@ func TestHandleCommands(t *testing.T) {
|
|||
t.Errorf("got false for /commands")
|
||||
}
|
||||
|
||||
if pos, err := s.world.GetPosition(inst); err != nil {
|
||||
if _, err := s.world.GetPosition(inst); err != nil {
|
||||
t.Error("Couldn't get position for the primary instance")
|
||||
} else if pos != move {
|
||||
t.Error("Mismatched position after commands")
|
||||
}
|
||||
|
||||
// TODO: Check position is correct
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue