Created
October 17, 2012 20:26
-
-
Save NimbusBP1729/3907934 to your computer and use it in GitHub Desktop.
weapon parent/child hierarchy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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