fix: Chunks are now goroutine safe

- added Chunk.ForEachEntity()
- refactored SendPacketExclude() to use it
- Chunk.Entities is now Chunk.entities, which is private.
- Chunk.AddEntity() && RemoveEntity() now lock the chunk mutex
This commit is contained in:
2023-11-21 01:49:57 -06:00
parent 06f4a4d33f
commit 459b71a109
2 changed files with 35 additions and 14 deletions

View File

@@ -58,9 +58,9 @@ func (server *ShardServer) sendAllPacket(plr *entity.Player, typeID uint32, pkt
func (server *ShardServer) removeEntityFromChunks(this entity.Entity, chunks []*entity.Chunk) {
for _, chunk := range chunks {
for e := range chunk.Entities {
chunk.ForEachEntity(func(e entity.Entity) bool {
if e == this {
continue
return false
}
// notify other players we're leaving
@@ -74,15 +74,17 @@ func (server *ShardServer) removeEntityFromChunks(this entity.Entity, chunks []*
thisPlr := this.(*entity.Player)
e.DisappearFromViewOf(thisPlr.Peer)
}
}
return false
})
}
}
func (server *ShardServer) addEntityToChunks(this entity.Entity, chunks []*entity.Chunk) {
for _, chunk := range chunks {
for e := range chunk.Entities {
chunk.ForEachEntity(func(e entity.Entity) bool {
if e == this {
continue
return false
}
// notify other players we're entering
@@ -96,7 +98,9 @@ func (server *ShardServer) addEntityToChunks(this entity.Entity, chunks []*entit
thisPlr := this.(*entity.Player)
e.EnterIntoViewOf(thisPlr.Peer)
}
}
return false
})
}
}