added shard.addEntity() and shard.removeEntity()

This commit is contained in:
unknown 2023-06-27 00:11:00 -05:00
parent c09a6cfd25
commit 80dc876517
2 changed files with 15 additions and 4 deletions

View File

@ -4,6 +4,18 @@ import (
"github.com/CPunch/gopenfusion/core/entity"
)
func (server *ShardServer) addEntity(e entity.Entity) {
pos := e.GetChunk()
server.addEntityToChunks(server.getViewableChunks(pos), e)
server.getChunk(pos).AddEntity(e)
}
func (server *ShardServer) removeEntity(e entity.Entity) {
pos := e.GetChunk()
server.removeEntityFromChunks(server.getViewableChunks(pos), e)
server.getChunk(pos).RemoveEntity(e)
}
func (server *ShardServer) getChunk(pos entity.ChunkPosition) *entity.Chunk {
chunk, ok := server.chunks[pos]
if !ok {
@ -35,7 +47,7 @@ func (server *ShardServer) sendOthersPacket(plr *entity.Player, typeID uint32, p
func (server *ShardServer) removeEntityFromChunks(chunks []*entity.Chunk, this entity.Entity) {
for _, chunk := range chunks {
for e, _ := range chunk.Entities {
for e := range chunk.Entities {
if e == this {
continue
}
@ -57,7 +69,7 @@ func (server *ShardServer) removeEntityFromChunks(chunks []*entity.Chunk, this e
func (server *ShardServer) addEntityToChunks(chunks []*entity.Chunk, this entity.Entity) {
for _, chunk := range chunks {
for e, _ := range chunk.Entities {
for e := range chunk.Entities {
if e == this {
continue
}

View File

@ -122,8 +122,7 @@ func (server *ShardServer) disconnect(peer *protocol.CNPeer) {
plr, ok := server.peers[peer]
if ok {
log.Printf("Player %d (AccountID %d) disconnected\n", plr.PlayerID, plr.AccountID)
server.removeEntityFromChunks(server.getViewableChunks(plr.Chunk), plr)
server.getChunk(plr.Chunk).RemoveEntity(plr)
server.removeEntity(plr)
}
log.Printf("Peer %p disconnected from SHARD\n", peer)