summaryrefslogtreecommitdiffhomepage
path: root/GameScreen.cpp
blob: dcae176a87415035e427fa5195b9a40698ff4245 (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
55
56
57
58
59
#include "GameScreen.h"

#include <cmath>

#include <raylib.h>

#include "Bullets.h"


GameScreen::GameScreen() :
    m_pos {400, 300}
{
    m_const_bullets.reserve(10000);
}


void
GameScreen::update(const float dt)
{
    if (IsKeyDown(KEY_LEFT))
        m_pos.x -= dt * 80;
    if (IsKeyDown(KEY_RIGHT))
        m_pos.x += dt * 80;
    m_delay += dt;
    if (m_delay > TEST_INTERVAL) {
        m_delay -= TEST_INTERVAL;
        const int speed = 120;
        const int divisions = 17;
        for (double i = 0; i <= divisions; ++i) {
            const double angle = (0.1 + 0.8 * i / divisions) * M_PI;
            const double cos = std::cos(angle);
            const double sin = std::sin(angle);
            ConstantVelocityBullet bullet;
            bullet.position.x = 400;
            bullet.position.y = 20;
            bullet.velocity.x = cos * speed;
            bullet.velocity.y = sin * speed;
            m_const_bullets.push_back(std::move(bullet));
        }
    }
    ::update(dt, m_const_bullets);
    bool collided = false;
    for (const auto& bullet : m_const_bullets) {
        if (CheckCollisionCircles(m_pos, 10, bullet.position, 6))
            collided = true;
    }
    (void) collided;
}


void
GameScreen::draw()
{
    DrawCircle(m_pos.x, m_pos.y, 10, LIGHTGRAY);
    for (const auto& bullet : m_const_bullets)
        DrawCircle(bullet.position.x, bullet.position.y, 6, RED);
    DrawFPS(5, 5);
    DrawText(TextFormat("%d", m_const_bullets.size()), 5, 25, 20, DARKGRAY);
}