summaryrefslogtreecommitdiff
path: root/utils/utils.lua
blob: 047434a27ee1559a9cd2b2b3aeb525442c5ddf53 (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
require "data.directions"

utils = {}

-- other utilities

function utils.randomPosition()
    return math.random(love.graphics.getWidth()), math.random(love.graphics.getHeight())
end

function utils.resetGraphics()
   love.graphics.setColor(1,1,1)
end

function utils.copy(t, deep)
    local copy = {}

    for k, v in pairs(t) do
        if deep and type(v) == "table" then
            copy[k] = utils.copy(v, true)
        else
            copy[k] = v
        end
    end

    return copy
end

function utils.distance(x1, y1, x2, y2)
    local dx = x1 - x2
    local dy = y1 - y2
    return math.sqrt(dx * dx + dy * dy)
end

function utils.any(tbl)
    return tbl[math.random(#tbl)]
end

function utils.indexof(tbl, value)
    for i, v in ipairs(tbl) do
        if v == value then
            return i
        end
    end
    return nil
end

function utils.contains(tbl, x)
    for _, v in pairs(tbl) do
        if v == x then
            return true
        end
    end
    return false
end

function utils.whoseBody(physicsBody)
    -- check for objects in room
    local objects = G.currentRoom.objects
    for _, group in pairs(objects) do
        for _, object in pairs(group) do
            if object.physicsBody == physicsBody then
                return object
            end
        end
    end

    -- check for player
    if G.player.physicsBody == physicsBody then
        return G.player
    end

    -- check for walls
    for _, wall in pairs(G.currentRoom.walls) do
        if wall.physicsBody == physicsBody then
            return wall
        end
    end

    -- check for doors
    for _, door in pairs(G.currentRoom.doors) do
        if door.physicsBody == physicsBody then
            return door
        end
    end

    return nil
end

function utils.readObjectList(data, objectName)
    local list = {}
    local Enemy = require("objects/enemy")
    local Obstacle = require("objects/obstacle")
    for _, object in ipairs(data) do
        local objectType = object[1]
        local objectPosition = object[2]
        local obj = objectName == "enemy" and Enemy:new(objectType, objectPosition) or objectName == "obstacle" and Obstacle:new(objectType, objectPosition)
        table.insert(list, obj)
    end
    return list
end

function utils.atan2(y, x)
    if x > 0 then
        return math.atan(y / x)
    elseif x < 0 and y >= 0 then
        return math.atan(y / x) + math.pi
    elseif x < 0 and y < 0 then
        return math.atan(y / x) - math.pi
    elseif x == 0 and y > 0 then
        return math.pi / 2
    elseif x == 0 and y < 0 then
        return -math.pi / 2
    else
        return 0  -- Undefined case (x = 0, y = 0)
    end
end

function utils.angleToDirection(angle)
    local dir
    if angle >= -math.pi / 4 and angle < math.pi / 4 then
        dir = Direction.LEFT
    elseif angle >= math.pi / 4 and angle < 3 * math.pi / 4 then
        dir = Direction.UP
    elseif angle >= -3 * math.pi / 4 and angle < -math.pi / 4 then
        dir = Direction.DOWN
    else
        dir = Direction.RIGHT
    end

    return dir
end