Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created May 4, 2019 01:49
Show Gist options
  • Save TinkerWorX/49ebae662c95b6a9cfb1879c5bb4a425 to your computer and use it in GitHub Desktop.
Save TinkerWorX/49ebae662c95b6a9cfb1879c5bb4a425 to your computer and use it in GitHub Desktop.
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(owner) == "player", "bad argument #1 to 'unit.create' (player expected)")
assert(type(id) == "number", "bad argument #2 to 'unit.create' (number expected)")
assert(type(x) == "number", "bad argument #3 to 'unit.create' (number expected)")
assert(type(y) == "number", "bad argument #4 to 'unit.create' (number expected)")
assert(type(facing) == "number", "bad argument #5 to 'unit.create' (number expected)")
return unit.wrap(CreateUnit(owner, id, x, y, facing))
end
function unit.wrap(handle)
assert(handle == nil or type(handle) == "unit", "bad argument #1 to 'unit.wrap' (nil/unit expected)")
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment