Snippets

develephant Using a Factory to create unique object instances

Created by develephant last modified
local math = require('math')
local os = require('os')

math.randomseed(os.time())
local mRan = math.random
--==============================================================--
--== Instance Factory Module
--==============================================================--
local CardFactory = {}
--==============================================================--
--== Create Card Instance
--== This is called from outside the Module
--== You can pass in a pre-populated table
--== See main.lua
--==============================================================--
function CardFactory.new( meta_tbl )
  --==============================================================--
  --== The Object table (card)
  --==============================================================--
  local c = meta_tbl or {}
  --==============================================================--
  --== Here you can add card defaults, flags, etc.
  --==============================================================--
  --== Status
  c.discarded = false
  c.in_play = true
  --== Mana
  c.mana = c.starting_mana
  c.mana_max = 500
  --== ...
  --==============================================================--
  --== Example of a default method in the instance
  --== This method is available to all card instances
  --== Though they operate autonomously and will not affect
  --== other instances. It's basically a "blank" card
  --== with benefits.
  --==============================================================--
  function c:cast( spell_id )

    -- Do something magical here

    -- Consume some mana
    local cost = math.random(10,40)
    self.mana = self.mana - cost

    -- Debug
    print( "Cast", spell_id )
    print( "Cost", cost )
    print( "Mana", math.max( 0, self.mana ) )
  end
  --==============================================================--
  --== Add Mana
  --==============================================================--
  function c:addMana( amount )
    self.mana = math.min( self.mana_max, ( self.mana + amount ) )
    return self.mana
  end
  --==============================================================--
  --== Check Mana
  --==============================================================--
  function c:getMana()
    return math.max( 0, self.mana )
  end
  --==============================================================--
  --== Chck play state
  --==============================================================--
  function c:isInPlay()
    return self.in_play
  end
  --==============================================================--
  --== Return the instance
  --==============================================================--
  return c
end
-- Return the Module
return CardFactory
--==============================================================--
--== Card Factory Preflight
--==============================================================--
local json 		= require('json')
local CardFactory = require('factory')
--==============================================================--
--== Deck - key/value table map
--==============================================================--
local deck = {}
--==============================================================--
--== Create a deck of 52 unique instances and store them
--== We use the actual card table itself as a table key in the
--== deck. This can come in handy in many places and uses.
--==============================================================--
local card
for i=1, 52 do -- Make a 52 card deck
	--== Creates new unique card instance
	--== with some initial meta data. Like index pos.
	card = CardFactory.new( { idx = i, starting_mana = 1000 } )
	--== Place the `card` in the `deck` map
	--== which creates a Set of cards (no dupes). By using
	--== an number index as the value, we can look
	--== it up later, and even flip the whole table.
	deck[card] = i
end

--==============================================================--
--== An "echo-back" and the `cast` method triggered as a test
--== on all cards in the deck.
--== This would have to be managed differently of course in the
--== "real" world situation.
--==============================================================--
for card, idx in pairs( deck ) do
	print( tostring(card) ) -- The instance/table identifier
	--== Card is using its `cast` method.
	--== See factory.lua
	card:cast( 'SPELL_'..tostring( card ) )
	print( card:getMana() )
end

--==============================================================--
--== Get by index
--==============================================================--
function getByIndex( idx )
	for k, v in pairs( deck ) do
		if k.idx == idx then
			return v
		end
	end
	return nil
end

--==============================================================--
--== Clearing a card from the deck
--== This demonstrates the simplicity of keeping track of your table
--== objects, by using a self-reference. That's Lua. ;)
--==============================================================--
function clearCard( card ) -- The card table
	-- Clean up if needed
	card:clear() --fictitious method

	deck[ card ] = nil --bye
end

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.