can put things down now

master
Jordan Orelli 4 years ago
parent ddde174d9e
commit 695c4cca17

@ -6,6 +6,7 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math"
"github.com/jordanorelli/astro-domu/internal/wire"
"github.com/jordanorelli/blammo"
)
type inventory struct {
@ -28,6 +29,7 @@ type item struct {
}
type inventoryView struct {
*blammo.Log
highlight int
avatar *wire.Entity // probably but not necessarily the player avatar
*inventory
@ -86,26 +88,33 @@ func (v *inventoryView) putdownHandler(e *tcell.EventKey) change {
key := e.Key()
if key == tcell.KeyRune {
v.Info("key %c pressed while holding %d at %v", e.Rune(), v.items[v.highlight].ID, v.avatar.Position)
switch e.Rune() {
case 'w':
v.keyHandler = v.selectHandler
return putdown{
ID: v.items[v.highlight].ID,
Location: v.avatar.Position.Add(math.Up),
}
case 'a':
v.keyHandler = v.selectHandler
return putdown{
ID: v.items[v.highlight].ID,
Location: v.avatar.Position.Add(math.Left),
}
case 's':
v.keyHandler = v.selectHandler
return putdown{
ID: v.items[v.highlight].ID,
Location: v.avatar.Position.Add(math.Down),
}
case 'd':
v.keyHandler = v.selectHandler
return putdown{
ID: v.items[v.highlight].ID,
Location: v.avatar.Position.Add(math.Left),
Location: v.avatar.Position.Add(math.Right),
}
}
}
@ -150,6 +159,7 @@ type openInventory struct{}
func (openInventory) exec(ui *UI) {
if ui.root == inGameView {
ui.state.detail = &inventoryView{
Log: ui.Log.Child("inventory"),
avatar: ui.state.avatar,
inventory: &ui.state.inventory,
}

@ -26,15 +26,16 @@ func (l login) exec(ui *UI) {
panic("unable to login: " + err.Error())
}
welcome := res.Body.(*wire.Welcome)
ui.Info("cool beans! a login response: %#v", welcome)
ui.state.playerName = l.name
if ui.state.room == nil {
ui.state.room = new(wire.Room)
}
p := welcome.Players[l.name]
room := welcome.Rooms[p.Room]
e := room.Entities[p.Avatar]
ui.state.avatar = &e
// e := room.Entities[p.Avatar]
avatar := welcome.Avatar
ui.state.avatar = &avatar
ui.Info("initial avatar: %v", avatar)
ui.state.room = &room
ui.root = inGameView

@ -133,6 +133,7 @@ func (ui *UI) handleNotification(v wire.Value) bool {
if ui.state.avatar != nil {
id := ui.state.avatar.ID
if ep, ok := n.Entities[id]; ok {
ui.Info("our position moved to: %v", ep.Position)
*ui.state.avatar = *ep
}
}

@ -27,6 +27,10 @@ func (p *player) start(c chan Request, conn *websocket.Conn, r *room) {
Rooms: make(map[string]wire.Room),
Players: make(map[string]wire.Player),
Inventory: make([]wire.Entity, 0),
Avatar: wire.Entity{
ID: p.avatar.ID,
Glyph: p.avatar.Glyph,
},
}
p.inventory = make([]*entity, 0, 16)
ents := make(map[int]wire.Entity)
@ -190,13 +194,6 @@ type spawnPlayer struct{}
func (s spawnPlayer) exec(w *world, r *room, p *player, seq int) result {
p.fullSync = true
e := entity{
ID: <-w.nextID,
Glyph: '@',
solid: true,
behavior: p,
}
p.avatar = &e
p.inventory = append(p.inventory, &entity{
ID: <-w.nextID,
@ -210,10 +207,10 @@ func (s spawnPlayer) exec(w *world, r *room, p *player, seq int) result {
for n, _ := range r.tiles {
t := &r.tiles[n]
x, y := n%r.Width, n/r.Width
e.Position = math.Vec{x, y}
if t.addEntity(&e) {
p.Info("player added to tile at %s", e.Position)
pos := math.Vec{n % r.Width, n / r.Width}
p.avatar.Position = pos
if t.addEntity(p.avatar) {
p.Info("player added to tile at %s", pos)
return result{}
}
}
@ -336,7 +333,10 @@ func (pd *Putdown) exec(w *world, r *room, pl *player, seq int) result {
}
}
nextTile.addEntity(pl.removeItem(pd.ID))
item = pl.removeItem(pd.ID)
item.Position = pd.Location
nextTile.addEntity(item)
return result{reply: wire.OK{}}
}

@ -236,10 +236,15 @@ func (w *world) register(c connect) {
outbox: make(chan wire.Response, 8),
pending: &Request{
From: c.login.Name,
Seq: 90,
Wants: &spawnPlayer{},
},
}
p.avatar = &entity{
ID: <-w.nextID,
Glyph: '@',
solid: true,
behavior: &p,
}
r.players[c.login.Name] = &p
w.players[c.login.Name] = &p

@ -3,6 +3,7 @@ package wire
type Welcome struct {
Rooms map[string]Room `json:"rooms"`
Players map[string]Player `json:"players"`
Avatar Entity `json:"avatar"`
Inventory []Entity `json:"inventory"`
}

Loading…
Cancel
Save