Navigating the board

This commit is contained in:
Simon Cambier 2022-06-01 23:14:22 +02:00
parent 17c7de2240
commit 65871f1789
4 changed files with 62 additions and 25 deletions

View File

@ -1,7 +1,12 @@
local Board = {} local Board = {}
function Board.new() function Board.new()
local debug = false local debug = false
local width = 10 local width = 10
local tile_width = 10
local margin = 4
local padding = 1
local t = [[ local t = [[
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0
]] ]]
@ -48,6 +53,20 @@ function Board.new()
return xy_idx(x, y, width) return xy_idx(x, y, width)
end, end,
draw_coords = function(self, x, y)
if not y then x, y = self:idx_xy(x) end
return margin + (x-1)*tile_width + (x-1)*(padding+1),
margin + (y-1)*tile_width + (y-1)*(padding+1)
end,
getSize = function(self)
return width
end,
getTileWidth = function(self)
return tile_width
end,
fill = function(self, idx, color, invert) fill = function(self, idx, color, invert)
if invert then if invert then
color = color == YELLOW and BLUE or YELLOW color = color == YELLOW and BLUE or YELLOW
@ -367,22 +386,12 @@ function Board.new()
end, end,
draw = function(self) draw = function(self)
local w = 10 local w = width
local padding = 1
local margin = 4
for k,v in ipairs(tiles) do for k,v in ipairs(tiles) do
local x,y = self:idx_xy(k) local x,y = self:draw_coords(k)
local color = v == BLUE and 12 or v == YELLOW and 8 or 1 local color = v == BLUE and 12 or v == YELLOW and 8 or 1
if color == 1 then fillp() else fillp() end if color == 1 then fillp() else fillp() end
rectfill2( rectfill2(x, y, w, w, color)
margin + (x-1)*w + (x-1)*(padding+1),
margin + (y-1)*w + (y-1)*(padding+1),
w, w, color)
if debug then
print(k,
margin + (x-1)*w + (x-1)*(padding+1),
margin + (y-1)*w + (y-1)*(padding+1), 8)
end
end end
end end
} }

View File

@ -1,7 +1,15 @@
function stateGame() function stateGame()
local board = Board.new() local board = Board.new()
function _enter() local selectedId = 1
function drawSelectedTile()
local x, y = board:draw_coords(selectedId)
local w = board:getTileWidth()
rect2(x-1, y-1, w+2, w+2, 6)
end
local function _enter()
-- Create a board -- Create a board
repeat repeat
board:reset() board:reset()
@ -41,14 +49,34 @@ function stateGame()
printh(board:tostring()) printh(board:tostring())
end end
local function _draw()
board:draw()
drawSelectedTile()
end
local function _update()
local size = board:getSize()
local x, y = board:idx_xy(selectedId)
if btnp(UP) then
y -= 1
elseif btnp(DOWN) then
y += 1
elseif btnp(LEFT) then
x -= 1
elseif btnp(RIGHT) then
x += 1
end
printh(x.." "..y)
if (x<1) x=size
if (x>size) x=1
if (y<1) y=size
if (y>size) y=1
selectedId = board:xy_idx(x, y)
end
return { return {
_enter = _enter, _enter = _enter,
_update = function() _update = _update,
_draw = _draw,
end,
_draw = function()
board:draw()
end,
} }
end end

View File

@ -1,7 +1,7 @@
function stateMenu() function stateMenu()
local selected = 1 local selected = 1
function onBtnDraw(btn) local function onBtnDraw(btn)
if selected == btn.data.i then if selected == btn.data.i then
btn.color = 7 btn.color = 7
end end
@ -18,7 +18,7 @@ function stateMenu()
onDraw=onBtnDraw}) onDraw=onBtnDraw})
} }
function _enter() local function _enter()
end end

View File

@ -3,11 +3,11 @@ function stateRules()
local fade = split"0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,9,10,9,4,2,1" local fade = split"0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,9,10,9,4,2,1"
local color = 1 local color = 1
function blink(x,y,w,h) local function blink(x,y,w,h)
rect2(x,y,w,h,fade[color]) rect2(x,y,w,h,fade[color])
end end
function goBack() local function goBack()
setState(states.menu) setState(states.menu)
end end