Store log entries for actions in the rover

This commit is contained in:
Marc Di Luzio 2020-07-09 18:19:49 +01:00
parent 84be8bff05
commit 0dc3cab9c0
2 changed files with 38 additions and 2 deletions

View file

@ -1,10 +1,23 @@
package game
import (
"fmt"
"log"
"time"
"github.com/mdiluz/rove/pkg/objects"
"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
type Rover struct {
// Unique name of this rover
@ -31,6 +44,21 @@ type Rover struct {
// Charge is the amount of energy the rover has
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"`
// 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,
},
)
}

View file

@ -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
w.Rovers[rover.Name] = rover
@ -160,6 +161,7 @@ func (w *World) RoverRecharge(rover string) (int, error) {
// Add one charge
if i.Charge < i.MaximumCharge {
i.Charge++
i.AddLogEntryf("recharged to %d", i.Charge)
}
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")
}
i.AddLogEntryf("warped to %+v", pos)
i.Pos = pos
w.Rovers[rover] = i
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
_, obj := w.Atlas.QueryPosition(newPos)
if !obj.IsBlocking() {
i.AddLogEntryf("moved %s to %+v", b.String(), newPos)
// Perform the move
i.Pos = newPos
w.Rovers[rover] = i
} else {
// 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.AddLogEntryf("had a collision, new integrity %d", i.Integrity)
if i.Integrity == 0 {
// TODO: The rover needs to be left dormant with the player
} else {
@ -308,6 +314,7 @@ func (w *World) RoverStash(rover string) (objects.Type, error) {
return objects.None, nil
}
r.AddLogEntryf("stashed %c", obj.Type)
r.Inventory = append(r.Inventory, obj)
w.Rovers[rover] = r
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 {
r.Inventory = r.Inventory[:len(r.Inventory)-1]
r.Integrity = r.Integrity + 1
r.AddLogEntryf("repaired self to %d", r.Integrity)
w.Rovers[rover] = r
}
case CommandRecharge: