minor chunk refactoring

- rename GetChunk -> GetChunkPos
- rename SetChunk -> SetChunkPos
This commit is contained in:
CPunch 2023-11-21 01:36:23 -06:00
parent 3e04103ae4
commit 06f4a4d33f
5 changed files with 16 additions and 16 deletions

View File

@ -12,11 +12,11 @@ const (
type Entity interface { type Entity interface {
GetKind() EntityKind GetKind() EntityKind
GetChunk() ChunkPosition GetChunkPos() ChunkPosition
GetPosition() (x int, y int, z int) GetPosition() (x int, y int, z int)
GetAngle() int GetAngle() int
SetChunk(chunk ChunkPosition) SetChunkPos(chunk ChunkPosition)
SetPosition(x, y, z int) SetPosition(x, y, z int)
SetAngle(angle int) SetAngle(angle int)

View File

@ -36,7 +36,7 @@ func (npc *NPC) GetKind() EntityKind {
return ENTITY_KIND_NPC return ENTITY_KIND_NPC
} }
func (npc *NPC) GetChunk() ChunkPosition { func (npc *NPC) GetChunkPos() ChunkPosition {
return npc.Chunk return npc.Chunk
} }
@ -48,7 +48,7 @@ func (npc *NPC) GetAngle() int {
return npc.Angle return npc.Angle
} }
func (npc *NPC) SetChunk(chunk ChunkPosition) { func (npc *NPC) SetChunkPos(chunk ChunkPosition) {
npc.Chunk = chunk npc.Chunk = chunk
} }

View File

@ -44,7 +44,7 @@ func (plr *Player) GetKind() EntityKind {
return ENTITY_KIND_PLAYER return ENTITY_KIND_PLAYER
} }
func (plr *Player) GetChunk() ChunkPosition { func (plr *Player) GetChunkPos() ChunkPosition {
return plr.Chunk return plr.Chunk
} }
@ -56,7 +56,7 @@ func (plr *Player) GetAngle() int {
return plr.Angle return plr.Angle
} }
func (plr *Player) SetChunk(chunk ChunkPosition) { func (plr *Player) SetChunkPos(chunk ChunkPosition) {
plr.Chunk = chunk plr.Chunk = chunk
} }

View File

@ -5,15 +5,15 @@ import (
) )
func (server *ShardServer) addEntity(e entity.Entity) { func (server *ShardServer) addEntity(e entity.Entity) {
pos := e.GetChunk() pos := e.GetChunkPos()
server.addEntityToChunks(server.getViewableChunks(pos), e) server.addEntityToChunks(e, server.getViewableChunks(pos))
server.getChunk(pos).AddEntity(e) server.getChunk(pos).AddEntity(e)
} }
func (server *ShardServer) removeEntity(e entity.Entity) { func (server *ShardServer) removeEntity(e entity.Entity) {
// TODO: chunk cleanup // TODO: chunk cleanup
pos := e.GetChunk() pos := e.GetChunkPos()
server.removeEntityFromChunks(server.getViewableChunks(pos), e) server.removeEntityFromChunks(e, server.getViewableChunks(pos))
server.getChunk(pos).RemoveEntity(e) server.getChunk(pos).RemoveEntity(e)
} }
@ -56,7 +56,7 @@ func (server *ShardServer) sendAllPacket(plr *entity.Player, typeID uint32, pkt
return nil return nil
} }
func (server *ShardServer) removeEntityFromChunks(chunks []*entity.Chunk, this entity.Entity) { func (server *ShardServer) removeEntityFromChunks(this entity.Entity, chunks []*entity.Chunk) {
for _, chunk := range chunks { for _, chunk := range chunks {
for e := range chunk.Entities { for e := range chunk.Entities {
if e == this { if e == this {
@ -78,7 +78,7 @@ func (server *ShardServer) removeEntityFromChunks(chunks []*entity.Chunk, this e
} }
} }
func (server *ShardServer) addEntityToChunks(chunks []*entity.Chunk, this entity.Entity) { func (server *ShardServer) addEntityToChunks(this entity.Entity, chunks []*entity.Chunk) {
for _, chunk := range chunks { for _, chunk := range chunks {
for e := range chunk.Entities { for e := range chunk.Entities {
if e == this { if e == this {
@ -114,9 +114,9 @@ func (server *ShardServer) updateEntityChunk(e entity.Entity, from entity.ChunkP
toEnter := entity.ChunkSliceDifference(newViewables, oldViewables) toEnter := entity.ChunkSliceDifference(newViewables, oldViewables)
// update chunks // update chunks
server.removeEntityFromChunks(toExit, e) server.removeEntityFromChunks(e, toExit)
server.addEntityToChunks(toEnter, e) server.addEntityToChunks(e, toEnter)
server.getChunk(from).RemoveEntity(e) server.getChunk(from).RemoveEntity(e)
server.getChunk(to).AddEntity(e) server.getChunk(to).AddEntity(e)
e.SetChunk(to) e.SetChunkPos(to)
} }

View File

@ -10,7 +10,7 @@ func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Ang
plr.Y = Y plr.Y = Y
plr.Z = Z plr.Z = Z
plr.Angle = Angle plr.Angle = Angle
server.updateEntityChunk(plr, plr.GetChunk(), entity.MakeChunkPosition(X, Y)) server.updateEntityChunk(plr, plr.GetChunkPos(), entity.MakeChunkPosition(X, Y))
} }
func (server *ShardServer) playerMove(peer *protocol.CNPeer, pkt protocol.Packet) error { func (server *ShardServer) playerMove(peer *protocol.CNPeer, pkt protocol.Packet) error {