Changes in client/Client/main.cpp [5c7f28d:1e250bf] in network-game


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r5c7f28d r1e250bf  
    1515#include <cstdio>
    1616#include <cstdlib>
    17 #include <cmath>
     17//#include <cmath>
    1818#include <sys/types.h>
    1919#include <string>
     
    5757void initWinSock();
    5858void shutdownWinSock();
    59 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, unsigned int& curPlayerId);
     59void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers,
     60                    map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId);
    6061int getRefreshRate(int width, int height);
    6162void drawMessageStatus(ALLEGRO_FONT* font);
     
    133134   ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    134135   ALLEGRO_TIMER *timer = NULL;
    135    bool key[4] = { false, false, false, false };
    136136   map<unsigned int, Player*> mapPlayers;
     137   map<unsigned int, Projectile> mapProjectiles;
    137138   unsigned int curPlayerId = -1;
    138139   ofstream outputLog;
     
    369370            case ALLEGRO_KEY_D:  // drop the current item
    370371               if (state == STATE_GAME) {
     372                  Player* p = NULL;
    371373                  try {
    372                      Player* p = mapPlayers.at(curPlayerId);
     374                     p = mapPlayers.at(curPlayerId);
     375                  } catch (const out_of_range& ex) {}
     376
     377                  if (p != NULL) {
    373378                     int flagType = OBJECT_NONE;
    374379
     
    383388                        msgProcessor.sendMessage(&msgTo, &server);
    384389                     }
    385                   } catch (const out_of_range& ex) {}
     390                  }
    386391               }
    387392               break;
     
    422427               for(it = playersInGame.begin(); it != playersInGame.end(); it++)
    423428               {
     429                  // need to check if the right-click was actually on this player
     430                  // right now, this code will target all players other than the current one
    424431                  target = it->second;
    425432                  cout << "set target" << endl;
     
    451458
    452459      if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
    453          processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
     460         processMessage(msgFrom, state, chatConsole, mapPlayers, mapProjectiles, curPlayerId);
    454461
    455462      if (redraw)
     
    623630}
    624631
    625 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, unsigned int& curPlayerId)
     632void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers,
     633                    map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId)
    626634{
    627635   // this is outdated since most messages now don't contain just a text string
     
    697705               cout << "Got a logout message" << endl;
    698706
    699                int playerId;
     707               unsigned int playerId;
    700708
    701709               // Check if it's about you or another player
     
    781789               break;
    782790            }
     791            case MSG_TYPE_PROJECTILE:
     792            {
     793               cout << "Received a PROJECTILE message" << endl;
     794
     795               unsigned int id, x, y, targetId;
     796
     797               memcpy(&id, msg.buffer, 4);
     798               memcpy(&x, msg.buffer+4, 4);
     799               memcpy(&y, msg.buffer+8, 4);
     800               memcpy(&targetId, msg.buffer+12, 4);
     801
     802               cout << "id: " << id << endl;
     803               cout << "x: " << x << endl;
     804               cout << "y: " << y << endl;
     805               cout << "Target: " << targetId << endl;
     806
     807               Projectile proj(x, y, targetId, 0);
     808               proj.setId(id);
     809
     810               mapProjectiles[id] = proj;
     811
     812               break;
     813            }
     814            case MSG_TYPE_REMOVE_PROJECTILE:
     815            {
     816                cout << "Received a REMOVE_PROJECTILE message" << endl;
     817
     818               int id;
     819               memcpy(&id, msg.buffer, 4);
     820               
     821               mapProjectiles.erase(id);
     822
     823               break;
     824            }
    783825            case MSG_TYPE_GAME_INFO:
    784826            {
     
    928970               else
    929971                  mapPlayers[p.getId()] = new Player(p);
    930 
    931                break;
    932             }
    933              case MSG_TYPE_LOGOUT:
    934             {
    935                cout << "Got a logout message" << endl;
    936 
    937                int playerId;
    938 
    939                // Check if it's about you or another player
    940                memcpy(&playerId, msg.buffer, 4);
    941                response = string(msg.buffer+4);
    942 
    943                if (playerId == curPlayerId)
    944                   cout << "Received MSG_TYPE_LOGOUT for self in STATE_GAME. This shouldn't happen." << endl;
    945                else
    946                   delete mapPlayers[playerId];
    947972
    948973               break;
Note: See TracChangeset for help on using the changeset viewer.