Changeset 6e66ffd in network-game for common


Ignore:
Timestamp:
May 21, 2013, 10:00:54 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
5f868c0
Parents:
cc1c6c1
Message:

Add functions to the WorldMap class to allow the server to notify clients of object creation and movement

Location:
common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • common/WorldMap.cpp

    rcc1c6c1 r6e66ffd  
    6767   vector<WorldMap::Object> vctObjectsInRegion;
    6868
     69   vector<WorldMap::Object>::iterator it;
     70   for(it = vctObjects->begin(); it != vctObjects->end(); it++) {
     71      if (it->pos.x/25 == x && it->pos.y/25 == y)
     72         vctObjectsInRegion.push_back(*it);
     73   }
     74
    6975   return vctObjectsInRegion;
    7076}
    7177
    72 void WorldMap::addObject(int x, int y, WorldMap::ObjectType t) {
    73    WorldMap::Object o(t, x, y);
    74 
     78// used by the server to create new objects
     79void WorldMap::addObject(WorldMap::ObjectType t, int x, int y) {
     80   WorldMap::Object o(t, vctObjects->size(), x, y);
    7581   vctObjects->push_back(o);
     82}
     83
     84// used by the client to update object positions or create objects it has not seen before
     85void WorldMap::updateObject(int id, WorldMap::ObjectType t, int x, int y) {
     86   vector<WorldMap::Object>::iterator it;
     87   bool foundObject = false;
     88
     89   for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
     90      if (it->id == id) {
     91         foundObject = true;
     92         it->pos.x = x;
     93         it->pos.y = y;
     94      }
     95   }
     96
     97   if (!foundObject) {
     98      WorldMap::Object o(t, id, x, y);
     99      vctObjects->push_back(o);
     100   }
    76101}
    77102
     
    190215               case 1:
    191216                  structure = STRUCTURE_BLUE_FLAG;
     217                  cout << "Should have added blue flag object" << endl;
    192218                  break;
    193219               case 2:
    194220                  structure = STRUCTURE_RED_FLAG;
     221                  cout << "Should have added red flag object" << endl;
    195222                  break;
    196223               }
     
    213240/*** Functions for Object ***/
    214241
    215 WorldMap::Object::Object(ObjectType type, POSITION pos) {
     242WorldMap::Object::Object(ObjectType type, int id, int x, int y) {
    216243   this->type = type;
    217    this->pos = pos;
    218 }
    219 
    220 WorldMap::Object::Object(ObjectType type, int x, int y) {
    221    this->type = type;
     244   this->id = id;
    222245   this->pos.x = x;
    223246   this->pos.y = y;
    224247}
    225248
     249WorldMap::Object::Object(ObjectType type, int id, POSITION pos) {
     250   this->type = type;
     251   this->id = id;
     252   this->pos = pos;
     253}
     254
    226255WorldMap::Object::~Object() {
    227256}
  • common/WorldMap.h

    rcc1c6c1 r6e66ffd  
    3333   class Object {
    3434   public:
     35      int id;
    3536      ObjectType type;
    3637      POSITION pos;
    3738
    38       Object(ObjectType type, int x, int y);
    39       Object(ObjectType type, POSITION pos);
     39      Object(ObjectType type, int id, int x, int y);
     40      Object(ObjectType type, int id, POSITION pos);
    4041
    4142      ~Object();
     
    5859
    5960   vector<Object> getObjects(int x, int y);
    60    void addObject(int x, int y, ObjectType type);
     61   void addObject(ObjectType type, int x, int y);
     62   void updateObject(int id, WorldMap::ObjectType t, int x, int y);
    6163
    6264   static WorldMap* createDefaultMap();
Note: See TracChangeset for help on using the changeset viewer.