feat: lay-off cards onto existing melds

dev
LeRoyce Pearson 2024-02-18 01:27:43 -07:00
parent 2166ea47cf
commit e87b3c75bf
1 changed files with 81 additions and 6 deletions

View File

@ -94,12 +94,12 @@ function TIC()
-- render melds on tabletop -- render melds on tabletop
local meld_x=2 local meld_x=2
for j,meld in ipairs(melds_on_tabletop) do for j,meld in ipairs(melds_on_tabletop) do
for i,card in ipairs(meld) do local sel_state = nil
local sel_state = nil if hovered and hovered.interest == meld then
if hovered and hovered.interest == card then sel_state = 1
sel_state = 1 end
end
for i,card in ipairs(meld) do
card:render(meld_x, 10, false, sel_state) card:render(meld_x, 10, false, sel_state)
meld_x=meld_x + 8 meld_x=meld_x + 8
end end
@ -249,6 +249,17 @@ player_state_action = {
hovered = nil hovered = nil
return player_state_discard_confirm return player_state_discard_confirm
end end
elseif table_index_of(melds_on_tabletop, point_of_interest.interest) then
local meld=point_of_interest.interest
local meld_with_draft=meld:with(cards_in_meld_draft)
if rummy_is_valid_meld(meld_with_draft) then
for i,card in ipairs(cards_in_meld_draft) do
table.remove(cards_in_hand, cards_in_hand:index_of(card))
table.insert(meld, card)
end
meld:sort(rummy_hand_sort_comparison)
cards_in_meld_draft=CardStack:new()
end
end end
return player_state_action return player_state_action
end, end,
@ -279,6 +290,19 @@ player_state_action = {
}) })
end end
if #cards_in_meld_draft>0 then
for i,meld in ipairs(melds_on_tabletop) do
local meld_with_draft=meld:with(cards_in_meld_draft)
if rummy_is_valid_meld(meld_with_draft) then
table.insert(points_of_interest, {
x=i * 36,
y=10,
interest=meld
})
end
end
end
return points_of_interest return points_of_interest
end end
} }
@ -287,7 +311,12 @@ player_state_action = {
player_state_secondary_action = { player_state_secondary_action = {
update=function(point_of_interest) update=function(point_of_interest)
if cards_in_hand:contains(point_of_interest.interest) then if cards_in_hand:contains(point_of_interest.interest) then
cards_in_meld_draft=CardStack:new({ point_of_interest.interest }) if cards_in_meld_draft:contains(point_of_interest.interest) then
table.remove(cards_in_meld_draft, cards_in_meld_draft:index_of(point_of_interest.interest))
else
table.insert(cards_in_meld_draft, point_of_interest.interest)
cards_in_meld_draft:sort(rummy_hand_sort_comparison)
end
elseif point_of_interest.interest=="Discard" then elseif point_of_interest.interest=="Discard" then
if #cards_in_meld_draft==1 then if #cards_in_meld_draft==1 then
table.remove(cards_in_hand, cards_in_hand:index_of(cards_in_meld_draft[1])) table.remove(cards_in_hand, cards_in_hand:index_of(cards_in_meld_draft[1]))
@ -296,6 +325,17 @@ player_state_secondary_action = {
hovered = nil hovered = nil
return player_state_discard_confirm return player_state_discard_confirm
end end
elseif table_index_of(melds_on_tabletop, point_of_interest.interest) then
local meld=point_of_interest.interest
local meld_with_draft=meld:with(cards_in_meld_draft)
if rummy_is_valid_meld(meld_with_draft) then
for i,card in ipairs(cards_in_meld_draft) do
table.remove(cards_in_hand, cards_in_hand:index_of(card))
table.insert(meld, card)
end
meld:sort(rummy_hand_sort_comparison)
cards_in_meld_draft=CardStack:new()
end
end end
return player_state_secondary_action return player_state_secondary_action
end, end,
@ -318,6 +358,19 @@ player_state_secondary_action = {
}) })
end end
if #cards_in_meld_draft>0 then
for i,meld in ipairs(melds_on_tabletop) do
local meld_with_draft=meld:with(cards_in_meld_draft)
if rummy_is_valid_meld(meld_with_draft) then
table.insert(points_of_interest, {
x=i * 36,
y=10,
interest=meld
})
end
end
end
return points_of_interest return points_of_interest
end end
} }
@ -422,6 +475,15 @@ function create_deck()
return cards return cards
end end
function table_index_of(table, value)
for i,v in ipairs(table) do
if v==value then
return i
end
end
return nil
end
-- CardStack -- CardStack
CardStack = {} CardStack = {}
function CardStack:new(obj) function CardStack:new(obj)
@ -484,6 +546,19 @@ function CardStack:index_of(card_sought)
return nil return nil
end end
-- returns a new CardStack containing the cards from self and other. Does not remove the cards from self and other.
function CardStack:with(other)
local new_stack=CardStack:new()
for i,card in ipairs(self) do
table.insert(new_stack, card)
end
for i,card in ipairs(other) do
table.insert(new_stack, card)
end
return new_stack
end
-- Card rendering stuff -- Card rendering stuff
suit_icon={34,35,33,36} suit_icon={34,35,33,36}
suit_color={0,0,2,2} suit_color={0,0,2,2}