jesterxl

Lua Class Closure example

Jan 5th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 0 0
  1. -- BaseObject.lua
  2.  
  3. BaseObject = {}
  4.  
  5. function BaseObject:new(x,y,width,height,world)
  6.  
  7.     local allYourBase = {}
  8.     allYourBase.x = x
  9.     allYourBase.y = y
  10.     allYourBase.width = width
  11.     allYourBase.height = height
  12.     -- etc
  13.  
  14.     return allYourBase
  15. end
  16.  
  17. return BaseObject
  18.  
  19. -- OrganicObject.lua
  20.  
  21. require "BaseObject"
  22.  
  23. OrganicObject = {}
  24.  
  25. function OrganicObject:new(x,y,width,height,world)
  26.     local organic = BaseObject:new(x,y,width,height,world)
  27.     organic.type = "organic"
  28.     organic.growth = 0
  29.  
  30.     function organic:init()
  31.         self.skinScale = (self.diagSize/self.diagStartSize)
  32.     end
  33.  
  34.     return organic
  35. end
  36.  
  37. return OrganicObject
  38.  
  39. -- Player.lua
  40.  
  41. require "OrganicObject"
  42.  
  43. Player = {}
  44.  
  45. function Player:new(x,y,width,height,world)
  46.     local player = OrganicObject:new(x,y,width,height)
  47.  
  48.     player.oldInit = player.init
  49.     function player:init()
  50.         -- additional overwrode logic
  51.         self:oldInit() -- call the super class' method
  52.     end
  53.  
  54.     player:init()
  55.  
  56.     return player
  57. end
  58.  
  59. return Player
Advertisement
Add Comment
Please, Sign In to add comment