i'm definitely just making it worse

master
Jordan Orelli 4 years ago
parent 5a15c3c9bc
commit e7471df15a

@ -37,14 +37,8 @@ func (v *gameView) handleEvent(ui *UI, e tcell.Event) bool {
} }
func (v *gameView) move(ui *UI, dx, dy int) { func (v *gameView) move(ui *UI, dx, dy int) {
reply, err := ui.client.Send(sim.Move{dx, dy}) // fuck lol
if err != nil { go ui.client.Send(sim.Move{dx, dy})
return
}
e := reply.Body.(*wire.Entity)
v.room.Entities[e.ID] = e
v.me = e
} }
func (v *gameView) draw(ui *UI) { func (v *gameView) draw(ui *UI) {

@ -22,6 +22,8 @@ func (ui *UI) Run() {
ui.setupTerminal() ui.setupTerminal()
defer ui.clearTerminal() defer ui.clearTerminal()
ui.room = new(wire.Room)
if err := ui.connect(); err != nil { if err := ui.connect(); err != nil {
return return
} }
@ -44,7 +46,11 @@ func (ui *UI) Run() {
ui.view = &gameView{ ui.view = &gameView{
Log: ui.Child("game-view"), Log: ui.Child("game-view"),
room: &room, room: &room,
me: room.Entities[meta.Avatar], me: &wire.Entity{
ID: meta.Avatar,
Glyph: room.Entities[meta.Avatar].Glyph,
Position: room.Entities[meta.Avatar].Position,
},
} }
ui.Info("running ui") ui.Info("running ui")
@ -100,8 +106,10 @@ 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 {
if ui.handleNotification(n.Body) { if ui.handleNotification(n.Body) {
ui.view.draw(ui) if ui.view != nil {
ui.screen.Show() ui.view.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")
@ -115,7 +123,14 @@ func (ui *UI) handleNotification(v wire.Value) bool {
switch n := v.(type) { switch n := v.(type) {
case *wire.Entity: case *wire.Entity:
ui.room.Entities[n.ID] = n ui.room.Entities[n.ID] = *n
return true
case *wire.Frame:
if ui.room == nil {
ui.room = new(wire.Room)
}
ui.room.Entities = n.Entities
return true return true
default: default:

@ -8,6 +8,5 @@ type Effect interface {
} }
type result struct { type result struct {
reply wire.Value reply wire.Value
announce wire.Value
} }

@ -36,12 +36,7 @@ func (m *Move) exec(r *room, p *player, seq int) result {
currentTile.here, nextTile.here = nil, p.avatar currentTile.here, nextTile.here = nil, p.avatar
p.avatar.Position = target p.avatar.Position = target
e := wire.Entity{ return result{reply: wire.OK{}}
ID: p.avatar.ID,
Position: p.avatar.Position,
Glyph: p.avatar.Glyph,
}
return result{reply: e, announce: e}
} }
// SpawnPlayer is a request to spawn a player // SpawnPlayer is a request to spawn a player
@ -87,9 +82,9 @@ func (s *SpawnPlayer) exec(r *room, p *player, seq int) result {
Rooms: make(map[string]wire.Room), Rooms: make(map[string]wire.Room),
Players: make(map[string]wire.Player), Players: make(map[string]wire.Player),
} }
ents := make(map[int]*wire.Entity) ents := make(map[int]wire.Entity)
for id, e := range r.allEntities() { for id, e := range r.allEntities() {
ents[id] = &wire.Entity{ ents[id] = wire.Entity{
ID: id, ID: id,
Position: e.Position, Position: e.Position,
Glyph: e.Glyph, Glyph: e.Glyph,
@ -109,11 +104,6 @@ func (s *SpawnPlayer) exec(r *room, p *player, seq int) result {
} }
return result{ return result{
reply: welcome, reply: welcome,
announce: wire.Entity{
ID: p.avatar.ID,
Position: p.avatar.Position,
Glyph: p.avatar.Glyph,
},
} }
} }

@ -25,14 +25,10 @@ func (r *room) update(dt time.Duration) {
p.pending = nil p.pending = nil
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} if res.reply != nil {
if res.announce != nil { p.outbox <- wire.Response{Re: req.Seq, Body: res.reply}
for _, p2 := range r.players { } else {
if p2 == p { p.outbox <- wire.Response{Re: req.Seq, Body: wire.OK{}}
continue
}
p2.outbox <- wire.Response{Body: res.announce}
}
} }
} }
@ -41,19 +37,40 @@ func (r *room) update(dt time.Duration) {
t.here.update(dt) t.here.update(dt)
} }
} }
frame := wire.Frame{
Entities: r.allEntities(),
Players: r.playerAvatars(),
}
for _, p := range r.players {
p.outbox <- wire.Response{Body: frame}
}
} }
func (r *room) allEntities() map[int]*entity { func (r *room) allEntities() map[int]wire.Entity {
all := make(map[int]*entity, 4) all := make(map[int]wire.Entity, 4)
for _, t := range r.tiles { for _, t := range r.tiles {
if t.here != nil { if t.here != nil {
e := t.here e := t.here
all[e.ID] = e all[e.ID] = wire.Entity{
ID: e.ID,
Position: e.Position,
Glyph: e.Glyph,
}
} }
} }
return all return all
} }
func (r *room) playerAvatars() map[string]int {
all := make(map[string]int, len(r.players))
for nick, p := range r.players {
all[nick] = p.avatar.ID
}
return all
}
func (r *room) addPlayer(p *player) { func (r *room) addPlayer(p *player) {
r.players[p.name] = p r.players[p.name] = p
} }

@ -0,0 +1,12 @@
package wire
type Frame struct {
Entities map[int]Entity `json:"entities"`
Players map[string]int `json:"players"`
}
func (Frame) NetTag() string { return "frame" }
func init() {
Register(func() Value { return new(Frame) })
}

@ -8,5 +8,5 @@ import (
type Room struct { type Room struct {
Name string `json:"name"` Name string `json:"name"`
math.Bounds `json:"bounds"` math.Bounds `json:"bounds"`
Entities map[int]*Entity `json:"entities"` Entities map[int]Entity `json:"entities"`
} }

Loading…
Cancel
Save