aboutsummaryrefslogtreecommitdiff
path: root/bullets.lua
blob: 01e6dc767d1f9f87060a2093537c94205452d442 (plain)
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
bullets = {}

function addbullet(x, y, velx, vely, evil)
    local bullet = {}

    bullet.type = "bullet"
    bullet.evil = evil
    bullet.x = x
    bullet.y = y
    bullet.velx = velx
    bullet.vely = vely

    function bullet.draw(bullet)
        spr(6, bullet.x, bullet.y)
    end

    function bullet.collide(object)
        if bullet.x+4 >= object.x and bullet.x+4 <= object.x+object.w and bullet.y+4 >= object.y and bullet.y+4 <= object.y+object.h and object.inv < 0 then
            object:shot()
            del(obj, bullet)
        end
    end

    function bullet.update(bullet)
        --applying velocity
        bullet.x += bullet.velx
        bullet.y += bullet.vely

        --collision detection
        if bullet.evil then
            foreach(players, bullet.collide)
        elseif bullet.good then
            --foreach(enemies, bullet.collide)
        end

        --delete bullet if off screen
        if bullet.y > 128 or bullet.y < -8 or bullet.x > 128 or bullet.x < -8 then
            del(obj, bullet)
        end
    end

    add(obj, bullet)
    add(bullet, #obj)
end

addbullet(120, 60, -1, 0, true)