-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamicClass.lua
368 lines (305 loc) · 10.3 KB
/
dynamicClass.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
local g = 10
local tick = 1/60
local runSpeed = 120 -- this is max, don't use more as it could fail on collisions when pushing 2+ objects
local deathZone = {-1000, -2000, 1000, 2000}
local collisionDelta = 7.5 -- do not change, as it could break collision detection
local stuckPreventionDelta = 7 -- do not change, as it could cause some visual drift on the edges of the platforms
require("common")
Dynamic = {}
function Dynamic:new(id, x, y, shape, width, height, baseSpeed, maxSpeed, angle, action, obstacles)
local o = {}
setmetatable(o, self)
self.__index = self
o.id = id or nil
o.type = "dynamic"
o.x = x or 0
o.y = y or 0
o.shape = shape or "rectangle"
o.width = width or 50
o.height = height or 50
o.square = o.width * o.height
o.baseSpeed = baseSpeed or 0
o.maxSpeed = maxSpeed or 10
o.action = action or "freeFall" -- | throwUp | throwAngle | stop
o.obstacles = obstacles or {}
o.angle = 0 -- in rads
o.time = 0
o.fixX = 0
o.fixY = 0
o.throwAngleTimeMultiplier = 10
o.statusL = 0
o.statusT = 0
o.statusR = 0
o.statusB = 0
o.platform = {}
o.acc = 0
o.direction = nil
o.isJump = false
o.isMovable = false
-- proj collisions test
o.isDead = false
-----------------------
o.deathZone = deathZone
return o
end
function Dynamic:setIsMovable(isMovable)
self.isMovable = isMovable
end
function Dynamic:setDeathZone(deathZoneTable)
self.deathZone = deathZoneTable
end
function Dynamic:setUpdateData(
x,
y,
width,
height,
baseSpeed,
maxSpeed,
action,
angle,
time,
fixX,
fixY,
throwAngleTimeMultiplier,
statusL,
statusT,
statusR,
statusB,
direction,
isJump,
platformX,
platformY,
platformWidth,
platformHeight
)
self.x = tonumber(x)
self.y = tonumber(y)
self.width = tonumber(width)
self.height = tonumber(height)
self.baseSpeed = tonumber(baseSpeed)
self.maxSpeed = tonumber(maxSpeed)
self.action = action
self.angle = tonumber(angle)
self.time = tonumber(time)
self.fixX = tonumber(fixX)
self.fixY = tonumber(fixY)
self.throwAngleTimeMultiplier = tonumber(throwAngleTimeMultiplier)
self.statusL = tonumber(statusL)
self.statusT = tonumber(statusT)
self.statusR = tonumber(statusR)
self.statusB = tonumber(statusB)
self.direction = direction
self.isJump = numToBool(tonumber(isJump))
self.platform.x = tonumber(platformX)
self.platform.y = tonumber(platformY)
self.platform.width = tonumber(platformWidth)
self.platform.height = tonumber(platformHeight)
end
function Dynamic:update(dt, obstacles, direction, updateCallbacks)
if direction ~= nil then
self.direction = direction
end
-- print("----" .. self.id .."-----ACTION: " .. self.action .. " statusB: " .. self.statusB .. " statusL: " .. self.statusL .. " statusR: " .. self.statusR .. " statusT " .. self.statusT)
-- print(self.direction)
-- print(self.isJump)
-- stuck prevention
if self.isMovable then
if self.statusB == 1 and self.statusL == 1 then
self.x = self.x + 1
self.y = self.y - 0.5
end
if self.statusB == 1 and self.statusR == 1 then
self.x = self.x - 1
self.y = self.y - 0.5
end
if self.statusB == 1
and self.platform.y ~= nil
then
if self.y + self.height + stuckPreventionDelta > self.platform.y then -- it's 7, NOT 7.5, as 7.5 causes some visual drift (WHY?)
self.y = self.y - 0.1
end
end
end
-------------------
if self.statusB == 1 then
if (self.x + collisionDelta < self.platform.x + self.platform.width and self.x + self.width - collisionDelta > self.platform.x)
or
(self.platform.x + collisionDelta < self.x + self.width and self.platform.x + self.platform.width - collisionDelta > self.x)
then
if self.direction == "left" and self.statusL ~= 1 then
self.x = self.x - runSpeed * dt
self.statusR = 0
updateCallbacks[self.direction](self, dt)
elseif self.direction == "right" and self.statusR ~= 1 then
self.x = self.x + runSpeed * dt
self.statusL = 0
updateCallbacks[self.direction](self, dt)
end
else
self.statusB = 0
self.action = "freeFall"
end
self.platform.x = 0
self.platform.y = 0
self.platform.width = 0
self.platform.height = 0
end
if self.action == "topBlocked" then
if self.statusB == 1 then
self.action = "stop"
else
self.action = "freeFall"
if self.baseSpeed > self.maxSpeed then
self.baseSpeed = 0
end
end
end
if self.action == "rightBlocked" or self.action == "leftBlocked" then
if self.statusB == 1 then
self.action = "stop"
else
self.action = "freeFall"
if self.baseSpeed > self.maxSpeed then
self.baseSpeed = 0
end
end
end
if self.action == "stop" then
self.baseSpeed = 0
end
if self.action == "freeFall" then
self:freeFallDelta(dt) -- без разницы вызывать собственный метод через self./self:
elseif self.action == "throwUp" then
self:throwUpDelta(dt) -- или через Dynamic.
if self.baseSpeed <= 0 then
self.action = "freeFall"
self:freeFallDelta(dt) -- но при вызове через "." нужно передавать в него self
end
elseif self.action == "throwAngle" then
self:throwAngleDelta(dt, updateCallbacks)
end
Dynamic.deathZoneCheck(self)
Dynamic.detectCollision(self, obstacles)
end
function Dynamic:deathZoneCheck()
if self.x < self.deathZone[1]
or self.x > self.deathZone[3]
or self.y < self.deathZone[2]
or self.y > self.deathZone[4]
then
self.isDead = true
end
end
function Dynamic:freeFallDelta(t)
self.isJump = false
if self.baseSpeed < self.maxSpeed then
speed = self.baseSpeed + g*t
self.y = self.y + speed
else
speed = self.baseSpeed
self.y = self.y + speed
end
self.baseSpeed = speed
self.statusL = 0
self.statusR = 0
end
function Dynamic:throwUpDelta(t)
if self.baseSpeed > 0 then
speed = self.baseSpeed - g*t
self.y = self.y - speed
self.baseSpeed = speed
end
end
function Dynamic:throwUp(v)
self.isJump = true
if self.action ~= "topBlocked" then
self.baseSpeed = v
self.action = "throwUp"
self.statusB = 0
end
end
-- function Dynamic.throwAngleDelta(self, t, callbacks) -- it works too
function Dynamic:throwAngleDelta(t, callbacks)
local angleDeg = self.angle * 180 / math.pi
if angleDeg < 90 then
self.direction = "right"
callbacks[self.direction](self, t)
elseif angleDeg > 90 then
self.direction = "left"
callbacks[self.direction](self, t)
end
self.time = self.time + t*self.throwAngleTimeMultiplier
-- print("--------BEFORE--------- self.throwAngleTimeMultiplier " .. self.throwAngleTimeMultiplier)
if self.throwAngleTimeMultiplier > 1 then
self.throwAngleTimeMultiplier = self.throwAngleTimeMultiplier - t*1.5
end
-- print("--------AFTER--------- self.throwAngleTimeMultiplier " .. self.throwAngleTimeMultiplier)
-- print("---------- self.time " .. self.time)
local speedX = self.baseSpeed*math.cos(self.angle)*self.time
local speedY = (self.baseSpeed*math.sin(self.angle)*self.time - (g*self.time^2)/2)
-- print("---------- self.baseSpeed " .. self.baseSpeed)
-- print("---------- speedX " .. speedX .. " speedY " .. speedY)
self.x = self.fixX + speedX
self.y = self.fixY - speedY
end
function Dynamic:throwAngle(v, alpha, throwAngleTimeMultiplier)
self.isJump = true
if self.action ~= "topBlocked" then
if alpha < 90 and self.statusR ~= 1 and self.action ~= "rightBlocked" then
self:applyAngleMovement(v, alpha, throwAngleTimeMultiplier)
self.statusL = 0
elseif alpha > 90 and self.statusL ~= 1 and self.action ~= "leftBlocked" then
self:applyAngleMovement(v, alpha, throwAngleTimeMultiplier)
self.statusR = 0
end
end
end
function Dynamic:applyAngleMovement(v, alpha, throwAngleTimeMultiplier)
self.fixX = self.x
self.fixY = self.y
self.angle = alpha*math.pi/180
self.baseSpeed = v
self.action = "throwAngle"
self.time = 0
self.throwAngleTimeMultiplier = throwAngleTimeMultiplier or 10
self.statusB = 0
-- print("--------INIT--------- self.throwAngleTimeMultiplier " .. self.throwAngleTimeMultiplier)
end
function Dynamic:detectCollision(obstacles)
for i,o in ipairs(obstacles) do
if o.id ~= self.id then
-- AABB
if (self.x - collisionDelta < o.x + o.width and
self.x + self.width + collisionDelta > o.x and
self.y - collisionDelta < o.y + o.height and
self.y + self.height + collisionDelta > o.y)
then
-------
if o.y + o.height <= self.y + collisionDelta then
self.statusT = 1
self.action = "topBlocked"
elseif o.x + o.width <= self.x + collisionDelta then
self.statusL = 1
self.action = "leftBlocked"
elseif o.x >= self.x + self.width - collisionDelta then
self.statusR = 1
self.action = "rightBlocked"
elseif self.y + self.height - collisionDelta < o.y then
self.statusB = 1
self.action = "stop"
self.isJump = false
self.platform.x = o.x
self.platform.y = o.y
self.platform.width = o.width
self.platform.height = o.height
end
end
end
end
end
function Dynamic:draw()
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
function Dynamic:handleProj()
self.isDead = true
end