a tile can have more than one entity now

but there can only be one solid entity per tile
master
Jordan Orelli 4 years ago
parent e36d7bdde2
commit bfb399d89d

@ -10,6 +10,7 @@ type entity struct {
ID int `json:"id"` ID int `json:"id"`
Position math.Vec `json:"pos"` Position math.Vec `json:"pos"`
Glyph rune `json:"glyph"` Glyph rune `json:"glyph"`
solid bool `json:"-"`
behavior behavior
} }

@ -155,18 +155,19 @@ func sendResponse(conn *websocket.Conn, res wire.Response) error {
type spawnPlayer struct{} type spawnPlayer struct{}
func (s spawnPlayer) exec(w *world, r *room, p *player, seq int) result { func (s spawnPlayer) exec(w *world, r *room, p *player, seq int) result {
for n, t := range r.tiles {
if t.here == nil {
x, y := n%r.Width, n/r.Width
e := entity{ e := entity{
ID: <-w.nextID, ID: <-w.nextID,
Position: math.Vec{x, y},
Glyph: '@', Glyph: '@',
solid: true,
behavior: doNothing{}, behavior: doNothing{},
} }
p.avatar = &e p.avatar = &e
t.here = &e
break for n, t := range r.tiles {
x, y := n%r.Width, n/r.Width
e.Position = math.Vec{x, y}
if t.addEntity(&e) {
return result{}
} }
} }
return result{} return result{}
@ -186,11 +187,10 @@ func (m *Move) exec(w *world, r *room, p *player, seq int) result {
currentTile := r.getTile(pos) currentTile := r.getTile(pos)
nextTile := r.getTile(target) nextTile := r.getTile(target)
if nextTile.here != nil { if !nextTile.addEntity(p.avatar) {
return result{reply: wire.Errorf("target cell (%d, %d) is occupied", target.X, target.Y)} return result{reply: wire.Errorf("target cell (%d, %d) is occupied", target.X, target.Y)}
} }
currentTile.removeEntity(p.avatar.ID)
currentTile.here, nextTile.here = nil, p.avatar
p.avatar.Position = target p.avatar.Position = target
return result{reply: wire.OK{}} return result{reply: wire.OK{}}
} }

@ -17,8 +17,7 @@ type room struct {
func (r *room) allEntities() map[int]wire.Entity { func (r *room) allEntities() map[int]wire.Entity {
all := make(map[int]wire.Entity, 4) all := make(map[int]wire.Entity, 4)
for _, t := range r.tiles { for _, t := range r.tiles {
if t.here != nil { for _, e := range t.here {
e := t.here
all[e.ID] = wire.Entity{ all[e.ID] = wire.Entity{
ID: e.ID, ID: e.ID,
Position: e.Position, Position: e.Position,
@ -42,11 +41,7 @@ func (r *room) addEntity(e *entity) bool {
if t == nil { if t == nil {
return false return false
} }
if t.here != nil { return t.addEntity(e)
return false
}
t.here = e
return true
} }
func (r *room) addPlayer(p *player) { func (r *room) addPlayer(p *player) {

@ -2,5 +2,27 @@ package sim
type tile struct { type tile struct {
floor floor floor floor
here *entity here []*entity
}
func (t *tile) addEntity(e *entity) bool {
if e.solid {
for _, other := range t.here {
if other.solid {
return false
}
}
}
t.here = append(t.here, e)
return true
}
func (t *tile) removeEntity(id int) {
here := t.here[:0]
for _, e := range t.here {
if e.ID != id {
here = append(here, e)
}
}
t.here = here
} }

@ -43,6 +43,7 @@ func newWorld(log *blammo.Log) *world {
ID: -1, ID: -1,
Position: math.Vec{5, 5}, Position: math.Vec{5, 5},
Glyph: 'd', Glyph: 'd',
solid: true,
behavior: doNothing{}, behavior: doNothing{},
}) })
@ -203,8 +204,8 @@ func (w *world) tick(d time.Duration) {
// run all object effects // run all object effects
for _, r := range w.rooms { for _, r := range w.rooms {
for _, t := range r.tiles { for _, t := range r.tiles {
if t.here != nil { for _, e := range t.here {
t.here.update(d) e.update(d)
} }
} }

Loading…
Cancel
Save