Navigating the board
This commit is contained in:
		
							parent
							
								
									17c7de2240
								
							
						
					
					
						commit
						65871f1789
					
				
							
								
								
									
										35
									
								
								board.lua
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								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
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user