Changeset 62ee2ce in network-game


Ignore:
Timestamp:
Feb 5, 2013, 7:02:32 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
384b7e0, 60017fc
Parents:
60b77d2
Message:

The client shows the map and converts between screen and map coordinates

Files:
5 edited
2 moved

Legend:

Unmodified
Added
Removed
  • client/Client/Client.vcxproj

    r60b77d2 r62ee2ce  
    6565  <ItemGroup>
    6666    <ClCompile Include="..\..\common\Common.cpp" />
     67    <ClCompile Include="..\..\common\WorldMap.cpp" />
    6768    <ClCompile Include="..\..\common\Message.cpp" />
    6869    <ClCompile Include="..\..\common\Player.cpp" />
     
    7778    <ClInclude Include="..\..\common\Common.h" />
    7879    <ClInclude Include="..\..\common\Compiler.h" />
     80    <ClInclude Include="..\..\common\WorldMap.h" />
    7981    <ClInclude Include="..\..\common\Message.h" />
    8082    <ClInclude Include="..\..\common\Player.h" />
  • client/Client/Client.vcxproj.filters

    r60b77d2 r62ee2ce  
    5555      <Filter>Source Files\common</Filter>
    5656    </ClCompile>
     57    <ClCompile Include="..\..\common\WorldMap.cpp">
     58      <Filter>Source Files\gui</Filter>
     59    </ClCompile>
    5760  </ItemGroup>
    5861  <ItemGroup>
     
    8487      <Filter>Header Files\common</Filter>
    8588    </ClInclude>
     89    <ClInclude Include="..\..\common\WorldMap.h">
     90      <Filter>Header Files\common</Filter>
     91    </ClInclude>
    8692  </ItemGroup>
    8793  <ItemGroup>
  • client/Client/main.cpp

    r60b77d2 r62ee2ce  
    2929#include "../../common/Message.h"
    3030#include "../../common/Common.h"
    31 #include "../../common/Player.h"_
     31#include "../../common/Player.h"
     32#include "../../common/WorldMap.h"
    3233
    3334#include "Window.h"
     
    4546void shutdownWinSock();
    4647void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
     48void drawMap(WorldMap* gameMap);
    4749void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
     50POSITION screenToMap(POSITION pos);
     51POSITION mapToScreen(POSITION pos);
    4852
    4953// callbacks
     
    156160   }
    157161
     162   WorldMap* gameMap = WorldMap::createDefaultMap();
     163
    158164   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
    159165   wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
     
    306312            msgTo.type = MSG_TYPE_PLAYER_MOVE;
    307313
    308             memcpy(msgTo.buffer, &curPlayerId, 4);
    309             memcpy(msgTo.buffer+4, &ev.mouse.x, 4);
    310             memcpy(msgTo.buffer+8, &ev.mouse.y, 4);
    311 
    312             sendMessage(&msgTo, sock, &server);
     314            POSITION pos;
     315            pos.x = ev.mouse.x;
     316            pos.y = ev.mouse.y;
     317            pos = screenToMap(pos);
     318
     319            if (pos.x != -1)
     320            {
     321               memcpy(msgTo.buffer, &curPlayerId, 4);
     322               memcpy(msgTo.buffer+4, &pos.x, 4);
     323               memcpy(msgTo.buffer+8, &pos.y, 4);
     324
     325               sendMessage(&msgTo, sock, &server);
     326            }
     327            else
     328               cout << "Invalid point: User did not click on the map" << endl;
    313329         }
    314330      }
     
    326342         wndCurrent->draw(display);
    327343
    328          drawPlayers(mapPlayers, curPlayerId);
    329 
    330344         chatConsole.draw(font, al_map_rgb(255,255,255));
    331345
     
    336350         else if(wndCurrent == wndMain) {
    337351            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
     352
     353            drawMap(gameMap);
     354            drawPlayers(mapPlayers, curPlayerId);
    338355         }
    339356
     
    352369   delete wndLogin;
    353370   delete wndMain;
     371
     372   delete gameMap;
    354373
    355374   al_destroy_event_queue(event_queue);
     
    392411   WSACleanup();
    393412#endif
     413}
     414
     415POSITION screenToMap(POSITION pos)
     416{
     417   pos.x = pos.x-300;
     418   pos.y = pos.y-100;
     419
     420   if (pos.x < 0 || pos.y < 0)
     421   {
     422      pos.x = -1;
     423      pos.y = -1;
     424   }
     425
     426   return pos;
     427}
     428
     429POSITION mapToScreen(POSITION pos)
     430{
     431   pos.x = pos.x+300;
     432   pos.y = pos.y+100;
     433
     434   return pos;
    394435}
    395436
     
    498539}
    499540
     541void drawMap(WorldMap* gameMap)
     542{
     543   POSITION mapPos;
     544   mapPos.x = 0;
     545   mapPos.y = 0;
     546   mapPos = mapToScreen(mapPos);
     547   for (int x=0; x<12; x++)
     548   {
     549      for (int y=0; y<12; y++)
     550      {
     551         WorldMap::TerrainType el = gameMap->getElement(x, y);
     552
     553         if (el == WorldMap::TERRAIN_GRASS)
     554            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(0, 255, 0));
     555         else if (el == WorldMap::TERRAIN_OCEAN)
     556            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(0, 0, 255));
     557         else if (el == WorldMap::TERRAIN_ROCK)
     558            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));
     559      }
     560   }
     561}
     562
    500563void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId)
    501564{
    502565   map<unsigned int, Player>::iterator it;
     566
     567   Player* p;
     568   POSITION pos;
    503569
    504570   for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    505571   {
    506       Player *p = &it->second;
     572      p = &it->second;
     573      pos = mapToScreen(p->pos);
    507574
    508575      if (p->id == curPlayerId)
    509          al_draw_filled_circle(p->pos.x, p->pos.y, 15, al_map_rgb(0, 255, 0));
     576         al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(255, 0, 0));
    510577      else
    511          al_draw_filled_circle(p->pos.x, p->pos.y, 30, al_map_rgb(255, 0, 0));
     578         al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0));
    512579   }
    513580}
  • common/Common.h

    r60b77d2 r62ee2ce  
    1818   int x;
    1919   int y;
    20 } PLAYER_POS;
     20} POSITION;
    2121
    2222#endif
  • common/Player.h

    r60b77d2 r62ee2ce  
    3636   string password;
    3737   sockaddr_in addr;
    38    PLAYER_POS pos;
     38   POSITION pos;
    3939};
    4040
  • common/WorldMap.cpp

    r60b77d2 r62ee2ce  
    1 #include "Map.h"
     1#include "WorldMap.h"
    22
    33using namespace std;
    44
    5 Map::Map(int width, int height)
     5WorldMap::WorldMap(int width, int height)
    66{
    77   this->width = width;
     
    2020}
    2121
    22 Map::~Map()
     22WorldMap::~WorldMap()
    2323{
    2424   for (int x=0; x<width; x++)
     
    2828}
    2929
    30 void Map::setElement(int x, int y, TerrainType t)
     30WorldMap::TerrainType WorldMap::getElement(int x, int y)
     31{
     32   return (*(*vctMap)[x])[y];
     33}
     34
     35void WorldMap::setElement(int x, int y, TerrainType t)
    3136{
    3237   (*(*vctMap)[x])[y] = t;
    3338}
    3439
    35 Map* Map::createDefaultMap()
     40WorldMap* WorldMap::createDefaultMap()
    3641{
    37    Map* m = new Map(20l, 20);
     42   WorldMap* m = new WorldMap(12l, 12);
    3843
    39    for(int x=0; x<20; x++)
     44   for(int x=0; x<12; x++)
    4045   {   
    41       for(int y=0; y<20; y++)
     46      for(int y=0; y<12; y++)
    4247      {
    43          if (x ==0 || y == 0 || x == 19 || y == 19)
     48         if (x ==0 || y == 0 || x == 11 || y == 11)
    4449            m->setElement(x, y, TERRAIN_OCEAN);
    4550         else
     
    4853   }
    4954
     55   m->setElement(5, 5, TERRAIN_ROCK);
     56
    5057   return m;
    5158}
  • common/WorldMap.h

    r60b77d2 r62ee2ce  
    1 #ifndef _MAP_H
    2 #define _MAP_H
     1#ifndef _WORLDMAP_H
     2#define _WORLDMAP_H
    33
    44#include <vector>
     
    66using namespace std;
    77
    8 class Map {
     8class WorldMap {
    99public:
    1010   enum TerrainType {
    1111      TERRAIN_NONE,
    1212      TERRAIN_GRASS,
    13       TERRAIN_OCEAN
     13      TERRAIN_OCEAN,
     14      TERRAIN_ROCK
    1415   };
    1516
     
    1718   vector<vector<TerrainType>*>* vctMap;
    1819
    19    Map(int width, int height);
     20   WorldMap(int width, int height);
    2021
    21    ~Map();
     22   ~WorldMap();
    2223
     24   TerrainType getElement(int x, int y);
    2325   void setElement(int x, int y, TerrainType type);
    2426
    25    static Map* createDefaultMap();
     27   static WorldMap* createDefaultMap();
    2628};
    2729
Note: See TracChangeset for help on using the changeset viewer.