使用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插件开发,有笔误之处,欢迎指正。

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