Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created July 18, 2019 22:56
Show Gist options
  • Save TinkerWorX/500f7c0a6ac5b850950e8f1b4bd7d03d to your computer and use it in GitHub Desktop.
Save TinkerWorX/500f7c0a6ac5b850950e8f1b4bd7d03d to your computer and use it in GitHub Desktop.
local assert = require("assert")
local type = require("apitype")
local vector2 = require("vector2")
local unit = {
__units = { },
__properties = {
x = {
get = function(self) return GetUnitX(self.handle) end,
set = function(self, value) SetUnitX(self.handle, value) end
},
y = {
get = function(self) return GetUnitY(self.handle) end,
set = function(self, value) SetUnitY(self.handle, value) end
},
facing = {
get = function(self) return GetUnitFacing(self.handle) end,
set = function(self, value) SetUnitFacing(self.handle, value) end
},
position = {
get = function(self) return vector2.create(GetUnitX(self.handle), GetUnitY(self.handle)) end,
set = function(self, value) SetUnitX(self.handle, value.x) SetUnitY(self.handle, value.y) end
},
location = {
get = function(self) return vector2.create(GetUnitX(self.handle), GetUnitY(self.handle)) end,
set = function(self, value)
local l = Location(value.x, value.y)
SetUnitPositionLoc(self.handle, l)
RemoveLocation(l)
end
},
name = {
get = function(self) return GetUnitName(self.handle) end,
set = function(self, value) BlzSetUnitName(self.handle, value) end
},
}
}
unit.__index = function(self, key)
if unit.__properties[key] then
return unit.__properties[key].get(self)
else
return unit[key] -- falback to reading the parent
end
end
unit.__newindex = function(self, key, value)
if unit.__properties[key] then
unit.__properties[key].set(self, value)
else
rawset(self, key, value) -- fallback to writing the instance
end
end
function unit.create(owner, id, x, y, facing)
assert.type(1, "player", type(owner), "owner", "unit.create")
assert.type(2, "number", type(id), "id", "unit.create")
assert.type(3, "number", type(x), "x", "unit.create")
assert.type(4, "number", type(y), "y", "unit.create")
assert.type(5, "number", type(facing), "facing", "unit.create")
return unit.wrap(CreateUnit(owner, id, x, y, facing))
end
function unit.wrap(handle)
assert.typeOrNil(1, "unit", type(handle), "handle", "unit.wrap")
if handle == nil then
return nil
end
if not unit.__units[handle] then
unit.__units[handle] = setmetatable({ __handle = handle }, unit)
end
return unit.__units[handle]
end
function unit:__tostring() return "(unit: " .. self.name .. ")" end
function unit:__concat(value)
if type(self) == "string" then
return self .. tostring(value)
end
return nil
end
function unit.getFilter() return unit.wrap(GetFilterUnit()) end
function unit.getEnum() return unit.wrap(GetEnumUnit()) end
function unit.getEntering() return unit.wrap(GetEnteringUnit()) end
function unit.getLeaving() return unit.wrap(GetLeavingUnit()) end
function unit:kill() KillUnit(self.__handle) end
function unit:remove() RemoveUnit(self.__handle) end
function unit:hide(doHide)
if doHide == nil then
ShowUnit(self.__handle, false)
else
ShowUnit(self.__handle, not doHide)
end
end
function unit:show(doShow)
if doShow == nil then
ShowUnit(self.__handle, true)
else
ShowUnit(self.__handle, doShow)
end
end
return unit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment