| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- local EntityMgr = class("EntityMgr")
- local MinePlayerEntity = require("MinePlayerEntity")
- local OtherPlayerEntity = require("OtherPlayerEntity")
- function EntityMgr:ctor()
- self.entityMap = {}
- end
- function EntityMgr:Dispose()
- self:RemoveAllEntity()
- self.entityMap = nil
- end
- function EntityMgr:CreateEntity(entityType, uid, pos)
- local entity = self:GetEntity(entityType, uid)
- if entity then
- entity:SynPosition(pos)
- return entity
- end
- if entityType == Enum.EntityType.MinePlayer then
- entity = MinePlayerEntity:new(uid, pos, Quaternion.identity)
- elseif entityType == Enum.EntityType.OtherPlayer then
- entity = OtherPlayerEntity:new(uid, pos, Quaternion.identity)
- else
- LogError("[Wboy] unkonw EntityType : " .. tostring(entityType) .. " uid : " .. tostring(uid))
- end
- if entity then
- if not self.entityMap then self.entityMap = {} end
- local entityDic = self.entityMap[entityType]
- if not entityDic then
- entityDic = {}
- self.entityMap[entityType] = entityDic
- end
- entityDic[uid] = entity
- entity:EnterMap()
- end
- return entity
- end
- function EntityMgr:HasEntity(entityType, uid)
- if not self.entityMap then return false end
- local entityDic = self.entityMap[entityType]
- if not entityDic then return false end
- local entity = entityDic[uid]
- if not entity then return false end
- return true
- end
- function EntityMgr:GetEntity(entityType, uid)
- if not self.entityMap then return nil end
- local entityDic = self.entityMap[entityType]
- if not entityDic then return nil end
- return entityDic[uid]
- end
- function EntityMgr:GetEntitys(entityType)
- if not self.entityMap then return nil end
- local entityDic = self.entityMap[entityType]
- return entityDic
- end
- function EntityMgr:RemoveEntity(entityType, uid)
- if not self.entityMap then return end
- local entityDic = self.entityMap[entityType]
- if not entityDic then return end
- local entity = entityDic[uid]
- if not entity then return end
- entity:Dispose()
- entityDic[uid] = nil
- end
- function EntityMgr:RemoveAllEntity()
- if self.entityMap then
- for entityType, entityDic in pairs(self.entityMap) do
- if entityDic then
- for uid, entity in pairs(entityDic) do
- if entity then
- entity:Dispose()
- entityDic[uid] = nil
- end
- end
- end
- end
- end
- self.entityMap = {}
- end
- function EntityMgr:EnterMap(entityType, uid, pos)
- return self:CreateEntity(entityType, uid, pos)
- end
- function EntityMgr:ExitMap(entityType, uid)
- self:RemoveEntity(entityType, uid)
- end
- function EntityMgr:EnterWorld(entity)
- if not entity then return end
- entity:EnterWorld()
- end
- function EntityMgr:ExitWorld(entity)
- if not entity then return end
- entity:ExitWorld()
- end
- function EntityMgr:AllEntityEnterWorld()
- if self.entityMap then
- for entityType, entityDic in pairs(self.entityMap) do
- if entityDic then
- for uid, entity in pairs(entityDic) do
- if entity then
- entity:EnterWorld()
- end
- end
- end
- end
- end
- end
- function EntityMgr:AllEntityExitWorld()
- if self.entityMap then
- for entityType, entityDic in pairs(self.entityMap) do
- if entityDic then
- for uid, entity in pairs(entityDic) do
- if entity then
- entity:ExitWorld()
- end
- end
- end
- end
- end
- end
- function EntityMgr:Update(deltaTime)
- if self.entityMap then
- for entityType, entityDic in pairs(self.entityMap) do
- if entityDic then
- for uid, entity in pairs(entityDic) do
- if entity then
- entity:Update(deltaTime)
- end
- end
- end
- end
- end
- end
- function EntityMgr:SynPosition(entityType, uid, pos)
- local entity = self:GetEntity(entityType, uid)
- if entity then
- entity:SynPosition(pos)
- end
- end
- function EntityMgr:GetPosition(entityType, uid)
- local entity = self:GetEntity(entityType, uid)
- if entity then
- return entity:GetPosition()
- end
- return nil
- end
- function EntityMgr:GetTransform(entityType, uid)
- local entity = self:GetEntity(entityType, uid)
- if entity and entity.view then
- return entity.view:GetTransform()
- end
- return nil
- end
- function EntityMgr:ForceRefreshGenerateView(entityType, uid)
- local entity = self:GetEntity(entityType, uid)
- if entity then
- entity:ForceRefreshGenerateView()
- end
- end
- return EntityMgr
|