world ticking

master
Jordan Orelli 4 years ago
parent 81801e640a
commit 0c812ce97b

@ -12,6 +12,7 @@ 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"
)
@ -21,6 +22,7 @@ type Server struct {
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 {

@ -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

@ -0,0 +1,8 @@
package sim
type floor int
const (
fl_nothing floor = iota
fl_rock
)

@ -0,0 +1,5 @@
package sim
// player represents a player character in the simulation
type player struct {
}

@ -0,0 +1,3 @@
package sim
type point struct { x, y int }

@ -1,4 +1,7 @@
package sim
type Room struct {
type room struct {
origin point
width int
height int
}

@ -0,0 +1,6 @@
package sim
type tile struct {
floor floor
contents []entity
}

@ -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)
}

@ -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] }

@ -0,0 +1,11 @@
package ui
import (
"github.com/gdamore/tcell/v2"
)
type tile struct {
r rune
combc []rune
style tcell.Style
}
Loading…
Cancel
Save