This commit is contained in:
Kari Sigurjonsson 2022-01-19 04:13:26 +00:00
parent 1aa46678da
commit 4c1452ba6e

103
main.lua
View file

@ -1,3 +1,6 @@
local min = math.min
local max = math.max
local lg = love.graphics
lg.setDefaultFilter("nearest","nearest")
@ -10,9 +13,9 @@ local marginX = 20
local marginY = 20
local padding = 10
local cardImage = lg.newImage("mysa.png")
local cardImageW = cardImage:getWidth()
local cardImageH = cardImage:getHeight()
local cardAtlas = lg.newImage("mysa.png")
local cardAtlasW = cardAtlas:getWidth()
local cardAtlasH = cardAtlas:getHeight()
local CARDW = 200
local CARDH = 300
@ -25,7 +28,7 @@ love.window.setMode(slotX(8), slotY(5))
local cardQuads = {}
for i = 1, 57 do
cardQuads[i] = lg.newQuad(((i-1) % 13) * CARDW, math.floor((i-1) / 13) * CARDH, CARDW, CARDH, cardImageW, cardImageH)
cardQuads[i] = lg.newQuad(((i-1) % 13) * CARDW, math.floor((i-1) / 13) * CARDH, CARDW, CARDH, cardAtlasW, cardAtlasH)
end
local backId = 54
@ -35,30 +38,14 @@ local Spade = 2
local Diamond = 3
local Club = 4
local suitNames = {
"Hearts", "Spade", "Diamonds", "Clubs"
}
local writeln = function(...)
local t = {...}
for k, v in ipairs(t) do
t[k] = tostring(v)
end
io.stderr:write(table.concat(t, "\t"), "\n")
end
local suitNames = { "Hearts", "Spade", "Diamonds", "Clubs" }
--*************************************************************************************************
local FaceUp = true
local FaceDown = false
local Card = {
id = 1,
x = 0,
y = 0,
w = CARDW,
h = CARDH
}
local Card = { id = 1, x = 0, y = 0, w = CARDW, h = CARDH }
function Card:show()
self.showing = FaceUp
@ -76,38 +63,14 @@ function Card:hitTest(x, y)
)
end
function Card:intersection(other)
--[[
left = max(r1.left, r2.left)
right = min(r1.right, r2.right)
bottom = max(r1.bottom, r2.bottom)
top = min(r1.top, r2.top)
Then, if intersection is not empty (left < right && bottom < top),
subtract it from the common area of two rectangles: r1.area + r2.area - intersection.area.
PS:
Assumption 1: rectangles are aligned by the coordinate axes, that's usually the case.
Assumption 2: y axis here increases upwards, for example, in a graphics application, the y axis increases downwards, you may need to use:
bottom = min(r1.bottom, r2.bottom)
top = max(r1.top, r2.top)
--]]
end
function Card:draw()
lg.setColor(1, 1, 1, 1)
if (self.showing == FaceUp) then
lg.draw(cardImage, cardQuads[self.id], self.x, self.y)
lg.draw(cardAtlas, cardQuads[self.id], self.x, self.y)
else
lg.draw(cardImage, cardQuads[backId], self.x, self.y)
lg.draw(cardAtlas, cardQuads[backId], self.x, self.y)
end
--lg.setColor(0.05, 0.05, 0.05, 1)
--lg.setColor(0.15, 0.15, 0.15, 1)
lg.setColor(0.25, 0.25, 0.25, 1)
lg.rectangle("line", self.x, self.y, CARDW, CARDH, 4)
end
@ -128,10 +91,7 @@ end
--*************************************************************************************************
local Deck = {
x = 0,
y = 0
}
local Deck = { x = 0, y = 0 }
function Deck:empty()
local j = #self
@ -309,6 +269,32 @@ local findDeckUnderMouse = function(x, y)
end
end
--[[
local intersectionArea = function(left1, top1, right1, bottom1, left2, top2, right2, bottom2)
if (left2 < right1 and right2 > left1 and top2 < bottom1 and bottom2 > top1) then
return
min( r1->right, r2->right) - max(r1->x, r2->x) --Width of intersecting rect.
*
min(r1->bottom, r2->bottom) - max(r1->y, r2->y) --Height ...
end
return 0
end
local findDeckUnderCard = function(card)
for _, deck in ipairs(decks) do
if (#deck > 0) then
local card = deck:findCardUnderMouse(x, y)
if (card) then
return deck, card
end
elseif (deck:hitTest(x, y)) then
return deck
end
end
end
--]]
local undo = function()
if (not handDeck or not undoDeck) then return end
@ -350,9 +336,9 @@ end
love.load = function()
math.randomseed(os.time())
love.graphics.setLineStyle("rough")
love.graphics.setLineWidth(1)
love.graphics.setPointSize(1)
lg.setLineStyle("rough")
lg.setLineWidth(1)
lg.setPointSize(1)
newGame()
end
@ -511,17 +497,18 @@ love.mousereleased = function(x, y, b)
end
love.keypressed = function(k)
if (k == "f1") then
if (k == "n") then
newGame()
elseif (k == "f3") then
elseif (k == "1") then
backId = 53
elseif (k == "f4") then
elseif (k == "2") then
backId = 54
end
end
love.update = function(dt)
hooverDeck, hooverCard = findDeckUnderMouse(mx, my)
if (handDeck) then
handDeck:move(mx - cardOffX, my - cardOffY)
end