aboutsummaryrefslogtreecommitdiff
path: root/Simulation/Corpse.cpp
blob: 82879ed95dbd1a7b59ad97f24bf0bfbaf31eae59 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Corpse.hpp"
#include "SimBase.hpp"

void Corpse::startup(SimBase *sim)
{
    hSim = sim;
    radius = Params::CORPSE_RAD;
}

void Corpse::setup(b2Vec2 pos, b2Vec2 lVel, float angle, float aVel)
{
    if (!body)
    {
        return;
    }

    lifetime = hSim->prms.corpseDecayTime;
    energy = hSim->prms.etFromCorpse;

    body->SetTransform(pos, angle);
    body->SetLinearVelocity(lVel);
    body->SetAngularVelocity(aVel);
}


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

    if (hSim->prms.corpseDecay)
    {
        --lifetime;
        energy = hSim->prms.etFromCorpse * ((float)lifetime / (float)hSim->prms.corpseDecayTime);
    }

    if (!lifetime || toBeDestroyed)
    {
        destroy();
    }
}


void Corpse::destroy()
{
    if (hSim->camera.trgtBody == body)
    {
        hSim->camera.prevCrds = hSim->camera.currentCrds;
        hSim->camera.trgtCrds = hSim->camera.currentCrds;
        hSim->camera.step = 0;
        hSim->camera.trgtBody = nullptr;
    }

    toBeDestroyed = false;
    hSim->tank.world.DestroyBody(body);
    body = nullptr;

    --hSim->corpseCount;
    hSim->text.corpseCnt.setString("Corpse count: " + nts(hSim->corpseCount));
}


void Corpse::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;
    }

    sf::Color corpseColor = mix(hSim->prms.corpseColor, hSim->prms.worldColor, ((float)lifetime / (float)hSim->prms.corpseDecayTime));
    if (hSim->camera.zoom > Params::CORPSE_RAD / 2.f)
    {
        hSim->corpsePoint.setFillColor(corpseColor);
        hSim->corpsePoint.setPosition(body->GetPosition().x, body->GetPosition().y);
        hSim->window.draw(hSim->corpsePoint);
    }
    else
    {
        hSim->corpseShell.setOutlineColor(corpseColor);
        hSim->corpseShell.setPosition(body->GetPosition().x, body->GetPosition().y);
        hSim->corpseNucleus.setFillColor(corpseColor);
        hSim->corpseNucleus.setPosition(body->GetPosition().x, body->GetPosition().y);
        hSim->corpseNucleus.setRotation(body->GetAngle() * Params::RAD_DGRS);
        hSim->window.draw(hSim->corpseShell);
        hSim->window.draw(hSim->corpseNucleus);
    }
}