Browse Source

randomizer, basic game states

pixelbath 2 years ago
parent
commit
1b77fe3f31
1 changed files with 114 additions and 35 deletions
  1. 114 35
      blockbattle/blockbattle.lua

+ 114 - 35
blockbattle/blockbattle.lua

@@ -3,7 +3,15 @@
 -- desc:   a falling blocks game totally not inspired by anything previous
 -- script: lua
 
-t=0
+local spriteIndexStart = 256
+local blockSize = 8
+
+local t = 0
+local t_move = 0
+local is_move_pressed = false
+local is_das_triggered = false
+
+-- i, o, j, l, t, s, z
 local pieces = {
     {
         {
@@ -137,8 +145,14 @@ local pieces = {
 
 local board = {}
 
+local game_state = {
+    piece_queue = {},
+    piece_bag = {},
+    update = nil,
+}
+
 local active_piece = {
-    type = 3,
+    type = nil,
     rotation = 1,
     x = 3, y = 0,
 }
@@ -149,13 +163,27 @@ local rules = {
     are = 4,
 }
 
-local spriteIndexStart = 256
-local blockSize = 8
+-- gaiden, vs, single
+local options = {
+    gametype = "gaiden",
+    shared_queue = true,
+}
 
-local t_move = 0
-local is_move_pressed = false
-local is_das_triggered = false
+function shuffle(x)
+    for i = #x, 2, -1 do
+		local j = math.random(i)
+		x[i], x[j] = x[j], x[i]
+	end
+end
 
+function fill_bag()
+    -- just throw a bag of randomized pieces in it
+    local new_bag = { 1, 2, 3, 4, 5, 6, 7 }
+    shuffle(new_bag)
+    for i = 1,7 do
+        table.insert(game_state.piece_bag, new_bag[i])
+    end
+end
 
 function init_board()
 	for y = 1, 20 do
@@ -167,27 +195,22 @@ function init_board()
 end
 
 function spawn_piece()
-
-end
-
-function draw_piece()
-	for y = 1, 4 do
-        for x = 1, 4 do
-            local block = pieces[active_piece.type][active_piece.rotation][y][x]
-            if block ~= 0 then
-                spr(
-                    spriteIndexStart + block, 
-                    (active_piece.x + x - 1) * blockSize,
-                    (active_piece.y + y - 1) * blockSize
-                )
-            end
-        end
+    if #game_state.piece_bag <= 4 then
+        fill_bag()
     end
+    active_piece = {
+        type = game_state.piece_bag[1],
+        rotation = 1,
+        x = 3, y = 0,
+    }
+    table.remove(game_state.piece_bag, 1)
 end
 
 function hard_drop()
+    -- active_piece.rotation = 1
     active_piece.type = active_piece.type + 1
     if active_piece.type > 7 then active_piece.type = 1 end
+    trace("type: "..tostring(active_piece.type)..", rot="..tostring(active_piece.rotation))
 end
 function soft_drop()
     active_piece.y = active_piece.y + 1
@@ -202,7 +225,7 @@ end
 function handle_input()
     is_move_pressed = false
     if btnp(0) then
-        hard_drop()
+        spawn_piece()
 	end
 	if btn(1) then
         is_move_pressed = true
@@ -256,26 +279,82 @@ function handle_input()
     end
 end
 
-function TIC()
+function draw_board(xpos, ypos)
+    -- draw next pieces
+    for i=1,3 do
+        local ptype = game_state.piece_bag[i]
+        draw_piece(200, 20 + (i * 20), ptype, 1)
+    end
+end
+
+function draw_piece(xpos, ypos, ptype, rotation)
+	for y = 1, 4 do
+        for x = 1, 4 do
+            local block = pieces[ptype][rotation][y][x]
+            if block ~= 0 then
+                spr(
+                    spriteIndexStart + block, 
+                    (x - 1) * blockSize + xpos,
+                    (y - 1) * blockSize + ypos
+                )
+            end
+        end
+    end
+end
+
+-- BEGIN state updates
+function update_intro()
+    if t == 30 then
+        game_state.update = update_menu
+    else
+        print("intro " .. tostring(30-t))
+    end
+end
+
+function update_menu()
+    print("Press X to start")
+    if btnp(5) then
+        game_state.update = update_game
+        init_board()
+        fill_bag()
+        spawn_piece()
+    end
+end
+
+function update_game()
     handle_input()
+    draw_board()
+	draw_piece(active_piece.x * blockSize, active_piece.y * blockSize, active_piece.type, active_piece.rotation)
+end
+-- END state updates
+
 
+game_state.update = update_intro
+
+
+function TIC()
 	cls(0)
-	draw_piece()
+    
+    -- call our current state's update
+    game_state.update()
+    t = t + 1
 end
 
 -- <SPRITES>
--- 001:bbbbbbbb9aaaaaab9aaaaaab9aaaaaab9aaaaaab9aaaaaab9aaaaaab89999999
--- 002:8aaaaaaa8999999a8999999a8999999a8999999a8999999a8999999a88888888
--- 003:4444444423333334233333342333333423333334233333342333333412222222
--- 004:cccccccc2444444c2444444c2444444c2444444c2444444c2444444c12222222
--- 005:55555555766666657666666576666665766666657666666576666665f7777777
--- 006:ddddddddfeeeeeedfeeeeeedfeeeeeedfeeeeeedfeeeeeedfeeeeeedffffffff
--- 007:3333333312222223122222231222222312222223122222231222222381111111
+-- 001:bbbbbbbc9aaaaaab9aaaaaab9aaaaaab9aaaaaab9aaaaaab9aaaaaab89999999
+-- 002:8aaaaaab8999999a8999999a8999999a8999999a8999999a8999999a88888888
+-- 003:4444444c23333334233333342333333423333334233333342333333412222222
+-- 004:3ccccccc2444444c2444444c2444444c2444444c2444444c2444444c12222222
+-- 005:55555554766666657666666576666665766666657666666576666665f7777777
+-- 006:dddddddcfeeeeeedfeeeeeedfeeeeeedfeeeeeedfeeeeeedfeeeeeedffffffff
+-- 007:3333333412222223122222231222222312222223122222231222222381111111
 -- 016:8bbbbbbb8abbbbba8aabbbaa8aaabaaa8aaa9aaa8aa999aa8a99999a88888888
 -- 017:f5555555f6555556f6655566f6665666f6667666f6677766f6777776ffffffff
--- 018:0000000000044000004cc4000444cc4004444440004444000004400000000000
--- 019:0aaaaaa0a000000aa000000aa000000aa000000aa000000aa000000a0aaaaaa0
--- 020:0666666060000006600000066000000660000006600000066000000606666660
+-- 018:0aaaaaa0a000000aa000000aa000000aa000000aa000000aa000000a0aaaaaa0
+-- 019:0666666060000006600000066000000660000006600000066000000606666660
+-- 020:000000000bc000000abbc0000aaabbc00aaaaff00aaff0000ff0000000000000
+-- 021:0000000005400000065540000666554006666ff0066ff0000ff0000000000000
+-- 022:0000000000044000004cc4000444cc4004444440004444000004400000000000
 -- </SPRITES>
 
 -- <PALETTE>