111 lines
3.0 KiB
Lua
111 lines
3.0 KiB
Lua
local ActorPool = {};
|
||
|
||
---@type int32 当前使用的 Actor
|
||
ActorPool.ActorCount = 0;
|
||
---@type AActor
|
||
ActorPool.Owner = nil;
|
||
---@type UClass 子项函数
|
||
ActorPool.ItemClass = nil;
|
||
---@type table<int32, AActor>
|
||
ActorPool.ActorMap = {};
|
||
---@type table<string, bool> 是否使用列表
|
||
ActorPool.ActorUseMap = {};
|
||
|
||
--- 初始化,创建 InNum 个放到
|
||
--- S
|
||
---@param InNum int32 需要创建的数据
|
||
---@param InClass string | UClass
|
||
---@param InOwner AActor 当前 Actor 池拥有者
|
||
---@param InLoc FVector 创建的位置
|
||
---@param InFunc fun(InActor: AActor, InIndex: int32)
|
||
function ActorPool:Init(InNum, InClass, InOwner, InLoc, InRot, InFunc)
|
||
self.ActorCount = InNum;
|
||
self.ItemClass = type(InClass) == 'string' and UE.LoadClass(InClass) or InClass;
|
||
self.Owner = InOwner == nil and UGCGameSystem.GameState or InOwner;
|
||
if InLoc == nil then InLoc = VectorHelper.VectorZero(); end
|
||
if InRot == nil then InRot = VectorHelper.RotZero(); end
|
||
--InOwner.ActorPool = self;
|
||
for i = 1, InNum do
|
||
local Item = UGCGameSystem.SpawnActor(self.Owner, self.ItemClass, InLoc, InRot, VectorHelper.ScaleOne(), self.Owner);
|
||
if Item == nil then
|
||
UGCLogSystem.Log("[ActorPool:Init] 无法创建出来对应 Actor:%s", tostring(InClass));
|
||
return;
|
||
end
|
||
local Index = #self.ActorMap + 1;
|
||
self.ActorMap[Index] = Item;
|
||
self.ActorUseMap[UE.GetName(Item)] = false;
|
||
if InFunc ~= nil and type(InFunc) == 'function' then
|
||
InFunc(Item, Index);
|
||
end
|
||
end
|
||
|
||
UGCLogSystem.LogTree(string.format("[ActorPool:Init] self.ActorMap ="), self.ActorMap)
|
||
return self.ActorMap;
|
||
end
|
||
|
||
--- 客户端初始化
|
||
function ActorPool:ClientInit(InTable)
|
||
|
||
end
|
||
|
||
--- 拿出一个使用
|
||
---@param InFunc fun(InActor: AActor)
|
||
---@return AActor | nil
|
||
function ActorPool:Pop(InFunc)
|
||
-- 直接拿出一个即可
|
||
for i, v in pairs(self.ActorMap) do
|
||
if not self:IsActorInUse(v) then
|
||
self.ActorUseMap[UE.GetName(v)] = true;
|
||
table.func(InFunc, v);
|
||
return v;
|
||
end
|
||
end
|
||
return nil;
|
||
end
|
||
|
||
--- 返还一个
|
||
---@param InActor AActor
|
||
---@param InFunc fun(InActor: AActor)
|
||
function ActorPool:Push(InActor, InFunc)
|
||
local Name = UE.GetName(InActor)
|
||
if not self.ActorUseMap[Name] then return false; end
|
||
table.func(InFunc, InActor);
|
||
self.ActorUseMap[Name] = false;
|
||
return true;
|
||
end
|
||
|
||
---@param InActor AActor
|
||
---@return boolean
|
||
function ActorPool:IsActorInUse(InActor)
|
||
return self.ActorUseMap[UE.GetName(InActor)];
|
||
end
|
||
|
||
---@return int32 获取当前已经使用的数量
|
||
function ActorPool:GetUsedCount()
|
||
local Count = 0;
|
||
for i, v in pairs(self.ActorUseMap) do
|
||
if v then Count = Count + 1; end
|
||
end
|
||
return Count;
|
||
end
|
||
|
||
---@return int 获取当前还未使用的数量
|
||
function ActorPool:GetNoUseCount()
|
||
return self.ActorCount - self:GetUsedCount();
|
||
end
|
||
|
||
--- 清空池子
|
||
function ActorPool:Clear()
|
||
for i, v in pairs(self.ActorMap) do
|
||
v:K2_DestroyActor();
|
||
end
|
||
self.ActorMap = {};
|
||
self.ActorUseMap = {};
|
||
self.ActorCount = 0;
|
||
end
|
||
|
||
--function ActorPool:SendRPC(FuncName, ...)
|
||
-- self.Owner:SendActorPoolRPC(FuncName, ...);
|
||
--end
|
||
|
||
return ActorPool; |