summaryrefslogtreecommitdiffhomepage
path: root/ExampleGenerator.cpp
blob: 4c86d947f5f8dba9048b24ef8fe8927998e62dce (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
47
48
49
50
51
52
53
54
#include "ExampleGenerator.h"

#include <cmath>
#include <utility>

#include <raylib.h>

#include "ConstantVelocity.h"


ExampleGenerator::ExampleGenerator(ConstantVelocityBullet::Vector& bullets) :
    m_bullets {bullets},
    m_delay {0},
    m_interval {0.04f},
    m_cone {0.3f},
    m_angle {0.3f},
    m_speed {100},
    m_shift {10},
    m_segments {2},
    m_color {240, 0, 0, 255}
{
}


void
ExampleGenerator::update(const float dt)
{
    m_delay += dt;
    if (m_delay > m_interval) {
        const auto pos = position();
        m_delay -= m_interval;
        for (float i = 0; i <= m_segments; ++i) {
            const float angle = (m_angle - m_cone / 2 + m_cone * i / m_segments) * M_PI;
            const float cos = std::cos(angle);
            const float sin = std::sin(angle);
            ConstantVelocityBullet bullet;
            bullet.color = m_color;
            bullet.radius = 4;
            bullet.velocity.x = cos * m_speed;
            bullet.velocity.y = sin * m_speed;
            bullet.position.x = pos.x + cos * m_shift;
            bullet.position.y = pos.y + sin * m_shift;
            m_bullets.push_back(std::move(bullet));
        }
        m_angle += dt * 1.1f;
        if (m_angle >= 0.7f) {
            m_angle -= 0.4f;
            if (m_color.g == 0)
                m_color.g = 150;
            else
                m_color.g = 0;
        }
    }
}