Store log entries for actions in the rover
This commit is contained in:
parent
84be8bff05
commit
0dc3cab9c0
2 changed files with 38 additions and 2 deletions
|
@ -1,10 +1,23 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/mdiluz/rove/pkg/objects"
|
"github.com/mdiluz/rove/pkg/objects"
|
||||||
"github.com/mdiluz/rove/pkg/vector"
|
"github.com/mdiluz/rove/pkg/vector"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RoverLogEntry describes a single log entry for the rover
|
||||||
|
type RoverLogEntry struct {
|
||||||
|
// Time is the timestamp of the entry
|
||||||
|
Time time.Time `json:"time"`
|
||||||
|
|
||||||
|
// Text contains the information in this log entry
|
||||||
|
Text string `json:"text"`
|
||||||
|
}
|
||||||
|
|
||||||
// Rover describes a single rover in the world
|
// Rover describes a single rover in the world
|
||||||
type Rover struct {
|
type Rover struct {
|
||||||
// Unique name of this rover
|
// Unique name of this rover
|
||||||
|
@ -31,6 +44,21 @@ type Rover struct {
|
||||||
// Charge is the amount of energy the rover has
|
// Charge is the amount of energy the rover has
|
||||||
Charge int `json:"charge"`
|
Charge int `json:"charge"`
|
||||||
|
|
||||||
// ChargeCharge is the maximum charge able to be stored
|
// MaximumCharge is the maximum charge able to be stored
|
||||||
MaximumCharge int `json:"maximum-Charge"`
|
MaximumCharge int `json:"maximum-Charge"`
|
||||||
|
|
||||||
|
// Logs Stores log of information
|
||||||
|
Logs []RoverLogEntry `json:"logs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddLogEntryf adds an entry to the rovers log
|
||||||
|
func (r *Rover) AddLogEntryf(format string, args ...interface{}) {
|
||||||
|
text := fmt.Sprintf(format, args...)
|
||||||
|
log.Printf("%s log entry: %s", r.Name, text)
|
||||||
|
r.Logs = append(r.Logs,
|
||||||
|
RoverLogEntry{
|
||||||
|
Time: time.Now(),
|
||||||
|
Text: text,
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,8 @@ func (w *World) SpawnRover() (string, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Spawned rover at %+v\n", rover.Pos)
|
// Add a log entry for robot creation
|
||||||
|
rover.AddLogEntryf("created at %+v", rover.Pos)
|
||||||
|
|
||||||
// Append the rover to the list
|
// Append the rover to the list
|
||||||
w.Rovers[rover.Name] = rover
|
w.Rovers[rover.Name] = rover
|
||||||
|
@ -160,6 +161,7 @@ func (w *World) RoverRecharge(rover string) (int, error) {
|
||||||
// Add one charge
|
// Add one charge
|
||||||
if i.Charge < i.MaximumCharge {
|
if i.Charge < i.MaximumCharge {
|
||||||
i.Charge++
|
i.Charge++
|
||||||
|
i.AddLogEntryf("recharged to %d", i.Charge)
|
||||||
}
|
}
|
||||||
w.Rovers[rover] = i
|
w.Rovers[rover] = i
|
||||||
|
|
||||||
|
@ -239,6 +241,7 @@ func (w *World) WarpRover(rover string, pos vector.Vector) error {
|
||||||
return fmt.Errorf("can't warp rover to occupied tile, check before warping")
|
return fmt.Errorf("can't warp rover to occupied tile, check before warping")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i.AddLogEntryf("warped to %+v", pos)
|
||||||
i.Pos = pos
|
i.Pos = pos
|
||||||
w.Rovers[rover] = i
|
w.Rovers[rover] = i
|
||||||
return nil
|
return nil
|
||||||
|
@ -266,12 +269,15 @@ func (w *World) MoveRover(rover string, b bearing.Bearing) (vector.Vector, error
|
||||||
// Get the tile and verify it's empty
|
// Get the tile and verify it's empty
|
||||||
_, obj := w.Atlas.QueryPosition(newPos)
|
_, obj := w.Atlas.QueryPosition(newPos)
|
||||||
if !obj.IsBlocking() {
|
if !obj.IsBlocking() {
|
||||||
|
i.AddLogEntryf("moved %s to %+v", b.String(), newPos)
|
||||||
// Perform the move
|
// Perform the move
|
||||||
i.Pos = newPos
|
i.Pos = newPos
|
||||||
w.Rovers[rover] = i
|
w.Rovers[rover] = i
|
||||||
} else {
|
} else {
|
||||||
// If it is a blocking tile, reduce the rover integrity
|
// If it is a blocking tile, reduce the rover integrity
|
||||||
|
i.AddLogEntryf("tried to move %s to %+v", b.String(), newPos)
|
||||||
i.Integrity = i.Integrity - 1
|
i.Integrity = i.Integrity - 1
|
||||||
|
i.AddLogEntryf("had a collision, new integrity %d", i.Integrity)
|
||||||
if i.Integrity == 0 {
|
if i.Integrity == 0 {
|
||||||
// TODO: The rover needs to be left dormant with the player
|
// TODO: The rover needs to be left dormant with the player
|
||||||
} else {
|
} else {
|
||||||
|
@ -308,6 +314,7 @@ func (w *World) RoverStash(rover string) (objects.Type, error) {
|
||||||
return objects.None, nil
|
return objects.None, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.AddLogEntryf("stashed %c", obj.Type)
|
||||||
r.Inventory = append(r.Inventory, obj)
|
r.Inventory = append(r.Inventory, obj)
|
||||||
w.Rovers[rover] = r
|
w.Rovers[rover] = r
|
||||||
w.Atlas.SetObject(r.Pos, objects.Object{Type: objects.None})
|
w.Atlas.SetObject(r.Pos, objects.Object{Type: objects.None})
|
||||||
|
@ -480,6 +487,7 @@ func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
|
||||||
if len(r.Inventory) > 0 && r.Integrity < r.MaximumIntegrity {
|
if len(r.Inventory) > 0 && r.Integrity < r.MaximumIntegrity {
|
||||||
r.Inventory = r.Inventory[:len(r.Inventory)-1]
|
r.Inventory = r.Inventory[:len(r.Inventory)-1]
|
||||||
r.Integrity = r.Integrity + 1
|
r.Integrity = r.Integrity + 1
|
||||||
|
r.AddLogEntryf("repaired self to %d", r.Integrity)
|
||||||
w.Rovers[rover] = r
|
w.Rovers[rover] = r
|
||||||
}
|
}
|
||||||
case CommandRecharge:
|
case CommandRecharge:
|
||||||
|
|
Loading…
Add table
Reference in a new issue