-- ============================================== -- 自定义回血技能组件(封装冷却/消耗/回血逻辑) -- ============================================== local HealSkill = Class(function(self, inst) self.inst = inst self.cooldown_time = 30 -- 冷却时间(秒),可自行修改 self.last_heal_time = 0 -- 上次回血时间 self.heal_amount = 50 -- 每次回血值,可自行修改 self.hunger_cost = 20 -- 每次消耗饥饿值,可自行修改 -- 序列化(联机存档同步用) self.inst:ListenForEvent("onsave", function(inst, data) data.last_heal_time = self.last_heal_time end) self.inst:ListenForEvent("onload", function(inst, data) if data and data.last_heal_time then self.last_heal_time = data.last_heal_time end end) end) -- 核心方法:执行回血 function HealSkill:TryHeal() local current_time = GetTime() local inst = self.inst -- 1. 检查是否在冷却中 if current_time - self.last_heal_time < self.cooldown_time then inst.components.talker:Say("技能还在冷却中!(剩余"..math.ceil(self.cooldown_time - (current_time - self.last_heal_time)).."秒)") return false end -- 2. 检查是否有足够饥饿值 if inst.components.hunger and inst.components.hunger.current < self.hunger_cost then inst.components.talker:Say("饥饿值不够,无法回血!") return false end -- 3. 检查生命值是否已满(无需回血) if inst.components.health and inst.components.health:GetPercent() >= 1 then inst.components.talker:Say("我的生命值已经满了!") return false end -- 4. 执行回血逻辑(仅服务器端修改属性,保证联机同步) if TheWorld.ismastersim then inst.components.hunger:DoDelta(-self.hunger_cost) -- 扣除饥饿 inst.components.health:DoDelta(self.heal_amount) -- 恢复生命 self.last_heal_time = current_time -- 更新冷却时间 inst.components.talker:Say("感觉好多了!") -- 角色提示 print("[X键回血MOD] "..inst.name.." 触发回血:+50生命,-20饥饿") -- 控制台日志 end return true end -- 注册自定义组件到游戏 AddComponent("healskill", HealSkill) -- ============================================== -- 绑定X键触发逻辑(区分客户端/服务器) -- ============================================== local function BindXKeyToHeal(inst) -- 只给玩家角色添加功能 if not inst:HasTag("player") then return end -- 挂载回血组件 if not inst.components.healskill then inst:AddComponent("healskill") end -- 客户端:检测X键按下(CONTROL_X对应键盘X键) inst:ListenForEvent("control", function(inst, control) if control == CONTROL_X then -- 向服务器发送请求(保证联机同步) if TheWorld.ismastersim then inst.components.healskill:TryHeal() else inst.replica.healskill:TryHeal() end end end) end -- 为客户端注册组件副本(联机同步用) local function RegisterHealSkillReplica() local function fn(self) self.inst = inst -- 客户端向服务器请求执行回血 function self:TryHeal() SendModRPCToServer(MOD_RPC.healskill.tryheal, self.inst) end end -- 注册副本组件 local HealSkillReplica = Class(fn) AddComponentReplica("healskill", HealSkillReplica) -- 注册RPC(远程过程调用,联机同步核心) MOD_RPC.healskill = MOD_RPC.healskill or {} MOD_RPC.healskill.tryheal = RegisterModRPC("healskill_tryheal", function(inst) if inst and inst.components.healskill then inst.components.healskill:TryHeal() end end) end -- ============================================== -- 游戏初始化时加载逻辑 -- ============================================== -- 1. 玩家初始化时绑定按键 AddPlayerPostInit(BindXKeyToHeal) -- 2. 游戏世界加载后注册副本组件 AddSimPostInit(RegisterHealSkillReplica) -- 控制台提示MOD加载完成 print("[X键回血MOD] 已加载完成!按下X键触发回血,冷却30秒,消耗20饥饿,恢复50生命。")