-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet_factory.lua
60 lines (46 loc) · 1.83 KB
/
bullet_factory.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- bullet returns a constructor for a bullet type
BulletFactory = function (speed, w, h, z, damage, color, name)
return function (x, y, owner, direction)
local entity = Entity(x, y - h/2, w, h, z)
local obstacleFilter = entity.getFilterFor('isObstacle')
local max_speed = speed
local current_speed = 0
local acceleration = 0.25
entity.set('isBullet', true)
entity.set("owner_id", owner.get("id"))
entity.set("damage", damage)
entity.draw = function ()
love.graphics.setColor(color)
love.graphics.rectangle("fill", entity.getX(), entity.getY(), w, h)
love.graphics.setColor(COLOR.WHITE)
end
entity.update = function (dt, world)
entity.setX(entity.getX() + direction*speed)
world.bump:move(entity, entity.getX(), entity.getY())
entity.resolveObstacleCollide(world)
-- remove bullets as they fly off the screen
if entity.getX() > global.screen_width or entity.getX() < 0 then
owner.incrementAmmo(name)
entity._unregister()
end
end
entity.resolveObstacleCollide = function(world)
local new_x, new_y = entity.getX(), entity.getY()
local cols, len = world.bump:check(entity, new_x, new_y, obstacleFilter)
if len == 0 then
world.bump:move(entity, new_x, new_y)
else
owner.incrementAmmo(name)
entity._unregister()
end
end
entity.resolveEntityCollide = function ()
owner.incrementAmmo(name)
-- only pellets vanish after a hit
if name == "pellet" then
entity._unregister()
end
end
return entity
end
end