Improve pickup/drop

This commit is contained in:
Pyrex 2024-02-12 15:03:55 -08:00
parent ff1c59adf3
commit dd46a76f54
4 changed files with 57 additions and 15 deletions

View File

@ -149,6 +149,7 @@ function board:update()
if (not was_idle and is_idle) then if (not was_idle and is_idle) then
self:on_idle() self:on_idle()
end end
if (is_idle) self.cursor:update()
end end
function board:draw() function board:draw()

View File

@ -36,4 +36,5 @@ function checkpoint:apply(board)
add(board.wells[w].contents,i) add(board.wells[w].contents,i)
end end
end end
board.cursor.grabbed=nil
end end

View File

@ -7,7 +7,7 @@ end
function completion_tracker:reset() function completion_tracker:reset()
for i=0,63 do for i=0,63 do
dset(i,0) if (i!=59) dset(i,0) -- don't clear music pref flag
end end
end end

View File

@ -10,12 +10,13 @@ function cursor:init(board)
self.board=board self.board=board
self.hover_x=self.ruleset.n_slots\2 self.hover_x=self.ruleset.n_slots\2
self.hover_y=1 self.hover_y=1
self.saved_hover_x_y=nil
self.grabbed=nil self.grabbed=nil
end end
function cursor:acceptance_state() function cursor:acceptance_state()
if self.grabbed then
local hover=self:hover_slot() local hover=self:hover_slot()
if self.grabbed then
if hover==self.grabbed then if hover==self.grabbed then
return acceptance_state.no_move return acceptance_state.no_move
end end
@ -28,7 +29,37 @@ function cursor:acceptance_state()
return acceptance_state.would_not_accept return acceptance_state.would_not_accept
end end
else else
return acceptance_state.not_grabbed if (self.board.slots[hover]:peek()) return acceptance_state.not_grabbed
return acceptance_state.would_not_accept
end
end
function cursor:save_hover()
self.saved_hover_x_y={self.hover_x,self.hover_y}
end
function cursor:restore_hover()
self.wants_to_restore_hover=true
end
function cursor:actually_restore_hover()
if (not self.saved_hover_x_y) return
self.wants_to_restore_hover=false
-- try restoring hover x
local old_hover_x,old_hover_y=self.hover_x,self.hover_y
self.hover_x,self.hover_y=unpack(self.saved_hover_x_y)
-- try very hard to be in a valid spot
for i in all{
function() self.hover_x,self.hover_y=old_hover_x,old_hover_y end,
function() self:move_x(-1) end,
function() self:move_y(1) end,
function() self:move_x(-1) end,
function() self:move_y(-1) end
} do
if (self:acceptance_state()!=acceptance_state.would_not_accept) return
i()
end end
end end
@ -38,6 +69,7 @@ function cursor:toggle_grab()
if acc==acceptance_state.not_grabbed then if acc==acceptance_state.not_grabbed then
if (not self.board.watcher:intercept("grab",slot)) sounds:dire() return if (not self.board.watcher:intercept("grab",slot)) sounds:dire() return
if (self.board.slots[slot]:peek()) self.grabbed=slot if (self.board.slots[slot]:peek()) self.grabbed=slot
self:save_hover()
sounds:menu() sounds:menu()
elseif acc==acceptance_state.would_accept then elseif acc==acceptance_state.would_accept then
if (not self.board.watcher:intercept("drop",slot)) sounds:dire() return if (not self.board.watcher:intercept("drop",slot)) sounds:dire() return
@ -46,25 +78,40 @@ function cursor:toggle_grab()
tar:add(card) tar:add(card)
self.grabbed=nil self.grabbed=nil
self.board:on_move(card) self.board:on_move(card)
self:restore_hover()
sounds:menu() sounds:menu()
elseif acc==acceptance_state.no_move or acc==acceptance_state.would_not_accept then elseif acc==acceptance_state.no_move or acc==acceptance_state.would_not_accept then
if (not self.board.watcher:intercept("cancel")) sounds:dire() return if (not self.board.watcher:intercept("cancel")) sounds:dire() return
self.grabbed=nil self.grabbed=nil
self:restore_hover()
sounds:menu() sounds:menu()
else else
assert(false,"invalid acceptance state") assert(false,"invalid acceptance state")
end end
end end
function cursor:update()
if (self.wants_to_restore_hover) self:actually_restore_hover()
end
function cursor:move_x(dx) function cursor:move_x(dx)
if (self.hover_y==0) return if (self.hover_y==0) return
if (dx==0) return
local orig_x=self.hover_x
while true do
self.hover_x+=dx self.hover_x+=dx
self.hover_x%=self.ruleset.n_slots -- TODO: Don't hard-code self.hover_x%=self.ruleset.n_slots
if (self.hover_x==orig_x) return
if (self:acceptance_state()!=acceptance_state.would_not_accept) return true
end
return
end end
function cursor:move_y(dy) function cursor:move_y(dy)
local old_y=self.hover_y
if (self.hover_y==0 and dy==1) self.hover_y=1 if (self.hover_y==0 and dy==1) self.hover_y=1
if (self.hover_y==1 and dy==-1) self.hover_y=0 if (self.hover_y==1 and dy==-1) self.hover_y=0
if (self:acceptance_state()==acceptance_state.would_not_accept) if (not self:move_x(-1)) self.hover_y=old_y
end end
function cursor:hover_slot() function cursor:hover_slot()
@ -84,22 +131,15 @@ function cursor:draw_at(l,i)
local card=self:grabbed_card() local card=self:grabbed_card()
local acc=self:acceptance_state() local acc=self:acceptance_state()
local dx,dy=0,0
if card and acc!=acceptance_state.would_accept and acc!=acceptance_state.no_move then
dx=flr(sin(time()/2)*2+0.5)
dy=flr(sin(time()/4)+0.5+1)
end
if card then if card then
i+=1 i+=1
local x,y=l:place_card(i) local x,y=l:place_card(i)
local card_fg=self.ruleset.deck:draw_card(x+dx,y+dy,card,{rotate=l.rotated}) local card_fg=self.ruleset.deck:draw_card(x,y,card,{rotate=l.rotated})
local fg=card_fg local fg=card_fg
if (acc==acceptance_state.would_accept) fg=9 draw_layout_hint(l,i,9,false,0,0)
draw_layout_hint(l,i,fg,false,dx,dy)
else else
local filled=false local filled=false
if (i<1) i=1 filled=true if (i<1) i=1 filled=true
draw_layout_hint(l,i,9,filled,dx,dy) draw_layout_hint(l,i,12,filled,0,0)
end end
end end