Changeset 60017fc in network-game for common


Ignore:
Timestamp:
Feb 18, 2013, 6:58:40 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
093c141
Parents:
62ee2ce
Message:

Added code for player movement

Location:
common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • common/Common.h

    r62ee2ce r60017fc  
    2020} POSITION;
    2121
     22typedef struct
     23{
     24   float x;
     25   float y;
     26} FLOAT_POSITION;
     27
    2228#endif
  • common/Player.cpp

    r62ee2ce r60017fc  
    44#include <sstream>
    55#include <cstring>
     6#include <cmath>
    67
    78using namespace std;
     
    1213   this->name = "";
    1314   this->password = "";
    14    this->pos.x = 0;
    15    this->pos.y = 0;
     15   this->pos.x = this->target.x = 0;
     16   this->pos.y = this->target.y = 0;
    1617}
    1718
     
    2324   this->pos.x = p.pos.x;
    2425   this->pos.y = p.pos.y;
     26   this->target.x = p.target.x;
     27   this->target.y = p.target.y;
    2528   this->addr = p.addr;
    2629}
     
    3134   this->name = name;
    3235   this->password = password;
    33    this->pos.x = 200;
    34    this->pos.y = 200;
     36   this->pos.x = this->target.x = 200;
     37   this->pos.y = this->target.y = 200;
    3538}
    3639
     
    4043   this->name = name;
    4144   this->password = "";
    42    this->pos.x = 200;
    43    this->pos.y = 200;
     45   this->pos.x = this->target.x = 200;
     46   this->pos.y = this->target.y = 200;
    4447   this->addr = addr;
    4548}
     
    7982   this->addr = addr;
    8083}
     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   // here we move 100 pixels per second
     106   float pixels = 100 * (diffTS.tv_sec+diffTS.tv_nsec/1000000000.0);
     107   cout << "We need to move " << pixels << "pixels" << endl;
     108
     109   double angle = atan2(target.y-pos.y, target.x-pos.x);
     110
     111   // we just need to check that we don't overjump the target
     112   pos.x += cos(angle)*pixels;
     113   pos.y += sin(angle)*pixels;
     114
     115   timeLastUpdated.tv_sec = curTS.tv_sec;
     116   timeLastUpdated.tv_nsec = curTS.tv_nsec;
     117}
  • common/Player.h

    r62ee2ce r60017fc  
    1212
    1313#include <string>
     14#include <sys/time.h>
    1415
    1516#include "Common.h"
     
    3233   void setAddr(sockaddr_in addr);
    3334
     35   void move();
     36
    3437   int id;
    3538   string name;
    3639   string password;
    3740   sockaddr_in addr;
    38    POSITION pos;
     41   FLOAT_POSITION pos;
     42   POSITION target;
     43   timespec timeLastUpdated;
    3944};
    4045
Note: See TracChangeset for help on using the changeset viewer.