source: network-game/common/Player.cpp@ 7b43385

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

Smooth player movement now works, albeit poorly.

  • Property mode set to 100644
File size: 3.1 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 this->timeLastUpdated = 0;
18}
19
20Player::Player(const Player& p)
21{
22 this->id = p.id;
23 this->name = p.name;
24 this->password = p.password;
25 this->pos.x = p.pos.x;
26 this->pos.y = p.pos.y;
27 this->target.x = p.target.x;
28 this->target.y = p.target.y;
29 this->addr = p.addr;
30}
31
32Player::Player(string name, string password)
33{
34 this->id = 0;
35 this->name = name;
36 this->password = password;
37 this->pos.x = this->target.x = 200;
38 this->pos.y = this->target.y = 200;
39}
40
41Player::Player(string name, sockaddr_in addr)
42{
43 this->id = 0;
44 this->name = name;
45 this->password = "";
46 this->pos.x = this->target.x = 200;
47 this->pos.y = this->target.y = 200;
48 this->addr = addr;
49}
50
51Player::~Player()
52{
53}
54
55void Player::serialize(char* buffer)
56{
57 memcpy(buffer, &this->id, 4);
58 memcpy(buffer+4, &this->pos.x, 4);
59 memcpy(buffer+8, &this->pos.y, 4);
60 memcpy(buffer+12, &this->target.x, 4);
61 memcpy(buffer+16, &this->target.y, 4);
62 strcpy(buffer+20, this->name.c_str());
63}
64
65void Player::deserialize(char* buffer)
66{
67 memcpy(&this->id, buffer, 4);
68 memcpy(&this->pos.x, buffer+4, 4);
69 memcpy(&this->pos.y, buffer+8, 4);
70 memcpy(&this->target.x, buffer+12, 4);
71 memcpy(&this->target.y, buffer+16, 4);
72 this->name.assign(buffer+20);
73
74 cout << "id: " << this->id << endl;
75 cout << "pos x: " << this->pos.x << endl;
76 cout << "pos y: " << this->pos.y << endl;
77 cout << "target x: " << this->target.x << endl;
78 cout << "target y: " << this->target.y << endl;
79 cout << "name: " << this->name << endl;
80}
81
82void Player::setId(int id)
83{
84 this->id = id;
85}
86
87void Player::setAddr(sockaddr_in addr)
88{
89 this->addr = addr;
90}
91
92void Player::move(void) {
93 int speed = 100; // pixels per second
94 unsigned long long curTime = getCurrentMillis();
95
96 // if we're at our target, don't move
97 if (pos.x == target.x && pos.y == target.y)
98 cout << "We're already at our target" << endl;
99 else {
100 cout << "equals test:" << endl;
101 float f = 5.0;
102 int i = 5;
103
104 if (f == i)
105 cout << "test passed" << endl;
106 else
107 cout << "test failed" << endl;
108
109 cout << "Player about to be moved" << endl;
110 cout << "cur pos x: " << this->pos.x << endl;
111 cout << "cur pos y: " << this->pos.y << endl;
112
113 float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
114 cout << "We need to move " << pixels << " pixels" << endl;
115
116 double angle = atan2(target.y-pos.y, target.x-pos.x);
117
118 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
119 if (dist <= pixels) {
120 pos.x = target.x;
121 pos.y = target.y;
122 }else {
123 pos.x += cos(angle)*pixels;
124 pos.y += sin(angle)*pixels;
125 }
126 cout << "new pos x: " << this->pos.x << endl;
127 cout << "new pos y: " << this->pos.y << endl;
128
129 }
130
131 timeLastUpdated = curTime;
132}
Note: See TracBrowser for help on using the repository browser.