summaryrefslogtreecommitdiff
path: root/objects/Door.lua
blob: 81c7b4cfe48c15b2c905781b592d974bacd9f119 (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
local Body = require "objects.Body"
require "data.directions"

local Door = {}
Door.__index = Door

local function calculateDoorLocation(obj)
    local roomX, roomY = obj.within:getX(), obj.within:getY()
    local roomW, roomH = obj.within:getWidth(), obj.within:getHeight()
    local loc, size, thickness = obj.location, obj.size, obj.thickness

    if loc == Direction.LEFT or loc == Direction.RIGHT then
        obj.x = (loc == Direction.LEFT) and (roomX - thickness / 2) or (roomX + roomW - thickness / 2)
        obj.y = (love.graphics.getHeight() - size) / 2
        obj.body = Body:new("rectangle", thickness, size)
    elseif loc == Direction.TOP or loc == Direction.BOTTOM then
        obj.x = (love.graphics.getWidth() - size) / 2
        obj.y = (loc == Direction.TOP) and (roomY - thickness / 2) or (roomY + roomH - thickness / 2)
        obj.body = Body:new("rectangle", size, thickness)
    end
end

function Door:new(location, within, to)
    local obj = {
        location = location,
        size = config.graphics.doorSize,
        thickness = config.graphics.doorThickness,

        within = within,
        to = to,

        x = nil, -- location and size is calculated afterwards
        y = nil,
        body = nil,
    }

    setmetatable(obj, Door)

    calculateDoorLocation(obj)

    return obj
end

function Door:setTo(room)
    self.to = room
end

return Door