source: network-game/common/Player.cpp@ 66906aa

Last change on this file since 66906aa was f401cac, checked in by dportnoy <dmp1488@…>, 12 years ago

Fixed some bugs in the player movement code

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#include "Player.h"
2
3#include <iostream>
4#include <sstream>
5#include <cstring>
6#include <cmath>
7
8using namespace std;
9
10Player::Player()
11{
12 this->id = 0;
13 this->name = "";
14 this->password = "";
15 this->pos.x = this->target.x = 0;
16 this->pos.y = this->target.y = 0;
17}
18
19Player::Player(const Player& p)
20{
21 this->id = p.id;
22 this->name = p.name;
23 this->password = p.password;
24 this->pos.x = p.pos.x;
25 this->pos.y = p.pos.y;
26 this->target.x = p.target.x;
27 this->target.y = p.target.y;
28 this->addr = p.addr;
29}
30
31Player::Player(string name, string password)
32{
33 this->id = 0;
34 this->name = name;
35 this->password = password;
36 this->pos.x = this->target.x = 200;
37 this->pos.y = this->target.y = 200;
38}
39
40Player::Player(string name, sockaddr_in addr)
41{
42 this->id = 0;
43 this->name = name;
44 this->password = "";
45 this->pos.x = this->target.x = 200;
46 this->pos.y = this->target.y = 200;
47 this->addr = addr;
48}
49
50Player::~Player()
51{
52}
53
54void Player::serialize(char* buffer)
55{
56 memcpy(buffer, &this->id, 4);
57 strcpy(buffer+4, this->name.c_str());
58 memcpy(buffer+5+this->name.length(), &this->pos.x, 4);
59 memcpy(buffer+9+this->name.length(), &this->pos.y, 4);
60}
61
62void Player::deserialize(char* buffer)
63{
64 memcpy(&this->id, buffer, 4);
65 this->name.assign(buffer+4);
66 memcpy(&this->pos.x, buffer+5+this->name.size(), 4);
67 memcpy(&this->pos.y, buffer+9+this->name.size(), 4);
68
69 cout << "id: " << this->id << endl;
70 cout << "name: " << this->name << endl;
71 cout << "x: " << this->pos.x << endl;
72 cout << "y: " << this->pos.y << endl;
73}
74
75void Player::setId(int id)
76{
77 this->id = id;
78}
79
80void Player::setAddr(sockaddr_in addr)
81{
82 this->addr = addr;
83}
84
85void Player::move(void) {
86 // timeLastMoved
87 // pos
88 // target
89 int speed = 100; // pixels per second
90
91 timespec curTS, diffTS;
92 clock_gettime(CLOCK_REALTIME, &curTS);
93
94 // get time elapsed
95 diffTS.tv_sec = curTS.tv_sec - timeLastUpdated.tv_sec;
96 diffTS.tv_nsec = curTS.tv_nsec - timeLastUpdated.tv_nsec;
97 if (diffTS.tv_nsec < 0) {
98 diffTS.tv_sec -= 1;
99 diffTS.tv_nsec += 1000000000;
100 }
101
102 cout << "elapsed secs: " << diffTS.tv_sec << endl;
103 cout << "elapsed nsecs: " << diffTS.tv_nsec << endl;
104
105 // if we're at our target, don't move
106 if (pos.x == target.x || pos.y == target.y)
107 cout << "We're already at our target" << endl;
108 else {
109 float pixels = speed * (diffTS.tv_sec+diffTS.tv_nsec/1000000000.0);
110 cout << "We need to move " << pixels << " pixels" << endl;
111
112 double angle = atan2(target.y-pos.y, target.x-pos.x);
113
114 // we just need to check that we don't overjump the target
115 pos.x += cos(angle)*pixels;
116 pos.y += sin(angle)*pixels;
117 }
118
119 timeLastUpdated.tv_sec = curTS.tv_sec;
120 timeLastUpdated.tv_nsec = curTS.tv_nsec;
121}
Note: See TracBrowser for help on using the repository browser.