From 57409bb7d6ba142030c2003713894ee113d92077 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Tue, 12 Jul 2022 21:47:10 +0200 Subject: [PATCH] WIP tile transition --- 0hh1.p8 | 11 ++++-- bg.lua | 4 +++ board.lua | 21 ++++++++---- coroutines.lua | 4 +-- globals.lua | 32 +++++++++++++++++- main.lua | 24 +++++-------- states/endgame.lua | 51 ++++++++++++++++++++++++++++ states/game.lua | 84 ++++++++++++++++++++++++++-------------------- states/loading.lua | 1 - states/menu.lua | 24 +++++++------ states/rules.lua | 11 ++++-- ui.lua | 8 ++--- utils.lua | 2 +- visuals.lua | 40 ++++++++++++++++++++++ 14 files changed, 234 insertions(+), 83 deletions(-) create mode 100644 states/endgame.lua create mode 100644 visuals.lua diff --git a/0hh1.p8 b/0hh1.p8 index 604d719..7ae82d3 100644 --- a/0hh1.p8 +++ b/0hh1.p8 @@ -1,23 +1,28 @@ pico-8 cartridge // http://www.pico-8.com version 36 __lua__ --- enable mouse -poke(0x5F2D, 3) + -- Font M3X6 by daniel linssen poke(0x5600, 4, 4, 7) poke4(0x5700,unpack(split"0x0000.0000,0x0000.0000,0x0202.0202,0x0000.0200,0x0000.0505,0x0000.0000,0x0505.0705,0x0000.0507,0x0407.0106,0x0000.0203,0x0204.0100,0x0000.0401,0x0102.0502,0x0000.0305,0x0000.0102,0x0000.0000,0x0101.0102,0x0000.0201,0x0202.0201,0x0000.0102,0x0205.0000,0x0000.0005,0x0702.0000,0x0000.0002,0x0000.0000,0x0000.0102,0x0700.0000,0x0000.0000,0x0000.0000,0x0000.0200,0x0202.0404,0x0000.0101,0x0505.0506,0x0000.0305,0x0202.0302,0x0000.0702,0x0204.0403,0x0000.0701,0x0403.0403,0x0000.0304,0x0406.0505,0x0000.0404,0x0403.0107,0x0000.0304,0x0503.0106,0x0000.0605,0x0204.0407,0x0000.0202,0x0502.0506,0x0000.0305,0x0605.0503,0x0000.0304,0x0002.0000,0x0000.0002,0x0002.0000,0x0000.0102,0x0102.0400,0x0000.0402,0x0007.0000,0x0000.0007,0x0402.0100,0x0000.0102,0x0204.0403,0x0000.0200,0x0505.0506,0x0000.0601,0x0604.0300,0x0000.0705,0x0505.0301,0x0000.0705,0x0101.0600,0x0000.0701,0x0505.0604,0x0000.0705,0x0705.0600,0x0000.0601,0x0702.0204,0x0000.0202,0x0705.0600,0x0000.0304,0x0505.0301,0x0000.0505,0x0202.0002,0x0000.0202,0x0202.0002,0x0000.0102,0x0305.0101,0x0000.0505,0x0202.0202,0x0000.0402,0x0707.0300,0x0000.0507,0x0505.0300,0x0000.0505,0x0505.0600,0x0000.0305,0x0305.0700,0x0000.0101,0x0705.0600,0x0000.0404,0x0101.0600,0x0000.0101,0x0701.0600,0x0000.0304,0x0207.0202,0x0000.0202,0x0505.0500,0x0000.0705,0x0505.0500,0x0000.0205,0x0705.0500,0x0000.0507,0x0205.0500,0x0000.0505,0x0605.0500,0x0000.0304,0x0204.0700,0x0000.0701,0x0101.0103,0x0000.0301,0x0202.0101,0x0000.0404,0x0202.0203,0x0000.0302,0x0000.0502,0x0000.0000,0x0000.0000,0x0000.0403,0x0000.0201,0x0000.0000,0x0507.0506,0x0000.0505,0x0507.0503,0x0000.0705,0x0101.0106,0x0000.0701,0x0505.0503,0x0000.0305,0x0103.0107,0x0000.0701,0x0301.0106,0x0000.0101,0x0501.0106,0x0000.0705,0x0507.0505,0x0000.0505,0x0202.0207,0x0000.0702,0x0404.0407,0x0000.0304,0x0503.0505,0x0000.0505,0x0101.0101,0x0000.0701,0x0507.0705,0x0000.0505,0x0505.0503,0x0000.0505,0x0505.0506,0x0000.0305,0x0103.0507,0x0000.0101,0x0505.0506,0x0000.0403,0x0503.0507,0x0000.0505,0x0407.0106,0x0000.0304,0x0202.0207,0x0000.0202,0x0505.0505,0x0000.0705,0x0505.0505,0x0000.0205,0x0705.0505,0x0000.0507,0x0202.0505,0x0000.0505,0x0205.0505,0x0000.0202,0x0202.0407,0x0000.0701,0x0302.0204,0x0000.0402,0x0202.0202,0x0000.0202,0x0602.0201,0x0000.0102,0x0704.0000,0x0000.0001,0x0205.0200,0x0000.0000")) -#include main.lua +#include globals.lua +-- #include board.lua #include coroutines.lua #include ui.lua #include utils.lua #include bg.lua +#include visuals.lua +-- #include states/menu.lua #include states/loading.lua #include states/game.lua #include states/rules.lua +#include states/endgame.lua +-- +#include main.lua __gfx__ 00000000110000000001110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 diff --git a/bg.lua b/bg.lua index e663b74..fe3feef 100644 --- a/bg.lua +++ b/bg.lua @@ -28,4 +28,8 @@ function draw_animated_bg(startx) i, color) end +end + +function draw_bg_menu() + end \ No newline at end of file diff --git a/board.lua b/board.lua index 1253e5e..468e502 100644 --- a/board.lua +++ b/board.lua @@ -2,7 +2,7 @@ local Board = {} function Board.new() local debug = false - local width = 8 -- tiles + local width = 4 -- tiles local tile_width = 10 -- pixels local padding = 1 local tiles = {} @@ -68,13 +68,21 @@ function Board.new() end, try_flip_tile = function(self, id) + local to_color = nil if locked[id] then return end + local from_color = tiles[id] if tiles[id] == 0 then - tiles[id] = YELLOW + to_color = YELLOW elseif tiles[id] == YELLOW then - tiles[id] = BLUE + to_color = BLUE else tiles[id] = 0 + -- empty tile end + if to_color then + tiles[id] = to_color + end + local x,y = self:draw_coords(id) + spawn_tile_transition(x, y, tile_width-1, tile_width-1, from_color, to_color) end, get_rows = function(self) @@ -420,14 +428,15 @@ function Board.new() local v = tiles[idx] if v > 0 then local x,y = self:draw_coords(idx) - local color = v == BLUE and 12 or 8 + local color = get_main_color(v) + local shade = get_shade_color(v) if color == 1 then fillp(β–’) else fillp(β–ˆ) end if self:is_locked(idx) then rectfill2(x, y, w, w, color) else roundedrect(x, y, w, w, color) - line(x+1, y+w-1, x+w-2, y+w-1, color+1) - line(x+w-1, y+1, x+w-1, y+w-2, color+1) + line(x+1, y+w-1, x+w-2, y+w-1, shade) + line(x+w-1, y+1, x+w-1, y+w-2, shade) end else fillp(β–ˆ) diff --git a/coroutines.lua b/coroutines.lua index 2ff1441..6292139 100644 --- a/coroutines.lua +++ b/coroutines.lua @@ -23,12 +23,12 @@ end -- to be used inside a coroutine function wait(seconds) - wait_frames(seconds*60) + wait_frames(seconds*30) end -- to be used inside a coroutine function wait_frames(frames) for i=1,frames do - yield() + yield() end end \ No newline at end of file diff --git a/globals.lua b/globals.lua index c7ff0e8..6aa53ae 100644 --- a/globals.lua +++ b/globals.lua @@ -1,3 +1,33 @@ local amplitude = 128 local mouse_x = 0 -local mouse_y = 0 \ No newline at end of file +local mouse_y = 0 + +-- +-- constants +-- + +local BLUE = 1 +local YELLOW = 2 + +-- πŸ…ΎοΈ Z[C]N ❎ [X]VM +-- X and C have the same position on QWERTY and AZERTY +local LEFT,RIGHT,UP,DOWN,BTN_O,BTN_X = 0,1,2,3,4,5 + +local patterns = { + 0b1000000000000000, + 0b1000000000100000, + 0b1010000000100000, + 0b1010000010100000, + 0b1010010010100000, + 0b1010010010100001, + 0b1010010110100001, + 0b1010010110100101, + 0b1110010110100101, + 0b1110010110110101, + 0b1111010110110101, + 0b1111010111110101, + 0b1111110111110101, + 0b1111110111110111, + 0b1111111111110111, + 0b1111111111111111, +} \ No newline at end of file diff --git a/main.lua b/main.lua index 8805388..aa985e0 100644 --- a/main.lua +++ b/main.lua @@ -1,14 +1,3 @@ --- --- constants --- - -local BLUE = 1 -local YELLOW = 2 - --- πŸ…ΎοΈ Z[C]N ❎ [X]VM --- X and C have the same position on QWERTY and AZERTY -local LEFT,RIGHT,UP,DOWN,BTN_O,BTN_X = 0,1,2,3,4,5 - -- -- Utils -- @@ -118,25 +107,30 @@ function _init() states = { rules = state_rules(), menu = state_menu(), - game = state_game(), loading = state_loading(), + game = state_game(), + endgame = state_endgame(), } - set_state(states.loading) + set_state(states.menu) end function _update60() frame_count += 1 - _coresolve() - gs._update() -- update mouse coords mouse_x = stat(32) mouse_y = stat(33) + + _coresolve() + gs._update() end function _draw() gs._draw() + for overlay in all(overlays) do + overlay:_draw() + end spr(1, stat(32), stat(33)) end diff --git a/states/endgame.lua b/states/endgame.lua new file mode 100644 index 0000000..064c4d0 --- /dev/null +++ b/states/endgame.lua @@ -0,0 +1,51 @@ +function state_endgame() + local board + local message = "웃웃 yay! 웃웃" + local message_x = 0 + + local btn_back = make_button({x=1, y=118, h=7, text="πŸ…ΎοΈ/C bACK TO mENU", color=8, + on_click=function() go_to_menu() end, + on_hover=function(btn) btn.color = 7 end}) + + local function go_to_menu() + set_state(states.menu) + end + + local function _enter(_board) + board = _board + message_x = (128 - print(message,0,-100))/2 + end + + local function _update() + btn_back:update() + if btnp(BTN_O) then + go_to_menu() + end + end + + local function _draw() + cls() + board:draw() + + -- wavy message + local offset=0 + for i = 1, #message, 1 do -- loop through every letter + letter = sub(message, i, i) -- grab this letter + local st = t() + 0.125 * i -- create a modified time for this letter + print(letter, message_x + offset, 20 + sin(st*0.5)*10, 7) -- draw this letter + offset += print(letter,0,-100) + end + + btn_back:draw() + end + + local function _leave() + end + + return { + _enter = _enter, + _update = _update, + _draw = _draw, + _leave = _leave + } +end \ No newline at end of file diff --git a/states/game.lua b/states/game.lua index 941ba85..1432a90 100644 --- a/states/game.lua +++ b/states/game.lua @@ -3,6 +3,7 @@ function state_game() local board local selected_id = 1 local timer_clue = 0 + local mx,mx = 0,0 local clues = {} local function show_clues() @@ -27,7 +28,6 @@ function state_game() fillp(β–ˆ) end - local mx,mx = 0,0 local function update_mouse() -- update mouse position if mx == mouse_x and my == mouse_y then return end @@ -39,10 +39,19 @@ function state_game() local x = mid(1, (mouse_x - board_x) \ tw + 1, bw) local y = mid(1, (mouse_y - board_y) \ tw + 1, bw) selected_id = board:xy_idx(x,y) - printh("x: " .. x .. " y: " .. y .. " id: " .. selected_id) + -- printh("x: " .. x .. " y: " .. y .. " id: " .. selected_id) + end + + local function check_endgame() + if board:is_complete() and board:is_valid() then + set_state(states.endgame, board) + end end local function _enter(_board) + -- mouse bound to buttons + poke(0x5F2D, 3) + board = _board -- lock the initial tiles board:lock_tiles() @@ -52,6 +61,43 @@ function state_game() -- stopcoroutine(show_clues) end + local function _update() + local size = board:get_size() + local x, y = board:idx_xy(selected_id) + local moved = false + + if btnp(UP) then + moved = true + y -= 1 + elseif btnp(DOWN) then + moved = true + y += 1 + elseif btnp(LEFT) then + moved = true + x -= 1 + elseif btnp(RIGHT) then + moved = true + x += 1 + end + if moved then + selected_id = board:xy_idx(x, y) + end + + if btnp(BTN_X) then + board:try_flip_tile(selected_id) + show_clues() + end + + update_mouse() + + if (x<1) x=size + if (x>size) x=1 + if (y<1) y=size + if (y>size) y=1 + + check_endgame() + end + local function _draw() cls() local x,y = board:draw_coords(1) @@ -80,40 +126,6 @@ function state_game() palt() end - local function _update() - local size = board:get_size() - local x, y = board:idx_xy(selected_id) - local moved = false - if btnp(UP) then - moved = true - y -= 1 - elseif btnp(DOWN) then - moved = true - y += 1 - elseif btnp(LEFT) then - moved = true - x -= 1 - elseif btnp(RIGHT) then - moved = true - x += 1 - end - if moved then - selected_id = board:xy_idx(x, y) - end - - if btnp(BTN_X) then - board:try_flip_tile(selected_id) - show_clues() - end - - update_mouse() - - if (x<1) x=size - if (x>size) x=1 - if (y<1) y=size - if (y>size) y=1 - end - return { _enter = _enter, _update = _update, diff --git a/states/loading.lua b/states/loading.lua index 3d55a8a..96ea0b3 100644 --- a/states/loading.lua +++ b/states/loading.lua @@ -114,7 +114,6 @@ function state_loading() end messages_str ..= "tHANK YOU FOR YOUR PATIENCE" - custom_font() startcoroutine(create_board) end diff --git a/states/menu.lua b/states/menu.lua index 4b07530..e936d57 100644 --- a/states/menu.lua +++ b/states/menu.lua @@ -8,18 +8,28 @@ function state_menu() end local buttons = { - make_button({x=10, y=10, w=30, text="play", data={i=1}, + make_button({x=10, y=10, w=30, text="pLAY", data={i=1}, on_click=function() set_state(states.loading) end, on_hover=function(btn) btn.color = 7 selected = 1 end, on_draw=on_btn_draw}), - make_button({x=10, y=20, w=30, text="rules", data={i=2}, + make_button({x=10, y=20, w=30, text="rULES", data={i=2}, on_click=function() set_state(states.rules) end, on_hover=function(btn) btn.color = 7 selected = 2 end, on_draw=on_btn_draw}) } local function _enter() + -- mouse not bound to buttons + poke(0x5F2D, 1) + end + local function _draw() + cls() + draw_bg_menu() + for k,button in ipairs(buttons) do + button:draw(selected == k) + end + print("pRESS ❎/X TO CONTINUE", 8, 120, 7) end return { @@ -34,19 +44,13 @@ function state_menu() selected -= 1 elseif btnp(DOWN) then selected += 1 - elseif btnp(BTN_O) then + elseif btnp(BTN_X) then buttons[selected]:on_click() end selected = mid(1, selected, #buttons) end, - _draw = function() - cls() - for k,button in ipairs(buttons) do - button:draw(selected == k) - end - print("press πŸ…ΎοΈ/c to continue", 8, 120, 7) - end + _draw = _draw } end \ No newline at end of file diff --git a/states/rules.lua b/states/rules.lua index 9029c28..6ae0a00 100644 --- a/states/rules.lua +++ b/states/rules.lua @@ -11,11 +11,19 @@ function state_rules() set_state(states.menu) end - local btn_back = make_button({x=1, y=118, w=30, h=7, text="❎menu", color=8, + local btn_back = make_button({x=1, y=118, w=30, h=7, text="πŸ…ΎοΈ/C mENU", color=8, on_click=function() go_back() end, on_hover=function(btn) btn.color = 7 end}) return { + _enter = function() + custom_font() + end, + + _exit = function() + standard_font() + end, + _update=function() if frame_count%8==0 then color += 1 @@ -34,7 +42,6 @@ function state_rules() _draw=function() cls() - custom_font() print("1) yOU CAN'T HAVE MORE THAN\n TWO (2) CONSECUTIVE TILES\n OF THE SAME COLOR", 2,2, 7) local x = 14 diff --git a/ui.lua b/ui.lua index f80fb48..0a03274 100644 --- a/ui.lua +++ b/ui.lua @@ -1,9 +1,10 @@ function make_button(options) local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed + local w = print(options.text, 0, -100) return { x = options.x, y = options.y, - w = options.w, + w = options.w or w, h = options.h or 6, data = options.data, text = options.text, @@ -38,9 +39,4 @@ function make_button(options) end end } -end - -function print_shadow(text, x, y, color) - print(text, x+1, y+1, 0) - print(text, x, y, color) end \ No newline at end of file diff --git a/utils.lua b/utils.lua index d07145a..7c4ab07 100644 --- a/utils.lua +++ b/utils.lua @@ -7,7 +7,7 @@ function print(t,x,y,col1,col2) end end end - oldprint(t, x, y, col1) + return oldprint(t, x, y, col1) end function str_width(str) diff --git a/visuals.lua b/visuals.lua new file mode 100644 index 0000000..3153256 --- /dev/null +++ b/visuals.lua @@ -0,0 +1,40 @@ +local overlays = {} + +function get_main_color(tint) + if tint == BLUE then + return 12 + elseif tint == YELLOW then + return 8 + else + return 1 + end +end + +function get_shade_color(tint) + if tint == BLUE then + return 13 + elseif tint == YELLOW then + return 4 + else + return 1 + end +end + +function spawn_tile_transition(x, y, w, h, from_tint, to_tint) + local p = #patterns + printh(patterns) + local from_color = get_main_color(from_tint) + local to_color = get_main_color(to_tint) + add(overlays, { + _draw = function(self) + fillp() + rectfill(x, y, x + w, y + h, from_color) + fillp(patterns[p]+0b0.1) + rectfill(x, y, x + w, y + h, to_color) + p -= 2 + if p < 1 then + del(overlays, self) + end + end + }) +end