summaryrefslogtreecommitdiff
path: root/objects/room.lua
blob: 401dff11d921e445188302cdefd92686b4c0d444 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
require "data.directions"
local roomData = require "data.roomData"
local roomNames = require "data.roomNames"
local roomType = require "data.roomType"
local Door = require "objects.door"
local audio = require "utils.audio"
local mapHelper = require "utils.mapHelper"

local Room = {}
Room.__index = Room

function Room:new(name)
    local dataName = name or utils.any(roomNames)
    local data = roomData[dataName]
    local obj = {
        dataName = dataName,
        name = data.name or "Initial Room",
        type = data.type or roomType.INITIAL,
        width = data.width or 0.85,
        height = data.height or 0.8,

        objects = {
            enemies = data.enemies and utils.readObjectList(data.enemies, "enemy") or {},
            obstacles = data.obstacles and utils.readObjectList(data.obstacles, "obstacle") or {},
            loots = data.loots and utils.readObjectList(data.loots, "loot") or {},
            shots = {}
        },

        doors = {},
        isCleared = false,

        music = data.music or nil,
    }

    setmetatable(obj, Room)

    return obj
end

-- getters

function Room:getWidth()
    return self.width * love.graphics.getWidth()
end

function Room:getHeight()
    return self.height * love.graphics.getHeight()
end

function Room:getX()
    return (love.graphics.getWidth() - self:getWidth()) / 2
end

function Room:getY()
    return (love.graphics.getHeight() - self:getHeight()) / 2
end

function Room:getLocation(location, offset)
    if type(location) ~= "string" then
        error("Invalid location type: " .. type(location))
    end

    local preset = {
        topLeft = {
            x = self:getX() + offset,
            y = self:getY() + offset
        },
        topRight = {
            x = self:getX() + self:getWidth() - offset,
            y = self:getY() + offset
        },
        bottomLeft = {
            x = self:getX() + offset,
            y = self:getY() + self:getHeight() - offset
        },
        bottomRight = {
            x = self:getX() + self:getWidth() - offset,
            y = self:getY() + self:getHeight() - offset
        },
        center = {
            x = self:getX() + self:getWidth() / 2,
            y = self:getY() + self:getHeight() / 2
        },
        topCenter = {
            x = self:getX() + self:getWidth() / 2,
            y = self:getY() + offset
        },
        bottomCenter = {
            x = self:getX() + self:getWidth() / 2,
            y = self:getY() + self:getHeight() - offset
        },
        leftCenter = {
            x = self:getX() + offset,
            y = self:getY() + self:getHeight() / 2
        },
        rightCenter = {
            x = self:getX() + self:getWidth() - offset,
            y = self:getY() + self:getHeight() / 2
        },
        random = {
            x = math.random(self:getX() + offset, self:getX() + self:getWidth() - offset),
            y = math.random(self:getY() + offset, self:getY() + self:getHeight() - offset),
        }
    }

    local doorLocations = {}
    for _, door in pairs(self.doors) do
        local doorX, doorY = door.x, door.y
        local doorW, doorH = door.w, door.h

        local lookupTable = {
            [Direction.LEFT] = {
                x = doorX + doorW, y = doorY
            },
            [Direction.RIGHT] = {
                x = doorX - doorW, y = doorY
            },
            [Direction.TOP] = {
                x = doorX, y = doorY + doorH
            },
            [Direction.BOTTOM] = {
                x = doorX, y = doorY - doorH
            },
        }

        local pos = lookupTable[door.location]
        table.insert(doorLocations, { x=pos.x, y=pos.y })
    end
    preset.anyDoor = utils.any(doorLocations)

    return preset[location].x, preset[location].y
end

function Room:isDoorFull()
    return #self.doors >= 4
end

function Room:hasDoor(location)
    return self.doors[location] ~= nil
end

function Room:getDooredDirections()
    local doored = {}
    for _, door in pairs(self.doors) do
        table.insert(doored, door.location)
    end
    return doored
end

function Room:getDoorlessDirections()
    local doored = self:getDooredDirections()
    local doorless = {}
    for direction = 1, 4 do
        if not utils.contains(doored, direction) then
            table.insert(doorless, direction)
        end
    end
    return doorless
end

-- control functions

function Room:addDoor(location, to)
    if self:hasDoor(location) then
        print("door location unavailable")
        return false
    end

    local door = Door:new(location, self, to)
    self.doors[location] = door

    return door
end

function Room:removeDoor(location)
    self.doors[location] = nil
end

function Room:connect(anotherRoom, way)
    if self:isDoorFull() or anotherRoom:isDoorFull() then
        return false
    end

    -- determine directions
    local directions = {}
    if type(way) == "string" then
        directions = Way[way]
    elseif type(way) == "number" then
        directions = Way[Ways[way]]
    else
        -- if not specified, choose randomly
        local pairedDirections = mapHelper.getAvailablePairedDirections(self, anotherRoom)
        directions = utils.any(pairedDirections)
    end
    if not directions then return false end

    -- connect room with determined direction
    return self:addDoor(directions[1], anotherRoom),
        anotherRoom:addDoor(directions[2], self)
end

-- initializers

function Room:addWalls()
    local x, y, w, h = self:getX(), self:getY(), self:getWidth(), self:getHeight()
    local thickness = 2
    local walls = {}

    local borders = {
        { x = x, y = y + h / 2, w = thickness, h = h },
        { x = x + w, y = y + h / 2, w = thickness, h = h },
        { x = x + w / 2, y = y, w = w, h = thickness },
        { x = x + w / 2, y = y + h, w = w, h = thickness }
    }

    for _, b in ipairs(borders) do
        local wall = {
            objectType = "wall",
            x = b.x, y = b.y
        }
        G.BodyLifeCycleManager:create(wall, { b.w, b.h }, "static")
        table.insert(walls, wall)
    end

    return walls
end


function Room:init()
    -- handle objects that need to be placed
    for _, group in pairs(self.objects) do
        for _, object in ipairs(group) do
            if object.placeInRoom then
                object:placeInRoom(self)
            end
        end
    end

    self.walls = self:addWalls()

    -- play music
    if not self.isCleared then
        if self.music then
            audio.music(self.music)
        elseif self.music == "no" then
            audio.stopMusic()
        else
            audio.music(config.defaultMusic)
        end
    end
end

function Room:removeAllBodies()
    for _, group in pairs(self.objects) do
        for _, obj in ipairs(group) do
            G.BodyLifeCycleManager:destroy(obj.physicsBody)
        end
    end
    for _, wall in ipairs(self.walls) do
        G.BodyLifeCycleManager:destroy(wall.body)
    end
    self.objects.shots = {}
end

function Room:update(dt)
    for _, group in pairs(self.objects) do
        for _, obj in ipairs(group) do
            obj:update(dt)
        end
    end
end

return Room