feat: upgrade to ts2lua-unreal v0.3.0 and refactor Game classes to pure C++ callback + TS composition hot-reload
This commit is contained in:
1 parent
4ad4a8f36f
commit
783dccf53d
64 files changed
+2368
-714
No files matched your search
@@ -1,7 +1,15 @@
|
||||
local Log = require("Framework.Log")
|
||||
local EventBus = require("Framework.EventBus")
|
||||
local HotReload = require("Framework.HotReload")
|
||||
local DEMO_COLORS = {
|
||||
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,
|
||||
@@ -43,24 +51,60 @@ local DEMO_COLORS = {
|
||||
local function MakeLinearColor(c)
|
||||
return UE.FLinearColor(c.R, c.G, c.B, c.A)
|
||||
end
|
||||
local LuaDemoActor = UnLua.Class()
|
||||
function LuaDemoActor.ReceiveBeginPlay(self)
|
||||
self.ElapsedTime = 0
|
||||
self.TextRefreshTimer = 0
|
||||
self.TickCount = 0
|
||||
self.PingCount = 0
|
||||
self.ReloadCount = 0
|
||||
self.ColorIndex = 0
|
||||
self.HasLoggedTick = false
|
||||
---
|
||||
-- @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:ReceiveBeginPlay() bound successfully! Lua=%s", _VERSION)
|
||||
local msg = string.format("[UnLua Demo] LuaDemoActor active (%s) - Press [Space] to Ping, [R] to HotReload", _VERSION)
|
||||
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,
|
||||
self.actor,
|
||||
msg,
|
||||
true,
|
||||
true,
|
||||
@@ -68,45 +112,47 @@ function LuaDemoActor.ReceiveBeginPlay(self)
|
||||
8
|
||||
)
|
||||
end
|
||||
function LuaDemoActor.ReceiveTick(self, DeltaSeconds)
|
||||
self.ElapsedTime = (self.ElapsedTime or 0) + DeltaSeconds
|
||||
self.TickCount = (self.TickCount or 0) + 1
|
||||
if not self.HasLoggedTick then
|
||||
self.HasLoggedTick = true
|
||||
Log.Info("LuaDemoActor:ReceiveTick() first tick verified (dt=%.4f)", DeltaSeconds)
|
||||
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.SpinSpeed or 60) * DeltaSeconds
|
||||
local yawDelta = self:GetSpinSpeed() * deltaSeconds
|
||||
local pitchDelta = yawDelta * 0.35
|
||||
if not self._tmpDeltaRot then
|
||||
self._tmpDeltaRot = UE.FRotator(pitchDelta, yawDelta, 0)
|
||||
else
|
||||
self._tmpDeltaRot.Pitch = pitchDelta
|
||||
self._tmpDeltaRot.Yaw = yawDelta
|
||||
self._tmpDeltaRot.Roll = 0
|
||||
end
|
||||
self:AddCubeLocalRotation(self._tmpDeltaRot)
|
||||
local zOffset = math.sin(self.ElapsedTime * (self.BobFrequency or 2)) * (self.BobAmplitude or 25)
|
||||
self:SetCubeVerticalOffset(zOffset)
|
||||
self.TextRefreshTimer = (self.TextRefreshTimer or 0) + DeltaSeconds
|
||||
if self.TextRefreshTimer >= 0.2 then
|
||||
self.TextRefreshTimer = 0
|
||||
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 LuaDemoActor.OnDemoPing(self, sourceName)
|
||||
self.PingCount = (self.PingCount or 0) + 1
|
||||
self.ColorIndex = ((self.ColorIndex or 0) + 1) % #DEMO_COLORS
|
||||
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",
|
||||
self.PingCount,
|
||||
"[UnLua Demo] Ping #%d from %s -> Cube color: %s (Speed=%.0f)",
|
||||
self.pingCount,
|
||||
tostring(sourceName),
|
||||
colorEntry.Name
|
||||
colorEntry.Name,
|
||||
self:GetSpinSpeed()
|
||||
)
|
||||
Log.Info(msg)
|
||||
UE.UKismetSystemLibrary.PrintString(
|
||||
self,
|
||||
self.actor,
|
||||
msg,
|
||||
true,
|
||||
true,
|
||||
@@ -114,17 +160,22 @@ function LuaDemoActor.OnDemoPing(self, sourceName)
|
||||
3
|
||||
)
|
||||
end
|
||||
function LuaDemoActor.OnAfterHotReload(self, moduleName)
|
||||
self.ReloadCount = (self.ReloadCount or 0) + 1
|
||||
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 completed (target=%s)",
|
||||
self.ReloadCount,
|
||||
tostring(moduleName)
|
||||
"[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,
|
||||
self.actor,
|
||||
msg,
|
||||
true,
|
||||
true,
|
||||
@@ -132,25 +183,148 @@ function LuaDemoActor.OnAfterHotReload(self, moduleName)
|
||||
4
|
||||
)
|
||||
end
|
||||
function LuaDemoActor.ApplyCurrentColor(self)
|
||||
local colorEntry = DEMO_COLORS[(self.ColorIndex or 0) + 1] or DEMO_COLORS[1]
|
||||
self:SetCubeColor(MakeLinearColor(colorEntry))
|
||||
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 LuaDemoActor.RefreshStatusText(self)
|
||||
local colorEntry = DEMO_COLORS[(self.ColorIndex or 0) + 1] or DEMO_COLORS[1]
|
||||
function DemoActorController.prototype.RefreshStatusText(self)
|
||||
local colorEntry = self:GetCurrentColor()
|
||||
local text = string.format(
|
||||
"UnLua Active (%s)\nTime: %.1fs | Pings: %d | Reloads: %d | Color: %s\n[Space] Ping Lua | [R] HotReload",
|
||||
"Pure-TS Actor (%s | %s)\nSpeed: %.0f deg/s | Color: %s\nTime: %.1fs | Pings: %d | Reloads: %d\n[Space] Ping | [R] HotReload",
|
||||
_VERSION,
|
||||
self.ElapsedTime or 0,
|
||||
self.PingCount or 0,
|
||||
self.ReloadCount or 0,
|
||||
colorEntry.Name
|
||||
____exports.DEMO_VERSION,
|
||||
self:GetSpinSpeed(),
|
||||
colorEntry.Name,
|
||||
self.elapsedTime,
|
||||
self.pingCount,
|
||||
self.reloadCount
|
||||
)
|
||||
self:SetStatusText(text)
|
||||
self.actor:SetStatusText(text)
|
||||
end
|
||||
function LuaDemoActor.ReceiveEndPlay(self, EndPlayReason)
|
||||
function DemoActorController.prototype.OnEndPlay(self, _endPlayReason)
|
||||
EventBus.OffAllForOwner(self)
|
||||
Log.Info("LuaDemoActor:ReceiveEndPlay(ticks=%d, pings=%d)", self.TickCount or 0, self.PingCount or 0)
|
||||
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
|
||||
return LuaDemoActor
|
||||
Reference in new issue
Block a user