source: network-game/common/Player.cpp@ 62ee2ce

Last change on this file since 62ee2ce was 5806dc2, checked in by dportnoy <dmp1488@…>, 12 years ago

Simplified Player serialization code

  • Property mode set to 100644
File size: 1.5 KB
RevLine 
[8e540f4]1#include "Player.h"
[2488852]2
3#include <iostream>
[3b8adee]4#include <sstream>
5#include <cstring>
[2488852]6
7using namespace std;
8
[01d0d00]9Player::Player()
10{
11 this->id = 0;
12 this->name = "";
13 this->password = "";
14 this->pos.x = 0;
15 this->pos.y = 0;
16}
17
18Player::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
[59061f6]28Player::Player(string name, string password)
29{
[01d0d00]30 this->id = 0;
[59061f6]31 this->name = name;
32 this->password = password;
[edfd1d0]33 this->pos.x = 200;
34 this->pos.y = 200;
[59061f6]35}
36
[8e540f4]37Player::Player(string name, sockaddr_in addr)
[2488852]38{
[01d0d00]39 this->id = 0;
[2488852]40 this->name = name;
[59061f6]41 this->password = "";
[edfd1d0]42 this->pos.x = 200;
43 this->pos.y = 200;
[2488852]44 this->addr = addr;
45}
46
[8e540f4]47Player::~Player()
[2488852]48{
49}
50
[3b8adee]51void Player::serialize(char* buffer)
[59061f6]52{
[80b3f94]53 memcpy(buffer, &this->id, 4);
54 strcpy(buffer+4, this->name.c_str());
[ad5d122]55 memcpy(buffer+5+this->name.length(), &this->pos.x, 4);
56 memcpy(buffer+9+this->name.length(), &this->pos.y, 4);
[59061f6]57}
[edfd1d0]58
[3b8adee]59void Player::deserialize(char* buffer)
[edfd1d0]60{
[80b3f94]61 memcpy(&this->id, buffer, 4);
[5806dc2]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);
[3b8adee]65
[80b3f94]66 cout << "id: " << this->id << endl;
[5806dc2]67 cout << "name: " << this->name << endl;
[80b3f94]68 cout << "x: " << this->pos.x << endl;
69 cout << "y: " << this->pos.y << endl;
[3b8adee]70}
71
[01d0d00]72void Player::setId(int id)
73{
74 this->id = id;
75}
76
[3b8adee]77void Player::setAddr(sockaddr_in addr)
78{
79 this->addr = addr;
[edfd1d0]80}
Note: See TracBrowser for help on using the repository browser.