-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplayer.cpp
More file actions
450 lines (400 loc) · 15.9 KB
/
Copy pathplayer.cpp
File metadata and controls
450 lines (400 loc) · 15.9 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "player.h"
#include "ball.h"
#include "socketthread.h"
#include "border.h"
#include "QGraphicsSceneMouseEvent"
#include <QDebug>
#include <stdlib.h>
#include <QGraphicsScene>
#include <QGraphicsLineItem>
#include <stdlib.h>
#include <cmath>
#include <QMediaPlayer>
#include "realplayer.h"
#include <QPropertyAnimation>rr
using namespace std;
Player::Player(double cxX, double cyY, SocketThread *thread, int number) : Circle(cxX, cyY), animation(new QPropertyAnimation(this)) //change the circle r, x, y too
{
this->rPN = number;
this->thread = thread;
this->vX = this->vY = 0;
r = 35;
width = 70; height = 77;//this may be changed...
line = new QGraphicsLineItem(0, 0, 0, 0, this);
//set Line default things
p1 = p2 = p3 = line->pen();
p1.setWidth(10); p2.setWidth(10); p3.setWidth(10);
p1.setColor(Qt::yellow); p2.setColor("orange"); p3.setColor(Qt::red); //seting line colors
//manipulating thread signals
//checking
if(number == 1)
setPos(0, 0);
else
setPos(1175, 0);
anForMovingX = new QPropertyAnimation(); anForMovingY = new QPropertyAnimation(this);
anForMovingX->setPropertyName("changeX"); anForMovingY->setPropertyName("changeY");
finalDesX = cxX; finalDexY = cyY;
//rePositioning();
this->setFlag(QGraphicsItem::ItemIsMovable);
this->setFlag(QGraphicsItem::ItemIsFocusable);
this->setAcceptTouchEvents(true);
this->setFlags(QGraphicsItem::ItemIsSelectable);
this->setAcceptHoverEvents(true);
animation->setStartValue(vX);
animation->setEndValue(.1);
animation->setPropertyName("movePlayers");
animation->setTargetObject(this);
}
//this func is for changing coordinates for QPropertyAnimation
void Player::setChangeX(int)
{
if(this->pos().x() - this->finalDesX > 0) {
if(abs(this->pos().x() - 5) > this->finalDesX)
this->setX(this->pos().x() - 5);
else {
this->setX(this->pos().x() - abs(this->pos().x() - this->finalDesX));
this->anForMovingX->stop();
}
}
else {
if(abs(this->pos().x() + 5) < this->finalDesX)
this->setX(this->pos().x() + 5);
else {
this->setX(this->pos().x() + abs(this->pos().x() - this->finalDesX));
this->anForMovingX->stop();
}
}
}
//this func changes the coordinate for QPropertyAnimation
void Player::setChangeY(int)
{
if(this->pos().y() - this->finalDexY > 0) {
if(abs(this->pos().y() - 5) > finalDexY)
this->setY(this->pos().y() - 5);
else {
this->setY(this->pos().y() - abs(this->pos().y() - this->finalDexY));
this->anForMovingY->stop();
}
}
else {
if(abs(this->pos().y() + 5) < finalDexY)
this->setY(this->pos().y() + 5);
else {
this->setY(this->pos().y() + abs(this->pos().y() - this->finalDexY));
this->anForMovingY->stop();
}
}
}
//this is for moving circle and checking collisions of circle
//chckin collision between player and ball, player and line
void Player::setMove(int)
{
//add some collision check and ...
this->setPos(this->pos().x() + vX, this->pos().y() + vY);
if(this->pos().x() <= 109 && (this->pos().y() <= 226 || this->pos().y() - this->r * 2 >= 458)) {
this->vX *= -.8;
this->setX(111);
}
if(this->pos().x() + this->r * 2 >= 1175 && (this->pos().y() <= 226 || this->pos().y() - this->r * 2 >= 458)) {
this->vX *= -.8;
this->setX(1173 - this->r * 2);
}
if(this->pos().y() <= 10) {
this->setY(11);
this->vY *= -.8;
}
if(this->pos().y() + this->r * 2 >= 674) {
this->setY(674 - this->r * 2);
this->vY *= -.8;
}
if(this->pos().x() < 109 && (this->pos().y() > 226 && this->pos().y() - this->r * 2 < 458)) {
if(this->pos().x() <= 35) {
this->vX *= -.8;
this->setX(36);
}
if(this->pos().y() <= 234) {
this->vY *= -.8;
this->setY(235);
}
if(this->pos().y() + this->r * 2 >= 458) {
this->vY *= -.8;
this->setY(457 - this->r * 2);
}
}
if(this->pos().x() + this->r * 2 > 1175 && (this->pos().y() > 230 && this->pos().y() - this->r * 2 < 458)) {
if(this->pos().x() + this->r * 2 >= 1250) {
this->vX *= -.8;
this->setX(1249 - this->r * 2);
}
if(this->pos().y() <= 234) {
this->vY *= -.8;
this->setY(235);
}
if(this->pos().y() + this->r * 2 >= 458) {
this->vY *= -.8;
this->setY(457 - this->r * 2);
}
}
QString st = "2 " + QString::number(this->playerNum) + " " + QString::number(this->rPN) + " " + QString::number(this->pos().x()) + " " + QString::number(this->pos().y());
st.append(QChar(23));
thread->sendMess(st);
vX > 0 ? vX -= fx : vX += fx;
vY > 0 ? vY -= fy : vY += fy;
if(abs(vX) < .3 && abs(vY) < .3) {
this->vX = 0;
this->vY = 0;
animation->stop();
return;
}
QList<QGraphicsItem *> l = this->collidingItems();
for(int i = 0; i < l.size(); i++) {
if(Player *c = dynamic_cast<Player *> (l[i])) {
//adding sound of collision between players...
QMediaPlayer *shot = new QMediaPlayer();
shot->setMedia(QUrl("qrc:/sounds/p2p.flac"));
shot->setVolume(110);
shot->play();
double thisCx = xC(this->pos().x()), thisCy = yC(this->pos().y()), otherCx = c->xC(c->pos().x()), otherCy = c->yC(c->pos().y());
thisCy *= -1; otherCy *= -1;
if(thisCx - otherCx == 0) {
double tmp = this->vY;
this->vY = c->vY;
c->vY = tmp;
}
else {
double mCenters = (thisCy - otherCy) / (thisCx - otherCx);
double mCNorm = -1. / mCenters;
double picOfVxOnMC = coss(mCenters) * vX, picOfVxOnMCN = coss(mCNorm) * vX, picOfVyOnMC = sinn(mCenters) * vY * -1, picOfVyOnMCN = sinn(mCNorm) * vY * -1;
double sPicOfVxONMC = coss(mCenters) * c->vX, sPicofVxOnMCN = coss(mCNorm) * c->vX, sPicOfVyOnMC = sinn(mCenters) * c->vY * -1, sPicOfVyOnMCN = sinn(mCNorm) * c->vY * -1;
if(mCenters < 0) {
picOfVxOnMC *= -1;
sPicOfVxONMC *= -1;
}
else {
picOfVxOnMCN *= -1;
sPicofVxOnMCN *= -1;
}
double fOnMC = picOfVxOnMC + picOfVyOnMC, fOnMCN = picOfVxOnMCN + picOfVyOnMCN, sOnMC = sPicOfVxONMC + sPicOfVyOnMC, sOnMCN = sPicofVxOnMCN + sPicOfVyOnMCN;
double tmp = fOnMC;
fOnMC = sOnMC;
sOnMC = tmp;
if(mCenters < 0) {
double fVx1 = fOnMCN * coss(mCNorm), fVx2 = fOnMC * coss(mCenters) * -1, fVy1 = fOnMCN * sinn(mCNorm) * -1, fVy2 = fOnMC * sinn(mCenters) * -1;
this->vX = fVx1 + fVx2; this->vY = fVy1 + fVy2;
double sVx1 = sOnMCN * coss(mCNorm), sVx2 = sOnMC * coss(mCenters) * -1, sVy1 = sOnMCN * sinn(mCNorm) * -1, sVy2 = sOnMC * sinn(mCenters) * -1;
c->vX = sVx1 + sVx2; c->vY = sVy1 + sVy2;
}
else {
double fVx1 = fOnMCN * coss(mCNorm) * -1, fVx2 = fOnMC * coss(mCenters), fVy1 = fOnMCN * sinn(mCNorm) * -1, fVy2 = fOnMC * sinn(mCenters) * -1;
this->vX = fVx1 + fVx2; this->vY = fVy1 + fVy2;
double sVx1 = sOnMCN * coss(mCNorm) * -1, sVx2 = sOnMC * coss(mCenters), sVy1 = sOnMCN * sinn(mCNorm) * -1, sVy2 = sOnMC * sinn(mCenters) * -1;
c->vX = sVx1 + sVx2; c->vY = sVy1 + sVy2;
}
this->startAnimaion();
c->startAnimaion();
}
}
else if(Ball *c = dynamic_cast<Ball *> (l[i])) {
//adding sound of collision between player and ball...
QMediaPlayer *shot = new QMediaPlayer();
shot->setMedia(QUrl("qrc:/sounds/p2p.flac"));
shot->setVolume(110);
shot->play();
double thisCx = xC(this->pos().x()), thisCy = yC(this->pos().y()), otherCx = c->xC(c->pos().x()), otherCy = c->yC(c->pos().y());
thisCy *= -1; otherCy *= -1;
if(thisCx - otherCx == 0) {
double tmp = this->vY;
this->vY = c->vY;
c->vY = tmp;
}
else {//collinding ball...
double mCenters = (thisCy - otherCy) / (thisCx - otherCx);
double mCNorm = -1. / mCenters;
double picOfVxOnMC = coss(mCenters) * vX, picOfVxOnMCN = coss(mCNorm) * vX, picOfVyOnMC = sinn(mCenters) * vY * -1, picOfVyOnMCN = sinn(mCNorm) * vY * -1;
double sPicOfVxONMC = coss(mCenters) * c->vX, sPicofVxOnMCN = coss(mCNorm) * c->vX, sPicOfVyOnMC = sinn(mCenters) * c->vY * -1, sPicOfVyOnMCN = sinn(mCNorm) * c->vY * -1;
if(mCenters < 0) {
picOfVxOnMC *= -1;
sPicOfVxONMC *= -1;
}
else {
picOfVxOnMCN *= -1;
sPicofVxOnMCN *= -1;
}
double fOnMC = picOfVxOnMC + picOfVyOnMC, fOnMCN = picOfVxOnMCN + picOfVyOnMCN, sOnMC = sPicOfVxONMC + sPicOfVyOnMC, sOnMCN = sPicofVxOnMCN + sPicOfVyOnMCN;
double tmp = fOnMC;
fOnMC = sOnMC;
sOnMC = tmp;
if(mCenters < 0) {
double fVx1 = fOnMCN * coss(mCNorm), fVx2 = fOnMC * coss(mCenters) * -1, fVy1 = fOnMCN * sinn(mCNorm) * -1, fVy2 = fOnMC * sinn(mCenters) * -1;
this->vX = fVx1 + fVx2; this->vY = fVy1 + fVy2;
double sVx1 = sOnMCN * coss(mCNorm), sVx2 = sOnMC * coss(mCenters) * -1, sVy1 = sOnMCN * sinn(mCNorm) * -1, sVy2 = sOnMC * sinn(mCenters) * -1;
c->vX = sVx1 + sVx2; c->vY = sVy1 + sVy2;
}
else {
double fVx1 = fOnMCN * coss(mCNorm) * -1, fVx2 = fOnMC * coss(mCenters), fVy1 = fOnMCN * sinn(mCNorm) * -1, fVy2 = fOnMC * sinn(mCenters) * -1;
this->vX = fVx1 + fVx2; this->vY = fVy1 + fVy2;
double sVx1 = sOnMCN * coss(mCNorm) * -1, sVx2 = sOnMC * coss(mCenters), sVy1 = sOnMCN * sinn(mCNorm) * -1, sVy2 = sOnMC * sinn(mCenters) * -1;
c->vX = sVx1 + sVx2; c->vY = sVy1 + sVy2;
}
this->startAnimaion();
c->startAnimaion();
}
}
}
}
//when mouse press on player
void Player::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(changable) {
//setting line default color
line->setPen(p1);
line->setLine(xC(0), yC(0), xC(0), yC(0));
this->scene()->addItem(line);
line->setParentItem(this);
line->setVisible(true);
}
}
//when mouse release
void Player::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(changable) {
//set line invisible
line->setVisible(false);
this->scene()->removeItem(line);
QString st = "1 " + QString::number(this->playerNum) + " " + "-1" + " " + "-1" + " ";
st.append(QChar(23));
thread->sendMess(st);
//settin speed
vX = -(event->pos().x() - this->xC(0)) / 3; // /7 is experiential
vY = -(event->pos().y() - this->yC(0)) / 3;
double max = sqrt(vX * vX + vY * vY);
if(max > 12) {
vX = 12 * vX / max;
vY = 12 * vY / max;
}
int vvX = vX * 1000, vvY = vY * 1000;
vX = vvX / 1000.; vY = vvY / 1000.;
// st = "2 " + QString::number(this->playerNum) + " " + QString::number(vX) + " " + QString::number(vY);
// st.append(QChar(23));
// thread->sendMess(st);
//*changeTurn = 9;
QString s = "12";
s.append(QChar(23));
thread->sendMess(s);
startAnimaion();
}
}
//when mouse is moving
void Player::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(changable) {
//make line biger by moving mouse... we may add triangle
int tmp = sqrt(event->pos().x() * event->pos().x() + event->pos().y() * event->pos().y());
if(tmp > 100) {
double xx = -(event->pos().x() - 2 * xC(0)),yy = -(event->pos().y() - 2 * yC(0));
xx = xC(0) - ((xC(0) - xx) * 100. / tmp);
yy = yC(0) - ((yC(0) - yy) * 100. / tmp);
line->setLine(xC(0), yC(0), xx, yy);
QString st = "1 " + QString::number(this->playerNum) + " " + QString::number(xx) + " " + QString::number(yy);
st.append(QChar(23));
thread->sendMess(st);
}
else {
line->setLine(xC(0), yC(0), -(event->pos().x() - 2 * xC(0)), -(event->pos().y() - 2 * yC(0)));
QString st = "1 " + QString::number(this->playerNum) + " " + QString::number(-(event->pos().x() - 2 * xC(0))) + " " + QString::number(-(event->pos().y() - 2 * yC(0))) + " ";
st.append(QChar(23));
thread->sendMess(st);
}
changeColorOfLine(tmp);
//send informations to server
}
else {
line->setVisible(false);
this->scene()->removeItem(line);
QString st = "1 " + QString::number(this->playerNum) + " " + "-1" + " " + "-1" + " ";
st.append(QChar(23));
thread->sendMess(st);
}
}
//detect collisions
bool Player::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const
{
if(const Ball *b = dynamic_cast<const Ball *> (other)) {
double tX = this->pos().x() + 35, tY = this->pos().y() + 35, oX = b->pos().x() + 15, oY = b->pos().y() + 15;
double tmp = sqrt((tX - oX) * (tX - oX) + (tY - oY) * (tY - oY));
if(tmp <= 50) {
return true;
}
return false;
}
else if (const Player *b = dynamic_cast<const Player *> (other)) {
double tX = this->pos().x(), tY = this->pos().y(), oX = b->pos().x(), oY = b->pos().y();
double tmp = sqrt((tX - oX) * (tX - oX) + (tY - oY) * (tY - oY));
if(tmp <= 70)
return true;
return false;
}
return false;
}
//when server sends movePlayer Singnal
void Player::movePlayer(double a, double b)
{
this->setX(a);
this->setY(b);
}
//when server sends drawLine Signal
void Player::drawLine(double a, double b)
{
if(a == -1 && b == -1) {
this->scene()->removeItem(line);
line->setVisible(false);
return;
}
line->setVisible(true);
//double aa = abs(a - xC(this->pos().x())); double bb = abs(b - yC(this->pos().y()));
int tmp = sqrt(a * a + b * b);
line->setLine(xC(0), yC(0), a, b);
changeColorOfLine(tmp);
}
//returns actual center
double Player::xC(double d) {
return d + 35;
}
double Player::yC(double d) {
return d + 35;
}
//this func will change the color of Line
void Player::changeColorOfLine(int tmp)
{
if(tmp < 60)
line->setPen(p1);
else {
if(tmp < 90)
line->setPen(p2);
else
line->setPen(p3);
}
}
//animation of starting...
void Player::rePositioning()
{
anForMovingX->setDuration(3000); anForMovingY->setDuration(3000);
anForMovingX->setStartValue(this->pos().x()); anForMovingX->setEndValue(this->finalDesX);
anForMovingY->setStartValue(this->pos().y()); anForMovingY->setEndValue(this->finalDexY);
anForMovingX->setTargetObject(this); anForMovingY->setTargetObject(this);
anForMovingX->start(); anForMovingY->start();
}
//this is for starting animation
void Player::startAnimaion()
{
//function for starting animation of Player
double tmp = sqrt(vX * vX + vY * vY);
fx = abs(vX) / (tmp * 10), fy = abs(vY) / (tmp * 10);
int ffX = fx * 1000, ffY = fy * 1000;
fx = ffX / 1000.; fy = ffY / 1000.;
animation->pause();
animation->setStartValue(vX);
animation->setEndValue(0);
animation->setDuration(10000);
animation->start();
}