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)
self.cards = cards or {}
print(self.cards) --NOTE DEBUG
self.x = 0
self.y = 0
self.r = 0
@@ -76,18 +75,49 @@ end
--TODO ? placeIn() to randomly place within without shuffling whole deck?
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
local new = {}
while (count > 1) and (#self.cards > 0) do
local card = remove(self.cards)
card.face = self.face
insert(new, card)
insert(new, remove(self.cards))
count = count - 1
end
self:update()
local deck = Deck(new)
deck.face = self.face
return Deck(new)
return deck
else
local card = remove(self.cards)
card.face = self.face
@@ -96,13 +126,18 @@ function Deck:drawCards(count)
return card
end
--]]
end
--on top of deck
function Deck:placeCardsOn(cards)
if type(cards) == "table" then
for _, card in ipairs(cards) do
insert(self.cards, card)
--if type(cards) == "table" then
-- for _, card in ipairs(cards) do
-- insert(self.cards, card)
-- end
if cards:isInstanceOf(Deck) then
for i=1,#cards.cards do
insert(self.cards, cards.cards[i])
end
else
insert(self.cards, cards)
@@ -111,12 +146,12 @@ end
--on bottom of deck
function Deck:placeCardsUnder(cards)
if type(cards) == "table" then
for _, card in ipairs(cards) do
insert(self.cards, card, 1)
if cards:isInstanceOf(Deck) then
for i=1,#cards.cards do
insert(self.cards, 1, cards.cards[i])
end
else
insert(self.cards, cards, 1)
insert(self.cards, 1, cards)
end
end
@@ -124,10 +159,4 @@ function Deck:getCards()
return self.cards
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

View File

@@ -5,7 +5,7 @@ local insert = table.insert
local lg = love.graphics
local lm = love.mouse
local inspect = require "lib.inspect"
local inspect = require "lib.inspect" --NOTE DEBUG
local items = {}
@@ -156,11 +156,13 @@ function love.mousepressed(x, y, button)
elseif items[item]:isInstanceOf(Card) then
local card = table.remove(items, item)
local deck = Deck({card, holding})
--deck:moveTo(card.x, card.y)
deck:moveTo(card:getPosition())
print(inspect(deck)) --NOTE DBEUG
insert(items, deck)
holding = false
deck.face = card.face
holding = deck
--deck:moveTo(card:getPosition())
--print(inspect(deck)) --NOTE DBEUG
--insert(items, deck)
--holding = false
elseif items[item]:isInstanceOf(Deck) then
items[item]:shuffleIn(holding)
holding = false
@@ -235,6 +237,15 @@ function love.mousepressed(x, y, button)
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
function love.keypressed(key)