Changeset e6c26b8 in network-game


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

Files:
10 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;
  • common/Common.h

    r373089e re6c26b8  
    11#ifndef _COMMON_H
    22#define _COMMON_H
    3 
    4 #include "Compiler.h"
    5 
    6 #if defined WINDOWS
    7    #include <winsock2.h>
    8    #include <ws2tcpip.h>
    9 #elif defined LINUX
    10    #include <fcntl.h>
    11    #include <assert.h>
    12 #endif
    133
    144#include <string>
  • common/MessageContainer.h

    r373089e re6c26b8  
    44#include <string>
    55
    6 #include "Common.h"
     6#include "Compiler.h"
    77
    8 #if defined LINUX
     8#if defined WINDOWS
     9   #include <winsock2.h>
     10#elif defined LINUX
    911   #include <netinet/in.h>
    1012#endif
  • common/MessageProcessor.cpp

    r373089e re6c26b8  
    33#include <iostream>
    44#include <fstream>
     5
     6#include "Compiler.h"
     7
     8#if defined WINDOWS
     9   #include <ws2tcpip.h>
     10#endif
     11
     12#include "Common.h"
    513
    614MessageProcessor::MessageProcessor() {
  • common/Player.cpp

    r373089e re6c26b8  
    206206}
    207207
    208 bool Player::updateTarget(map<unsigned int, Player>& mapPlayers) {
     208bool Player::updateTarget(map<unsigned int, Player*>& mapPlayers) {
    209209   if (this->isChasing) {
    210       this->target.x = mapPlayers[this->targetPlayer].pos.x;
    211       this->target.y = mapPlayers[this->targetPlayer].pos.y;
     210      this->target.x = mapPlayers[this->targetPlayer]->pos.x;
     211      this->target.y = mapPlayers[this->targetPlayer]->pos.y;
    212212
    213213      if (posDistance(this->pos, this->target.toFloat()) <= this->range) {
  • common/Player.h

    r373089e re6c26b8  
    55#include <map>
    66
    7 #include "Common.h"
     7#include "Compiler.h"
    88
    9 #if defined LINUX
     9#if defined WINDOWS
     10   #include <winsock2.h>
     11#elif defined LINUX
    1012   #include <netinet/in.h>
    1113#endif
     
    4648   void deserialize(char* buffer);
    4749
    48    bool updateTarget(map<unsigned int, Player>& mapPlayers);
     50   bool updateTarget(map<unsigned int, Player*>& mapPlayers);
    4951   bool move(WorldMap *map);
    5052
  • common/Projectile.cpp

    r373089e re6c26b8  
    7171}
    7272
    73 bool Projectile::move(map<unsigned int, Player>& mapPlayers) {
     73bool Projectile::move(map<unsigned int, Player*>& mapPlayers) {
    7474   // if the current target logs off, this method will run into problems
    7575
    7676   unsigned long long curTime = getCurrentMillis();
    7777
    78    Player targetP = mapPlayers[target];
     78   Player* targetP = mapPlayers[target];
    7979
    8080   if (timeLastUpdated == 0) {
     
    8585
    8686   float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
    87    double angle = atan2(targetP.pos.y-pos.y, targetP.pos.x-pos.x);
    88    float dist = sqrt(pow(targetP.pos.x-pos.x, 2) + pow(targetP.pos.y-pos.y, 2));
     87   double angle = atan2(targetP->pos.y-pos.y, targetP->pos.x-pos.x);
     88   float dist = sqrt(pow(targetP->pos.x-pos.x, 2) + pow(targetP->pos.y-pos.y, 2));
    8989
    9090   if (dist <= pixels) {
    91       pos.x = targetP.pos.x;
    92       pos.y = targetP.pos.y;
     91      pos.x = targetP->pos.x;
     92      pos.y = targetP->pos.y;
    9393      return true;
    9494   }else {
  • common/Projectile.h

    r373089e re6c26b8  
    2626
    2727   // returns true if it reached the target and should be deleted
    28    bool move(map<unsigned int, Player>& mapPlayers);
     28   bool move(map<unsigned int, Player*>& mapPlayers);
     29
     30   /*
     31    * target should become a Player*. When this object gets serialized, the player's id should be sent.
     32    * Deserialization in this case might be tricky since it will require a playerMap to turn the id into a Plauyer*
     33    */
    2934
    3035   int id;
Note: See TracChangeset for help on using the changeset viewer.