-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.lua
More file actions
85 lines (76 loc) · 1.96 KB
/
Copy pathentity.lua
File metadata and controls
85 lines (76 loc) · 1.96 KB
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
Entity = Object:extend()
function Entity:new(x, y, drawable, scale)
self.x = x
self.y = y
self.scale = scale
self.isOnFloor = false
self.gravity = 4000
self.jumpVelocity = 0
self.isAffectedByGravity = true
self.collisions = {}
if not drawable
then
self.drawable = love.graphics.newImage("res/default.png")
elseif type(drawable) == "string"
then
self.drawable = love.graphics.newImage(drawable)
else
self.drawable = drawable
end
if self.scale
then
self.width = self.drawable:getWidth() * self.scale
self.height = self.drawable:getHeight() * self.scale
else
self.width = self.drawable:getWidth()
self.height = self.drawable:getHeight()
end
end
function Entity:update(dt, world)
local tx, ty
local dy = 0
if (self.isAffectedByGravity)
then
tx, ty = world:check(self, self.x, self.y - 0.1, self.filter); -- check if top touch smth
if ty == self.y
then
self.jumpVelocity = -math.abs(self.jumpVelocity)
end
if self.isOnFloor and self.jumpVelocity > 0
then
self.jumpVelocity = 0
end
tx, ty = world:check(self, self.x, self.y + 0.1, self.filter); -- check if on floor
if ty == self.y
then
self.isOnFloor = true
else
self.isOnFloor = false
end
if (not self.isOnFloor) or (self.jumpVelocity ~= 0)
then
self.jumpVelocity = self.jumpVelocity + self.gravity * dt
dy = self.jumpVelocity * dt
end
local newX, newY, cols, nb_cols = world:move(self, self.x, self.y + dy, self.filter)
if newY == self.y
then
self.isOnFloor = true
else
self.y = newY
self.isOnFloor = false
end
end
_, _, self.collisions = world:check(self, self.x, self.y, self.filter)
end
function Entity:draw()
if self.scale
then
love.graphics.draw(self.drawable, self.x, self.y, 0, self.scale, self.scale)
else
love.graphics.draw(self.drawable, self.x, self.y)
end
end
function Entity:filter(other)
return "slide"
end