summaryrefslogtreecommitdiff
path: root/logic/controller.lua
blob: 302d764bf60279efa71bb151dec8a01c8165008b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require "data.directions"

local controller = {}

function controller.ctrlKeyPressed()
    local keydown = love.keyboard.isDown
    return keydown("lgui") or keydown("rgui") or keydown("lctrl") or keydown("rctrl")
end

local function playerChangeNextBullet(key)
    if key == "q" then
        if G.player.nextBullet == 1 then
            G.player.nextBullet = #G.player.hearts
        else
            G.player.nextBullet = G.player.nextBullet -1
        end
    elseif key == "e" then
        if G.player.nextBullet == #G.player.hearts then
            G.player.nextBullet = 1
        else
            G.player.nextBullet = G.player.nextBullet +1
        end
    end

    -- shortcut: press number key to move to a specific index
    local i = tonumber(key)
    if i ~= nil then
        G.player.nextBullet = i
    end

    -- shortcut: press ctrl/command + q/e
    -- to quickly move to the start/end of the heart sequence
    if controller.ctrlKeyPressed() then
        if key == "q" then
            G.player.nextBullet = 1
        elseif key == "e" then
            G.player.nextBullet = #G.player.hearts
        end
    end
end

local function playerPressKeysToMoves(dt)
    local moves = {
        { key = "d", facing = Direction.RIGHT, delta = 1, axis = "x" },
        { key = "a", facing = Direction.LEFT, delta = -1, axis = "x" },
        { key = "w", facing = Direction.TOP, delta = -1, axis = "y" },
        { key = "s", facing = Direction.BOTTOM, delta = 1, axis = "y" }
    }

    -- calculate velocity
    local v = { x=0, y=0 }
    for _, move in ipairs(moves) do
        if love.keyboard.isDown(move.key) then
            G.player.facing = move.facing
            v[move.axis] = move.delta * G.player.speed
        end
    end

    G.player:move(v)
end

local function playerClickMouseToShoot(dt)
    if love.mouse.isDown(1) then
        if G.player:ammoCooling() then return end

        local target = {
            x = love.mouse.getX(),
            y = love.mouse.getY()
        }
        G.player:shoot(target)
    else
        -- if player stopped pressing mouse
        -- reset charging timer
        G.player.chargingStarted = nil
    end
end

local function playerClickMouseToPunch(x, y)
    G.player:punch({ x = x, y = y})
end

-- settings

local function switchLanguageSetting(key)
    if key == "l" and controller.ctrlKeyPressed() then
        config.lang = config.lang == "en" and "zh" or "en"
    end
end

-- entry functions

-- controller that needs constant press check
function controller.update(dt)
    playerPressKeysToMoves(dt)
    playerClickMouseToShoot(dt)
end

-- controller that requires one key press
function controller.keypressed(key)
    playerChangeNextBullet(key)
    switchLanguageSetting(key)
end

function controller.mousereleased(x, y, button)
    if button == 2 then
        playerClickMouseToPunch(x, y)
    end
end

return controller