Changeset 6e66ffd in network-game


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

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    rcc1c6c1 r6e66ffd  
    530530   mapPos.y = 0;
    531531   mapPos = mapToScreen(mapPos);
     532
    532533   for (int x=0; x<12; x++)
    533534   {
     
    544545            al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(100, 100, 0));
    545546
    546          if (structure == WorldMap::STRUCTURE_BLUE_FLAG)
    547             al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(0, 0, 255));
    548          else if (structure == WorldMap::STRUCTURE_RED_FLAG)
    549             al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(255, 0, 0));
     547         if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
     548            al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
     549            //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(0, 0, 255));
     550         }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
     551            al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
     552            //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(255, 0, 0));
     553         }
     554      }
     555   }
     556
     557   for (int x=0; x<12; x++)
     558   {
     559      for (int y=0; y<12; y++)
     560      {
     561         vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
     562
     563         vector<WorldMap::Object>::iterator it;
     564         for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
     565            switch(it->type) {
     566               case WorldMap::OBJECT_BLUE_FLAG:
     567                  al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(0, 0, 255));
     568                  break;
     569               case WorldMap::OBJECT_RED_FLAG:
     570                  al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(255, 0, 0));
     571                  break;
     572            }
     573         }
    550574      }
    551575   }
  • 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();
  • server/server.cpp

    rcc1c6c1 r6e66ffd  
    9595
    9696   WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
    97  
     97
     98   // add some items to the map. They will be sent out
     99   // to players when they login
     100   // m->addObject(x*25+12, y*25+12, OBJECT_BLUE_FLAG);
     101   // m->addObject(x*25+12, y*25+12, OBJECT_RED_FLAG);
     102
    98103   sock = socket(AF_INET, SOCK_DGRAM, 0);
    99104   if (sock < 0) error("Opening socket");
Note: See TracChangeset for help on using the changeset viewer.