2025-01-04 23:00:19 +08:00

540 lines
17 KiB
Lua

---@type table<string, UClass>
---@private
UE.CachedClasses = {};
--- 组件位置
UE.ComponentPath = {
Camera = "/Script/Engine.CameraComponent",
SpringArm = "/Script/Engine.SpringArmComponent",
}
---@param InInfo PlayerAccountInfo
---@return PlayerAccountInfo
function UE.HandleAccountInfo(InInfo)
return {
UID = InInfo.UID,
PlayerName = InInfo.PlayerName,
TeamID = InInfo.TeamID,
Gender = InInfo.Gender,
PlatformGender = InInfo.PlatformGender,
PlayerLevel = InInfo.PlayerLevel,
SegmentLevel = InInfo.SegmentLevel,
IconURL = InInfo.IconURL,
};
end
function UE.InitKDA()
return {
Kill = 0,
Dead = 0,
Assist = 0,
}
end
function UE.GetPlayerName(PlayerKey)
local Info = UE.GetAccountInfo(PlayerKey);
if Info then return Info.PlayerName; end
return '';
end
function UE.GetPlayerUID(PlayerKey)
local Info = UE.GetAccountInfo(PlayerKey);
if Info then return Info.UID; end
return -1;
end
function UE.GetPlayerIconURL(PlayerKey)
local Info = UE.GetAccountInfo(PlayerKey);
if Info then return Info.IconURL; end
return '';
end
---@return table
function UE.InitArchiveData()
return {
GameTimes = 0,
}
end
---@param InClass UClass<AActor>
---@param InTable table<int32, AActor> 获得的
---@param InFunc fun(InIndex:int32, InActor:AActor): bool
---@return table<int32, AActor>
function UE.FindActorsByClass(InClass, InTable, InFunc)
if InTable == nil then InTable = {}; end
local AllActors = {};
if InClass == nil then InClass = UE.GetActorClass(); end
GameplayStatics.GetAllActorsOfClass(UGCGameSystem.GameState, InClass, AllActors);
for i, v in pairs(AllActors) do if InFunc == nil or InFunc(i, v) then table.insert(InTable, v); end end
return InTable;
end
---@param InTable table<int32, AActor>
---@param InTag FName
---@param InFunc fun(InActor: AActor): bool
---@return table<int32, AActor>
function UE.FindActorsByTag(InTag, InTable, InFunc)
if table.isEmpty(InTable) then InTable = {}; end
local AllActors = {};
GameplayStatics.GetAllActorsWithTag(UGCGameSystem.GameState, InTag, AllActors);
for i, v in pairs(AllActors) do if InFunc == nil or InFunc(v) then table.insert(InTable, v); end end
return InTable;
end
function UE.GetActorClass()
return UGCGameSystem.GameState.ActorClass;
end
---@param InTag FName
---@param InFunc fun(InActor: AActor): bool
---@return table<int32, AActor>
function UE.FindActorByTag(InTag, InFunc)
local AllActors = {};
GameplayStatics.GetAllActorsWithTag(UGCGameSystem.GameState, InTag, AllActors);
for i, v in pairs(AllActors) do if InFunc == nil or InFunc(v) then return v; end end
return nil;
end
---@param InClass UClass<AActor>
---@param InFunc fun(InIndex:int32, InActor:AActor): bool
---@return AActor
function UE.FindActorByClass(InClass, InFunc)
local Table = {};
UE.FindActorsByClass(InClass, Table, InFunc);
if table.isEmpty(Table) then return nil; end
return Table[1];
end
---@type table<UClass, AActor>
UE.GlobalCachedActor = {};
---@param InClass UClass
---@return nil | AActor
function UE.FindOrCreateActorByClass(InClass, bDestroyOther)
print(string.format('[UE.FindOrCreateActorByClass] 开始执行, ClassName = %s', UE.GetName(InClass)));
if InClass == nil then return nil; end
local CachedActor = UE.GlobalCachedActor[InClass];
if CachedActor ~= nil and UE.IsValid(CachedActor) then return CachedActor; end
local Actors = {}
UE.FindActorsByClass(InClass, Actors);
local Actor = nil;
for i, v in pairs(Actors) do
Actor = v;
if Actor ~= v and bDestroyOther then v:K2_DestroyActor(); end
end
if Actor ~= nil then
UE.GlobalCachedActor[InClass] = Actor;
return Actor;
end
return UE.CreateActor(InClass);
end
---@param ClassPath string 类路径
---@return UClass | nil
function UE.CachedLoadClass(ClassPath)
if UE.CachedClasses[ClassPath] == nil then UE.CachedClasses[ClassPath] = UE.LoadClass(ClassPath); end
if UE.CachedClasses[ClassPath] == nil then
print(string.format('[UE.CachedLoadClass] Error Cant Load Class By Path: %s', ClassPath));
return nil;
end
return UE.CachedClasses[ClassPath];
end
function UE.CreateActor(InClass, InCount, InOuter)
if InOuter == nil then InOuter = UGCGameSystem.GameState; end
if InCount == nil then InCount = 1 end
local Actors = {};
for i = 1, InCount do table.insert(Actors, UE.SpawnActor(InClass, InOuter)) end
if InCount == 1 then return Actors[1]; end
return Actors;
end
---@param InClass UClass
---@param Loc FVector
---@param Rot FRotator
---@param Scale FVector
function UE.SpawnActor(InClass, InOuter, Loc, Rot, Scale)
Loc = Loc == nil and VectorHelper.MakeVector1(0);
Rot = Rot == nil and VectorHelper.MakeRotator(VectorHelper.MakeVector1(0));
Scale = Scale == nil and VectorHelper.MakeVector1(1);
if InOuter == nil then InOuter = GameState; end
return ScriptGameplayStatics.SpawnActor(InOuter, InClass, Loc, Rot, Scale);
end
--- 玩家重生
---@param InPlayerKey PlayerKey
---@param InTime float
function UE.RespawnPlayer(InPlayerKey, InTime)
UGCEventSystem.SetTimer(GameState, function()
local Pawn = UGCGameSystem.GetPlayerPawnByPlayerKey(InPlayerKey);
if Pawn then Pawn:K2_DestroyActor(); end
UGCGameSystem.RespawnPlayer(InPlayerKey);
end, InTime);
end
--- 获取当前时间
function UE.GetCurrentTime() return KismetSystemLibrary.GetGameTimeInSeconds(UGCGameSystem.GameState); end
---@param InPlayerKey PlayerKey
---@return PlayerAccountInfo
function UE.GetAccountInfo(InPlayerKey)
UGCLogSystem.Log("[UE.GetAccountInfo] InPlayerKey = %s", tostring(InPlayerKey));
return GameState.PlayerDatas.AccountInfo[InPlayerKey];
end
--- 移动玩家到某个地方
---@param InPawn UGCPlayerPawn_C
---@param Location FVector
---@param Rotation FRotator
function UE.MovePlayerToLocation(InPawn, Location, Rotation)
if InPawn == nil then return end
InPawn:bShouldDumpCallstackWhenMovingfast_Tofalse()
InPawn:SetClientLocationOrRotation(Location, Rotation, true, Rotation ~= nil, false);
InPawn:bShouldDumpCallstackWhenMovingfast_ToTrue()
end
--- 激活对应的 Camera
---@param InPawn UGCPlayerPawn_C
---@param InCamera UCameraComponent
---@param InArm USpringArmComponent
function UE.ActiveCamera(InPawn, InCamera, InArm)
if InPawn == nil then return end
UE.ForeachComponent(InPawn, UE.ComponentPath.Camera, function(c) c:Deactivate() end)
UE.ForeachComponent(InPawn, UE.ComponentPath.SpringArm, function(c) c:Deactivate() end)
InCamera:Activate();
InArm:Activate();
table.func(InPawn, "SetCurrentCamera", InCamera, InArm);
end
--- 遍历 Actor 上面的所有组件
---@param InActor AActor
---@param InComponent UActorComponent
---@param cb fun(c: UActorComponent)
function UE.ForeachComponent(InActor, InComponent, cb)
local Path = UE.ComponentPath[InComponent] == nil and InComponent or UE.ComponentPath[InComponent];
for i, v in pairs(InActor:GetComponentsByClass(LoadClass(Path))) do cb(v); end
end
---清空地图上的 Wrapper
function UE.ClearWrappers(bWeapon, bPart, bBullet, bEquipment, bSkin, bSupplies)
local AllActors = {};
local Func = function(b)
if b == nil then return true end
return b;
end
bWeapon = Func(bWeapon);
bPart = Func(bPart);
bBullet = Func(bBullet);
bEquipment = Func(bEquipment);
bSkin = Func(bSkin);
bSupplies = Func(bSupplies);
UE.FindActorsByClass(GameState:GetWrapperClass(), AllActors, function(InIndex, InActor)
if InActor.DefineID == nil then return true; end
local ItemId = InActor.DefineID.TypeSpecificID;
if ItemId == nil or (not InActor.DefineID.bValidItem) then return true; end
UGCLogSystem.Log("[UE.ClearWrappers] ItemId = %s", tostring(ItemId));
local Type = GetCustomItemType(ItemId);
if (bWeapon and Type == ECustomItemType.Weapon) or
(bPart and Type == ECustomItemType.Part) or
(bBullet and Type == ECustomItemType.Bullet) or
(bEquipment and Type == ECustomItemType.Equipment) or
(bSkin and Type == ECustomItemType.Skin) or
(bSupplies and Type == ECustomItemType.Supplies) then
InActor:K2_DestroyActor()
end
end);
end
--- 组件朝向玩家
---@param InComponent USceneComponent
---@param InActor AActor
---@param Lock string 锁定的轴,如果是 Y 轴:那么就相当于让组件对着玩家
function UE.ComponentTowardActor(InComponent, InActor, Lock)
if InComponent == nil then return ; end
local Rot = KismetMathLibrary.Conv_VectorToRotator(VectorHelper.Sub(InActor:K2_GetActorLocation(), InComponent:GetOwner():K2_GetActorLocation()));
if Lock ~= nil then
if Lock == 'Z' or Lock == 'Yaw' then
Rot = { Roll = Rot.Roll, Pitch = Rot.Pitch, Yaw = 0 };
elseif Lock == 'Y' or Lock == 'Pitch' then
Rot = { Roll = Rot.Roll, Pitch = 0, Yaw = Rot.Yaw };
elseif Lock == 'X' or Lock == 'Roll' then
Rot = { Roll = 0, Pitch = Rot.Pitch, Yaw = Rot.Yaw };
end
end
InComponent:K2_SetWorldRotation(Rot);
end
--- 获取服务器时间
function UE.GetServerTime()
if UGCGameSystem.GameState then return UGCGameSystem.GameState:GetServerTime(); end
return nil
end
---@param InTable table<int32|string, AActor>
---@param InFunc fun(InActor:AActor):bool
function UE.DestroyTable(InTable, InFunc)
if table.isEmpty(InTable) then return ; end
for i, v in pairs(InTable) do if InFunc == nil or InFunc(v) then v:K2_DestroyActor(); end end
InTable = {};
end
function UE.IsPlayerPawn(InPawn) return UE.IsA(InPawn, ObjectPath.UGCPlayerPawn) end
---@param InPawn UGCPlayerPawn_C
function UE.IsValidPawn(InPawn)
return InPawn ~= nil and UE.IsValid(InPawn) and InPawn:IsAlive();
end
---@param TeamId int32
---@param OutPawns table<int32, UGCPlayerPawn_C>
---@param InFunc fun(InPawn:UGCPlayerPawn_C):bool
function UE.GetValidPawnsByTeamID(TeamId, OutPawns, InFunc)
local PlayerKeys = UGCTeamSystem.GetPlayerKeysByTeamID(TeamId);
if OutPawns == nil then OutPawns = {}; end
for i, PlayerKey in pairs(PlayerKeys) do
local Pawn = UGCGameSystem.GetPlayerPawnByPlayerKey(PlayerKey);
if UE.IsValidPawn(Pawn) then
if InFunc == nil or (type(InFunc) == 'function') then
table.insert(OutPawns, Pawn);
end
end
end
return OutPawns;
end
function UE.GetOtherTeamIDs(TeamID)
local TeamIds = UGCTeamSystem.GetTeamIDs();
local Ret = {};
for i, v in pairs(TeamIds) do
if v ~= TeamID then table.insert(Ret, v); end
end
return Ret;
end
---@param TeamID int32
---@param OutPawns table<int32, UGCPlayerPawn_C>
---@param InFunc fun(InPawn:UGCPlayerPawn_C):bool
function UE.GetOtherPawnsByTeamID(TeamID, OutPawns, InFunc)
local AllTeamIds = UE.GetOtherTeamIDs(TeamID);
if OutPawns == nil then OutPawns = {}; end
for _i, TeamId in pairs(AllTeamIds) do
local PlayerKeys = UGCTeamSystem.GetPlayerKeysByTeamID(TeamId)
UE.GetValidPawnsByTeamID(TeamId, OutPawns, InFunc);
end
return OutPawns;
end
function UE.ReturnToLobby()
NetUtil.SendPkg("giveup_enter_game");
LobbySystem.ReturnToLobby();
end
function UE.LoadClass1(Path, cb)
local Class = UE.LoadClass(Path);
if cb ~= nil then cb(Class); end
return Class
end
UE.AsyncCachedItems = {};
---@param Path string
---@param cb fun(TargetClass: UClass)
function UE.AsyncLoadClass(Path, cb)
if cb == nil then return end
if UE.AsyncCachedItems[Path] then
cb(UE.AsyncCachedItems[Path]);
return ;
end
CommonUtils:AsyncLoadClass(UGCGameSystem.GameState, Path, function(TargetClass)
if TargetClass and UE.IsValid(TargetClass) then cb(TargetClass); end
end)
end
---@param Path string
---@param cb fun(TargetClass: UClass)
function UE.AsyncLoadClass_Cached(Path, cb)
if UE.AsyncCachedItems[Path] ~= nil then
if cb then cb(UE.AsyncCachedItems[Path]); end
return ;
end
if cb == nil then return end
CommonUtils:AsyncLoadClass(UGCGameSystem.GameState, Path, function(TargetClass)
if TargetClass and UE.IsValid(TargetClass) then
UE.AsyncCachedItems[Path] = TargetClass;
if cb then cb(UE.AsyncCachedItems[Path]); end
end
end)
end
---@param Path string
---@param cb fun(TargetObject: UObject)
function UE.AsyncLoadObject(Path, cb)
if cb == nil then return end
if UE.AsyncCachedItems[Path] then
cb(UE.AsyncCachedItems[Path]);
return ;
end
CommonUtils:AsyncLoadObject(UGCGameSystem.GameState, Path, function(TargetObject)
if TargetObject and UE.IsValid(TargetObject) then cb(TargetObject); end
end)
end
---@param Path string
---@param cb fun(TargetObject: UObject)
function UE.AsyncLoadObject_Cached(Path, cb)
if UE.AsyncCachedItems[Path] ~= nil then
if cb then cb(UE.AsyncCachedItems[Path]); end
return ;
end
CommonUtils:AsyncLoadObject(UGCGameSystem.GameState, Path, function(TargetObject)
if TargetObject and UE.IsValid(TargetObject) then
UE.AsyncCachedItems[Path] = TargetObject;
if cb then cb(TargetObject); end
end
end)
end
function UE.AsyncLoadObj(Path, cb)
CommonUtils:AsyncLoadObject(UGCGameSystem.GameState, Path, function(TargetObject)
if TargetObject and UE.IsValid(TargetObject) then
if cb then cb(TargetObject); end
end
end)
end
---@param InFunc fun(Pawn:UGCPlayerPawn_C)
function UE.ForeachAllPawns(InFunc)
for i, Pawn in pairs(UGCGameSystem.GetAllPlayerPawn()) do
if UE.IsValidPawn(Pawn) then InFunc(Pawn); end
end
end
---@param InFunc fun(PC:UGCPlayerController_C)
function UE.ForeachAllPCs(InFunc)
for i, PC in pairs(UGCGameSystem.GetAllPlayerController()) do
if PC and UE.IsValid(PC) then InFunc(PC); end
end
end
---@param InPlayerKey PlayerKey
---@param InFunc fun(Pawn:UGCPlayerPawn_C)
function UE.ForeachSelfTeam(InPlayerKey, InFunc, bWithoutSelf)
if InFunc == nil then return end
local LocalPawn = UGCGameSystem.GetPlayerPawnByPlayerKey(InPlayerKey);
local LocalTeamId = UGCPawnAttrSystem.GetTeamID(LocalPawn);
for i, Pawn in pairs(UGCGameSystem.GetAllPlayerPawn()) do
if UE.IsValidPawn(Pawn) then
if Pawn.PlayerKey == InPlayerKey then
if not bWithoutSelf then
InFunc(Pawn);
end
else
local TeamId = UGCPawnAttrSystem.GetTeamID(Pawn);
if TeamId == LocalTeamId then InFunc(Pawn); end
end
end
end
end
function UE.IsSameTeam(PlayerKey1, PlayerKey2)
return UGCPlayerStateSystem.GetTeamID(PlayerKey1) == UGCPlayerStateSystem.GetTeamID(PlayerKey2);
end
---@param InPlayerKey PlayerKey
---@param InFunc fun(Pawn:UGCPlayerPawn_C)
function UE.ForeachEnemyTeam(InPlayerKey, InFunc)
if InFunc == nil then return end
local LocalPawn = UGCGameSystem.GetPlayerPawnByPlayerKey(InPlayerKey);
local LocalTeamId = UGCPawnAttrSystem.GetTeamID(LocalPawn);
for i, Pawn in pairs(UGCGameSystem.GetAllPlayerPawn()) do
if UE.IsValidPawn(Pawn) then
local TeamId = UGCPawnAttrSystem.GetTeamID(Pawn);
if TeamId ~= LocalTeamId then InFunc(Pawn); end
end
end
end
---@param InFunc fun(PC:UGCPlayerController_C)
function UE.ForeachAllPCs(InFunc)
for i, PC in pairs(UGCGameSystem.GetAllPlayerController()) do
if PC and UE.IsValid(PC) then InFunc(PC); end
end
end
UE.WrapperClass = nil;
function UE.GetWrapperClass()
if UE.WrapperClass == nil then
UE.WrapperClass = ScriptGameplayStatics.FindClass("PickUpWrapperActor")
end
return UE.WrapperClass;
end
---@param InComponent UShapeComponent
---@param Func fun(InPawn:UGCPlayerPawn_C)
---@param InOwner AActor
function UE.BeginOverlap(InComponent, Func, InOwner)
if Func == nil then return ; end
InComponent.OnComponentBeginOverlap:Add(function(Self, OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult)
if OtherActor ~= nil and UE.IsPlayerPawn(OtherActor) then
UGCLogSystem.Log("[UE.BeginOverlap] OtherActor = %s, InOwner = %s", UE.GetName(OtherActor), UE.GetName(InOwner));
Func(InOwner, OtherActor)
end
end, InOwner);
end
---@param InComponent UShapeComponent
---@param Func fun(InPawn:UGCPlayerPawn_C)
---@param InOwner AActor
function UE.EndOverlap(InComponent, Func, InOwner)
if Func == nil then return ; end
InComponent.OnComponentEndOverlap:Add(function(Self, OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex)
if OtherActor ~= nil and UE.IsPlayerPawn(OtherActor) then
UGCLogSystem.Log("[UE.EndOverlap] OtherActor = %s, InOwner = %s", UE.GetName(OtherActor), UE.GetName(InOwner));
Func(InOwner, OtherActor);
end
end, InOwner);
end
---@type table<PlayerKey, table<int32, UParticleSystemComponent>>
UE.SpawnedEmitters = {};
---@param Particle UParticleSystem
---@param Pawn UGCPlayerPawn_C
---@return UParticleSystemComponent
function UE.SpawnEmitterAttached(Particle, Pawn, Offset)
if Offset == nil then Offset = VectorHelper.VectorZero() end
if type(Particle) == 'string' then Particle = ObjectPath[Particle]; end
local Emitter = GameplayStatics.SpawnEmitterAttached(Particle, Pawn.Mesh, "", Offset, VectorHelper.RotZero(), Pawn:GetActorScale3D(), EAttachLocation.SnapToTargetIncludingScale, true);
if UE.SpawnedEmitters[Pawn.PlayerKey] == nil then
UE.SpawnedEmitters[Pawn.PlayerKey] = {};
end
table.insert(UE.SpawnedEmitters[Pawn.PlayerKey], Emitter);
return Emitter;
end
---@param Particle UParticleSystem
---@param Location FVector
function UE.SpawnEmitterAtLocation(Particle, Location)
if type(Particle) == 'string' then Particle = ObjectPath[Particle]; end
return GameplayStatics.SpawnEmitterAtLocation(GameState, Particle, Location, VectorHelper.RotZero(), VectorHelper.ScaleOne(), true);
end
function UE.RespawnAllPlayer(InTime)
if InTime == nil or InTime <= 0 then
UE.ForeachAllPCs(function(PC)
local Pawn = UGCGameSystem.GetPlayerPawnByPlayerKey(PC.PlayerKey);
if Pawn then Pawn:K2_DestroyActor(); end
UGCGameSystem.RespawnPlayer(PC.PlayerKey);
end)
end
UGCEventSystem.SetTimer(GameState, function()
UE.ForeachAllPCs(function(PC)
local Pawn = UGCGameSystem.GetPlayerPawnByPlayerKey(PC.PlayerKey);
if Pawn then Pawn:K2_DestroyActor(); end
UGCGameSystem.RespawnPlayer(PC.PlayerKey);
end)
end, InTime);
end