|
@@ -25,8 +25,13 @@ player = {
|
|
|
}
|
|
|
|
|
|
local board_w, board_h = 8, 8
|
|
|
+local board = {}
|
|
|
+local gem_sprites = {}
|
|
|
+local board_x, board_y = 50, 4
|
|
|
|
|
|
-board = {}
|
|
|
+function prt(text, x, y, color)
|
|
|
+ print(text, x, y, color, true, 1, true)
|
|
|
+end
|
|
|
|
|
|
function generate_gem(exclude_idx)
|
|
|
-- exclude_idx = exclude_idx or 0
|
|
@@ -37,27 +42,46 @@ function generate_gem(exclude_idx)
|
|
|
return out_gem
|
|
|
end
|
|
|
|
|
|
+function generate_gem_sprite(idx, xpos, ypos)
|
|
|
+ return {
|
|
|
+ spr = gems[idx].spr,
|
|
|
+ x = xpos, y = ypos,
|
|
|
+ dx = xpos, dy = ypos,
|
|
|
+ t = 0,
|
|
|
+ }
|
|
|
+end
|
|
|
+
|
|
|
-- build the global board table from scratch
|
|
|
function generate_board(allow_matches)
|
|
|
allow_matches = allow_matches or 1
|
|
|
for y = 1, board_h do
|
|
|
- board[y] = {}
|
|
|
+ board[y], gem_sprites[y] = {}, {}
|
|
|
for x = 1, board_w do
|
|
|
- -- TODO: use allow_matches
|
|
|
- local value = generate_gem()
|
|
|
- if not allow_matches then
|
|
|
-
|
|
|
- end
|
|
|
- board[y][x] = value
|
|
|
+ local gem_idx = generate_gem()
|
|
|
+ board[y][x] = gem_idx
|
|
|
+ gem_sprites[y][x] = generate_gem_sprite(gem_idx, (x-1)*16, (y-1)*16)
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
function swap_gems(src_x, src_y, dest_x, dest_y)
|
|
|
+ -- local xdiff, ydiff = math.abs(dest_x-src_x), math.abs(dest_y-src_y)
|
|
|
+ -- if xdiff ~= 1 or ydiff ~= 1 then return end
|
|
|
+ -- if xdiff == 1 and ydiff == 1 then return end
|
|
|
+
|
|
|
local src_gem = board[src_y][src_x]
|
|
|
local dest_gem = board[dest_y][dest_x]
|
|
|
board[src_y][src_x] = dest_gem
|
|
|
board[dest_y][dest_x] = src_gem
|
|
|
+
|
|
|
+ -- set the new destination for swap
|
|
|
+ if src_x ~= dest_x then
|
|
|
+ gem_sprites[src_y][src_x].dx = (dest_x - 1) * 16
|
|
|
+ gem_sprites[dest_y][dest_x].dx = (src_x - 1) * 16
|
|
|
+ elseif src_y ~= dest_y then
|
|
|
+ gem_sprites[src_y][src_x].dy = (dest_y - 1) * 16
|
|
|
+ gem_sprites[dest_y][dest_x].dy = (src_y - 1) * 16
|
|
|
+ end
|
|
|
end
|
|
|
|
|
|
-- return: table of x,y pair tables, or nil
|
|
@@ -67,26 +91,59 @@ function find_matches()
|
|
|
-- brute force horizontal
|
|
|
for y_test = 1, board_h do
|
|
|
-- seed first gem
|
|
|
- local last_gem = board[y_test][1]
|
|
|
- local consecutive_gems = 1
|
|
|
- for x_test = 1, board_w do
|
|
|
+ local last_gem_x = board[y_test][1]
|
|
|
+ local consecutive_x = 1
|
|
|
+ for x_test = 2, board_w do
|
|
|
local gem_idx = board[y_test][x_test]
|
|
|
- if last_gem == gem_idx then
|
|
|
- consecutive_gems = consecutive_gems + 1
|
|
|
+ if last_gem_x == gem_idx then
|
|
|
+ consecutive_x = consecutive_x + 1
|
|
|
+
|
|
|
+ -- check once on the last column
|
|
|
+ if x_test == board_w and consecutive_x >= 3 then
|
|
|
+ for i = x_test - consecutive_x + 1, x_test do
|
|
|
+ table.insert(matches, { i, y_test })
|
|
|
+ end
|
|
|
+ end
|
|
|
else
|
|
|
- if consecutive_gems >= 3 then
|
|
|
- for i = x_test - consecutive_gems, x_test do
|
|
|
- table.insert(matches, { x_test-1, y_test-1 })
|
|
|
+ if consecutive_x >= 3 then
|
|
|
+ for i = x_test - consecutive_x, x_test - 1 do
|
|
|
+ table.insert(matches, { i, y_test })
|
|
|
end
|
|
|
end
|
|
|
- consecutive_gems = 1
|
|
|
+ consecutive_x = 1
|
|
|
end
|
|
|
- last_gem = gem_idx
|
|
|
- print(consecutive_gems, (x_test-1) * 16, (y_test-1) * 16)
|
|
|
+ last_gem_x = gem_idx
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ -- brute force vertical
|
|
|
+ for x_test = 1, board_w do
|
|
|
+ -- seed first gem
|
|
|
+ local last_gem_y = board[1][x_test]
|
|
|
+ local consecutive_y = 1
|
|
|
+ for y_test = 2, board_h do
|
|
|
+ local gem_idx = board[y_test][x_test]
|
|
|
+ if last_gem_y == gem_idx then
|
|
|
+ consecutive_y = consecutive_y + 1
|
|
|
+
|
|
|
+ -- check once on the last row
|
|
|
+ if y_test == board_h and consecutive_y >= 3 then
|
|
|
+ for i = y_test - consecutive_y + 1, y_test do
|
|
|
+ table.insert(matches, { x_test, i })
|
|
|
+ end
|
|
|
+ end
|
|
|
+ else
|
|
|
+ if consecutive_y >= 3 then
|
|
|
+ for i = y_test - consecutive_y, y_test - 1 do
|
|
|
+ table.insert(matches, { x_test, i })
|
|
|
+ end
|
|
|
+ end
|
|
|
+ consecutive_y = 1
|
|
|
+ end
|
|
|
+ last_gem_y = gem_idx
|
|
|
end
|
|
|
end
|
|
|
|
|
|
- -- print(#matches)
|
|
|
if #matches == 0 then
|
|
|
return nil
|
|
|
end
|
|
@@ -94,14 +151,32 @@ function find_matches()
|
|
|
return matches
|
|
|
end
|
|
|
|
|
|
-function draw_gem(piece_type, xpos, ypos)
|
|
|
- spr(gems[piece_type].spr, xpos*16, ypos*16, 0, 1, 0, 0, 2, 2)
|
|
|
+
|
|
|
+function check_clear_matches()
|
|
|
+ local matches = find_matches()
|
|
|
+ if not matches then return end
|
|
|
+
|
|
|
+ for i, gem in pairs(matches) do
|
|
|
+
|
|
|
+ end
|
|
|
end
|
|
|
|
|
|
function board_draw()
|
|
|
for y = 1, board_h do
|
|
|
for x = 1, board_w do
|
|
|
- draw_gem(board[y][x], x - 1, y - 1)
|
|
|
+ local sprite = gem_sprites[y][x]
|
|
|
+ spr(sprite.spr, sprite.x + board_x, sprite.y + board_y, 0, 1, 0, 0, 2, 2)
|
|
|
+
|
|
|
+ if math.abs(sprite.dx - sprite.x) < .01 then
|
|
|
+ sprite.x = sprite.dx
|
|
|
+ else
|
|
|
+ sprite.x = sprite.x + (sprite.dx - sprite.x) / 2
|
|
|
+ end
|
|
|
+ if math.abs(sprite.dy - sprite.y) < .01 then
|
|
|
+ sprite.y = sprite.dy
|
|
|
+ else
|
|
|
+ sprite.y = sprite.y + (sprite.dy - sprite.y) / 2
|
|
|
+ end
|
|
|
end
|
|
|
end
|
|
|
end
|
|
@@ -151,9 +226,9 @@ function player_update()
|
|
|
end
|
|
|
|
|
|
function player_draw()
|
|
|
- rectb((player.x-1)*16, (player.y-1)*16, 16, 16, player.cycle_pal[player.cycle_clr])
|
|
|
+ rectb((player.x-1)*16 + board_x, (player.y-1)*16 + board_y, 16, 16, player.cycle_pal[player.cycle_clr])
|
|
|
if player.is_selected then
|
|
|
- rectb((player.sel_x-1)*16, (player.sel_y-1)*16, 16, 16, 12)
|
|
|
+ rectb((player.sel_x-1)*16 + board_x, (player.sel_y-1)*16 + board_y, 16, 16, 12)
|
|
|
end
|
|
|
end
|
|
|
|
|
@@ -161,8 +236,9 @@ function debug_match_draw()
|
|
|
local matches = find_matches()
|
|
|
if not matches then return end
|
|
|
|
|
|
- for i = 1, #matches do
|
|
|
- rectb((matches[i][1]-1)*16, (matches[i][2]-1)*16, 16, 16, 6)
|
|
|
+ for i,_ in pairs(matches) do
|
|
|
+ rectb((matches[i][1]-1)*16 + board_x, (matches[i][2]-1)*16 + board_y, 16, 16, 6)
|
|
|
+ -- prt(matches[i][1] .. ', ' .. matches[i][2], 200, 7*i, 6)
|
|
|
end
|
|
|
end
|
|
|
|
|
@@ -173,8 +249,8 @@ function TIC()
|
|
|
|
|
|
cls()
|
|
|
board_draw()
|
|
|
- player_draw()
|
|
|
debug_match_draw()
|
|
|
+ player_draw()
|
|
|
end
|
|
|
|
|
|
-- <SPRITES>
|