Skip to content

Instantly share code, notes, and snippets.

@eguneys
Last active December 22, 2021 01:06
Show Gist options
  • Save eguneys/25e8b7e25c0a243a576b8c912fc31c71 to your computer and use it in GitHub Desktop.
Save eguneys/25e8b7e25c0a243a576b8c912fc31c71 to your computer and use it in GitHub Desktop.
Card Stack Example
function has_card_and_pos(card, x, y)
return { card=card, pos = { x, y } }
end
function draw_card(hascardandpos)
love.graphics.print(hascardandpos.card, hascardandpos.pos[1], hascardandpos.pos[2])
end
function cardstack_and_pos(x, y)
return { targets = {}, cards = {}, pos = { x, y } }
end
function _cardstack_refresh_targets(stack)
stack.targets = {}
for i, card in ipairs(stack.cards) do
table.insert(stack.targets, { 0, i * 20 })
end
end
function cardstack_add_card(stack, card)
table.insert(stack.cards, card)
_cardstack_refresh_targets(stack)
end
function update_cardstack(stack)
for i, card in ipairs(stack.cards) do
card.pos[1] = approach(card.pos[1], stack.targets[i][1])
card.pos[2] = approach(card.pos[2], stack.targets[i][2])
end
end
function draw_cardstack(stack)
for _, card in ipairs(stack.cards) do
draw_card(card)
end
end
function approach(src, dest)
if src < dest then return src + 1
elseif src > dest then return src - 1
else return dest end
end
function love.load()
stack = cardstack_and_pos(50, 50)
cardstack_add_card(stack, has_card_and_pos('one', 10, 10))
cardstack_add_card(stack, has_card_and_pos('two', 40, 40))
cardstack_add_card(stack, has_card_and_pos('three', 60, 60))
end
function love.update()
update_cardstack(stack)
end
function love.draw()
draw_cardstack(stack)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment