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"`
Position math.Vec `json:"pos"`
Glyph rune `json:"glyph"`
solid bool `json:"-"`
behavior
}

@ -155,18 +155,19 @@ func sendResponse(conn *websocket.Conn, res wire.Response) error {
type spawnPlayer struct{}
func (s spawnPlayer) exec(w *world, r *room, p *player, seq int) result {
e := entity{
ID: <-w.nextID,
Glyph: '@',
solid: true,
behavior: doNothing{},
}
p.avatar = &e
for n, t := range r.tiles {
if t.here == nil {
x, y := n%r.Width, n/r.Width
e := entity{
ID: <-w.nextID,
Position: math.Vec{x, y},
Glyph: '@',
behavior: doNothing{},
}
p.avatar = &e
t.here = &e
break
x, y := n%r.Width, n/r.Width
e.Position = math.Vec{x, y}
if t.addEntity(&e) {
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)
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)}
}
currentTile.here, nextTile.here = nil, p.avatar
currentTile.removeEntity(p.avatar.ID)
p.avatar.Position = target
return result{reply: wire.OK{}}
}

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

@ -2,5 +2,27 @@ package sim
type tile struct {
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,
Position: math.Vec{5, 5},
Glyph: 'd',
solid: true,
behavior: doNothing{},
})
@ -203,8 +204,8 @@ func (w *world) tick(d time.Duration) {
// run all object effects
for _, r := range w.rooms {
for _, t := range r.tiles {
if t.here != nil {
t.here.update(d)
for _, e := range t.here {
e.update(d)
}
}

Loading…
Cancel
Save