Skip to content

Latest commit

 

History

History
183 lines (140 loc) · 5.39 KB

File metadata and controls

183 lines (140 loc) · 5.39 KB

FollowCursor 函数重复定义修复报告

问题描述

在实现FollowCursor功能时,错误地在code.lua末尾重新定义了三个已存在的全局回调函数,导致原有实现被覆盖:

函数 原始位置 重复定义位置 后果
onUpdate() 第1559行 第2638行 战力计算、回合逻辑全部失效
onObjectEnterZone() 第2073行 第2662行 gameInfo.deckA/deckB 永远不会被赋值
onObjectDrop() 第2148行 第2688行 卡牌打出逻辑失效

根本原因: Lua中同名函数后者覆盖前者,TTS的全局回调只能有一个定义。


修复方案

将FollowCursor的逻辑合并进原有函数中,而不是重新定义。

1. 修复 onUpdate()

位置: 第1559行

修复内容: 在函数开头添加FollowCursor追踪逻辑

function onUpdate()
  -- 🔵 新增:FollowCursor 追踪逻辑
  for playerColor, fcData in pairs(followCursorData) do
    if fcData.isTracking and fcData.cardObj then
      if fcData.cardObj.isDestroyed() then
        print("[FollowCursor] 卡牌已被销毁,停止追踪: " .. playerColor)
        stopFollowCursor(playerColor)
      else
        local player = Player[playerColor]
        if player then
          local pos = player.getPointerPosition()
          pos.y = pos.y + 2
          fcData.cardObj.setPosition(pos)
        end
      end
    end
  end

  -- 以下保持原有逻辑不变 
  purplePower = 0
  yellowPower = 0
  -- ... (原来的所有代码)
end

删除: 末尾第2638行的重复定义


2. 修复 onObjectEnterZone()

位置: 第2073行

修复内容: 在函数末尾添加FollowCursor出牌区检测

function onObjectEnterZone(zoneEntered, object)
  -- 原有逻辑(保持不变)
  if gameInfo.board and object.hasTag("GwentCard") then
    -- ... 原代码 ...
  end
  if gameInfo.board and object.type == "Deck" then
    -- ... 原代码(赋值 deckA/deckB)...
  end
  
  -- 🔵 新增:FollowCursor 出牌区检测
  if not zoneEntered or not object then return end
  for _, tagName in ipairs(zoneEntered.getTags()) do
    if table.contains(PLAY_ZONES, tagName) then
      for playerColor, fcData in pairs(followCursorData) do
        if fcData.isTracking and fcData.cardObj and object.guid == fcData.cardObj.guid then
          print("[FollowCursor] " .. playerColor .. " 打出了卡牌到 " .. tagName .. "")
          stopFollowCursor(playerColor)
          return
        end
      end
    end
  end
end

删除: 末尾第2662行的重复定义


3. 修复 onObjectDrop()

位置: 第2148行

修复内容: 在函数末尾添加FollowCursor放下后重新拾起逻辑

function onObjectDrop(colorName, object)
  -- 原有逻辑(保持不变)
  if gameInfo.board and object.hasTag("GwentCard") and object.type == "Card" then
    -- ... 原代码 ...
  end
  -- ... 原代码 ...
  
  -- 🔵 新增:FollowCursor 放下后重新拾起
  if not object then return end
  local fcData = followCursorData[colorName]
  if fcData and fcData.isTracking and fcData.cardObj and object.guid == fcData.cardObj.guid then
    print("[FollowCursor] " .. colorName .. " 放下了卡牌,重新拾起!")
    Wait.frames(function()
      if followCursorData[colorName] and followCursorData[colorName].cardObj
         and not followCursorData[colorName].cardObj.isDestroyed() then
        local pos = Player[colorName].getPointerPosition()
        pos.y = pos.y + 2
        followCursorData[colorName].cardObj.setPositionSmooth(pos, false, false)
        followCursorData[colorName].isTracking = true
      end
    end, 2)
  end
end

删除: 末尾第2688行的重复定义


验证结果

修复前

$ grep "^function onUpdate()" code.lua
function onUpdate()  # 第1559行
function onUpdate()  # 第2638行 ❌ 重复定义

$ grep "^function onObjectEnterZone(" code.lua
function onObjectEnterZone(zoneEntered, object)  # 第2073行
function onObjectEnterZone(zone, object)          # 第2662行 ❌ 重复定义

$ grep "^function onObjectDrop(" code.lua
function onObjectDrop(colorName, object)  # 第2148行
function onObjectDrop(player_color, object)  # 第2688行 ❌ 重复定义

修复后

$ grep "^function onUpdate()" code.lua
function onUpdate()  # 第1559行 ✅ 唯一

$ grep "^function onObjectEnterZone(" code.lua
function onObjectEnterZone(zoneEntered, object)  # 第2073行 ✅ 唯一

$ grep "^function onObjectDrop(" code.lua
function onObjectDrop(colorName, object)  # 第2148行 ✅ 唯一

关键教训

Lua 全局回调函数(如 onUpdateonObjectEnterZoneonObjectDrop)只能定义一次。

新功能必须合并进已有函数,而非重新声明。否则会导致:

  1. 原有逻辑完全失效
  2. 关键变量(如 gameInfo.deckA/deckB)无法赋值
  3. 游戏核心功能(战力计算、回合结算、卡牌打出)崩溃

相关文件

  • code.lua - 主要修复文件
    • 第1559-1703行:onUpdate() - 已合并FollowCursor逻辑
    • 第2073-2102行:onObjectEnterZone() - 已合并FollowCursor逻辑
    • 第2148-2246行:onObjectDrop() - 已合并FollowCursor逻辑
    • 第2628-2675行:FollowCursor辅助函数(无重复定义)

修复日期: 2026年5月18日 修复人员: Lingma AI Assistant 审核状态: ✅ 已完成