Changeset a1a3bd5 in network-game for client


Ignore:
Timestamp:
Apr 23, 2013, 1:31:54 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
227baaa
Parents:
054b50b
Message:

Made client changes for smooth player movement, changed the player move method to check the map for obstacles and return a bool indicating success or failure.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r054b50b ra1a3bd5  
    7575bool doexit;
    7676
    77 map<unsigned int, Player> mapPlayers;
    78 
    7977Window* wndLogin;
    8078Window* wndMain;
     
    226224      }
    227225      else if(ev.type == ALLEGRO_EVENT_TIMER) {
    228          redraw = true;
     226         redraw = true; // seems like we should just call a draw function here instead
    229227      }
    230228      else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
    231229         doexit = true;
     230
     231         // perform a check to see if it's time to send an update to the server
     232         // need to store num ticks since the lst update for this
     233         // also check how often each update actually happens and how much it deviates from 60 times per second
    232234      }
    233235      else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
     
    264266      if (receiveMessage(&msgFrom, sock, &from) >= 0)
    265267         processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
    266  
    267       // update player positions
    268       map<unsigned int, Player>::iterator it;
    269       for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    270       {
    271          it->second.move();
    272       }
    273 
    274       if (redraw && al_is_event_queue_empty(event_queue))
     268
     269      if (redraw)
    275270      {
    276271         redraw = false;
     
    286281         else if(wndCurrent == wndMain) {
    287282            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
     283
     284            // update player positions
     285            map<unsigned int, Player>::iterator it;
     286            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
     287            {
     288               it->second.move();   // ignore return value
     289            }
    288290
    289291            drawMap(gameMap);
     
    382384{
    383385   string response = string(msg.buffer);
     386
     387   cout << "Processing message" << endl;
     388   cout << response << endl;
    384389
    385390   switch(state)
     
    419424                  cout << "Got a valid login response with the player" << endl;
    420425                  cout << "Player id: " << curPlayerId << endl;
     426                  cout << "map size: " << mapPlayers.size() << endl;
    421427               }
     428
     429               break;
     430            }
     431            case MSG_TYPE_PLAYER:   // kind of hacky to put this here
     432            {
     433               cout << "Got MSG_TYPE_PLAYER message in Start" << endl;
     434
     435               Player p("", "");
     436               p.deserialize(msg.buffer);
     437               p.timeLastUpdated = getCurrentMillis();
     438               mapPlayers[p.id] = p;
     439
     440               cout << "new player id: " << p.id << endl;
     441               cout << "map size: " << mapPlayers.size() << endl;
    422442
    423443               break;
     
    437457            case MSG_TYPE_LOGIN:
    438458            {
     459               cout << "Got a login message" << endl;
     460               
    439461               chatConsole.addLine(response);
    440 
     462               cout << "Added new line" << endl;
     463
     464               break;
     465            }
     466            case MSG_TYPE_LOGOUT:
     467            {
    441468               cout << "Got a logout message" << endl;
     469
    442470               if (response.compare("You have successfully logged out.") == 0)
    443471               {
     
    446474                  wndCurrent = wndLogin;
    447475               }
    448                else
    449                {
    450                   cout << "Added new line" << endl;
    451                }
    452 
    453                break;
    454             }
    455             case MSG_TYPE_LOGOUT:
    456             {
    457                cout << "Got a logout message, but we don't process it" << endl;
    458476
    459477               break;
     
    461479            case MSG_TYPE_PLAYER:
    462480            {
     481               cout << "Got MSG_TYPE_PLAYER message in Login" << endl;
     482
    463483               Player p("", "");
    464484               p.deserialize(msg.buffer);
    465485               p.timeLastUpdated = getCurrentMillis();
    466486               mapPlayers[p.id] = p;
     487
     488               break;
     489            }
     490            case MSG_TYPE_PLAYER_MOVE:
     491            {
     492               cout << "Got a player move message" << endl;
     493
     494               unsigned int id;
     495               int x, y;
     496
     497               memcpy(&id, msg.buffer, 4);
     498               memcpy(&x, msg.buffer+4, 4);
     499               memcpy(&y, msg.buffer+8, 4);
     500
     501               mapPlayers[id].target.x = x;
     502               mapPlayers[id].target.y = y;
    467503
    468504               break;
     
    498534      {
    499535         WorldMap::TerrainType el = gameMap->getElement(x, y);
     536         WorldMap::ObjectType obj = gameMap->getObject(x, y);
    500537
    501538         if (el == WorldMap::TERRAIN_GRASS)
     
    505542         else if (el == WorldMap::TERRAIN_ROCK)
    506543            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));
     544
     545         if (obj == WorldMap::OBJECT_RED_FLAG)
     546            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         else if (obj == WorldMap::OBJECT_BLUE_FLAG)
     548            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));
    507549      }
    508550   }
Note: See TracChangeset for help on using the changeset viewer.