1 | #include "Player.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <sstream>
|
---|
5 | #include <cstring>
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 | Player::Player()
|
---|
10 | {
|
---|
11 | this->id = 0;
|
---|
12 | this->name = "";
|
---|
13 | this->password = "";
|
---|
14 | this->pos.x = 0;
|
---|
15 | this->pos.y = 0;
|
---|
16 | }
|
---|
17 |
|
---|
18 | Player::Player(const Player& p)
|
---|
19 | {
|
---|
20 | this->id = p.id;
|
---|
21 | this->name = p.name;
|
---|
22 | this->password = p.password;
|
---|
23 | this->pos.x = p.pos.x;
|
---|
24 | this->pos.y = p.pos.y;
|
---|
25 | this->addr = p.addr;
|
---|
26 | }
|
---|
27 |
|
---|
28 | Player::Player(string name, string password)
|
---|
29 | {
|
---|
30 | this->id = 0;
|
---|
31 | this->name = name;
|
---|
32 | this->password = password;
|
---|
33 | this->pos.x = 200;
|
---|
34 | this->pos.y = 200;
|
---|
35 | }
|
---|
36 |
|
---|
37 | Player::Player(string name, sockaddr_in addr)
|
---|
38 | {
|
---|
39 | this->id = 0;
|
---|
40 | this->name = name;
|
---|
41 | this->password = "";
|
---|
42 | this->pos.x = 200;
|
---|
43 | this->pos.y = 200;
|
---|
44 | this->addr = addr;
|
---|
45 | }
|
---|
46 |
|
---|
47 | Player::~Player()
|
---|
48 | {
|
---|
49 | }
|
---|
50 |
|
---|
51 | void Player::serialize(char* buffer)
|
---|
52 | {
|
---|
53 | memcpy(buffer, &this->id, 4);
|
---|
54 | strcpy(buffer+4, this->name.c_str());
|
---|
55 | memcpy(buffer+5+this->name.length(), &this->pos.x, 4);
|
---|
56 | memcpy(buffer+9+this->name.length(), &this->pos.y, 4);
|
---|
57 | }
|
---|
58 |
|
---|
59 | void Player::deserialize(char* buffer)
|
---|
60 | {
|
---|
61 | memcpy(&this->id, buffer, 4);
|
---|
62 | this->name.assign(buffer+4);
|
---|
63 | memcpy(&this->pos.x, buffer+5+this->name.size(), 4);
|
---|
64 | memcpy(&this->pos.y, buffer+9+this->name.size(), 4);
|
---|
65 |
|
---|
66 | cout << "id: " << this->id << endl;
|
---|
67 | cout << "name: " << this->name << endl;
|
---|
68 | cout << "x: " << this->pos.x << endl;
|
---|
69 | cout << "y: " << this->pos.y << endl;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void Player::setId(int id)
|
---|
73 | {
|
---|
74 | this->id = id;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void Player::setAddr(sockaddr_in addr)
|
---|
78 | {
|
---|
79 | this->addr = addr;
|
---|
80 | }
|
---|