-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulletpool.lua
More file actions
51 lines (44 loc) · 934 Bytes
/
Copy pathbulletpool.lua
File metadata and controls
51 lines (44 loc) · 934 Bytes
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
local count = 0
bulletpool = {}
local bp = bulletpool
local meta = {} --metatable is required to prevent indexing fields with functions via 'pairs'
setmetatable(bp, meta)
meta.__index = meta
meta.add = function(bul)
if count >= config.MAX_BULLETS then
count = 0
end
count = count + 1
bp[count] = bul
end
meta.update = function(dt)
for k, v in pairs(bp) do
v:updatePos(dt)
if not player:hasCollision(v.x, v.y) then
for _, n in pairs(npcPool) do--check collisions with npc
if n:hasCollision(v.x, v.y) then
n:damage(v.damage)
bp[k] = nil
break
end
end
else
player:damage(v.damage)
bp[k] = nil
end
end
end
meta.free = function()
for k, v in pairs(bp) do
local vx, vy = camera.ToScreen(v:getPos())
if vx < 0 or vx > screen.width or vy < 0 or vy > screen.height then
bp[k] = nil
end
end
end
meta.draw = function()
for _, v in pairs(bp) do
v:draw()
end
end
return bp