i wanna dieeeeeeeeeee

master
Jordan Orelli 4 years ago
parent 5fc2aed131
commit eec726bce4

@ -23,3 +23,7 @@ type behavior interface {
// update is the standard tick function // update is the standard tick function
update(time.Duration) update(time.Duration)
} }
type doNothing struct{}
func (d doNothing) update(time.Duration) {}

@ -33,7 +33,9 @@ func (m *Move) exec(r *room, p *player, seq int) result {
if r.tiles[n].here != nil { if r.tiles[n].here != nil {
return result{reply: wire.Errorf("target cell (%d, %d) is occupied", target[0], target[1])} return result{reply: wire.Errorf("target cell (%d, %d) is occupied", target[0], target[1])}
} }
r.tiles[p.entity.Position[1]*r.width+p.entity.Position[0]].here = nil
p.entity.Position = target p.entity.Position = target
r.tiles[n].here = p.entity
return result{reply: p.entity, announce: p.entity} return result{reply: p.entity, announce: p.entity}
} }
@ -44,6 +46,8 @@ type SpawnPlayer struct {
queued bool queued bool
} }
var lastEntityID = 0
func (s *SpawnPlayer) exec(r *room, _ *player, seq int) result { func (s *SpawnPlayer) exec(r *room, _ *player, seq int) result {
if !s.queued { if !s.queued {
r.Info("spawn player requested for: %s", s.Name) r.Info("spawn player requested for: %s", s.Name)
@ -53,6 +57,7 @@ func (s *SpawnPlayer) exec(r *room, _ *player, seq int) result {
return result{} return result{}
} }
lastEntityID++
p := &player{ p := &player{
Log: r.Log.Child("players").Child(s.Name), Log: r.Log.Child("players").Child(s.Name),
room: r, room: r,
@ -60,13 +65,15 @@ func (s *SpawnPlayer) exec(r *room, _ *player, seq int) result {
outbox: s.Outbox, outbox: s.Outbox,
pending: make([]Request, 0, 32), pending: make([]Request, 0, 32),
entity: &Entity{ entity: &Entity{
ID: 999, ID: lastEntityID,
Position: [2]int{0, 0}, Position: [2]int{0, 0},
Glyph: '@', Glyph: '@',
behavior: doNothing{},
}, },
} }
p.pending = append(p.pending, Request{Seq: seq, From: s.Name, Wants: s}) p.pending = append(p.pending, Request{Seq: seq, From: s.Name, Wants: s})
r.players[s.Name] = p r.players[s.Name] = p
r.tiles[0].here = p.entity
s.queued = true s.queued = true
return result{} return result{}
} }

@ -22,6 +22,12 @@ func (r *room) update(dt time.Duration) {
for _, req := range p.pending { for _, req := range p.pending {
res := req.Wants.exec(r, p, req.Seq) res := req.Wants.exec(r, p, req.Seq)
p.outbox <- wire.Response{Re: req.Seq, Body: res.reply} p.outbox <- wire.Response{Re: req.Seq, Body: res.reply}
for _, p2 := range r.players {
if p2 == p {
continue
}
p2.outbox <- wire.Response{Body: res.reply}
}
} }
p.pending = p.pending[0:0] p.pending = p.pending[0:0]
} }

@ -3,17 +3,19 @@ package ui
import ( import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/sim" "github.com/jordanorelli/astro-domu/internal/sim"
"github.com/jordanorelli/astro-domu/internal/wire"
) )
type Mode interface { type Mode interface {
handleEvent(*UI, tcell.Event) bool handleEvent(*UI, tcell.Event) bool
notify(wire.Value)
draw(*UI) draw(*UI)
} }
type roomDisplay struct { type roomDisplay struct {
width int width int
height int height int
position point entities map[int]sim.Entity
} }
func (m *roomDisplay) handleEvent(ui *UI, e tcell.Event) bool { func (m *roomDisplay) handleEvent(ui *UI, e tcell.Event) bool {
@ -38,16 +40,19 @@ func (m *roomDisplay) handleEvent(ui *UI, e tcell.Event) bool {
return true return true
} }
func (m *roomDisplay) notify(v wire.Value) {
if e, ok := v.(*sim.Entity); ok {
m.entities[e.ID] = *e
}
}
func (m *roomDisplay) move(ui *UI, dx, dy int) { func (m *roomDisplay) move(ui *UI, dx, dy int) {
reply, err := ui.client.Send(sim.Move{dx, dy}) reply, err := ui.client.Send(sim.Move{dx, dy})
if err != nil { if err != nil {
return return
} }
e := reply.Body.(*sim.Entity) e := reply.Body.(*sim.Entity)
m.position.x = e.Position[0] m.entities[e.ID] = *e
m.position.y = e.Position[1]
// m.position.x = clamp(m.position.x+dx, 0, m.width-1)
// m.position.y = clamp(m.position.y+dy, 0, m.height-1)
} }
func (m *roomDisplay) draw(ui *UI) { func (m *roomDisplay) draw(ui *UI) {
@ -74,6 +79,7 @@ func (m *roomDisplay) draw(ui *UI) {
ui.screen.SetContent(offset.x+m.width, y+offset.y, '│', nil, tcell.StyleDefault) ui.screen.SetContent(offset.x+m.width, y+offset.y, '│', nil, tcell.StyleDefault)
} }
// add all characters for _, e := range m.entities {
ui.screen.SetContent(m.position.x+offset.x, m.position.y+offset.y, '@', nil, tcell.StyleDefault) ui.screen.SetContent(e.Position[0]+offset.x, e.Position[1]+offset.y, e.Glyph, nil, tcell.StyleDefault)
}
} }

@ -40,9 +40,14 @@ func (ui *UI) Run() {
} }
ui.Info("spawned into room %s", welcome.Room) ui.Info("spawned into room %s", welcome.Room)
entities := make(map[int]sim.Entity, len(welcome.Contents))
for _, e := range welcome.Contents {
entities[e.ID] = e
}
ui.mode = &roomDisplay{ ui.mode = &roomDisplay{
width: welcome.Size[0], width: welcome.Size[0],
height: welcome.Size[1], height: welcome.Size[1],
entities: entities,
} }
ui.Info("running ui") ui.Info("running ui")
if ui.handleUserInput() { if ui.handleUserInput() {
@ -97,6 +102,9 @@ func (ui *UI) clearTerminal() {
func (ui *UI) handleNotifications(c <-chan wire.Response) { func (ui *UI) handleNotifications(c <-chan wire.Response) {
for n := range c { for n := range c {
ui.Info("ignoring notification: %v", n) ui.Info("ignoring notification: %v", n)
ui.mode.notify(n.Body)
ui.mode.draw(ui)
ui.screen.Show()
} }
ui.Info("notifications channel is closed so we must be done") ui.Info("notifications channel is closed so we must be done")
ui.Info("clearing and finalizing screen from notifications goroutine") ui.Info("clearing and finalizing screen from notifications goroutine")

Loading…
Cancel
Save