- Timestamp:
- Feb 24, 2013, 12:09:43 AM (12 years ago)
- Branches:
- master
- Children:
- ca44f82
- Parents:
- 66906aa
- Location:
- common
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Common.cpp
r66906aa r8f85180 1 1 #include "Common.h" 2 3 #include <iostream> 4 using namespace std; 5 6 #if defined WINDOWS 7 #include <Windows.h> 8 #elif defined LINUX 9 #include <time.h> 10 #endif 2 11 3 12 void set_nonblock(int sock) … … 13 22 #endif 14 23 } 24 25 unsigned long long getCurrentMillis() 26 { 27 unsigned long long numMilliseconds; 28 29 #if defined WINDOWS 30 numMilliseconds = GetTickCount(); 31 #elif defined LINUX 32 timespec curTime; 33 clock_gettime(CLOCK_REALTIME, &curTime); 34 35 numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000; 36 #endif 37 38 return numMilliseconds; 39 } -
common/Common.h
r66906aa r8f85180 13 13 14 14 void set_nonblock(int sock); 15 unsigned long long getCurrentMillis(); 15 16 16 17 typedef struct -
common/Player.cpp
r66906aa r8f85180 15 15 this->pos.x = this->target.x = 0; 16 16 this->pos.y = this->target.y = 0; 17 this->timeLastUpdated = 0; 17 18 } 18 19 … … 55 56 { 56 57 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); 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()); 60 63 } 61 64 … … 63 66 { 64 67 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 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); 68 73 69 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; 70 79 cout << "name: " << this->name << endl; 71 cout << "x: " << this->pos.x << endl;72 cout << "y: " << this->pos.y << endl;73 80 } 74 81 … … 84 91 85 92 void Player::move(void) { 86 // timeLastMoved87 // pos88 // target89 93 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; 94 unsigned long long curTime = getCurrentMillis(); 104 95 105 96 // if we're at our target, don't move 106 if (pos.x == target.x ||pos.y == target.y)97 if (pos.x == target.x && pos.y == target.y) 107 98 cout << "We're already at our target" << endl; 108 99 else { 109 float pixels = speed * ( diffTS.tv_sec+diffTS.tv_nsec/1000000000.0);100 float pixels = speed * (curTime-timeLastUpdated) / 1000.0; 110 101 cout << "We need to move " << pixels << " pixels" << endl; 111 102 112 103 double angle = atan2(target.y-pos.y, target.x-pos.x); 113 104 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; 105 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2)); 106 if (dist <= pixels) { 107 pos.x = target.x; 108 pos.y = target.y; 109 }else { 110 pos.x += cos(angle)*pixels; 111 pos.y += sin(angle)*pixels; 112 } 117 113 } 118 114 119 timeLastUpdated.tv_sec = curTS.tv_sec; 120 timeLastUpdated.tv_nsec = curTS.tv_nsec; 115 timeLastUpdated = curTime; 121 116 } -
common/Player.h
r66906aa r8f85180 41 41 FLOAT_POSITION pos; 42 42 POSITION target; 43 timespectimeLastUpdated;43 unsigned long long timeLastUpdated; 44 44 }; 45 45
Note:
See TracChangeset
for help on using the changeset viewer.