aboutsummaryrefslogtreecommitdiff
path: root/Simulation/Pellet.cpp
blob: 1a911ff75a1ae7bd9b6c40dd10f12cdf825c8b07 (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
60
61
62
63
64
65
66
#include "Pellet.hpp"
#include "SimBase.hpp"

void Pellet::startup(SimBase *sim)
{
    hSim = sim;
    radius = Params::PELLET_RAD;
}


void Pellet::update()
{
    if (!body)
    {
        return;
    }

    if (toBeDestroyed)
    {
        destroy();
    }
}


void Pellet::destroy()
{
    toBeDestroyed = false;
    hSim->tank.world.DestroyBody(body);
    body = nullptr;

    --hSim->pelletCount;
    hSim->text.pelletCnt.setString("Pellet count: " + nts(hSim->pelletCount) + " / " + nts(hSim->prms.pelletQtty));
}


void Pellet::draw()
{
    if (!body)
    {
        return;
    }

    sf::Vector2f vSize = hSim->window.getView().getSize();
    sf::Vector2f vCent = hSim->window.getView().getCenter();
    if ( body->GetPosition().x + radius < vCent.x - vSize.x / 2.f ||
         body->GetPosition().y + radius < vCent.y - vSize.y / 2.f ||
         body->GetPosition().x - radius > vCent.x + vSize.x / 2.f ||
         body->GetPosition().y - radius > vCent.y + vSize.y / 2.f  )
    {
        return;
    }

    if (hSim->camera.zoom > Params::PELLET_RAD / 2.f)
    {
        hSim->pelletPoint.setPosition(body->GetPosition().x, body->GetPosition().y);
        hSim->window.draw(hSim->pelletPoint);
    }
    else
    {
        hSim->pelletShell.setPosition(body->GetPosition().x, body->GetPosition().y);
        hSim->pelletNucleus.setPosition(body->GetPosition().x, body->GetPosition().y);
        hSim->pelletNucleus.setRotation(body->GetAngle() * hSim->prms.RAD_DGRS);
        hSim->window.draw(hSim->pelletShell);
        hSim->window.draw(hSim->pelletNucleus);
    }
}