Changeset a1a3bd5 in network-game


Ignore:
Timestamp:
Apr 23, 2013, 1:31:54 AM (11 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.

Files:
6 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   }
  • common/Player.cpp

    r054b50b ra1a3bd5  
    8383}
    8484
    85 void Player::move(void) {
     85bool Player::move(WorldMap map) {
    8686   int speed = 100; // pixels per second
    8787   unsigned long long curTime = getCurrentMillis();
     88   bool moveCanceled = false;
    8889
    8990   // if we're at our target, don't move
    9091   if (pos.x != target.x || pos.y != target.y) {
    9192      float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
     93      double angle = atan2(target.y-pos.y, target.x-pos.x);
     94      float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
     95      POSITION newPos;
    9296
    93       double angle = atan2(target.y-pos.y, target.x-pos.x);
    94 
    95       float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
    9697      if (dist <= pixels) {
    9798         pos.x = target.x;
    9899         pos.y = target.y;
    99100      }else {
    100          pos.x += cos(angle)*pixels;
    101          pos.y += sin(angle)*pixels;
     101         newPos.x = int(pos.x + cos(angle)*pixels);
     102         newPos.y = int(pos.y + sin(angle)*pixels);
     103
     104         switch(map.getElement(newPos.x/25, newPos.y/25)) {
     105            case WorldMap.TerrainType.TERRAIN_OCEAN:
     106            case WorldMap.TerrainType.TERRAIN_ROCK:
     107               target.x = pos.x;
     108               target.y = pos.y;
     109               moveCanceled = true;
     110               break;
     111         }
    102112      }
    103113   }
    104114
    105115   timeLastUpdated = curTime;
     116   return !moveCanceled;
    106117}
  • common/Player.h

    r054b50b ra1a3bd5  
    1414
    1515#include "Common.h"
     16#include "WorldMap.h"
    1617
    1718using namespace std;
     
    3233   void setAddr(sockaddr_in addr);
    3334
    34    void move();
     35   bool move(WorldMap map);
    3536
    3637   int id;
  • common/WorldMap.cpp

    r054b50b ra1a3bd5  
    1515
    1616   vctMap = new vector<vector<TerrainType>*>(width);
     17   vctObjects = new vector<vector<ObjectType>*>(width);
    1718
    1819   for (int x=0; x<width; x++) {
    19       vector<TerrainType>* newVector = new vector<TerrainType>(height);
     20      vector<TerrainType>* newMapVector = new vector<TerrainType>(height);
     21      vector<ObjectType>* newObjectVector = new vector<ObjectType>(height);
    2022
    21       for (int y=0; y<height; y++)
    22          (*newVector)[y] = TERRAIN_NONE;
     23      for (int y=0; y<height; y++) {
     24         (*newMapVector)[y] = TERRAIN_NONE;
     25         (*newObjectVector)[y] = OBJECT_NONE;
     26      }
    2327
    24       (*vctMap)[x] = newVector;
     28      (*vctMap)[x] = newMapVector;
     29      (*vctObjects)[x] = newObjectVector;
    2530   }
    2631}
     
    2833WorldMap::~WorldMap()
    2934{
    30    for (int x=0; x<width; x++)
     35   for (int x=0; x<width; x++) {
    3136      delete (*vctMap)[x];
     37      delete (*vctObjects)[x];
     38   }
    3239
    3340   delete vctMap;
     41   delete vctObjects;
    3442}
    3543
     
    4351   cout << "getting element" << endl;
    4452   (*(*vctMap)[x])[y] = t;
     53}
     54
     55WorldMap::ObjectType WorldMap::getObject(int x, int y)
     56{
     57   return (*(*vctObjects)[x])[y];
     58}
     59
     60void WorldMap::setObject(int x, int y, ObjectType t)
     61{
     62   cout << "getting object" << endl;
     63   (*(*vctObjects)[x])[y] = t;
    4564}
    4665
     
    5776         else
    5877            m->setElement(x, y, TERRAIN_GRASS);
     78
     79         m->setObject(x, y, OBJECT_NONE);
    5980      }
    6081   }
     
    102123            istringstream iss(line);
    103124            string token;
    104             int type;
    105             TerrainType terrain;
    106125
    107             for(int x=0; x<width; x++)
    108             {
     126            if (row < height) {
     127               // load terrain
     128
     129               int type;
     130               TerrainType terrain;
     131
     132               for(int x=0; x<width; x++)
     133               {
     134                  getline(iss, token, ',');
     135                  cout << "token: " << token << endl;
     136                  type = atoi(token.c_str());
     137                  cout << "type: " << type << endl;
     138
     139                  switch(type) {
     140                  case 1:
     141                     terrain = TERRAIN_GRASS;
     142                     break;
     143                  case 2:
     144                     terrain = TERRAIN_OCEAN;
     145                     break;
     146                  case 3:
     147                     terrain = TERRAIN_ROCK;
     148                     break;
     149                  }
     150
     151                  cout << "About to set element" << endl;
     152                  cout << "x: " << x << endl;
     153                  cout << "row: " << row << endl;
     154                  m->setElement(x, row, terrain);
     155               }
     156            }else {
     157               // load objects
     158
     159               int x, y, type;
     160               ObjectType object;
     161
    109162               getline(iss, token, ',');
    110                cout << "token: " << token << endl;
     163               cout << "token(x): " << token << endl;
     164               x = atoi(token.c_str());
     165
     166               getline(iss, token, ',');
     167               cout << "token(y): " << token << endl;
     168               y = atoi(token.c_str());
     169
     170               getline(iss, token, ',');
     171               cout << "token(type): " << token << endl;
    111172               type = atoi(token.c_str());
    112                cout << "type: " << type << endl;
    113173
    114174               switch(type) {
     175               case 0:
     176                  object = OBJECT_NONE;
     177                  break;
    115178               case 1:
    116                   terrain = TERRAIN_GRASS;
     179                  object = OBJECT_RED_FLAG;
    117180                  break;
    118181               case 2:
    119                   terrain = TERRAIN_OCEAN;
    120                   break;
    121                case 3:
    122                   terrain = TERRAIN_ROCK;
     182                  object = OBJECT_BLUE_FLAG;
    123183                  break;
    124184               }
    125185
    126                cout << "About to set element" << endl;
    127                cout << "x: " << x << endl;
    128                cout << "row: " << row << endl;
    129                m->setElement(x, row, terrain);
     186               m->setObject(x, y, object);
    130187            }
    131188         }
  • common/WorldMap.h

    r054b50b ra1a3bd5  
    1717   };
    1818
     19   enum ObjectType {
     20      OBJECT_NONE,
     21      OBJECT_RED_FLAG,
     22      OBJECT_BLUE_FLAG
     23   };
     24
    1925   int width, height;
    2026   vector<vector<TerrainType>*>* vctMap;
     27   vector<vector<ObjectType>*>* vctObjects;
    2128
    2229   WorldMap(int width, int height);
     
    2734   void setElement(int x, int y, TerrainType type);
    2835
     36   ObjectType getObject(int x, int y);
     37   void setObject(int x, int y, ObjectType type);
     38
    2939   static WorldMap* createDefaultMap();
    3040   static WorldMap* loadMapFromFile(string filename);
  • data/map.txt

    r054b50b ra1a3bd5  
    12122,1,1,1,1,1,1,1,1,1,1,2
    13132,2,2,2,2,2,2,2,2,2,2,2
     146,1,1
     156,10,2
Note: See TracChangeset for help on using the changeset viewer.