summaryrefslogtreecommitdiff
path: root/ai.lua
blob: 3900af9f7052f9a111a7eac9494a122c551812d7 (plain) (blame)
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
61
62
63
64
65
66
AiController = {}
AiController.__index = AiController

function AiController:new(controlling)
    return setmetatable({
        controlling = controlling,
        time = 0
    }, AiController)
end

function AiController:follow(target)
    self.target = target
end

function AiController:update(dt)
    if self.target ~= nil and not self.target.knockback then
        diff = self.target.position - self.controlling.position
        self.controlling.velocity = diff:normal() * 20
        self.controlling:transition_to(States.Walk)
    end
    self.time += dt
    if self.time >= 5 then
        self:shoot()
        self.time = 0
    end
end

function gliding_gun()
    _glider_damage = { amount = 2, knockback = { magnitude = 300, time = 0.070 } }
    GlidingGunBuilder = EntityBuilder:new(World)
    function GlidingGunBuilder:transition_state()
    end
    return GlidingGunBuilder:b_type(Entities.Bullet)
        :b_line_of_sight(vec2(1, 0))
        :b_position(vec2(100, 100))
        :b_health()
        :b_render_order(0)
        :b_collidable()
        :b_live_for(2)
        :b_velocity(vec2(-40, 0))
        :b_collision_bounds(vec2(3, 4), vec2(5, 6))
        :b_add_state(
            States.Active, {
                animation = {
                    pos_y = { sequence = { 75, 76, 77, 78 }, dt = 0.10 },
                    neg_y = { sequence = { 75, 76, 77, 78 }, dt = 0.10, reflect = vec2(true, true) },
                    pos_x = { sequence = { 75, 76, 77, 78 }, dt = 0.10 },
                    neg_x = { sequence = { 75, 76, 77, 78 }, dt = 0.10, reflect = vec2(true, true) }
                }
            }
        )
        :b_damage(_glider_damage)
        :b_state(States.Active)
        :build()
end

function AiController:shoot()
    diff = (self.target.position - self.controlling.position):normal() * 50
    a, b, c = gliding_gun(), gliding_gun(), gliding_gun()
    a.position = vec2(self.controlling.position)
    a.velocity = diff:clockwise_rotate(-0.05)
    b.position = vec2(self.controlling.position)
    b.velocity = diff:clockwise_rotate(0.05)
    c.position = vec2(self.controlling.position)
    c.velocity = diff
end