localmath=require('math')localos=require('os')math.randomseed(os.time())localmRan=math.random--==============================================================----== Instance Factory Module--==============================================================--localCardFactory={}--==============================================================----== Create Card Instance--== This is called from outside the Module--== You can pass in a pre-populated table--== See main.lua--==============================================================--functionCardFactory.new(meta_tbl)--==============================================================----== The Object table (card)--==============================================================--localc=meta_tblor{}--==============================================================----== Here you can add card defaults, flags, etc.--==============================================================----== Statusc.discarded=falsec.in_play=true--== Manac.mana=c.starting_manac.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.--==============================================================--functionc:cast(spell_id)-- Do something magical here-- Consume some manalocalcost=math.random(10,40)self.mana=self.mana-cost-- Debugprint("Cast",spell_id)print("Cost",cost)print("Mana",math.max(0,self.mana))end--==============================================================----== Add Mana--==============================================================--functionc:addMana(amount)self.mana=math.min(self.mana_max,(self.mana+amount))returnself.manaend--==============================================================----== Check Mana--==============================================================--functionc:getMana()returnmath.max(0,self.mana)end--==============================================================----== Chck play state--==============================================================--functionc:isInPlay()returnself.in_playend--==============================================================----== Return the instance--==============================================================--returncend-- Return the ModulereturnCardFactory
--==============================================================----== Card Factory Preflight--==============================================================--localjson=require('json')localCardFactory=require('factory')--==============================================================----== Deck - key/value table map--==============================================================--localdeck={}--==============================================================----== 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.--==============================================================--localcardfori=1,52do-- 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]=iend--==============================================================----== 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.--==============================================================--forcard,idxinpairs(deck)doprint(tostring(card))-- The instance/table identifier--== Card is using its `cast` method.--== See factory.luacard:cast('SPELL_'..tostring(card))print(card:getMana())end--==============================================================----== Get by index--==============================================================--functiongetByIndex(idx)fork,vinpairs(deck)doifk.idx==idxthenreturnvendendreturnnilend--==============================================================----== 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. ;)--==============================================================--functionclearCard(card)-- The card table-- Clean up if neededcard:clear()--fictitious methoddeck[card]=nil--byeend
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.