Factory Manager - ssk.factoryMgr.*

The factory manager is a module designed to help you manage and access object builders: Factories

The manager uses a uniform interface and the expectation that every factory will return a single object reference.

Manager Functions

The manager has thes functions:

Factory Instance Functions

All factories should support the new() function: + new( [ group [, x [, y [, params ]]]] )

Typical Factory Structure

The file containing a factory definition usually has the following structure:

Factories Are Extensible

The above factory definition is pretty basic, but due to the fact that factories are modules, it is easy to extend this concept.

For example, if you include a built-in-editor in your game, it may be useful to add these functions to your factories:

Example Factory - smiley.lua


-- =============================================================
-- Copyright Roaming Gamer, LLC. 2008-2016 (All Rights Reserved)
-- =============================================================
-- Smiley Factory - Just a simple example...
-- =============================================================
local physics   = require "physics"

-- =============================================================
-- Localizations
-- =============================================================
-- Commonly used Lua Functions
local getTimer          = system.getTimer
local mRand                 = math.random
local mAbs                  = math.abs
--
-- Common SSK Display Object Builders
local newCircle = ssk.display.newCircle;local newRect = ssk.display.newRect
local newImageRect = ssk.display.newImageRect;local newSprite = ssk.display.newSprite
local quickLayers = ssk.display.quickLayers
--
-- Common SSK Helper Modules
local easyIFC = ssk.easyIFC;local persist = ssk.persist
--
-- Common SSK Helper Functions
local isValid = display.isValid;local isInBounds = ssk.easyIFC.isInBounds
local normRot = ssk.misc.normRot;local easyAlert = ssk.misc.easyAlert

-- =============================================================
-- Locals
-- =============================================================
local initialized = false

-- =============================================================
-- Forward Declarations
-- =============================================================

-- =============================================================
-- Factory Module Begins
-- =============================================================

local factory = {}

-- ==
--    init() - One-time initialization only.
-- ==
function factory.init( params )
    if(initialized) then return end
    initialized = true
end

-- ==
--    reset() - Reset any per-game logic/settings.
-- ==
function factory.reset( params )
end

-- ==
--    new() - Create new instance(s) of this factory's object(s).
-- ==
function factory.new( group, x, y, params )
    params = params or { size = 60 }

    local smiley = newImageRect( group, x, y, "images/smiley.png", 
                                {   size = params.size  } )

    return smiley
end

return factory

Learn By Example

The best way to learn how factories work is by example. Please look in the validation code that comes with SSK2 and pay particular attention to example Factory Manager found here:

~/validation/tests/factories/*.lua.

Validation Sample: 001_factoryTest.lua

-- =============================================================
-- Copyright Roaming Gamer, LLC. 2008-2016 (All Rights Reserved)
-- =============================================================

-- =============================================================
-- Localizations
-- =============================================================
-- Lua
local getTimer = system.getTimer; local mRand = math.random
local mAbs = math.abs
local strMatch = string.match; local strGSub = string.gsub; local strSub = string.sub
--
-- Common SSK Display Object Builders
local newCircle = ssk.display.newCircle;local newRect = ssk.display.newRect
local newImageRect = ssk.display.newImageRect;local newSprite = ssk.display.newSprite
local quickLayers = ssk.display.quickLayers
--
-- Common SSK Helper Modules
local easyIFC = ssk.easyIFC;local persist = ssk.persist
--
-- Common SSK Helper Functions
local isValid = display.isValid;local isInBounds = ssk.easyIFC.isInBounds
local normRot = math.normRot;local easyAlert = ssk.misc.easyAlert
--
-- SSK 2D Math Library
local addVec = ssk.math2d.add;local subVec = ssk.math2d.sub;local diffVec = ssk.math2d.diff
local lenVec = ssk.math2d.length;local len2Vec = ssk.math2d.length2;
local normVec = ssk.math2d.normalize;local vector2Angle = ssk.math2d.vector2Angle
local angle2Vector = ssk.math2d.angle2Vector;local scaleVec = ssk.math2d.scale
ssk.misc.countLocals(1)

-- =============================================================
-- =============================================================
local test = {}

local factoryMgr = ssk.factoryMgr

factoryMgr.register( "smiley", "tests.factories.example1" )
factoryMgr.register( "corona", "tests.factories.example2" )
factoryMgr.register( "rg", "tests.factories.example3" )

factoryMgr.init()

function test.run( group, params )
   group = group or display.currentStage
   params = params or {}

   local tmp = factoryMgr.new( "rg", group, centerX, centerY, { size = 80 } )

   local tmp = factoryMgr.new( "corona", group, centerX - 100, centerY, { size = 50 } )

   local tmp = factoryMgr.new( "smiley", group, centerX + 100, centerY )


end


return test

RoamingGamer Copyright © Roaming Gamer, LLC. 2008-2016; All Rights Reserved