--[[ 实现基于事件系统,注意以下特点: * 不保证更新方法的执行顺序,与注册无关,运行过程中函数的执行顺序也可能变化 * 支持在更新方法执行过程中进行注册和删除 --]] local LuaUpdater = {} local EventSystem = require('Event/EventSystem') local UpdateES = EventSystem.CreateEventSystem() local EventUpdate = "EventUpdate" local EventLateUpdate = "EventLateUpdate" local EventFixedUpdate = "EventFixedUpdate" local EventRealTimeUpdate = "EventRealTimeUpdate" local EventRealTimeLateUpdate = "EventRealTimeLateUpdate" local TypeUpdate = 1 local TypeLateUpdate = 2 local TypeFixedUpdate = 3 local function OnUpdate(deltaTime, timeSinceUpdate) UpdateES.Dispatch(EventUpdate, deltaTime, timeSinceUpdate) end local function OnLateUpdate(deltaTime, timeSinceUpdate) UpdateES.Dispatch(EventLateUpdate, deltaTime, timeSinceUpdate) end local function OnFixedUpdate(fixedDeltaTime, fixedTimeSinceUpdate) UpdateES.Dispatch(EventFixedUpdate, fixedDeltaTime, fixedTimeSinceUpdate) end local function OnRealTimeUpdate(deltaTime, timeSinceUpdate) UpdateES.Dispatch(EventRealTimeUpdate, deltaTime, timeSinceUpdate) end local function OnRealTimeLateUpdate(deltaTime, timeSinceUpdate) UpdateES.Dispatch(EventRealTimeLateUpdate, deltaTime, timeSinceUpdate) end function LuaUpdater.Startup(func) if USE_LUA_STANDALONE then LuaUpdater.CheckBattle_Update = function(deltaTime, timeSinceUpdate) UpdateES.Dispatch(EventFixedUpdate, deltaTime, timeSinceUpdate) end else CSExt.RegistUpdateFunc(false, TypeUpdate, OnUpdate) CSExt.RegistUpdateFunc(false, TypeLateUpdate, OnLateUpdate) CSExt.RegistUpdateFunc(false, TypeFixedUpdate, OnFixedUpdate) CSExt.RegistUpdateFunc(true, TypeUpdate, OnRealTimeUpdate) -- resgist late and fixed when need -- CSExt.RegistUpdateFunc(true, TypeLateUpdate, OnRealTimeLateUpdate) end end function LuaUpdater.Shutdown(func) CSExt.RegistUpdateFunc(false, TypeUpdate, nil) CSExt.RegistUpdateFunc(false, TypeLateUpdate, nil) CSExt.RegistUpdateFunc(false, TypeFixedUpdate, nil) CSExt.RegistUpdateFunc(true, TypeUpdate, nil) CSExt.RegistUpdateFunc(true, TypeLateUpdate, nil) end --基于游戏时间 function LuaUpdater.StartUpdate(func) UpdateES.Regist(EventUpdate, func) end function LuaUpdater.StopUpdate(func) UpdateES.UnRegist(EventUpdate, func) end function LuaUpdater.StartLateUpdate(func) UpdateES.Regist(EventLateUpdate, func) end function LuaUpdater.StopLateUpdate(func) UpdateES.UnRegist(EventLateUpdate, func) end function LuaUpdater.StartFixedUpdate(func) UpdateES.Regist(EventFixedUpdate, func) end function LuaUpdater.StopFixedUpdate(func) UpdateES.UnRegist(EventFixedUpdate, func) end --基于真实时间 function LuaUpdater.StartRealTimeUpdate(func) UpdateES.Regist(EventRealTimeUpdate, func) end function LuaUpdater.StopRealTimeUpdate(func) UpdateES.UnRegist(EventRealTimeUpdate, func) end function LuaUpdater.StartRealTimeLateUpdate(func) UpdateES.Regist(EventRealTimeLateUpdate, func) end function LuaUpdater.StopRealTimeLateUpdate(func) UpdateES.UnRegist(EventRealTimeLateUpdate, func) end return LuaUpdater