Stage2.lua 5.54 KB
Newer Older
1
-- The main part of the game
2
-- The worst code I've written
Philipp Hansch's avatar
Philipp Hansch committed
3 4 5 6

Gamestate.stage2 = Gamestate.new()
local state = Gamestate.stage2

7 8
Player = require 'Player'
Stars = require 'Stars'
Philipp Hansch's avatar
Philipp Hansch committed
9 10
local Planet = require 'Planet'
local Freighter = require 'Freighter'
11
local BigShip = require 'BigShip'
Philipp Hansch's avatar
Philipp Hansch committed
12
local Conversation = require 'Conversation'
13
local PSystems = require 'ParticleSystems'
Philipp Hansch's avatar
Philipp Hansch committed
14

15 16
player = Player.create()
stars = Stars.create()
Philipp Hansch's avatar
Philipp Hansch committed
17
local planet = Planet.create()
18
local bigShip = BigShip.create()
19
local psystems = PSystems.create()
20 21 22 23

local conversation = Conversation.create(conv_stage2_1_title,
                                        conv_stage2_1_text,
                                        conv_stage2_1_confirm)
Philipp Hansch's avatar
Philipp Hansch committed
24

25 26 27 28
local conversation2 = Conversation.create(conv_stage2_2_title,
                                        conv_stage2_2_text,
                                        conv_stage2_2_confirm)

29

30
local maxKills = 10 --this is when the game ends
31
local conv2triggerKills = 2 -- when the 2nd conversation appears
Philipp Hansch's avatar
Philipp Hansch committed
32

33 34
local Freighters = {}
local freighterCount, killCount = 0, 0
35
local drawUI, drawUI2 = true, false
Philipp Hansch's avatar
Philipp Hansch committed
36 37 38 39 40

function state:init()
    Timer.clear()
    player:load()
    stars:load()
41 42
    conversation:load()
    conversation2:load()
43
    bigShip:load()
Philipp Hansch's avatar
Philipp Hansch committed
44 45
    planet:load()

46 47
    psystems:initFTLJump()
    psystems:initExplosion()
Philipp Hansch's avatar
Philipp Hansch committed
48 49 50
end

function state:update(dt)
51
    if not drawUI and not drawUI2 then
Philipp Hansch's avatar
Philipp Hansch committed
52
        player:update(dt)
53
        jumpRechargeBar:update(dt)
Philipp Hansch's avatar
Philipp Hansch committed
54 55
    end

Philipp Hansch's avatar
Philipp Hansch committed
56
    if not drawUI2 then
57 58 59
        self:updateFreighterStatus(dt)
    end

60
    --print(freighterCount, killCount, drawUI2)
61
    if freighterCount < 4 and killCount < maxKills and not drawUI2 then
62
        local randomAdd = math.random(3, 5)
63 64
        -- add between 3 and 5 freighters
        Timer.addPeriodic(1.5, function() self:createFreighter() end, randomAdd)
65 66
        freighterCount = freighterCount + randomAdd
    end
Philipp Hansch's avatar
Philipp Hansch committed
67

Philipp Hansch's avatar
Philipp Hansch committed
68 69 70 71 72 73 74 75 76
    if killCount > maxKills and freighterCount == 0 then
        Timer.add(5, function() Gamestate.switch(Gamestate.stage3) end)
    end

    if killCount == conv2triggerKills then
        drawUI2 = true
        killCount = killCount + 1
    end

77
    psystems:update(dt)
Philipp Hansch's avatar
Philipp Hansch committed
78 79 80 81 82 83
end

function state:draw()
    stars:draw()
    planet:draw()

84
    --only draw these AFTER entering the system
Philipp Hansch's avatar
Philipp Hansch committed
85 86
    if not drawUI then
        player:draw()
87
        jumpRechargeBar:draw()
Philipp Hansch's avatar
Philipp Hansch committed
88 89 90 91 92 93 94 95 96 97 98 99
    end

    --draw freighter(s)
    for i,v in ipairs(Freighters) do
        v:draw()
    end

    if drawUI then
        love.graphics.setColor(0, 0, 0, 100)
        love.graphics.rectangle("fill", 0, 0, width, height)
        love.graphics.setColor(255, 255, 255, 255)
        conversation:draw()
100 101 102 103 104 105
    end

    if drawUI2 then
        love.graphics.setColor(0, 0, 0, 100)
        love.graphics.rectangle("fill", 0, 0, width, height)
        love.graphics.setColor(255, 255, 255, 255)
106
        conversation2:draw()
107 108

        player:draw()
109 110
    end

111
    psystems:draw()
Philipp Hansch's avatar
Philipp Hansch committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125
end

function state:keyreleased(key)
    if key == ' ' and not drawUI then
        player:fireSingle()
        player:stopFire()
    end
    if key == 'up' or key == 'w' then
        --if keys are released, slow the ship down
        player:doSlowdown()
    end
end

function state:keypressed(key)
126
    if key == 'return' and (drawUI or drawUI2) then --when UI is confirmed
127 128 129

        player:stopFire() --stop firing after UI is hidden

130
        -- only play jump animation when just arrived in system
131 132
        if killCount == 0 and drawUI then
            drawUI = false
133 134
            self:FTLJump(player.x, player.y)
        end
135 136 137 138 139

        --to hide the second conversation
        if killCount > 0 and drawUI2 then
            drawUI2 = false
        end
Philipp Hansch's avatar
Philipp Hansch committed
140
    end
141
    if key == ' ' and not drawUI and not drawUI2 then
Philipp Hansch's avatar
Philipp Hansch committed
142 143 144 145
        player:fireConstantly()
    end
end

146
function state:updateFreighterStatus(dt)
Philipp Hansch's avatar
Philipp Hansch committed
147
    for i,freighter in ipairs(Freighters) do
148
        freighter:update(dt)
Philipp Hansch's avatar
Philipp Hansch committed
149 150 151 152 153
        for j,shot in ipairs(player.Shots) do

            if shot:checkCollision(freighter) then

                if(freighter.hp > 1) then
154
                    self:hit() --play sfx
Philipp Hansch's avatar
Philipp Hansch committed
155 156 157
                    player:removeShot(j)
                    freighter.hp = freighter.hp - 1
                else
158 159
                    self:explode(freighter.x, freighter.y) -- play sfx and animation

Philipp Hansch's avatar
Philipp Hansch committed
160 161 162 163 164 165 166 167 168 169 170 171
                    freighter.visible = false
                    self:killFreighter(i)
                    player:removeShot(j)
                end
            end
        end
    end
end

function state:createFreighter()
    freighter = Freighter.create()
    freighter:load()
172
    self:FTLJump(freighter.x, freighter.y) -- play jump animation and sfx
Philipp Hansch's avatar
Philipp Hansch committed
173 174 175 176 177
    table.insert(Freighters, freighter)
end

function state:killFreighter(freighter)
    table.remove(Freighters, freighter)
178 179
    freighterCount = freighterCount - 1
    killCount = killCount + 1
Philipp Hansch's avatar
Philipp Hansch committed
180 181
end

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
---------------------------
-- Audio and particle systems
---------------------------
function state:FTLJump(x, y)
    psystems[1]:setPosition(x, y)
    psystems[1]:start()

    love.audio.play(sfx_ftl)
    love.audio.rewind(sfx_ftl)
end

function state:explode(x, y)
    psystems[2]:setPosition(x, y)
    psystems[2]:start()

    love.audio.setVolume(0.1)
    love.audio.play(sfx_freighter_explosion)
    love.audio.rewind(sfx_freighter_explosion)
    love.audio.setVolume(1.0)
Philipp Hansch's avatar
Philipp Hansch committed
201 202
end

203 204 205 206 207
function state:hit()
    love.audio.setVolume(0.1)
    love.audio.play(sfx_freighter_hit)
    love.audio.rewind(sfx_freighter_hit)
    love.audio.setVolume(1.0)
Philipp Hansch's avatar
Philipp Hansch committed
208
end