summaryrefslogtreecommitdiffhomepage
path: root/ConstantVelocity.cpp
blob: efe31175713b9dd6277950c7fb94520b2ea8592a (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
#include "ConstantVelocity.h"

#include <raylib.h>


ConstantVelocitySystem::ConstantVelocitySystem()
{
    m_bullets.reserve(RESERVED);
}


void
ConstantVelocitySystem::update(const float dt)
{
    const int max_height = GetScreenHeight() + MARGIN;
    const int min_width = 0 - MARGIN;
    const int max_width = GetScreenWidth() + MARGIN;
    auto it = m_bullets.begin();
    while (it != m_bullets.end()) {
        auto& bullet = *it;
        bullet.position.x += bullet.velocity.x * dt;
        bullet.position.y += bullet.velocity.y * dt;
        if (bullet.position.y > max_height || bullet.position.x < min_width || bullet.position.x > max_width)
            it = m_bullets.erase(it);
        else
            ++it;
    }
}


void
ConstantVelocitySystem::draw()
{
    for (const auto& bullet : m_bullets)
        DrawCircle(bullet.position.x, bullet.position.y, bullet.radius, bullet.color);
}