Files
UnrealLuaDemo/Script/lua/Game/LuaDemoActor.lua
T

331 lines
9.5 KiB
Lua

local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local ____exports = __module.env
local Log = __MODULE__.Import("Framework.Log", __module)
local EventBus = __MODULE__.Import("Framework.EventBus", __module)
local HotReload = __MODULE__.Import("Framework.HotReload", __module)
local globalPingCount, globalReloadCount
____exports.DEMO_VERSION = "v1"
--- 目标旋转速度(度/秒,直接驱动方块自转速度,热更后下一帧立即生效)
____exports.ACTOR_SPIN_SPEED = 60
--- 当前展示配色(热更修改颜色值与名称后,__module.OnReimport 立即刷新方块材质与 3D 文本)
____exports.DEMO_COLORS = {
{
Name = "Cyan",
R = 0.08,
G = 0.78,
B = 0.95,
A = 1
},
{
Name = "Gold",
R = 0.98,
G = 0.72,
B = 0.12,
A = 1
},
{
Name = "Magenta",
R = 0.92,
G = 0.22,
B = 0.65,
A = 1
},
{
Name = "Lime",
R = 0.25,
G = 0.9,
B = 0.35,
A = 1
},
{
Name = "Orange",
R = 0.98,
G = 0.42,
B = 0.14,
A = 1
}
}
---
-- @noSelf
local function MakeLinearColor(c)
return UE.FLinearColor(c.R, c.G, c.B, c.A)
end
---
-- @noSelf
local function IsActorAlive(actor)
if actor == nil then
return false
end
if UE ~= nil and UE.UKismetSystemLibrary ~= nil and UE.UKismetSystemLibrary.IsValid ~= nil then
return UE.UKismetSystemLibrary.IsValid(actor)
end
return true
end
____exports.DemoActorController = __TS__DefineClass("DemoActorController", __module)
local DemoActorController = ____exports.DemoActorController
DemoActorController.name = "DemoActorController"
function DemoActorController.prototype.____constructor(self, actor)
self.elapsedTime = 0
self.textRefreshTimer = 0
self.tickCount = 0
self.pingCount = 0
self.reloadCount = 0
self.colorIndex = 0
self.hasLoggedTick = false
self.actor = actor
self._tmpDeltaRot = UE.FRotator(0, 0, 0)
end
function DemoActorController.prototype.IsValid(self)
return IsActorAlive(self.actor)
end
function DemoActorController.prototype.GetSpinSpeed(self)
return ____exports.ACTOR_SPIN_SPEED
end
function DemoActorController.prototype.GetCurrentColor(self)
return ____exports.DEMO_COLORS[self.colorIndex + 1] or ____exports.DEMO_COLORS[1]
end
function DemoActorController.prototype.OnBeginPlay(self)
EventBus.On("Demo.Ping", self.OnDemoPing, self)
EventBus.On(HotReload.EVENT_AFTER_RELOAD, self.OnAfterHotReload, self)
self:ApplyCurrentColor()
self:RefreshStatusText()
Log.Info(
"LuaDemoActor.OnBeginPlay() ready! version=%s speed=%.1f color=%s Lua=%s",
____exports.DEMO_VERSION,
self:GetSpinSpeed(),
self:GetCurrentColor().Name,
_VERSION
)
local msg = string.format(
"[UnLua Demo] Pure-TS Actor active (%s | Speed=%.0f | Color=%s) - [Space] Ping, [R] HotReload",
____exports.DEMO_VERSION,
self:GetSpinSpeed(),
self:GetCurrentColor().Name
)
UE.UKismetSystemLibrary.PrintString(
self.actor,
msg,
true,
true,
UE.FLinearColor(0.2, 1, 0.4, 1),
8
)
end
function DemoActorController.prototype.OnTick(self, deltaSeconds)
self.elapsedTime = self.elapsedTime + deltaSeconds
self.tickCount = self.tickCount + 1
if not self.hasLoggedTick then
self.hasLoggedTick = true
Log.Info(
"LuaDemoActor.OnTick() first tick verified (dt=%.4f, speed=%.1f)",
deltaSeconds,
self:GetSpinSpeed()
)
end
local yawDelta = self:GetSpinSpeed() * deltaSeconds
local pitchDelta = yawDelta * 0.35
self._tmpDeltaRot.Pitch = pitchDelta
self._tmpDeltaRot.Yaw = yawDelta
self._tmpDeltaRot.Roll = 0
self.actor:AddCubeLocalRotation(self._tmpDeltaRot)
local zOffset = math.sin(self.elapsedTime * (self.actor.BobFrequency or 2)) * (self.actor.BobAmplitude or 25)
self.actor:SetCubeVerticalOffset(zOffset)
self.textRefreshTimer = self.textRefreshTimer + deltaSeconds
if self.textRefreshTimer >= 0.2 then
self.textRefreshTimer = 0
self:RefreshStatusText()
end
end
function DemoActorController.prototype.OnDemoPing(self, sourceName)
self.pingCount = self.pingCount + 1
globalPingCount = globalPingCount + 1
self.colorIndex = (self.colorIndex + 1) % #____exports.DEMO_COLORS
local colorEntry = self:ApplyCurrentColor()
self:RefreshStatusText()
local msg = string.format(
"[UnLua Demo] Ping #%d from %s -> Cube color: %s (Speed=%.0f)",
self.pingCount,
tostring(sourceName),
colorEntry.Name,
self:GetSpinSpeed()
)
Log.Info(msg)
UE.UKismetSystemLibrary.PrintString(
self.actor,
msg,
true,
true,
MakeLinearColor(colorEntry),
3
)
end
function DemoActorController.prototype.OnAfterHotReload(self, moduleName)
self.reloadCount = self.reloadCount + 1
globalReloadCount = globalReloadCount + 1
self:ApplyCurrentColor()
self:RefreshStatusText()
local msg = string.format(
"[UnLua Demo] HotReload #%d applied! target=%s | version=%s | Speed=%.0f | Color=%s",
self.reloadCount,
tostring(moduleName),
____exports.DEMO_VERSION,
self:GetSpinSpeed(),
self:GetCurrentColor().Name
)
Log.Info(msg)
UE.UKismetSystemLibrary.PrintString(
self.actor,
msg,
true,
true,
UE.FLinearColor(1, 0.85, 0.2, 1),
4
)
end
function DemoActorController.prototype.ResetToPrimaryColor(self)
self.colorIndex = 0
end
function DemoActorController.prototype.ApplyCurrentColor(self)
local colorEntry = self:GetCurrentColor()
self.actor:SetCubeColor(MakeLinearColor(colorEntry))
return colorEntry
end
function DemoActorController.prototype.RefreshStatusText(self)
local colorEntry = self:GetCurrentColor()
local text = string.format(
"Pure-TS Actor (%s | %s)\nSpeed: %.0f deg/s | Color: %s\nTime: %.1fs | Pings: %d | Reloads: %d\n[Space] Ping | [R] HotReload",
_VERSION,
____exports.DEMO_VERSION,
self:GetSpinSpeed(),
colorEntry.Name,
self.elapsedTime,
self.pingCount,
self.reloadCount
)
self.actor:SetStatusText(text)
end
function DemoActorController.prototype.OnEndPlay(self, _endPlayReason)
EventBus.OffAllForOwner(self)
Log.Info("LuaDemoActor.OnEndPlay(ticks=%d, pings=%d)", self.tickCount, self.pingCount)
end
---
-- @module_data
local activeControllers = {}
---
-- @module_data
globalPingCount = 0
---
-- @module_data
globalReloadCount = 0
---
-- @noSelf
local function GetActorKey(actor)
if actor.GetName ~= nil then
return actor:GetName()
end
return tostring(actor)
end
---
-- @noSelf
function ____exports.OnBeginPlay(actor)
local key = GetActorKey(actor)
local old = activeControllers[key]
if old ~= nil then
old:OnEndPlay(0)
end
local ctrl = __TS__New(____exports.DemoActorController, actor)
activeControllers[key] = ctrl
ctrl:OnBeginPlay()
end
---
-- @noSelf
function ____exports.OnTick(actor, deltaSeconds)
local ctrl = activeControllers[GetActorKey(actor)]
if ctrl ~= nil then
ctrl:OnTick(deltaSeconds)
end
end
---
-- @noSelf
function ____exports.OnEndPlay(actor, endPlayReason)
local key = GetActorKey(actor)
local ctrl = activeControllers[key]
if ctrl ~= nil then
ctrl:OnEndPlay(endPlayReason)
activeControllers[key] = nil
end
end
---
-- @noSelf
function ____exports.GetGlobalStats()
return {
pings = globalPingCount,
reloads = globalReloadCount,
version = ____exports.DEMO_VERSION,
spinSpeed = ____exports.ACTOR_SPIN_SPEED,
primaryColor = ____exports.DEMO_COLORS[1].Name
}
end
---
-- @noSelf
function ____exports.GetActiveControllerCount()
local count = 0
for _, ctrl in pairs(activeControllers) do
if ctrl ~= nil then
count = count + 1
end
end
return count
end
__module.OnReimport = function()
for key, ctrl in pairs(activeControllers) do
if ctrl ~= nil then
if ctrl:IsValid() then
ctrl:ResetToPrimaryColor()
ctrl:ApplyCurrentColor()
ctrl:RefreshStatusText()
else
activeControllers[key] = nil
end
end
end
Log.Info(
"Game.LuaDemoActor OnReimport applied! version=%s speed=%.1f color=%s activeActors=%d",
____exports.DEMO_VERSION,
____exports.ACTOR_SPIN_SPEED,
____exports.DEMO_COLORS[1].Name,
____exports.GetActiveControllerCount()
)
end
function __module.StashData(env, to)
local ____has = to.__has__
if not ____has then
____has = {}
to.__has__ = ____has
end
to.activeControllers = activeControllers
____has.activeControllers = true
to.globalPingCount = globalPingCount
____has.globalPingCount = true
to.globalReloadCount = globalReloadCount
____has.globalReloadCount = true
end
function __module.RestoreData(env, from)
local ____has = from.__has__
if not ____has then
return
end
if ____has.activeControllers then
activeControllers = from.activeControllers
end
if ____has.globalPingCount then
globalPingCount = from.globalPingCount
end
if ____has.globalReloadCount then
globalReloadCount = from.globalReloadCount
end
end
function __module.UpdateImports()
end