Skip to content

Instantly share code, notes, and snippets.

@uhziel
Created December 2, 2012 02:14
Show Gist options
  • Select an option

  • Save uhziel/4186601 to your computer and use it in GitHub Desktop.

Select an option

Save uhziel/4186601 to your computer and use it in GitHub Desktop.
采用 Lua 复刻 Warcraft3 的脚本系统
Trigger = {}
function Trigger:New()
local o = {}
setmetatable(0, self)
self.__index = self
return o
end
function Trigger:RegisterEvent(event, ...)
end
function Trigger:AddCondition(condition)
end
function Trigger:AddAction(action)
end
Timer = {}
function Timer:New()
end
function Timer:Start(time, func)
end
function Timer:Pause()
end
function Timer:Destroy()
end
-- 杀死进入触发区域中单位类型为'hfoo'的单位
local t = Trigger:New()
t:RegisterEvent('ENTER_RECT_SIMPLE', GetPlayableMapRect())
t:AddCondition(function () return GetTriggerUnit():GetUnitTypeID() == 'hfoo' end)
t:AddAction(function () KillUnit(GetTriggerUnit()) end)
-- ItemRemoval
-- 扔掉道具后一段时间会消失。如果中间捡起,就不消失。
local function ItemDropActions(item)
local timer = Timer:New()
timer.item = item
item.timer = timer
timer:Start(30, function (timer)
timer.item:Remove()
timer:Destroy()
end)
end
local t_drop_item = Trigger:New()
t_drop_item:RegisterEvent('PLAYER_UNIT_DROP_ITEM')
t_drop_item:AddAction(ItemDropActions)
local function ItemPickupActions(item)
item.timer:Pause()
item.timer:Destroy()
end
local t_pickup_item = Trigger:New()
t_pickup_item:RegisterEvent('PLAYER_UNIT_PICKUP_ITEM')
t_pickup_item:AddAction(ItemPickupActions)
-- AI
-- 玩家下线后,AI 接管。
-- 如果血量少于 20%,逃离到温泉旁边
-- 如果在 500 范围内找到攻击目标,攻击目标
-- 如果没有攻击目标,随机选择一个点进行巡逻
local function AILoop(timer)
-- hero is a unit
local hero = timer.hero
local e = 5
if hero:GetUnitState(UNIT_STATE_LIFE) <= 0 then
e = 1.5
else
local order = hero:GetUnitCurrentOrder()
if order == 'smart' or o == 'attack' or o == 'patrol' or
o == 'move' or o == 'hold' or o == nil then
local enemy = GetEnemy(hero, 500)
if enemy then
hero:IssueTargetOrder('attack', enemy)
else
local rect = GetRandomRect()
hero:IssuePointOrderLoc('patrol', rect)
end
end
end
timer:Start(e, false, AILoop)
end
local function StartAI(hero)
local timer = Timer:New()
timer.hero = hero
timer.Start(0, false, AILoop)
end
local function PlayerLeave(player)
local hero = player:GetHero()
StartAI(hero)
end
local t_player_leave = Trigger:New()
for _, v in ipairs(all_player) do
t_player_leave:RegisterEvent('PLAYER_LEAVE', v)
end
t_player_leave:AddAction(StartAI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment