Repository navigation
Expand file tree
/
Copy pathShape.cpp
More file actions
122 lines (103 loc) · 2.76 KB
/
Copy pathShape.cpp
File metadata and controls
122 lines (103 loc) · 2.76 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "Shape.h"
#include "Game.h"
#include <math.h>
#include "Utility.h"
#include "ParticleSystem.h"
const float PI = 3.14f;
#include <iostream>
Shape::Shape(Game* game_, b2Vec2* position_, bool dynamic_, int id, float density_, float friction_)
{
game = game_;
position = position_;
dynamic = dynamic_;
density = density_;
friction = friction_;
// Define the dynamic body. We set its position and call the body factory.
b2BodyDef bodyDef;
bodyDef.position.Set(position->x * game->getUtility()->getPTM(),
position->y * game->getUtility()->getPTM());
if(dynamic)
bodyDef.type = b2_dynamicBody;
body = game->getWorld()->CreateBody(&bodyDef);
userData.id = id;
userData.parent = this;
userData.toBeRemoved = false;
userData.isItem = false;
body->SetUserData((void*)&userData);
updateClock.restart();
particleSystem = nullptr;
}
Shape::~Shape(void)
{
game->getWorld()->DestroyBody(body);
//delete position; //<- causes client to crash
}
void Shape::update()
{
shape->setPosition((body->GetPosition().x + game->getUtility()->getOffSetX()) * game->getUtility()->getMTP(),
(-body->GetPosition().y + game->getUtility()->getOffSetY()) * game->getUtility()->getMTP());
shape->setRotation((-body->GetAngle() / PI) * 180);
//particles
if(particleSystem != nullptr)
{
particleSystem->setEmitter( shape->getPosition() );
particleSystem->update();
game->getWindow()->draw(*particleSystem);
if(!particleSystem->isRunning())
{
delete particleSystem;
particleSystem = nullptr;
}
}
else if(userData.id > -1)
checkForContacts();
}
sf::Shape* Shape::getShape()
{
return shape;
}
b2Body* Shape::getBody()
{
return body;
}
sf::Time Shape::timeSinceUpdate() const
{
return updateClock.getElapsedTime();
}
void Shape::resetUpdateClock()
{
updateClock.restart();
}
void Shape::makeStatic()
{
if(body->GetType() != b2BodyType::b2_staticBody)
{
// Make all players release their hooks when hooked box become static
list<Player*> players = game->getRemotePlayers();
players.push_back(game->getPlayer());
for(Player* player : players) {
if(player->getHook()->getGrabbedBox() == body)
player->releaseHook();
}
body->SetType(b2BodyType::b2_staticBody);
body->GetFixtureList()->SetUserData((void*)98);
dynamic = false;
startParticles();
}
}
void Shape::checkForContacts()
{
b2ContactEdge* edge = body->GetContactList();
for(;edge; edge = edge->next)
{
if(edge->contact->IsTouching() && body->GetLinearVelocity().Length() > 1)
{
startParticles();
}
}
}
void Shape::startParticles()
{
if(particleSystem == nullptr)
particleSystem = new ParticleSystem(1000, shape->getPosition());
}