使用lua腳本開發wow插件基礎篇

在入門篇中,我們講到了如何在wow中編寫一個簡單的程序 ,在對話框中輸出一下helloworld 什麼的。

但是,這還是遠遠不夠的。所以接下來將會帶着大家純手工打造一個簡單的戰鬥傷害統計插件,代碼大部分已經完善,倉庫地址在git上https://github.com/RAOE/Tracker_X

 

首先根據用戶的要求

1.能夠在每次戰鬥中顯示當前戰鬥中 承受的傷害 當前戰鬥中造成的傷害

2.能夠將當前戰鬥的統計數據發送到對話框中,自己可以看到

3.界面要求 能實時展示

4 . ....更多

 

在wow插件編寫規範中指定,xml爲樣式界面文件,lua爲邏輯處理。在編寫lua語言之前,可以先自己綁定一個宏命令在按鈕中

宏的語法爲 /console reloadui (爲了方便插件的調試用的) 該語法表示重載插件界面。

 

xml 這樣的標記類的語言,相信大家寫程序的都會使用到,在這裏就不做過多的闡述,在wow的插件中xml主要用來寫出各種各樣的界面使用。那麼如何讓界面層與lua代碼有相關的交互呢,wow官方文檔給出的是event事件,通過註冊事件,以及事件的響應來完成對界面與後端代碼lua的交互。

<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Button name="CombatTrackerFrame" movable="true" enableMouse="true" parent="UIParent" frameStrata="LOW">
    <Size x="400" y="40"/>
    <Anchors>
      <Anchor point="TOP" realtivePoint="BOTTOM" realtiveTo="Minimap">
        <Offset x="0" y="-5"/>
      </Anchor>
    </Anchors>  
    <Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" title="true">
      <BackgroundInsets>
         <AbsInset left="11" right="12" top="12" bottom="11"/>
      </BackgroundInsets>
      <TitleSize>
          <AbsValue val="32"/>
      </TitleSize>
      <EdgeSize>
        <AbsValue val="32"/>
      </EdgeSize>
    </Backdrop>
    <Layers>
      <Layer level="OVERLAY">
        <FontString name="$parentText" inherits="GameFontNormalSmall" justifyH="CENTER" setAllPoints="true" 
           text="[傷害統計插件]by九幽"/>
      </Layer>
    </Layers>
  </Button>
</Ui>

在上面的xml案例中,button表示創建的按鈕 size x y 分別表示了按鈕的長度和寬度。Anchors錨點,表示相對於小地圖來說 座標-5並水平劇種  titleSize 以及EdigeSize 表示定義標題的尺寸。好了我們的界面大致就完成了。雖然有點簡陋,但後續可以對其進行優化。

在button 後面需要註冊相應的event事件,這樣方便lua代碼來捕獲並處理相關的邏輯將一下script腳本放在 </Button>後面

 <Scripts>
        <OnEvent>
            CombatTracker_OnEvent(self,event,...);
        </OnEvent>
        <OnClick>
            CombatTracker_ReportDPS();
        </OnClick>
        <OnDragStart>
            self:StartMoviwng();
        </OnDragStart>
         <OnDragStop>
            self:StopMovingOrSizing();
        </OnDragStop>
        <OnLoad>
            ChatFrame1:AddMessage("戰鬥插件加載");
            self:RegisterEvent("UNIT_COMBAT");
            self:RegisterEvent("PLAYER_REGEN_ENABLED");
            self:RegisterEvent("PLAYER_REGEN_DISABLED");
            self:RegisterForClicks("RightButtonUp");
            self:RegisterForDrag("LeftButton");    
            ChatFrame1:AddMessage("戰鬥插件加載完成...");
        </OnLoad>
     </Scripts>

在Onload事件中表示當插件被系統裝載之後,會註冊UNIT_COMBAT PLAYER_REGEN_ENABLE PLAYER_REGERN_DISABLED  REIGHTBUTTONUP LEFTBUTTON 分別表示了 戰鬥事件 開始戰鬥,離開戰鬥鼠標右擊 ,鼠標左擊

接下來就是編寫 lua  代碼來對這些事件進行處理了。

local start_time = 0
local end_time = 0
local total_time =0
local total_damage=0
local averange_dps= 0 
local player_damage= 0
local player_averange_dps=0
local player_health=0
ChatFrame1:AddMessage("歡迎使用TrackerX!!")
player_health = UnitHealth("player")
function CombatTracker_OnEvent(frame, event, ...)
    printMsg(UNIT_MAXPOWER("player"))
    if event == "PLAYER_REGEN_ENABLED" then 
        ChatFrame1:AddMessage("離開戰鬥")
        CombatTrackerFrameText:SetText("[傷害統計插件]by九幽")
        CombatTracker_ReportDPS()
        end_time = GetTime()
        total_time = end_time - start_time
        averange_dps =total_damage /total_timeatttwt
        player_averange_dps = player_damage/total_time
        CombatTracker_UpdateText()
    elseif event == "PLAYER_REGEN_DISABLED" then 
        CombatTrackerFrameText:SetText("進入戰鬥狀態")
        total_damage = 0 
        start_time=GetTime()
    elseif event == "UNIT_COMBAT" then 
        local unit,action,modifier,damage,damagetype= ...
        if unit == "player" and action ~= "HEAL" then
          total_damage=total_damage+damage
          end_time = GetTime()
          total_time = end_time - start_time
          averange_dps =total_damage/total_time
          printMsg(UnitHealth("player"))
          printMsg(UnitName("player"))
          printMsg(UnitSex("player"))
          if UnitHealth("player") < player_health * 0.5 then 
            ChatFrame1:AddMessage("當前生命值過低,請使用恢復藥劑  請使用強效治療藥劑")
          end 
        end 
        if unit =="target" and action ~= "HEAL" then 
          player_damage = player_damage+damage
        end
        CombatTracker_UpdateText()
    end  
end
function CombatTracker_UpdateText()
   local status = string.format( "%d總量/ %d總量2/ %d秒/ %.2f平均傷害值1 /%.2f平均傷害值2",player_damage,total_damage,total_time,averange_dps,player_averange_dps)
   CombatTrackerFrameText:SetText(status)
end 
function CombatTracker_ReportDPS()
    local msgformat = "戰鬥時長%d 秒,受到了%d點傷害 .平均每秒受到的傷害:%.2f,玩家造成了 %d 點傷害"
    local msg =string.format(msgformat,total_time,total_damage,averange_dps,player_damage)
    ChatFrame1:AddMessage(tostring(msg))
    if GetNumPartyMembers()>0 then 
      SendChatMessage(msg,"PARTY")
    else 
      ChatFrame1:AddMessage(msg)
    end 
end 
function printMsg(msg)
  ChatFrame1:AddMessage(tostring(msg))
end 

在上面的事件中,我們定義了一些功能,if event == “PLAYER_REGEN_ENABLED” 表示玩家離開了戰鬥,相應的我們就通過wow內置的接口去更新我們的界面 ,並計算出當前戰鬥的戰鬥信息並輸出。其他的event也是這樣處理的。我也定義了ReportDPS 函數,用來當鼠標右擊的時候,可以去發送戰鬥傷害數據,printMsg函數主要是用於打印msg信息。

在編寫完畢後,我們只需要定義toc文件即可。

toc文件非常簡單。只需要簡單描述這個插件的基本信息就可以

## Interface: 20300
## Title: CombatTracker
## Title-zhCN: CombatTracker
## Title-zhTW: CombatTracker
## Description: CombatTracker aaaa
CombatTracker.xml
CombatTracker.lua

最後效果如圖:

 

最後,由於自己也是剛接觸wow插件開發,有筆誤之處,歡迎指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章