-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
148 lines (124 loc) · 3.62 KB
/
client.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "client.h"
using namespace std;
Client::Client() : m_listenThread(&Client::Listen, this) //, m_readytoplayThread(&Client::PlaySignal, this)
{
c_socket.bind(sf::Socket::AnyPort);
std::cout << "Enter server ip: ";
std::cin >> sIp;
sf::IpAddress send(sIp);
serverIp = send;
sf::RenderWindow dummy_window(sf::VideoMode(500, 500), "Dummy Window");
for (int i = 0; i < 10; i++)
{
BulletCounter[i] = 0;
}
for (int i = 0; i < 1000; i++)
{
BulletDirection[i] = {0, 0};
}
}
Client::Client(sf::IpAddress l_ip) : m_listenThread(&Client::Listen, this)
{
c_socket.bind(sf::Socket::AnyPort);
serverIp = l_ip;
SendConnection();
}
void Client::SendConnection()
{
sf::Socket::Status value;
value = c_socket.send(text.c_str(), text.length() + 1, serverIp, 2000);
if (value == 0)
{
std::cout << "Request to Connect Sent " << std::endl;
}
}
void Client::ReceiveConfirmation()
{
sf::Packet packet;
std::string confirmation;
sf::IpAddress ip;
unsigned short port;
c_socket.receive(packet, ip, port);
packet >> confirmation >> m_totalplayers >> m_playerid;
if (sizeof(confirmation) > 10)
{
std::cout << confirmation << endl
<< "Total Players: " << m_totalplayers << endl
<< "Your Player No: " << m_playerid << endl;
std::cout << "\n\n\t\tWAITING FOR OTHER PLAYERS...\n\n"
<< std::endl;
}
m_playerid = m_playerid - 1;
}
bool Client::ReadyToPlay()
{
return readytoplay;
}
void Client::PlaySignal()
{
sf::Packet packet;
int playnum;
sf::IpAddress ip;
unsigned short port;
c_socket.receive(packet, ip, port);
packet >> playnum;
if (playnum == -2)
{
readytoplay = true;
}
}
void Client::Send(sf::Packet packet)
{
c_socket.send(packet, serverIp, 2000);
}
void Client::Listen()
{
float xtank, ytank, xbullet, ybullet, angle;
int playerid, bulletno;
unsigned int health;
sf::Socket::Status value;
sf::IpAddress tempip;
unsigned short tempport;
sf::Packet packet;
while (1)
{
value = c_socket.receive(packet, tempip, tempport);
if (value == 0)
{
packet >> playerid >> xtank >> ytank >> angle >> health >> bulletno >> xbullet >> ybullet;
setState(playerid, {xtank, ytank}, angle, health, bulletno, {xbullet, ybullet});
}
}
}
void Client::setState(int playerid, sf::Vector2f position, float angle, unsigned int health, int bulletno, sf::Vector2f bulletdirection)
{
Tankpositionarray[playerid] = position;
TankRotationArray[playerid] = angle;
TankHealthArray[playerid] = health;
BulletDirection[playerid * 100 + bulletno] = bulletdirection;
if (bulletno != BulletCounter[playerid])
{
BulletCounter[playerid] = bulletno;
}
}
sf::Vector2f Client::getTankPosition(int playerid) { return Tankpositionarray[playerid]; }
sf::Vector2f Client::getBulletDirection(int playerid) { return BulletDirection[playerid * 100 + BulletCounter[playerid]]; }
int Client::SendBulletNotoState(int playerid) { return BulletCounter[playerid]; }
float Client::getTankDirection(int playerid) { return TankRotationArray[playerid]; }
unsigned int Client::getTankHealth(int playerid) { return TankHealthArray[playerid]; }
void Client::sendState()
{
for (int i = 0; i < m_totalplayers; i++)
{
if (i == m_playerid)
{
continue;
}
}
}
void Client::Thread()
{
m_listenThread.launch();
}
int Client::getPlayerId() { return m_playerid; }
int Client::getTotalPlayer() { return m_totalplayers; }