Skip to content

Commit

Permalink
Merge branch 'keybinds'
Browse files Browse the repository at this point in the history
  • Loading branch information
sornas committed Nov 10, 2019
2 parents 5efbf10 + dd54633 commit 82c9abc
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 36 deletions.
87 changes: 87 additions & 0 deletions functions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require("values")

function write_centered(text, font, y, win)
local w = font:getWidth(text)
love.graphics.setFont(font)
Expand All @@ -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
37 changes: 8 additions & 29 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,7 @@ function love.keypressed(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()
Expand All @@ -64,7 +52,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
Expand All @@ -81,26 +69,14 @@ function love.keypressed(key)
end
end
elseif key == "space" then
hs_holder.." "
hs_holder = hs_holder.." "
end
end
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

Expand Down Expand Up @@ -193,9 +169,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 "..time_to_string(highscore).." seconds, held by "..hs_holder,
write_centered("Current highscore is "..time_to_string(highscore).." seconds, held by "..holder,
medium_font, y + 2*h, win_w)
write_centered("Press <space> to start game!", medium_font, 3.2*win_h / 4, win_w)

Expand Down
11 changes: 10 additions & 1 deletion modes.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
require("objects")
require("functions")

mode_see = {
func_draw = function()
draw_ellipse(player)
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)
Expand All @@ -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,
Expand All @@ -37,7 +43,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)
Expand Down
12 changes: 6 additions & 6 deletions values.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ medium_font = love.graphics.newFont("yf16font.ttf", 15)
medium_font:setFilter( "nearest", "nearest" )
big_font:setFilter( "nearest", "nearest" )

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"}

rotation_speed = math.pi * 2 -- half a lap per second

Expand Down

0 comments on commit 82c9abc

Please sign in to comment.