Changeset e6c26b8 in network-game for client/Client/main.cpp


Ignore:
Timestamp:
Oct 1, 2013, 8:08:24 PM (11 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
master
Children:
95ffe57
Parents:
373089e
Message:

The client dynamically allocates memory for players and passes around a map with player pointers and some includes are now in individual files instead of in Common.h

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r373089e re6c26b8  
    5454void initWinSock();
    5555void shutdownWinSock();
    56 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed);
     56void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player*>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed);
    5757int getRefreshRate(int width, int height);
    5858void drawMessageStatus(ALLEGRO_FONT* font);
     
    133133   ALLEGRO_TIMER *timer = NULL;
    134134   bool key[4] = { false, false, false, false };
    135    map<unsigned int, Player> mapPlayers;
     135   map<unsigned int, Player*> mapPlayers;
    136136   map<unsigned int, Projectile> mapProjectiles;
    137137   unsigned int curPlayerId = -1;
     
    362362               if (state == STATE_GAME) {
    363363                  // find the current player in the player list
    364                   map<unsigned int, Player>::iterator it;
     364                  map<unsigned int, Player*>::iterator it;
    365365                  Player* p = NULL;
    366366                  for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    367367                  {
    368                      if (it->second.id == curPlayerId)
    369                         p = &it->second;
     368                     if (it->second->id == curPlayerId)
     369                        p = it->second;
    370370                  }
    371371
     
    416416                  cout << "Invalid point: User did not click on the map" << endl;
    417417            }else if (ev.mouse.button == 2) {   // right click
    418                   map<unsigned int, Player>::iterator it;
     418                  map<unsigned int, Player*>::iterator it;
    419419
    420420                  cout << "Detected a right-click" << endl;
     
    423423                  for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    424424                  {
    425                      if (it->second.id == curPlayerId)
    426                         curPlayer = &it->second;
     425                     if (it->second->id == curPlayerId)
     426                        curPlayer = it->second;
    427427                  }
    428428
     
    432432                     // need to check if the right-click was actually on this player
    433433                     // right now, this code will target all players other than the current one
    434                      target = &it->second;
     434                     target = it->second;
    435435                     if (target->id != curPlayerId && target->team != curPlayer->team)
    436436                     {
     
    502502
    503503            // update players
    504             map<unsigned int, Player>::iterator it;
     504            map<unsigned int, Player*>::iterator it;
    505505            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    506506            {
    507                it->second.updateTarget(mapPlayers);
     507               it->second->updateTarget(mapPlayers);
    508508            }
    509509
    510510            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    511511            {
    512                it->second.move(gameMap);   // ignore return value
     512               it->second->move(gameMap);    // ignore return value
    513513            }
    514514
     
    528528               Projectile proj = it2->second;
    529529
    530                FLOAT_POSITION target = mapPlayers[proj.target].pos;
     530               FLOAT_POSITION target = mapPlayers[proj.target]->pos;
    531531               float angle =  atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
    532532
     
    572572      delete game;
    573573
     574   map<unsigned int, Player*>::iterator it;
     575
     576   for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
     577      delete it->second;
     578   }
     579
    574580   al_destroy_event_queue(event_queue);
    575581   al_destroy_display(display);
     
    617623}
    618624
    619 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed)
     625void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player*>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed)
    620626{
    621627   string response = string(msg.buffer);
     
    671677                  Player p("", "");
    672678                  p.deserialize(msg.buffer);
    673                   mapPlayers[p.id] = p;
     679
     680                  if (mapPlayers.find(p.id) != mapPlayers.end())
     681                     delete mapPlayers[p.id];
     682                  mapPlayers[p.id] = new Player(p);
    674683                  curPlayerId = p.id;
    675684
     
    708717                  p.isDead = false;
    709718
    710                mapPlayers[p.id] = p;
     719               if (mapPlayers.find(p.id) != mapPlayers.end())
     720                    delete mapPlayers[p.id];
     721               mapPlayers[p.id] = new Player(p);
    711722
    712723               break;
     
    721732               memcpy(&y, msg.buffer+8, 4);
    722733
    723                mapPlayers[id].target.x = x;
    724                mapPlayers[id].target.y = y;
     734               mapPlayers[id]->target.x = x;
     735               mapPlayers[id]->target.y = y;
    725736
    726737               break;
     
    781792               cout << "target id: " << targetID << endl;
    782793
    783                Player* source = &mapPlayers[id];
     794               Player* source = mapPlayers[id];
    784795               source->targetPlayer = targetID;
    785796               source->isChasing = true;
Note: See TracChangeset for help on using the changeset viewer.