-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard.lua
More file actions
90 lines (78 loc) · 3.7 KB
/
card.lua
File metadata and controls
90 lines (78 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
--[[
controlling the creation and values of resource card objects,
and the positioning in the hand, these are then managed elsewhere
]]
-- width and height of a card
local width = 182
local height = 252
Card = {}
local PLAYAREA = require("playarea") -- needed to position cards relative to it
local playareax, playareay, playareaw, playareah = PLAYAREA:get()
local rowlength = width * 3
local rowheight = height * 2
local sparelength = playareaw - rowlength
local spareheight = playareah - rowheight
local hpadding = sparelength / 4
local vpadding = spareheight / 3
-- positions in the playarea for cards to be placed
local top_left = { x = playareax + hpadding, y = playareay + vpadding }
local top_mid = { x = playareax + 2 * hpadding + width, y = playareay + vpadding }
local top_right = { x = playareax + 3 * hpadding + 2 * width, y = playareay + vpadding }
local bot_left = { x = playareax + hpadding, y = playareay + 2 * vpadding + height }
local bot_mid = { x = playareax + 2 * hpadding + width, y = playareay + 2 * vpadding + height }
local bot_right = { x = playareax + 3 * hpadding + 2 * width, y = playareay + 2 * vpadding + height }
-- base resource table
local resources = {
m = { value = "m", name = "money", order = 2, playPos = bot_mid, img = love.graphics.newImage("assets/images/cards/resource_cards/money.png") },
c = { value = "c", name = "cultist", order = 3, playPos = bot_right, img = love.graphics.newImage("assets/images/cards/resource_cards/cultist.png") },
f = { value = "f", name = "food", order = 4, playPos = top_left, img = love.graphics.newImage("assets/images/cards/resource_cards/food.png") },
p = { value = "p", name = "prisoner", order = 5, playPos = top_mid, img = love.graphics.newImage("assets/images/cards/resource_cards/prisoner.png") },
s = { value = "s", name = "suspicion", order = 6, playPos = top_right, img = love.graphics.newImage("assets/images/cards/resource_cards/suspicion.png") },
r = { value = "r", name = "relic", order = 1, playPos = bot_left, img = love.graphics.newImage("assets/images/cards/resource_cards/relic.png") }
}
local function createCard(cr)
-- defining the cards attributes
local card = {
x = resources[cr].playPos.x,
y = resources[cr].playPos.y,
width = width,
height = height,
selected = false,
dragging = false,
hovered = false,
offsetX = 0,
offsetY = 0,
originalX = 0,
originalY = 0,
playX = resources[cr].playPos.x,
playY = resources[cr].playPos.y,
resource = resources[cr]
}
return card
end
local function setHandPosition(c, handSize, pos)
-- hand width should be the size of 5 non overlapped cards
local handWidth = c.width * 5
-- card overlap decided based on the amount of cards in hand to make it fit the above width
local cardOverlapMultiplier = c.width / (handWidth / handSize)
-- this prevents negative overlap causing large gaps
if cardOverlapMultiplier < 1 then cardOverlapMultiplier = 1 end
-- defining where to start placing the cards
local startX
if cardOverlapMultiplier == 1 then
startX = ((handspace.w - c.width * handSize) / 2) + handspace.x
else
local actualWidth = c.width + (handSize - 1) * (c.width / cardOverlapMultiplier)
startX = ((handspace.w - actualWidth) / 2) + handspace.x
end
local yLevel = handspace.h - (c.height / 2)
if not c.selected or not c.dragging then
c.originalX = startX + (pos * (c.width / cardOverlapMultiplier))
if c.hovered then
c.originalY = handspace.h - c.height
else
c.originalY = yLevel
end
end
end
return { create = createCard, setHandPosition = setHandPosition }