Repository navigation
Expand file tree
/
Copy pathRemover.cpp
More file actions
128 lines (112 loc) · 3.07 KB
/
Copy pathRemover.cpp
File metadata and controls
128 lines (112 loc) · 3.07 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
123
124
125
126
127
128
#include "Remover.h"
#include "Shape.h"
#include "Game.h"
#include "PacketParser.h"
#include "Server.h"
#include "Client.h"
#include "Textor.h"
Remover::Remover(Game* g):
packetParser(g->getPacketParser()), game(g)
{
killOffset = 60;
staleOffset = 20;
}
Remover::~Remover(void)
{
}
void Remover::checkShapeRemoval(Shape* shape)
{
//check for killing shape
if(shape->getShape()->getPosition().y > updateKillOffset() && shape != game->getLastStaticShape())
{
deletion.push(shape);
// cout << "shape killed at "<< updateKillOffset() << endl;
}
//check for making shape stale
else if(shape->getShape()->getPosition().y > updateStaleOffset()) //make sure there is still a static platform to stand on
{
shape->makeStatic();
sf::Packet p = packetParser->pack<int>(UDPNetwork::SHAPE_STATIC, shape->getId());
dynamic_cast<Server*>(game->getLocalHost())->broadCast(p);
}
}
void Remover::checkPlayerKill(Player* player)
{
if(player->getBox()->getShape()->getPosition().y > updateKillOffset()
&& !player->isDead())
{
string s = player->getAlias() + " is now sleeping with the fishes.";
player->setDeath(true);
sf::Vector2f v = sf::Vector2f(game->getWindow()->getSize().x/2, game->getWindow()->getSize().y/2);
v += sf::Vector2f(game->getViewOffset());
game->getTextor()->writeTemporaryText(s, v, 3);
sf::Packet p = packetParser->pack(UDPNetwork::PLAYER_DEAD);
if(game->getLocalHost()->isServer())
{
dynamic_cast<Server*>(game->getLocalHost())->broadCast(p);
}
else
{
dynamic_cast<Client*>(game->getLocalHost())->sendToServer(p);
}
}
}
void Remover::removeShapes()
{
while(!deletion.empty())
{
Shape* del = deletion.top();
auto& boxes = game->getShapes();
vector<Shape*>::iterator todelete = std::find_if(boxes.begin(), boxes.end(), [del](Shape* b)
{
return del == b;
}
);
//if found delete
if(todelete != boxes.end())
{
sf::Packet p = packetParser->pack<int>(UDPNetwork::REMOVE_SHAPE, (*todelete)->getId());
dynamic_cast<Server*>(game->getLocalHost())->broadCast(p);
delete deletion.top();
boxes.erase(todelete);
deletion.pop();
}
else
deletion.pop();
}
}
float Remover::updateKillOffset()
{
return updateStaleOffset() + killOffset;
}
float Remover::updateStaleOffset()
{
return game->getWater()->getPosition().y;
}
void Remover::respawnPlayer(Player* player)
{
if(player->isDead())
{
b2Vec2 spawn = game->getLastStaticShape()->getBody()->GetPosition();
spawn.y += 10;
player->resetPlayer(&spawn);
/*string s = player->getName() + " has returned from the grave.";
sf::Vector2f v = sf::Vector2f(game->getWindow()->getSize().x/2, game->getWindow()->getSize().y/2);
v += sf::Vector2f(game->getViewOffset());
game->getTextor()->writeTemporaryText(s, v, 3);*/
player->setDeath(false);
sf::Packet p = packetParser->pack(UDPNetwork::PLAYER_RES);
p << player->getLocalID();
p << spawn.x;
p << spawn.y;
UDPNetwork* lh = game->getLocalHost();
if(lh->isServer())
{
dynamic_cast<Server*>(lh)->broadCast(p);
}
else
{
dynamic_cast<Client*>(lh)->sendToServer(p);
}
}
}