summaryrefslogtreecommitdiff
path: root/logic/map.lua
blob: df1d386097a3d4cd88a420777a105c941c2e3a38 (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
111
112
113
local Room = require "objects.room"

require "data.directions"
local roomNames = require "data.roomNames"
local roomType = require "data.roomType"

map = {}

-- control functions

function map.switchRoom(to)
    local destination

    if type(to) == "table" then
        destination = to
    elseif type(to) == "number" then
        destination = G.currentRoom.doors[Direction[to]]
    end

    destination:init()
    G.currentRoom = destination
    G.enemies = G.currentRoom.enemies
end

function map.generate(roomCount, initial)
    local firstRoom = Room:new(initial and "initialRoom" or utils.any(roomNames))
    local rooms = { firstRoom }
    local availableRooms = { firstRoom }

    for i = 2, roomCount do
        local newRoom = Room:new(utils.any(roomNames))

        -- select an available room and connect
        local prevRoom = utils.any(availableRooms)
        if not newRoom:connect(prevRoom) then
            print("room connection failed")
        end
        if prevRoom:isDoorFull() then
            table.remove(availableRooms, utils.indexof(availableRooms, prevRoom))
        end

        table.insert(rooms, newRoom)
        table.insert(availableRooms, newRoom)
    end

    return rooms
end

function map.continue(roomCount, from)
    -- continue game by extending the map
    local newMap = map.generate(roomCount or 5)
    local room = from or G.currentRoom

    -- try extending from the last room
    if not room:connect(newMap[1]) then
        -- if not doorless direction available in this room
        -- try extend from a random Room
        repeat
            room = utils.any(G.allRooms)
            if room ~= from and not room:isDoorFull() then
                room:connect(newMap[1])
                break
            end
        until false
    end

    for _, newRoom in ipairs(newMap) do
        table.insert(G.allRooms, newRoom)
    end
end

-- boolean functions

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

    -- check if room is cleared
    if #G.enemies==0 and not room.isCleared then
        room.isCleared = true
        if room.type ~= roomType.INITIAL then
            G.roomCleared = G.roomCleared + 1
        end
        print "room cleared"

        local unclearedRooms = {}
        for _, room in ipairs(G.allRooms) do
            if not room.isCleared then
                table.insert(unclearedRooms, room)
            end
        end

        print("uncleared rooms: ", #unclearedRooms)
    end

    -- extend map if all rooms are cleared
    if map.allCleared() then
        print "all cleared and try to continue"
        map.continue()
    end
end