-
Notifications
You must be signed in to change notification settings - Fork 0
/
tanxinterface.h
129 lines (119 loc) · 2.89 KB
/
tanxinterface.h
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
#ifndef TANXINTERFACE_H
#define TANXINTERFACE_H
#include <QObject>
#include <QMap>
#include <QWebSocket>
#define BULLET_SPEED 16.
#define TANK_SPEED 6.
#define TEAM_BLUE 0
#define TEAM_RED 1
#define TEAM_GREEN 2
#define TEAM_YELLOW 3
#define RESPAWN_REPAIR 5000
#define RESPAWN_DAMAGE 5000
#define RESPAWN_SHIELD 15000
struct Tank
{
int id;
int team;
QString owner;
double x, y;
double dx, dy; // Relative to bullet speed (norm: TANK_SPEED / BULLET_SPEED)
int angle;
int hp;
int sp;
bool dead;
int killer;
int score;
};
struct Pickable
{
enum PickableType {
Repair = 0,
Damage = 1,
Shield = 2
} t;
double x, y;
qint64 respawn_timestamp;
inline Pickable() {}
inline Pickable(PickableType t, double x, double y, qint64 now) : t(t), x(x), y(y)
{
init(now);
}
inline void init(qint64 now)
{
switch (t)
{
case Repair:
respawn_timestamp = now + RESPAWN_REPAIR;
return;
case Damage:
respawn_timestamp = now + RESPAWN_DAMAGE;
return;
case Shield:
respawn_timestamp = now + RESPAWN_SHIELD;
return;
}
}
};
struct Bullet
{
int tank;
double x, y;
double dx, dy; // Unit length
bool isRed;
double duration;
qint64 launched, expire;
};
struct GameData
{
QMap<QString, QString> users;
QString mySSID;
QMap<int, Tank> tanks;
int myID, myTeam;
int teamScores[4];
QMap<int, int> currentPickables;
QMap<int, Bullet> bullets;
Pickable pickables[9];
};
class TanxInterface : public QObject
{
Q_OBJECT
public:
explicit TanxInterface(QObject *parent = NULL, bool checkForExpiredBullets = true);
~TanxInterface();
void setShooting(bool enabled = true);
void setTarget(double angle);
void setTarget(double angle, bool shootingEnabled);
void setName(QString userName);
void move(double dx, double dy);
void targettedMove(double angle, double dx, double dy, bool shootingEnabled);
bool isConnected() const;
signals:
void initialized();
void gotUpdate();
void endOfGame(int scoreTeam1, int scoreTeam2, int scoreTeam3, int scoreTeam4);
void disconnected();
void newBullet(int id, const Bullet &bullet);
void delBullet(int id);
void newTank(const Tank &tank);
void delTank(int id);
void newUserName(QString id, QString name);
public slots:
void endConnection();
private slots:
void onConnected();
void onDisconnected();
void onError(QAbstractSocket::SocketError error);
void onTextReceived(QString str);
private:
QString angleToStream(double angle);
double streamToAngle(double sValue);
public:
QWebSocket wSocket;
GameData data;
bool checkForExpiredBullets;
bool endedConnection;
bool isShooting;
};
#endif // TANXINTERFACE_H