-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic_entity.lua
65 lines (50 loc) · 1.5 KB
/
dynamic_entity.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
require "entity"
DynamicEntity = Entity:extend()
local physicsMult = 65;
function DynamicEntity:new(x, y, width, height, image, world, maxVelX, maxVelY)
DynamicEntity.super.new(self, x, y, width, height, image, world, "dynamic_entity")
self.maxVelX = maxVelX;
self.maxVelY = maxVelY;
self.xVel = 0;
self.yVel = 0;
self.direction = 1;
self.grounded = false;
GRAVITY = world.gravity or 9.8
end
function DynamicEntity:new(x, y, width, height, image, world, maxVelX, maxVelY, ent_name, mass)
DynamicEntity.super.new(self, x, y, width, height, image, world, ent_name)
self.maxVelX = maxVelX;
self.maxVelY = maxVelY;
self.xVel = 0;
self.yVel = 0;
self.direction = 1;
self.grounded = false;
self.mass = mass or 1
GRAVITY = world.gravity or 9.8
end
function DynamicEntity:checkCols(cols)
self.grounded = false
for i,v in ipairs (cols) do
if cols[i].normal.y == -1 then
self.yVel = 0
self.grounded = true
elseif cols[i].normal.y == 1 then
self.yVel = -self.yVel/4
end
if cols[i].normal.x ~= 0 then
self.xVel = 0
end
end
end
function DynamicEntity:updatePhysics(dt)
if self.grounded == true then
self.xVel = self.xVel - 50 * dt * physicsMult
else
self.xVel = self.xVel - (8 * self.mass) * dt * physicsMult
end
self.yVel = self.yVel + (GRAVITY) * dt * physicsMult
if self.xVel > self.maxVelX then self.xVel = self.maxVelX end
if self.xVel < 0 then self.xVel = 0 end
self.x = self.x + self.direction*self.xVel*dt
self.y = self.y + (self.yVel)*dt
end