2 Commits 1f9fbb09d4 ... 4e9d4b761e

Author SHA1 Message Date
  pixelbath 4e9d4b761e match system done, trying to decouple engine from vis 10 months ago
  pixelbath 3b66e0b508 play with palette swaps 10 months ago
2 changed files with 127 additions and 30 deletions
  1. 23 2
      bdash.lua
  2. 104 28
      diamondmine.lua

+ 23 - 2
bdash.lua

@@ -26,6 +26,20 @@ function append_elem(obj, append_obj)
 	table.insert(obj.children, append_obj)
 end
 
+-- this method swaps active palette entries
+function swap_palette(idx)
+	local map_addr = 0x3ff0
+	if idx == 0 then
+		poke4(map_addr*2 + 12, 5)
+		poke4(map_addr*2 + 14, 3)
+		poke4(map_addr*2 + 15, 2)
+	end
+end
+
+-- this method pokes new colors into the palette
+function direct_palette(idx)
+end
+
 function render_ui(obj, xs, ys)
 	xs = xs or 0
 	ys = ys or 0
@@ -148,8 +162,14 @@ function draw_toolbox()
 	end
 end
 
+function draw_map()
+	map(0, 0)
+end
+
+swap_palette(0)
+
 function TIC()
-	update_editor()
+	-- update_editor()
 
 	if btn(0) then y=y-1 end
 	if btn(1) then y=y+1 end
@@ -157,7 +177,8 @@ function TIC()
 	if btn(3) then x=x+1 end
 
 	cls(0)
-	render_ui(ui_container)
+	-- render_ui(ui_container)
+	draw_map()
 	-- draw_grid(1)
 	-- draw_toolbox()
 end

+ 104 - 28
diamondmine.lua

@@ -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>