mirror of
https://github.com/mvvasilev/last_light.git
synced 2025-04-19 12:49:52 +03:00
50 lines
795 B
Go
50 lines
795 B
Go
package engine
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type GameEvent struct {
|
|
time time.Time
|
|
contents string
|
|
}
|
|
|
|
func (ge *GameEvent) Time() time.Time {
|
|
return ge.time
|
|
}
|
|
|
|
func (ge *GameEvent) Contents() string {
|
|
return ge.contents
|
|
}
|
|
|
|
type GameEventLog struct {
|
|
logs []*GameEvent
|
|
|
|
maxSize int
|
|
}
|
|
|
|
func CreateGameEventLog(maxSize int) *GameEventLog {
|
|
return &GameEventLog{
|
|
logs: make([]*GameEvent, 0, 10),
|
|
maxSize: maxSize,
|
|
}
|
|
}
|
|
|
|
func (log *GameEventLog) Log(contents string) {
|
|
log.logs = append(log.logs, &GameEvent{
|
|
time: time.Now(),
|
|
contents: contents,
|
|
})
|
|
|
|
if len(log.logs) > log.maxSize {
|
|
log.logs = log.logs[1:len(log.logs)]
|
|
}
|
|
}
|
|
|
|
func (log *GameEventLog) Tail(n int) []*GameEvent {
|
|
if n > len(log.logs) {
|
|
return log.logs
|
|
}
|
|
|
|
return log.logs[len(log.logs)-n : len(log.logs)]
|
|
}
|