summaryrefslogtreecommitdiff
path: root/logic/map.lua
blob: 01091ebb0a9005a1367ea43434c72c2e199487ed (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
require "data.directions"
local mapGen = require "logic.MapGenerator"
local roomType = require "data.roomType"
local translator = require "i18n.translator"
local audio = require "utils.audio"
local MobSpawn = require "logic.MobSpawn"

local map = {}

-- control functions

function map.switchRoom(destination)
    -- unload current room
    G.currentRoom:unload()

    -- load new room
    destination:init()
    G.currentRoom = destination

    if speaker.clearOnSwitchingRoom then
        speaker.clear()
    end
end

-- handle room clearance

function map.allCleared()
    local allCleared = true
    for _, room in ipairs(G.allRooms) do
        if not room.isCleared then
            allCleared = false
            break
        end
    end
    return allCleared
end

-- entry functions

function map.update()
    local room = G.currentRoom

    -- if room's cleared once or in a wave currently
    -- try to summon mob waves
    local waveGoingOn = room.currentWave and room.currentWave <= MobSpawn.waveNumber(room)
    if (#room.objects.enemies==0 or waveGoingOn) and not room.isCleared then
        -- do not clear room if beforeClear event is not finished
        if room.type == roomType.COMBAT
            and not MobSpawn.waves(G.currentRoom) then
            return
        end
        -- if after beforeClear event, there no enemy left_x
        -- then consider room cleared
        if #room.objects.enemies==0 and not room.currentWave then
            room.isCleared = true
            -- handle post-clear actions
            if room.type ~= roomType.INITIAL then
                G.roomCleared = G.roomCleared + 1
                audio.play("levelComplete")
            end
        end
    end

    -- extend map if all rooms are cleared
    if map.allCleared() then
        mapGen.continue()
        speaker.speak(translator.T("newDoor"))
    end

    -- notify user if one's entered a new map
    if G.currentRoom.type == roomType.INITIAL and
        G.mapExpanded then
        speaker.speak(translator.T("newRealm"))
        G.mapExpanded = false
    end
end

return map