Skip to content

Instantly share code, notes, and snippets.

@Achie72
Created April 13, 2024 16:28
Show Gist options
  • Save Achie72/4094b117cf77fa62b2354fea2dc978ed to your computer and use it in GitHub Desktop.
Save Achie72/4094b117cf77fa62b2354fea2dc978ed to your computer and use it in GitHub Desktop.
How we handle setting, getting and swapping lines!
elseif state == "tools" then
-- pressing left will only move you to first item
if btnp(0) then toolbar.cursor = max(0, toolbar.cursor-1) end
-- pressing right will only mvoe to the last item
if btnp(1) then toolbar.cursor = min(12, toolbar.cursor+1) end
-- if we are on tools I want to have a float-in menu
-- that comes in from the bottom, showcasing the tools
toolbar.y = lerp(toolbar.y, toolbar.yOnScreen, 0.2)
-- go back to planning phase
if btnp(5) then
state = "plan"
end
-- go to use item phase
if btnp(4) then
if toolbar.cursor == 0 then
state = "shovel"
shovelState.selected = {}
shovelState.selectedLines = {}
shovelState.mode = false
end
end
elseif state == "shovel" then
-- in shovel state we want to be able to
-- swap to rows or to columns
-- up-down left-right to move the whole selector
-- need one button to exit state
-- need one button to do the action
-- somehow we need a button to switch between row or column selection
-- left and moves the cursor
-- up switches state, false is row, true is column
-- btnp(5) -- exit mode
-- button(4) is selection
-- we want to select one line, then move cursor, select another
-- but redo any time either of them
if btnp(4) then
-- if we have to selections already swap them
-- add selected position to mode
add(shovelState.selected, shovelState.cursor)
add(shovelState.selectedLines, get_tile_or_row(shovelState.cursor, shovelState.mode))
set_tile_or_row({16,16,16,16,16,16,16,16,16,16,16,16}, shovelState.cursor, shovelState.mode)
end
if #shovelState.selected == 2 then
-- do the swapero
-- i have 0-farm size stored
local swapLine = shovelState.selectedLines[1]
-- row is false, column is true, copy
-- the first selected one into the temp
-- variable for swapping
local selectionTwoTiles = shovelState.selectedLines[2]
-- swap the value of selection2 into selection one
set_tile_or_row(selectionTwoTiles, shovelState.selected[1], shovelState.mode)
-- swap the value of the temporay into selection two
set_tile_or_row(swapLine, shovelState.selected[2], shovelState.mode)
shovelState.selected = {}
shovelState.selectedLines = {}
end
-- moving the cursor
if btnp(0) then
shovelState.cursor -= 1
if shovelState.cursor < 0 then shovelState.cursor = 0 end
end
if btnp(1) then
shovelState.cursor += 1
if shovelState.cursor > farm.size then shovelState.cursor = farm.size end
end
-- swap mode
if btnp(2) then
if #shovelState.selected > 0 then
set_tile_or_row(shovelState.selectedLines[1], shovelState.selected[1], shovelState.mode)
end
shovelState.mode = not shovelState.mode
-- reset already done selections
-- TODO: DON't forget to rest
shovelState.selected = {}
shovelState.selectedLines = {}
end
if btnp(5) then
state = "tools"
shovelState.selected = {}
shovelState.selectedLines = {}
end
end
function set_tile_or_row(_values, _index, _isColumn)
for i=1,#_values do
local tileId = 0
if _isColumn then
-- columns, so we have fixed X variable y
tileId = mset(farm.topEdge+_index, farm.topEdge+i-1, _values[i])
else
-- rows, so we have variable x, but fix y
tileId = mset(farm.topEdge+i-1, farm.topEdge+_index, _values[i])
end
end
end
function get_tile_or_row(_index, _isColumn)
local fetchedTiles = {}
for i=farm.topEdge, farm.topEdge + farm.size do
local tileId = 0
if _isColumn then
-- columns, so we have fixed X variable y
tileId = mget(farm.topEdge+_index, i)
else
-- rows, so we have variable x, but fix y
tileId = mget(i, farm.topEdge+_index)
end
add(fetchedTiles, tileId)
end
return fetchedTiles
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment