From 0c812ce97b6afd7dea263443a0900906ffbf6764 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 26 Oct 2020 23:52:56 +0000 Subject: [PATCH] world ticking --- internal/server/server.go | 11 ++++++++--- internal/sim/entity.go | 2 ++ internal/sim/floor.go | 8 ++++++++ internal/sim/player.go | 5 +++++ internal/sim/point.go | 3 +++ internal/sim/room.go | 5 ++++- internal/sim/tile.go | 6 ++++++ internal/sim/world.go | 40 +++++++++++++++++++++++++++++++++++++++ internal/ui/buffer.go | 19 +++++++++++++++++++ internal/ui/tile.go | 11 +++++++++++ 10 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 internal/sim/floor.go create mode 100644 internal/sim/player.go create mode 100644 internal/sim/point.go create mode 100644 internal/sim/tile.go create mode 100644 internal/ui/buffer.go create mode 100644 internal/ui/tile.go diff --git a/internal/server/server.go b/internal/server/server.go index 62d35ed..2c4c312 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -12,15 +12,17 @@ import ( "github.com/gorilla/websocket" "github.com/jordanorelli/astro-domu/internal/errors" + "github.com/jordanorelli/astro-domu/internal/sim" "github.com/jordanorelli/astro-domu/internal/wire" "github.com/jordanorelli/blammo" ) type Server struct { *blammo.Log - Host string - Port int - http *http.Server + Host string + Port int + http *http.Server + world *sim.World sync.Mutex lastSessionID int @@ -48,6 +50,9 @@ func (s *Server) Start() error { s.Log = blammo.NewLog("astro", options...).Child("server") } + s.world = sim.NewWorld(s.Log.Child("world")) + go s.world.Run(3) + addr := fmt.Sprintf("%s:%d", s.Host, s.Port) lis, err := net.Listen("tcp", addr) if err != nil { diff --git a/internal/sim/entity.go b/internal/sim/entity.go index 15a45ad..11238d2 100644 --- a/internal/sim/entity.go +++ b/internal/sim/entity.go @@ -1,5 +1,7 @@ package sim +import "time" + // entity is any entity that can be simulated. type entity interface { // update is the standard tick function diff --git a/internal/sim/floor.go b/internal/sim/floor.go new file mode 100644 index 0000000..75dd2f0 --- /dev/null +++ b/internal/sim/floor.go @@ -0,0 +1,8 @@ +package sim + +type floor int + +const ( + fl_nothing floor = iota + fl_rock +) diff --git a/internal/sim/player.go b/internal/sim/player.go new file mode 100644 index 0000000..625af19 --- /dev/null +++ b/internal/sim/player.go @@ -0,0 +1,5 @@ +package sim + +// player represents a player character in the simulation +type player struct { +} diff --git a/internal/sim/point.go b/internal/sim/point.go new file mode 100644 index 0000000..38d7e67 --- /dev/null +++ b/internal/sim/point.go @@ -0,0 +1,3 @@ +package sim + +type point struct { x, y int } diff --git a/internal/sim/room.go b/internal/sim/room.go index a31f9a4..94b1d04 100644 --- a/internal/sim/room.go +++ b/internal/sim/room.go @@ -1,4 +1,7 @@ package sim -type Room struct { +type room struct { + origin point + width int + height int } diff --git a/internal/sim/tile.go b/internal/sim/tile.go new file mode 100644 index 0000000..0977bb2 --- /dev/null +++ b/internal/sim/tile.go @@ -0,0 +1,6 @@ +package sim + +type tile struct { + floor floor + contents []entity +} diff --git a/internal/sim/world.go b/internal/sim/world.go index 3f19aa9..8706334 100644 --- a/internal/sim/world.go +++ b/internal/sim/world.go @@ -1,4 +1,44 @@ package sim +import ( + "time" + + "github.com/jordanorelli/blammo" +) + +// World is the entire simulated world. A world consists of many rooms. type World struct { + *blammo.Log + rooms []room +} + +func NewWorld(log *blammo.Log) *World { + return &World{ + Log: log, + rooms: []room{ + room{ + origin: point{0, 0}, + width: 10, + height: 10, + }, + }, + } +} + +func (w *World) Run(hz int) { + period := time.Second / time.Duration(hz) + w.Info("starting world with a tick rate of %dhz, frame duration of %v", hz, period) + ticker := time.NewTicker(period) + lastTick := time.Now() + for { + select { + case <-ticker.C: + w.tick(time.Since(lastTick)) + lastTick = time.Now() + } + } +} + +func (w *World) tick(d time.Duration) { + w.Info("tick. elapsed: %v", d) } diff --git a/internal/ui/buffer.go b/internal/ui/buffer.go new file mode 100644 index 0000000..fa9cc9a --- /dev/null +++ b/internal/ui/buffer.go @@ -0,0 +1,19 @@ +package ui + +// buffer is a rect of tiles +type buffer struct { + width int + height int + tiles []tile +} + +func newBuffer(width, height int) *buffer { + return &buffer{ + width: width, + height: height, + tiles: make([]tile, width*height), + } +} + +func (b *buffer) set(x, y int, t tile) { b.tiles[y*b.width+x] = t } +func (b *buffer) get(x, y int) tile { return b.tiles[y*b.width+x] } diff --git a/internal/ui/tile.go b/internal/ui/tile.go new file mode 100644 index 0000000..787ec6d --- /dev/null +++ b/internal/ui/tile.go @@ -0,0 +1,11 @@ +package ui + +import ( + "github.com/gdamore/tcell/v2" +) + +type tile struct { + r rune + combc []rune + style tcell.Style +}