Created
February 15, 2013 03:47
-
-
Save omidahourai/4958451 to your computer and use it in GitHub Desktop.
These snippets are described in detail as part of the Object-Oriented Lua Tutorial available at: http://www.ardentkid.com/blog/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk
This file contains 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
--Classes/Objects/Animal.lua | |
local scene = scene | |
local Super = require('Classes.Object') | |
local Animal = Class(Super) --inherits Object Class | |
--INSTANCE FUNCTIONS | |
function Animal:initialize(img) | |
--calls parent Class's initialize() | |
--with "self" as this instance | |
Super.initialize(self, img) | |
self.health = 100 | |
self.lives = 1 | |
end | |
function Animal:show(config) | |
--calls parent Class's show() | |
--with "self" as this instance | |
Super.show(self, config) | |
end | |
function Animal:hide() | |
--calls parent Class's hide() | |
--with "self" as this instance | |
Super.hide(self) | |
end | |
function Animal:move() | |
--code to make animal move goes here. | |
end | |
function Animal:jump() | |
--code to make animal jump goes here. | |
end | |
return Animal |
This file contains 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
--Cat.lua | |
local scene = scene | |
local Cat = {Instances={}}; scene.Cat = Cat | |
--STATIC CLASS FUNCTIONS (self refers to the Class) | |
function Cat:new() | |
local instance = setmetatable({}, self) | |
instance:initialize(...) | |
return instance | |
end | |
function Cat:get() | |
local Instances = self.Instances | |
if (not Instances[1]) then self:new() end | |
return table.remove(Instances, 1) | |
end | |
--INSTANCE FUNCTIONS (self refers to an instance) | |
function Cat:dispose() | |
table.insert(self.Instances, self) | |
end | |
function Cat:initialize() | |
local image = display.newImage('cat.png') | |
image.isVisible = false | |
self.image = image | |
table.insert(self.Instances, self) | |
end | |
function Cat:show(config) | |
--show this cat somewhere on-screen | |
local image = self.image | |
image.x, image.y = config.x, config.y | |
image.isVisible = true | |
--start listeninig for these mouse events | |
scene:addEventListener('onMouseShow', self) | |
scene:addEventListener('onMouseHide', self) | |
end | |
function Cat:hide() | |
--hide this cat | |
self.image.isVisible = false | |
--stop this cat from responding to mice. | |
scene:removeEventListener('onMouseShow', self) | |
scene:removeEventListener('onMouseHide', self) | |
--return instance to Instances array | |
self:dispose() | |
end | |
--CAT INSTANCE CUSTOM EVENT LISTENERS | |
function Cat:onMouseShow(event) | |
--code to start chasing the mouse goes here. | |
end | |
function Cat:onMouseHide(event) | |
--code to stop chasing the mouse goes here. | |
end | |
--CREATE 3 CATS | |
for i=1,3 do Cat:new() end | |
return Cat |
This file contains 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
--Classes/Objects/Animals/Cat.lua | |
local scene = scene | |
local Super = require('Classes.Objects.Animal') | |
local Cat = Class(Super) --inherits Animal Class | |
Cat.Instances = {} | |
--INSTANCE FUNCTIONS | |
function Cat:initialize(img) | |
--calls parent Class's initialize() | |
--with "self" as this instance | |
Super.initialize(self, img) | |
self.lives = 9 --overwrite lives for cat | |
--add cat to Instances table | |
table.insert(self.Instances, self) | |
end | |
function Cat:show(config) | |
--calls parent Class's show() | |
--with "self" as this instance | |
Super.show(self, config) | |
--start listeninig for these mouse events | |
scene:addEventListener('onMouseShow', self) | |
scene:addEventListener('onMouseHide', self) | |
end | |
function Cat:hide() | |
--calls parent Class's hide() | |
--with "self" as this instance | |
Super.hide(self) | |
--stop this cat from responding to mice. | |
scene:removeEventListener('onMouseShow', self) | |
scene:removeEventListener('onMouseHide', self) | |
--return instance to Instances table | |
self:dispose() | |
end | |
function Cat:pounce() | |
--code to make cat pounce goes here. | |
end | |
--INSTANCE CUSTOM EVENT LISTENERS | |
function Cat:onMouseShow(event) | |
--code to start chasing the mouse goes here. | |
end | |
function Cat:onMouseHide(event) | |
--code to stop chasing the mouse goes here. | |
end | |
--CREATE 3 CATS | |
for i=1,3 do Cat:new('cat.png') end | |
return Cat |
This file contains 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
--Class.lua | |
local Base -- base of the class hierarchy (forward reference) | |
function Class(Super) | |
Super = Super or Base | |
local prototype = setmetatable({}, Super) | |
prototype.class = prototype | |
prototype.super = Super | |
prototype.__index = prototype | |
return prototype | |
end | |
Base = Class() | |
function Base:new(...) | |
local instance = setmetatable({}, self) | |
instance:initialize(...) | |
return instance | |
end | |
function Base:initialize() end | |
function Base:get() | |
local Instances = self.Instances | |
if (not Instances[1]) then local obj = self:new() end | |
return table.remove(Instances, 1) | |
end | |
function Base:dispose() | |
table.insert(self.Instances, self) | |
end |
This file contains 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
--Enemy.lua | |
local scene = scene | |
local Enemy = {Instances={}} scene.Enemy = Enemy | |
--STATIC CLASS FUNCTIONS (self refers to the Enemy Class) | |
function Enemy:new(...) | |
--decorate instance with class attributes, then initialize | |
local instance = setmetatable({}, self) | |
instance:initialize(...) | |
return instance | |
end | |
function Enemy:get() --Gets an available enemy instance | |
local Instances = self.Instances | |
if (not Instances[1]) then self:new() end | |
return table.remove(Instances, 1) | |
end | |
--NON-STATIC/ INSTANCE FUNCTIONS (self refers to an enemy instance) | |
function Enemy:dispose() | |
--makes an enemy-in-use available again | |
table.insert(self.Instances, self) | |
end | |
function Enemy:initialize() | |
--create enemy display object | |
local image = display.newImage('img_enemy.png') | |
self.image = image | |
--add to Instances table | |
table.insert(self.Instances, self) | |
end | |
function Enemy:show(config) | |
--each enemy can show itself | |
local image = self.image | |
image.x, image.y = config.x, config.y | |
image.isVisible = true | |
end | |
function Enemy:hide() | |
--each enemy can hide/ return itself to the Instances array | |
self.image.isVisible = false | |
self:dispose() | |
end | |
function Enemy:attack() | |
--each enemy can attack | |
end | |
function Enemy:move() | |
--each enemy can move | |
end | |
--Pool 10 enemies at load-time | |
for i=1,10 do Enemy:new() end | |
return Enemy |
This file contains 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
--Hud.lua | |
local scene = scene | |
-- This can be a display group or simple table, {} | |
local Hud = display.newGroup() | |
-- Insert group into scene | |
scene:insert(Hud); scene.Hud = Hud | |
-- Define class properties | |
Hud.currentKillCount = 0 | |
Hud.totalKillCount = 0 | |
Hud.missedCount = 0 | |
Hud.padding = 20 | |
-- Require our Hud components | |
require('Classes.Hud.MultBox') | |
require('Classes.Hud.HitBar') | |
require('Classes.Hud.Score') | |
require('Classes.Hud.HealthHearts') | |
function Hud:show(fadeIn) | |
-- Code to show Hud | |
end | |
function Hud:hide(fadeOut) | |
-- Code to hide Hud | |
end | |
function Hud:clean() | |
-- Cleans on restart or scene exit | |
end | |
return Hud |
This file contains 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
--Mouse.lua | |
local scene = scene | |
local Mouse = {Instances={}}; scene.Mouse = Mouse | |
--STATIC CLASS FUNCTIONS (self refers to the Class) | |
function Mouse:new() | |
local instance = setmetatable({}, self) | |
instance:initialize(...) | |
return instance | |
end | |
function Mouse:get() | |
local Instances = self.Instances | |
if (not Instances[1]) then self:new() end | |
return table.remove(Instances, 1) | |
end | |
--INSTANCE FUNCTIONS (self refers to an instance) | |
function Mouse:dispose() | |
table.insert(self.Instances, self) | |
end | |
function Mouse:initialize() | |
local image = display.newImage('mouse.png') | |
image.isVisible = false | |
self.image = image | |
table.insert(self.Instances, self) | |
end | |
function Mouse:show(config) | |
--show this mouse somewhere on-screen | |
local image = self.image | |
image.x, image.y = config.x, config.y | |
image.isVisible = true | |
--tell the world this mouse has shown | |
scene:dispatchEvent({name='onMouseShow', | |
x=self.x, y=self.y}) | |
end | |
function Mouse:hide() | |
--hide this mouse | |
self.image.isVisible = false | |
--then tell the world this mouse has hidden. | |
scene:dispatchEvent({name='onMouseHide'}) | |
--return instance to Instances array | |
self:dispose() | |
end | |
--CREATE 1 MOUSE | |
Mouse:new() | |
return Mouse |
This file contains 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
--MultBox.lua | |
local scene = scene | |
local Hud = scene.Hud --this was tied to the scene at the top of Hud.lua | |
local MultBox = display.newRoundedRect(0,0,100,100,20) | |
Hud:insert(MultBox); Hud.MultBox = MultBox | |
--more code for positioning, attributes, functionality, etc |
This file contains 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
--Classes/Object.lua | |
local scene = scene | |
local Object = Class() --inherits Base Class | |
--INSTANCE FUNCTIONS | |
function Object:initialize(img) | |
--creates our instance's display image | |
local image = display.newImage(img) | |
self.image = image | |
end | |
function Object:show(config) | |
--show this object somewhere on-screen | |
local image = self.image | |
image.x, image.y = config.x, config.y | |
image.isVisible = true | |
end | |
function Object:hide() | |
--hide this object | |
local image = self.image | |
image.isVisible = false | |
end | |
return Object |
This file contains 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
--SceneGame.lua | |
local SceneGame = storyboard.newScene() | |
local dir = 'Classes.Game.' | |
function SceneGame:createScene(event) | |
--LOAD ALL SCENE ASSETS AND CODE | |
scene = self.view | |
local scene = scene | |
require(dir..'Constants') | |
require(dir..'Landscape') | |
require(dir..'Hud') | |
require(dir..'Player') | |
require(dir..'SpawnMachine') | |
require(dir..'PauseMenu') | |
require(dir..'Effects') | |
require(dir..'Collision') | |
end | |
function SceneGame:enterScene(event) | |
--REMOVE PREVIOUS SCENE AFTER TRANSITION | |
storyboard.removeScene('SceneTitle') | |
end | |
function SceneGame:exitsScene(event) | |
--REMOVE ALL CODE FROM package.loaded | |
require(dir..'Cleanup') | |
package.loaded[dir..'Cleanup'] = nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment