-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspike.lua
More file actions
78 lines (60 loc) · 2.05 KB
/
Copy pathspike.lua
File metadata and controls
78 lines (60 loc) · 2.05 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
local Player = require("player")
local Spike = {
img = love.graphics.newImage("assets/spikes.png")
}
Spike.__index = Spike
Spike.width = Spike.img:getWidth()
Spike.height = Spike.img:getHeight()
local ActiveSpikes = {}
function Spike.new(x, y)
local instance = setmetatable({}, Spike) -- create a new instance of the Spike class
instance.x = x
instance.y = y + instance.height
instance.damage = 1
instance.scaleX = 1
instance.toBeRemoved = false
instance.physics = {}
instance.physics.body = love.physics.newBody(World, instance.x, instance.y, "static")
instance.physics.shape = love.physics.newRectangleShape(instance.width, instance.height)
instance.physics.fixture = love.physics.newFixture(instance.physics.body, instance.physics.shape)
instance.physics.fixture:setSensor(true)
instance.randomTimeOffset = math.random(0, 100)
table.insert(ActiveSpikes, instance)
end
function Spike:update(dt)
-- for i, instance in ipairs(ActiveSpikes) do
-- if CheckCollision(Player.x, Player.y, Player.width, Player.height, instance.x, instance.y, instance.width, instance.height) then
-- table.remove(ActiveSpikes, i)
-- end
-- end
end
function Spike:draw()
love.graphics.draw(self.img, self.x, self.y, 0, self.scaleX, 1 , self.width/2, self.height)
end
function Spike:drawAll()
for i, instance in ipairs(ActiveSpikes) do
instance:draw()
end
end
function Spike:updateAll(dt)
for i, instance in ipairs(ActiveSpikes) do
instance:update(dt)
end
end
function Spike.removeAll()
for i,v in ipairs(ActiveSpikes) do
v.physics.body:destroy()
end
ActiveSpikes = {}
end
function Spike.beginContact(a, b, collision)
for i, instance in ipairs(ActiveSpikes) do
if a == instance.physics.fixture or b == instance.physics.fixture then
if(a == Player.physics.fixture or b == Player.physics.fixture) then
Player:takeDamage(instance.damage)
return true
end
end
end
end
return Spike