I've started a new project, which is a kind of TCG. I'm currently using an array to store all the informations of the cards (name, health, effects...). For the effect part, I use functions to represent those effects (deal() to deal damage, play() to play a card...) The problem is that those functions are to be executed once the object is created, because they use variables that are created only with the object. But when I start the programm, it seems like it executes them even though I don't call them, since they're in the array.
main.lua :
Code: Select all
Object = require("Librairy/classic")
require("Librairy/card")
hand = {}
board = {}
--[[
All cards are encoded like this :
{Name, Health, Effect trigger, Effect}
Effect triggers :
"s" : Spawn
"d" : Death
"k" : Kill
"p" : Permanent
"h" : Harm (each time it hits)
Tokens are cards that are spawned by characters. They're uncollectible.
]]--
function love.load()
Tokens = {
{"Teleporter", 1, "s"}, --The teleporter will be a spawning plateform
{"Doll", 1, "d", effect = function() deal(board[self.crea], -1) end}, --Heal his creator once it dies
{"Mech", 1, "d", effect = function() swap(self, board[self.crea]) end} --Swap with his creator once it dies
}
Deck = {
{"Jetpack", 3, "s"}, --Spawn wherever he wants
{"Engineer", 4, "d", effect = function() play(Tokens[1], self.id) end}, --Create a Teleporter at death. Self.id is the position of the card object in the 'board' array (will be added later)
{"Magician", 6, "k", effect = function() deal(hand[love.math.random(1, #hand)], 1) end} --Deal one damage to a random card in your hand once it kills
}
draw(5) --Draw your first five cards
end
function love.update(dt)
--Nothing yet
end
function love.draw()
love.graphics.push()
--I'll add later camera movement
for i,card in ipairs(hand) do
--Draw the cards that are in your hand
if card.cond == "s" then love.graphics.setColor(255, 255, 255, 255) end
if card.cond == "d" then love.graphics.setColor(255, 0, 0, 255) end
if card.cond == "k" then love.graphics.setColor(0, 255, 0, 255) end
if card.cond == "p" then love.graphics.setColor(0, 0, 255, 255) end
if card.cond == "h" then love.graphics.setColor(255, 255, 0, 255) end
love.graphics.rectangle("fill", 71*i, 500, 70, 100)
love.graphics.setColor(0, 0, 0, 255)
love.graphics.print(card.health, 71*i + 2, 502)
love.graphics.print(card.name, 71*i + 5, 522)
end
love.graphics.pop()
end
--Draw cards
function draw(amount)
for i=1,amount do
draw = love.math.random(1, #Deck)
table.insert(hand, Card(Deck[draw]))
end
end
--Play a card on the board
function play(location, id, byWho)
if location == hand then table.remove(hand, i) end
if location == Tokens then table.insert(board, Card(location[id], byWho)) else table.insert(board, location[id]) end
end
--Deal damage to a card on the board
function deal(target, amount)
if target == "r" then target = board[love.math.random(1, #board)] end
target.health = target.health - amount
end
--Swap two cards' position
function swap(target1, target2)
local x = target1.x
local y = target1.y
target1.x = target2.x
target1.y = target2.y
target2.x = x
target2.y = y
end
Code: Select all
Card = Object:extend()
function Card:new(card, creator)
self.name = card[1]
self.health = card[2]
self.cond = card[3]
self.effect = card.effect
if creator then self.crea = creator end
end