-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.lua
46 lines (37 loc) · 991 Bytes
/
Bullet.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
local Player = require 'Player'
local Bullet = {}
Bullet.bullets = {}
Bullet.toRemove = {}
function Bullet.new(source)
local bullet = {}
bullet.w = 10
bullet.speed = 400
bullet.x = Player.x + Player.w / 2 - bullet.w / 2
bullet.y = Player.y - bullet.w
function bullet:draw()
love.graphics.setColor(1,1,1)
love.graphics.rectangle("fill", bullet.x, bullet.y, bullet.w, bullet.w)
end
Bullet.bullets[#Bullet.bullets + 1] = bullet
return bullet
end
function Bullet.update(dt)
for i = 1, #Bullet.bullets do
Bullet.bullets[i].y = Bullet.bullets[i].y - Bullet.bullets[i].speed * dt
if Bullet.bullets[i].y < -Bullet.bullets[i].w then
table.insert(Bullet.toRemove, Bullet.bullets[i])
end
end
for i = 1, #Bullet.toRemove do
for j = 1, #Bullet.bullets do
if Bullet.toRemove[i] == Bullet.bullets[j] then
table.remove(Bullet.bullets, j)
table.remove(Bullet.toRemove, i)
end
end
end
end
function Bullet.deleteAll()
Bullet.bullets = {}
end
return Bullet