-
Notifications
You must be signed in to change notification settings - Fork 69
/
util.lua
129 lines (111 loc) · 3.1 KB
/
util.lua
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
--- Collides two AABBs
-- @param b1 First bounding box
-- @param b2 Second bounding box
-- @return True if they overlap
function collideBoxes(b1,b2)
if b1.x > b2.x+b2.w or b1.x+b1.w < b2.x
or b1.y > b2.y+b2.h or b1.y+b1.h < b2.y then
return false
else
return true
end
end
--- Returns a random entry in a table
function table.random(t)
return t[math.random(#t)]
end
function math.sign(n)
if n < 0 then
return -1
elseif n > 0 then
return 1
else
return 0
end
end
function math.round(x)
return math.floor(x+0.5)
end
function collideX(self)
if self.xspeed == 0 then return end
local collision = false
local last -- Last object collision detected
for _, yoff in ipairs({self.corners[3], (self.corners[3]+self.corners[4])/2, self.corners[4]}) do
for _, xoff in ipairs({self.corners[1], self.corners[2]}) do
-- Collide with solid tiles
if map:collidePoint(self.x+xoff, self.y+yoff) then
collision = true
local cx = math.floor((self.x+xoff)/16)*16
if self.xspeed > 0 then
self.x = cx-self.corners[2]-0.0001
else
self.x = cx+16-self.corners[1]
end
end
-- Collide with objects
for i,v in ipairs(map.objects) do
if v.solid == true and self:collideBox(v:getBBox()) then
collision = true
last = v
local bbox = v:getBBox()
if self.xspeed > 0 then
self.x = bbox.x-self.corners[2]-0.0001
else
self.x = bbox.x+bbox.w-self.corners[1]+0.0001
end
end
end
end
end
return collision, last
end
function collideY(self)
if self.yspeed == 0 then return end
self.onGround = false
local collision = false
for _, yoff in ipairs({self.corners[3], self.corners[4]}) do
for _, xoff in ipairs({self.corners[1], self.corners[2]}) do
-- Collide with solid tiles
if map:collidePoint(self.x+xoff, self.y+yoff) then
collision = true
local cy = math.floor((self.y+yoff)/16)*16
if self.yspeed > 0 then
self.y = cy-self.corners[4]-0.0001
self.onGround = true
else
self.y = cy+16-self.corners[3]
end
end
end
end
return collision
end
function cap(val, min, max)
return math.max(math.min(val, max), min)
end
function wrap(val, min, max)
if val < min then val = max end
if val > max then val = min end
return val
end
function drawBox(x,y,w,h)
lg.setColor(30/255,23/255,18/255)
lg.rectangle("fill",x+1,y+1,w-2,h-2)
lg.setColor(1,1,1)
-- Draw sides
lg.draw(img.menu_box, quad.box_left, x, y+1, 0, 1, (h-2))
lg.draw(img.menu_box, quad.box_left, x+w, y+1, 0, -1, (h-2))
lg.draw(img.menu_box, quad.box_top, x+1, y, 0, (w-2), 1)
lg.draw(img.menu_box, quad.box_top, x+1, y+h, 0, (w-2), -1)
-- Draw corners
lg.draw(img.menu_box, quad.box_corner, x,y)
lg.draw(img.menu_box, quad.box_corner, x+w,y, 0, -1, 1)
lg.draw(img.menu_box, quad.box_corner, x,y+h, 0, 1, -1)
lg.draw(img.menu_box, quad.box_corner, x+w,y+h, 0, -1, -1)
end
function secondsToString(time)
local hours = string.format("%02d", math.floor(time / 60^2))
local minutes = string.format("%02d", math.floor((time % 60^2) / 60))
local seconds = string.format("%02d", math.floor(time % 60))
return hours .. ":" .. minutes .. ":" .. seconds
end