aboutsummaryrefslogtreecommitdiff
path: root/Simulation/Entity.cpp
blob: d5ccb7569793018c581eb6e9aa1c5843149b563b (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 "Entity.hpp"
#include "SimBase.hpp"

void Entity::create()
{
    if (!hSim || body || !radius)
    {
        return;
    }

    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position = { hSim->prms.worldRad, hSim->prms.worldRad };
    float hypotenuse = hSim->prms.worldRad - radius;
    while (bodyDef.position.x * bodyDef.position.x + bodyDef.position.y * bodyDef.position.y > hypotenuse * hypotenuse)
    {
        bodyDef.position.x = realRand(-hSim->prms.worldRad, hSim->prms.worldRad);
        bodyDef.position.y = realRand(-hSim->prms.worldRad, hSim->prms.worldRad);
    }
    bodyDef.angle = realRand(0.f, 360.f / Params::RAD_DGRS);
    bodyDef.linearDamping = 0.1f;
    bodyDef.angularDamping = 0.1f;
    bodyDef.allowSleep = true;
    bodyDef.userData = this;

    body = hSim->tank.world.CreateBody(&bodyDef);

    b2CircleShape shape;
    shape.m_radius = radius;

    b2FixtureDef fixture;
    fixture.shape = &shape;
    fixture.friction = 1.f;
    fixture.restitution = 1.f;
    fixture.density = 1.f;

    body->CreateFixture(&fixture);

    if (radius == Params::PELLET_RAD)
    {
        ++hSim->pelletCount;
        hSim->text.pelletCnt.setString("Pellet count: " + nts(hSim->pelletCount) + " / " + nts(hSim->prms.pelletQtty));
    }
    else if (radius == Params::CORPSE_RAD)
    {
        ++hSim->corpseCount;
        hSim->text.corpseCnt.setString("Corpse count: " + nts(hSim->corpseCount));
    }
    else if (radius == Params::GUPPIE_RAD)
    {
        ++hSim->guppieCount;
        hSim->text.guppieCnt.setString("Guppie count: " + nts(hSim->guppieCount));
        ((Guppie *)this)->energy = hSim->prms.initEnergy;
        body->SetLinearDamping(2.f);
        body->SetAngularDamping(2.f);

        ((b2CircleShape *)body->GetFixtureList()->GetShape())->m_radius = hSim->prms.EGG_RAD;
    }
}