summaryrefslogtreecommitdiff
path: root/objects/Obstacle.lua
blob: ee0e8181ccbe059cfe140fc0f2f7a617bc34c689 (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
require "data.directions"

local Body = require "objects.Body"

local Obstacle = {}
Obstacle.__index = Obstacle

function Obstacle:new(name, location, body)
    local obj = {
        name = name,
        presetLocation = (type(location) == "string" and location) or nil,
        x = (type(location) == "table" and location.x) or nil,
        y = (type(location) == "table" and location.y) or nil,
        body = body or Body:new("rectangle", 50, 50)
    }

    setmetatable(obj, Obstacle)

    return obj
end

function Obstacle:getBorders()
    local left = self.x
    local right = self.x + self.body.w
    local top = self.y
    local bottom = self.y + self.body.h

    return { left, right, top, bottom }
end

function Obstacle:isMet(body, currentX, currentY, dv, dir)
    -- x,y should be the predicted next location of this body
    -- dv = speed * dt * direction
    local x, y
    if dir == Direction.LEFT or dir == Direction.RIGHT then
        x = currentX + dv
        y = currentY
    elseif dir == Direction.UP or dir == Direction.DOWN then
        x = currentX
        y = currentY + dv
    end

    return self.body:collide(body, self.x, self.y, x, y)
end

return Obstacle