From dd54633751e5be56b0686182df92b85e64af98e7 Mon Sep 17 00:00:00 2001 From: Regina Hansson Date: Sun, 10 Nov 2019 14:34:10 +0100 Subject: [PATCH] Change keybinds, switch between rotate and move when changing mode --- functions.lua | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 38 +++++----------------- modes.lua | 12 ++++++- values.lua | 12 +++---- 4 files changed, 112 insertions(+), 37 deletions(-) diff --git a/functions.lua b/functions.lua index 4a17cbd..d5a8cee 100644 --- a/functions.lua +++ b/functions.lua @@ -1,3 +1,5 @@ +require("values") + function write_centered(text, font, y, win) local w = font:getWidth(text) love.graphics.setFont(font) @@ -21,3 +23,88 @@ function time_to_string(time) return time end end + +-- Used by modes to move player + +function reset_rotations() + player.rotation_dir = 0 + if pressed_keys.right == "rot" then + pressed_keys.right = "none" + move_on_key(key_right) + end + if pressed_keys.left == "rot" then + pressed_keys.left = "none" + move_on_key(key_left) + end +end + +function reset_movements() + player.x_dir = 0 + player.y_dir = 0 + if pressed_keys.right == "move" then + pressed_keys.right = "none" + rotate_on_key(key_right) + end + if pressed_keys.left == "move" then + pressed_keys.left = "none" + rotate_on_key(key_left) + end + if pressed_keys.down == "move" then + pressed_keys.down = "none" + end + if pressed_keys.up == "move" then + pressed_keys.up = "none" + end +end + +function rotate_on_key(key) + if key == key_right and pressed_keys.right ~= "move" then + player.rotation_dir = player.rotation_dir + 1 + pressed_keys.right = "rot" + elseif key == key_left and pressed_keys.left ~= "move" then + player.rotation_dir = player.rotation_dir - 1 + pressed_keys.left = "rot" + end +end + +function move_on_key(key) + if key == key_right and pressed_keys.right ~= "rot" then + player.x_dir = player.x_dir + 1 + pressed_keys.right = "move" + elseif key == key_left and pressed_keys.left ~= "rot" then + player.x_dir = player.x_dir - 1 + pressed_keys.left = "move" + elseif key == key_down and pressed_keys.down ~= "rot" then + player.y_dir = player.y_dir + 1 + pressed_keys.down = "move" + elseif key == key_up and pressed_keys.up ~= "rot" then + player.y_dir = player.y_dir - 1 + pressed_keys.up = "move" + end +end + +function stop_rotate_on_key(key) + if key == key_right and pressed_keys.right == "rot" then + player.rotation_dir = player.rotation_dir - 1 + pressed_keys.right = "none" + elseif key == key_left and pressed_keys.left == "rot" then + player.rotation_dir = player.rotation_dir + 1 + pressed_keys.left = "none" + end +end + +function stop_move_on_key(key) + if key == key_right and pressed_keys.right == "move" then + player.x_dir = player.x_dir - 1 + pressed_keys.right = "none" + elseif key == key_left and pressed_keys.left == "move" then + player.x_dir = player.x_dir + 1 + pressed_keys.left = "none" + elseif key == key_down and pressed_keys.down == "move" then + player.y_dir = player.y_dir - 1 + pressed_keys.down = "none" + elseif key == key_up and pressed_keys.up == "move" then + player.y_dir = player.y_dir + 1 + pressed_keys.up = "none" + end +end diff --git a/main.lua b/main.lua index 7b6e188..0e76937 100644 --- a/main.lua +++ b/main.lua @@ -34,27 +34,14 @@ function love.keypressed(key) if key == "escape" then state = "start" end - if key == "9" then - end + -- -- Modes if key == key_see or key == key_move or key == key_attack then mode = "mode"..key end -- Movement - if key == rot_r then - player.rotation_dir = player.rotation_dir + 1 - elseif key == rot_l then - player.rotation_dir = player.rotation_dir - 1 - elseif key == inc_x then - player.x_dir = player.x_dir + 1 - elseif key == dec_x then - player.x_dir = player.x_dir - 1 - elseif key == dec_y then - player.y_dir = player.y_dir + 1 - elseif key == inc_y then - player.y_dir = player.y_dir - 1 - end + modes[mode].func_key_pressed(key) if key == "space" then modes[mode].func_shoot() @@ -75,7 +62,7 @@ function love.keypressed(key) end elseif state == "take_name" then - if key == "return" then + if key == "return" or key == "escape" then state = "start" end if key == "backspace" and #hs_holder > 0 then @@ -92,19 +79,7 @@ end function love.keyreleased(key) if state == "game_running" then - if key == rot_r then - player.rotation_dir = player.rotation_dir - 1 - elseif key == rot_l then - player.rotation_dir = player.rotation_dir + 1 - elseif key == inc_x then - player.x_dir = player.x_dir - 1 - elseif key == dec_x then - player.x_dir = player.x_dir + 1 - elseif key == dec_y then - player.y_dir = player.y_dir - 1 - elseif key == inc_y then - player.y_dir = player.y_dir + 1 - end + modes[mode].func_key_released(key) end end @@ -198,9 +173,12 @@ function show_startscreen() local y = 1.7*win_h / 4 local dist = 90 + local holder = hs_holder + if #holder < 1 then holder = "A. Nonymous" end + -- Print write_centered("Shape Shifter", big_font, 0.7*win_h / 4, win_w) - write_centered("Current highscore is "..highscore.." seconds, held by "..hs_holder, + write_centered("Current highscore is "..highscore.." seconds, held by "..holder, medium_font, y + 2*h, win_w) write_centered("Press to start game!", medium_font, 3.2*win_h / 4, win_w) diff --git a/modes.lua b/modes.lua index 79a7897..92fc9ef 100644 --- a/modes.lua +++ b/modes.lua @@ -1,4 +1,5 @@ require("objects") +require("functions") mode_see = { func_draw = function() @@ -6,7 +7,10 @@ mode_see = { draw_shot() draw_enemies() end, + func_key_pressed = rotate_on_key, + func_key_released = stop_rotate_on_key, func_update = function(dt) + reset_movements() rotate(player, dt) update(player) update_other_objects(dt) @@ -19,9 +23,11 @@ mode_move = { func_draw = function() draw_rectangle(player) end, + func_key_pressed = move_on_key, + func_key_released = stop_move_on_key, func_update = function(dt) + reset_rotations() move(player,dt) - rotate(player, dt) update(player) update_other_objects(dt) end, @@ -35,7 +41,10 @@ mode_attack = { draw_shot() draw_enemies() end, + func_key_pressed = rotate_on_key, + func_key_released = stop_rotate_on_key, func_update = function(dt) + reset_movements() rotate(player, dt) update(player) update_other_objects(dt) @@ -57,6 +66,7 @@ function draw_enemies() end end + enemies_spawned = 0 --TODO MOVE! function update_other_objects(dt) diff --git a/values.lua b/values.lua index 9f3ee8f..0332b7e 100644 --- a/values.lua +++ b/values.lua @@ -1,15 +1,15 @@ require("slam") -- KEYMAP -rot_l = "j" -rot_r = "k" -inc_x = "d" -dec_x = "a" -inc_y = "w" -dec_y = "s" +key_right = "right" +key_left = "left" +key_up = "up" +key_down = "down" key_see = "1" key_move = "2" key_attack = "3" +-- "none", "rot", "move" +pressed_keys = {left = "none", right = "none", up = "none", down = "none"} win_w = 800 win_h = 600