-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.h
146 lines (105 loc) · 3.45 KB
/
player.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef PLAYER_H
#define PLAYER_H
#include "rengine.h"
#include <set>
#include <tacopie/network/tcp_client.hpp>
#include <SimpleJSON/json.hpp>
class PolygonNode;
class GameWindow;
class Player;
class Bullet;
using namespace rengine;
using namespace std;
using tacopie::tcp_client;
typedef Animation<RectangleNodeBase, float, &RectangleNodeBase::setX, &AnimationCurves::linear> RectangleXAnimation;
typedef Animation<RectangleNodeBase, float, &RectangleNodeBase::setY, &AnimationCurves::linear> RectangleYAnimation;
typedef Animation<TransformNode, float, &TransformNode::setMatrix_x, &AnimationCurves::linear> TransformXAnimation;
typedef Animation<TransformNode, float, &TransformNode::setMatrix_y, &AnimationCurves::linear> TransformYAnimation;
class Player : public Node
{
public:
static int s_idCounter;
const int id;
Player() = delete;
Player(const Player &) = delete;
Player(const vec4 color, GameWindow *world);
~Player();
Node *node();
bool handleEvent(Event *event);
bool handleCommand(const string &command, const vector<string> &arguments);
GameWindow *world() { return m_world; }
vec2 position() const { return m_position; }
rect2d geometry() const;
void reset();
void die();
void setTcpConnection(shared_ptr<tcp_client> conn);
void closeConnection();
bool isActive() const;
bool isAlive() const;
void sendUpdate(const json::JSON &worldState) const;
json::JSON serializeState() const;
void update();
const std::string &name() const { return m_name; }
void setName(const string &name);
vector<int> visiblePlayerIds() const;
void addBullet(Bullet *bullet);
void removeBullet(Bullet *bullet);
protected:
void onPreprocess() override;
private:
void onTcpMessage(const tcp_client::read_result& res);
void updateVisibility();
Node *m_rootNode = nullptr;
TransformNode *m_posNode = nullptr;
TransformNode *m_rotateNode = nullptr;
PolygonNode *m_playerNode = nullptr;
float m_rotation;
vec2 m_position;
vec2 m_cursorPosition;
PolygonNode *m_polygon = nullptr;
GameWindow *m_world = nullptr;
vec4 m_color;
shared_ptr<tcp_client> m_tcpConnection;
string m_networkBuffer;
bool m_dead = false;
mutex m_commandMutex;
string m_command;
vector<string> m_arguments;
TextureNode *m_nameNode;
shared_ptr<TransformXAnimation> m_xAnimation;
shared_ptr<TransformYAnimation> m_yAnimation;
vector<int> m_visiblePlayers;
set<Bullet*> m_bullets;
std::shared_ptr<GlyphTextureJob> m_nameJob;
std::string m_name;
};
class Bullet : public RectangleNode
{
public:
static int s_idCounter;
const int id;
Bullet();
~Bullet();
RENGINE_ALLOCATION_POOL_DECLARATION(Bullet, BulletNode);
void setOwner(Player *owner);
void setTarget(vec2 target);
void start();
void checkHit();
static Bullet *create(Player *owner, vec2 target, vec4 color) {
Bullet *node = create();
node->setOwner(owner);
node->setTarget(target);
node->setColor(color);
node->setGeometry(rect2d::fromPosSize(owner->position() - vec2(3, 3), vec2(6, 6)));
return node;
}
json::JSON serializeState() const;
private:
Player *m_owner = nullptr;
GameWindow *m_world = nullptr;
shared_ptr<RectangleXAnimation> m_xAnimation;
shared_ptr<RectangleYAnimation> m_yAnimation;
vec2 m_target;
bool m_startedInside;
};
#endif // PLAYER_H