This commit is contained in:
Paul Liverman
2015-12-06 00:22:58 -08:00
parent 072322f1d7
commit a0ac914028
2 changed files with 65 additions and 25 deletions

View File

@@ -12,7 +12,6 @@ Deck.static.height = 89*2
function Deck:initialize(cards) function Deck:initialize(cards)
self.cards = cards or {} self.cards = cards or {}
print(self.cards) --NOTE DEBUG
self.x = 0 self.x = 0
self.y = 0 self.y = 0
self.r = 0 self.r = 0
@@ -76,18 +75,49 @@ end
--TODO ? placeIn() to randomly place within without shuffling whole deck? --TODO ? placeIn() to randomly place within without shuffling whole deck?
function Deck:drawCards(count) function Deck:drawCards(count)
if #self.cards < 2 then
--return self as card (may break!?)
--self = self.cards[1]
return self
end
if count and (count > 1) then
if count >= #self.cards then
return self
end
local new = {}
for i=1,count do
insert(new, remove(self.cards))
end
local deck = Deck(new)
deck.face = self.face
return deck
else
--[[
if #self.cards == 1 then
return self
end
--]]
local card = remove(self.cards)
card.face = self.face
return card
end
--[[
if count and (count > 1) then if count and (count > 1) then
local new = {} local new = {}
while (count > 1) and (#self.cards > 0) do while (count > 1) and (#self.cards > 0) do
local card = remove(self.cards) insert(new, remove(self.cards))
card.face = self.face count = count - 1
insert(new, card)
end end
self:update() local deck = Deck(new)
deck.face = self.face
return Deck(new) return deck
else else
local card = remove(self.cards) local card = remove(self.cards)
card.face = self.face card.face = self.face
@@ -96,13 +126,18 @@ function Deck:drawCards(count)
return card return card
end end
--]]
end end
--on top of deck --on top of deck
function Deck:placeCardsOn(cards) function Deck:placeCardsOn(cards)
if type(cards) == "table" then --if type(cards) == "table" then
for _, card in ipairs(cards) do -- for _, card in ipairs(cards) do
insert(self.cards, card) -- insert(self.cards, card)
-- end
if cards:isInstanceOf(Deck) then
for i=1,#cards.cards do
insert(self.cards, cards.cards[i])
end end
else else
insert(self.cards, cards) insert(self.cards, cards)
@@ -111,12 +146,12 @@ end
--on bottom of deck --on bottom of deck
function Deck:placeCardsUnder(cards) function Deck:placeCardsUnder(cards)
if type(cards) == "table" then if cards:isInstanceOf(Deck) then
for _, card in ipairs(cards) do for i=1,#cards.cards do
insert(self.cards, card, 1) insert(self.cards, 1, cards.cards[i])
end end
else else
insert(self.cards, cards, 1) insert(self.cards, 1, cards)
end end
end end
@@ -124,10 +159,4 @@ function Deck:getCards()
return self.cards return self.cards
end end
function Deck:update()
if #self.cards < 2 then
self = self.cards[1] --turn into a Card (no idea if this will break anything Oo)
end
end
return Deck return Deck

View File

@@ -5,7 +5,7 @@ local insert = table.insert
local lg = love.graphics local lg = love.graphics
local lm = love.mouse local lm = love.mouse
local inspect = require "lib.inspect" local inspect = require "lib.inspect" --NOTE DEBUG
local items = {} local items = {}
@@ -156,11 +156,13 @@ function love.mousepressed(x, y, button)
elseif items[item]:isInstanceOf(Card) then elseif items[item]:isInstanceOf(Card) then
local card = table.remove(items, item) local card = table.remove(items, item)
local deck = Deck({card, holding}) local deck = Deck({card, holding})
--deck:moveTo(card.x, card.y) deck.face = card.face
deck:moveTo(card:getPosition()) holding = deck
print(inspect(deck)) --NOTE DBEUG
insert(items, deck) --deck:moveTo(card:getPosition())
holding = false --print(inspect(deck)) --NOTE DBEUG
--insert(items, deck)
--holding = false
elseif items[item]:isInstanceOf(Deck) then elseif items[item]:isInstanceOf(Deck) then
items[item]:shuffleIn(holding) items[item]:shuffleIn(holding)
holding = false holding = false
@@ -235,6 +237,15 @@ function love.mousepressed(x, y, button)
end end
end end
end end
-- this is stupid and I shouldn't have to do it this way (I think)
for i=#items,1,-1 do
if items[i]:isInstanceOf(Deck) and (#items[i].cards < 2) then
items[i].cards[1].face = items[i].face
items[i].cards[1]:moveTo(items[i]:getPosition())
items[i] = items[i].cards[1] --should delete the Deck since no references..or at least hide it away forever..yay memory leaks?
end
end
end end
function love.keypressed(key) function love.keypressed(key)