Changeset e6c26b8 in network-game for client/Client


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

Location:
client/Client
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • client/Client/GameRender.cpp

    r373089e re6c26b8  
    5252         }
    5353      }
    54    }
    55 }
    56 
    57 void GameRender::drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
    58 {
    59    map<unsigned int, Player>::iterator it;
    60 
    61    Player* p;
    62    POSITION pos;
    63    ALLEGRO_COLOR color;
    64 
    65    for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    66    {
    67       p = &it->second;
    68 
    69       if (p->isDead)
    70          continue;
    71 
    72       pos = mapToScreen(p->pos.toInt());
    73 
    74       if (p->id == curPlayerId)
    75          al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
    76      
    77       if (p->team == 0)
    78          color = al_map_rgb(0, 0, 255);
    79       else if (p->team == 1)
    80          color = al_map_rgb(255, 0, 0);
    81      
    82       al_draw_filled_circle(pos.x, pos.y, 12, color);
    83 
    84       // draw player class
    85       int fontHeight = al_get_font_line_height(font);
    86 
    87       string strClass;
    88       switch (p->playerClass) {
    89       case Player::CLASS_WARRIOR:
    90          strClass = "W";
    91          break;
    92       case Player::CLASS_RANGER:
    93          strClass = "R";
    94          break;
    95       case Player::CLASS_NONE:
    96          strClass = "";
    97          break;
    98       default:
    99          strClass = "";
    100          break;
    101       }
    102       al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
    103 
    104       // draw player health
    105       al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
    106       if (p->maxHealth != 0)
    107          al_draw_filled_rectangle(pos.x-11, pos.y-23, pos.x-11+(22*p->health)/p->maxHealth, pos.y-17, al_map_rgb(255, 0, 0));
    108 
    109       if (p->hasBlueFlag)
    110          al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
    111       else if (p->hasRedFlag)
    112          al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
    11354   }
    11455}
  • client/Client/GameRender.h

    r373089e re6c26b8  
    2424public:
    2525   static void drawMap(WorldMap* gameMap);
    26    static void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
    2726   static void drawPlayers(map<unsigned int, Player*>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
    2827};
  • 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.