-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbullets.lua
47 lines (41 loc) · 1.64 KB
/
bullets.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
bullets = {
img = love.graphics.newImage("bullet.png")
}
function shoot(dt)
if player.shoot_timer >= player.firing_speed then
bullet_create()
player.shoot_timer = 0
else
player.shoot_timer = player.shoot_timer + dt
end
end
function bullet_create()
--create bullet
table.insert(bullets, {})
bullets[#bullets].direction = player.direction
bullets[#bullets].body = love.physics.newBody(world, player.body:getX() - math.cos(bullets[#bullets].direction) * 40, player.body:getY() - math.sin(bullets[#bullets].direction) * 40, "dynamic")
bullets[#bullets].shape = love.physics.newRectangleShape(5, 20)
bullets[#bullets].fixture = love.physics.newFixture(bullets[#bullets].body, bullets[#bullets].shape)
bullets[#bullets].speed = player.body:getLinearVelocity() + 1000
bullets[#bullets].fixture:setUserData("bullet")
-- push bullet forward
bullets[#bullets].body:applyForce(-(bullets[#bullets].speed * math.cos(bullets[#bullets].direction)), -(bullets[#bullets].speed * math.sin(bullets[#bullets].direction)))
end
function bullets_update(dt)
--checks for out of bounds
for i, o in ipairs(bullets) do
if (o.body:getX() < -10) or (o.body:getX() > width + 10) or (o.body:getY() < -10) or (o.body:getY() > height + 10) then
table.remove(bullets, i)
end
if o.fixture:getUserData() == "bullet_D" then
debuxText = debugText .. "deleting" .. "\n"
o.body:destroy()
table.remove(bullets, i)
end
end
end
function bullets_draw()
for i, o in ipairs(bullets) do
love.graphics.draw(bullets.img, o.body:getX(), o.body:getY(), o.direction - 0.5 * math.pi, 1, 1, 3, 3)
end
end