From 65871f1789245bad847e92b7c2ebe2c07e8ffdcf Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Wed, 1 Jun 2022 23:14:22 +0200 Subject: [PATCH] Navigating the board --- board.lua | 35 ++++++++++++++++++++++------------- states/game.lua | 44 ++++++++++++++++++++++++++++++++++++-------- states/menu.lua | 4 ++-- states/rules.lua | 4 ++-- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/board.lua b/board.lua index 147f4af..0a20d1a 100644 --- a/board.lua +++ b/board.lua @@ -1,7 +1,12 @@ local Board = {} function Board.new() local debug = false + local width = 10 + local tile_width = 10 + local margin = 4 + local padding = 1 + 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 ]] @@ -48,6 +53,20 @@ function Board.new() return xy_idx(x, y, width) 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) if invert then color = color == YELLOW and BLUE or YELLOW @@ -367,22 +386,12 @@ function Board.new() end, draw = function(self) - local w = 10 - local padding = 1 - local margin = 4 + local w = width 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 if color == 1 then fillp(▒) else fillp(█) end - rectfill2( - 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 + rectfill2(x, y, w, w, color) end end } diff --git a/states/game.lua b/states/game.lua index 7653b16..b318418 100644 --- a/states/game.lua +++ b/states/game.lua @@ -1,7 +1,15 @@ function stateGame() 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 repeat board:reset() @@ -41,14 +49,34 @@ function stateGame() printh(board:tostring()) 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 { _enter = _enter, - _update = function() - - end, - - _draw = function() - board:draw() - end, + _update = _update, + _draw = _draw, } end \ No newline at end of file diff --git a/states/menu.lua b/states/menu.lua index 4bd89c1..9fc6cd9 100644 --- a/states/menu.lua +++ b/states/menu.lua @@ -1,7 +1,7 @@ function stateMenu() local selected = 1 - function onBtnDraw(btn) + local function onBtnDraw(btn) if selected == btn.data.i then btn.color = 7 end @@ -18,7 +18,7 @@ function stateMenu() onDraw=onBtnDraw}) } - function _enter() + local function _enter() end diff --git a/states/rules.lua b/states/rules.lua index 07607ec..95ea88b 100644 --- a/states/rules.lua +++ b/states/rules.lua @@ -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 color = 1 - function blink(x,y,w,h) + local function blink(x,y,w,h) rect2(x,y,w,h,fade[color]) end - function goBack() + local function goBack() setState(states.menu) end