-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.lua
More file actions
88 lines (76 loc) · 2.58 KB
/
Copy pathentity.lua
File metadata and controls
88 lines (76 loc) · 2.58 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
86
87
88
Entity = Object:extend()
function Entity:new(x, y, image_path)
self.x = x
self.y = y
self.image = love.graphics.newImage(image_path)
self.width = self.image:getWidth()
self.height = self.image:getHeight()
self.power = 100
self.gravity = 0
self.weight = 400
self.last = {}
self.last.x = self.x
self.last.y = self.y
end
function Entity:update(dt)
self.last.x = self.x
self.last.y = self.y
end
function Entity:draw()
love.graphics.draw(self.image, self.x, self.y, 0, self.scale, self.scale)
end
function Entity:checkCollision(e)
-- e will be the other entity with which we check if there is collision.
-- self = x and e = y
return self.x < e.x + e.width and
e.x < self.x + self.width and
self.y < e.y + e.height and
e.y < self.y + self.height
end
function Entity:wasVerticallyAligned(e)
-- It's basically the collisionCheck function, but with the x and width part removed.
-- It uses last.y because we want to know this from the previous position
return self.last.y < e.last.y + e.height and self.last.y + self.height > e.last.y
end
function Entity:wasHorizontallyAligned(e)
-- It's basically the collisionCheck function, but with the y and height part removed.
-- It uses last.x because we want to know this from the previous position
return self.last.x < e.last.x + e.width and self.last.x + self.width > e.last.x
end
function Entity:resolveCollision(e)
if self:checkCollision(e) then
self.tempStrength = e.tempStrength
if self:wasVerticallyAligned(e) then
if self.x + self.width/2 < e.x + e.width/2 then
-- Replace these with the functions
self:collide(e, "right")
else
self:collide(e, "left")
end
elseif self:wasHorizontallyAligned(e) then
if self.y + self.height/2 < e.y + e.height/2 then
self:collide(e, "bottom")
else
self:collide(e, "top")
end
end
return true
end
return false
end
function Entity:collide(e, direction)
if direction == "right" then
local pushback = self.x + self.width - e.x
self.x = self.x - pushback
elseif direction == "left" then
local pushback = e.x + e.width - self.x
self.x = self.x + pushback
elseif direction == "bottom" then
local pushback = self.y + self.height - e.y
self.y = self.y - pushback
self.gravity = 0
elseif direction == "top" then
local pushback = e.y + e.height - self.y
self.y = self.y + pushback
end
end