Don't allow aiming backwards

main
Tyler Scott 2022-02-13 09:23:36 +07:00
parent c03a9462a9
commit 444e4305c5
1 changed files with 27 additions and 20 deletions

View File

@ -177,13 +177,9 @@ local function move_bubble(x, y, velocity_x, velocity_y)
} }
end end
local function generate_aim_guide(towards_x, towards_y) local function generate_aim_guide(angle)
-- TODO: don't allow aiming backwards local velocity_x = math.cos(angle) * game.bubble_speed
local diff_x = towards_x - game.launcher_x local velocity_y = math.sin(angle) * game.bubble_speed
local diff_y = towards_y - game.launcher_y
local dist = math.sqrt(diff_x * diff_x + diff_y * diff_y)
local velocity_x = diff_x / dist * game.bubble_speed
local velocity_y = diff_y / dist * game.bubble_speed
local new_x = game.launcher_x local new_x = game.launcher_x
local new_y = game.launcher_y local new_y = game.launcher_y
local radius = 5 local radius = 5
@ -354,7 +350,7 @@ local function start_level()
game.show_aim_guide = true game.show_aim_guide = true
game.aim_guide = {} game.aim_guide = {}
generate_aim_guide(game.window_width / 2, game.window_height / 2) generate_aim_guide(game.launcher_rotation)
game.bursting_bubbles = {} game.bursting_bubbles = {}
game.falling_bubbles = {} game.falling_bubbles = {}
@ -527,7 +523,7 @@ function love.update(dt)
game.ceiling_drop_tween = nil game.ceiling_drop_tween = nil
end end
if game.show_aim_guide then if game.show_aim_guide then
generate_aim_guide(love.mouse:getX(), love.mouse:getY()) generate_aim_guide(game.launcher_rotation)
end end
end end
@ -632,7 +628,7 @@ function love.update(dt)
end end
if game.show_aim_guide then if game.show_aim_guide then
generate_aim_guide(love.mouse:getX(), love.mouse:getY()) generate_aim_guide(game.launcher_rotation)
end end
-- calculate remaining bubble types -- calculate remaining bubble types
@ -952,13 +948,13 @@ function love.mousepressed(x, y, button, is_touch, presses)
game.current_bubble.y == game.launcher_y and game.current_bubble.y == game.launcher_y and
game.current_bubble.velocity_x == 0 and game.current_bubble.velocity_x == 0 and
game.current_bubble.velocity_y == 0 then game.current_bubble.velocity_y == 0 then
local diff_x = x - game.launcher_x
local diff_y = y - game.launcher_y
local dist = math.sqrt(diff_x * diff_x + diff_y * diff_y)
-- TODO: don't allow shooting backwards
game.current_bubble.velocity_x = diff_x / dist * game.bubble_speed -- use the angle from mousemoved to calculate the velocity so that shooting
game.current_bubble.velocity_y = diff_y / dist * game.bubble_speed -- backwards is not possible
game.current_bubble.velocity_x =
math.cos(game.launcher_rotation) * game.bubble_speed
game.current_bubble.velocity_y =
math.sin(game.launcher_rotation) * game.bubble_speed
game.bubbles_launched = game.bubbles_launched + 1 game.bubbles_launched = game.bubbles_launched + 1
@ -973,13 +969,24 @@ function love.mousemoved(x, y, dx, dy, is_touch)
if not game.game_over then if not game.game_over then
local diff_x = x - game.launcher_x local diff_x = x - game.launcher_x
local diff_y = y - game.launcher_y local diff_y = y - game.launcher_y
local rotation = math.atan2(diff_y, diff_x) local angle = math.atan2(diff_y, diff_x)
if rotation >= -tau * 7 / 16 and rotation <= -tau / 16 then
game.launcher_rotation = rotation -- force angle to be positive
if y < game.launcher_y then
angle = angle + tau
end end
-- don't allow aiming backwards
if (angle >= 0 and angle <= tau / 4) or angle >= tau * 25 / 26 then
angle = tau * 25 / 26
elseif angle >= tau / 4 and angle <= tau * 14 / 26 then
angle = tau * 14 / 26
end
game.launcher_rotation = angle
if game.show_aim_guide then if game.show_aim_guide then
generate_aim_guide(x, y) generate_aim_guide(game.launcher_rotation)
end end
end end
end end