-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollisions.lua
159 lines (138 loc) · 2.93 KB
/
collisions.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
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
require "global"
function detect_collisions()
for i = 1, #entities do
for j = 1, #entities do
if j ~= i then
local e1 = entities[i]
local e2 = entities[j]
if e1 and e2
and e1.on_collide
and e1.x < e2.x + e2.width
and e1.x + e1.width > e2.x
and e1.y < e2.y + e2.height
and e1.height + e1.y > e2.y
then
local e1cx = e1.x + e1.width / 2.0
local e2cx = e2.x + e2.width / 2.0
local dx
if e1cx < e2cx then
dx = e2.x - (e1.x + e1.width)
else
dx =(e2.x + e2.width) - e1.x
end
local e1cy = e1.y + e1.height / 2.0
local e2cy = e2.y + e2.height / 2.0
local dy
if e1cy < e2cy then
dy = e2.y - (e1.y + e1.height)
else
dy = (e2.y + e2.height) - e1.y
end
e1:on_collide(e1, e2, dx, dy)
end
end
end
end
end
function solid_collisions(e1)
for j = 1, #solids do
local e2 = solids[j]
if e1 and e2
and e1.on_collide
and e1.x < e2.x + e2.width
and e1.x + e1.width > e2.x
and e1.y < e2.y + e2.height
and e1.height + e1.y > e2.y
then
local e1cx = e1.x + e1.width / 2.0
local e2cx = e2.x + e2.width / 2.0
local dx
if e1cx < e2cx then
dx = e2.x - (e1.x + e1.width)
else
dx =(e2.x + e2.width) - e1.x
end
local e1cy = e1.y + e1.height / 2.0
local e2cy = e2.y + e2.height / 2.0
local dy
if e1cy < e2cy then
dy = e2.y - (e1.y + e1.height)
else
dy = (e2.y + e2.height) - e1.y
end
e1:on_collide(e1, e2, dx, dy)
end
end
end
function solid_at(x, y, exclude)
for i = 1, #solids do
local e = solids[i];
if (e.type == "ground" or e.type == "bridge" or e.type == "fatknight" and e.hp > 0)
and e ~= exclude
and x >= e.x and x < e.x + e.width
and y >= e.y and y < e.y + e.height
then
return true;
end
end
return false;
end
function ground_at(x, y, exclude)
for i = 1, #solids do
local e = solids[i];
if (e.type == "ground")
and e ~= exclude
and x >= e.x and x < e.x + e.width
and y >= e.y and y < e.y + e.height
then
return true;
end
end
return false;
end
function bridge_at(x, y, exclude)
for i = 1, #solids do
local e = solids[i];
if (e.type == "bridge")
and e ~= exclude
and x >= e.x and x < e.x + e.width
and y >= e.y and y < e.y + e.height
then
return true;
end
end
return false;
end
function ladder_at(x, y, exclude)
for i = 1, #entities do
local e = entities[i];
if (e.type == "ladder")
and e ~= exclude
and x >= e.x and x < e.x + e.width
and y >= e.y and y < e.y + e.height
then
return true;
end
end
return false;
end
function object_collide(entity, type)
for i = 1, #entities do
for j = 1, #entities do
if j ~= i then
local e1 = entities[i]
local e2 = entities[j]
if e1 == entity
and e2.type == type
and e1.x < e2.x + e2.width
and e1.x + e1.width > e2.x
and e1.y < e2.y + e2.height
and e1.height + e1.y > e2.y
then
return e2
end
end
end
end
return false
end