Skip to content

Instantly share code, notes, and snippets.

@NimbusBP1729
Created October 17, 2012 20:26
Show Gist options
  • Save NimbusBP1729/3907934 to your computer and use it in GitHub Desktop.
Save NimbusBP1729/3907934 to your computer and use it in GitHub Desktop.
weapon parent/child hierarchy
--This is the only method in my weapon file that is referenced without a semicolon
-- Creates a new weapon object
-- @return the weapon object created
function Weapon.addWeaponMethods(myWeapon)
for k,v in pairs(Weapon) do
if not myWeapon[k] then
myWeapon[k] = v
end
end
return myWeapon
end
--an example function that will be common to all Weapons
-- Called when the weapon begins colliding with another node
-- @return nil
function Weapon:collide(node, dt, mtv_x, mtv_y)
if node.character then return end
if not node then return end
if node.die then
node:die(self.damage)
end
end
--This is a snippet of a Mallet file
function Mallet.new(node, collider, plyr, malletItem)
local mallet = {}
setmetatable(mallet, Mallet)
mallet = Weapon.addWeaponMethods(mallet)
--a few key are set..
return mallet
end
--The function essentially treats Mallet as a subclass of Weapon and only accesses the parent class's methods if it doesn't have one by the same name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment