Changeset ff2133a in network-game


Ignore:
Timestamp:
Jun 11, 2013, 1:24:09 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
5b1e31e
Parents:
11d21ee
Message:

Move player chasing behavior to the Player class so the same behavior can be run client-side

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    r11d21ee rff2133a  
    191191   return moving;
    192192}
     193
     194void Player::updateTarget(map<unsigned int, Player>& mapPlayers) {
     195   if (this->isChasing) {
     196      this->target.x = mapPlayers[this->targetPlayer].pos.x;
     197      this->target.y = mapPlayers[this->targetPlayer].pos.y;
     198
     199      if (posDistance(this->pos, this->target.toFloat()) <= this->range) {
     200         this->target.x = this->pos.x;
     201         this->target.y = this->pos.y;
     202
     203         this->isChasing = false;
     204         this->isAttacking = true;
     205         this->timeAttackStarted = getCurrentMillis();
     206      }
     207   }
     208}
  • common/Player.h

    r11d21ee rff2133a  
    1212
    1313#include <string>
     14#include <map>
    1415
    1516#include "Common.h"
     
    4647   void deserialize(char* buffer);
    4748
     49   void updateTarget(map<unsigned int, Player>& mapPlayers);
    4850   bool move(WorldMap *map);
    4951
    50    void takeFlag(int flag, WorldMap *map);
    51    void dropFlag(int flag, WorldMap *map);
     52   void takeFlag(int flag, WorldMap* map);
     53   void dropFlag(int flag, WorldMap* map);
    5254
    5355   int id;
  • common/Projectile.cpp

    r11d21ee rff2133a  
    7272
    7373bool Projectile::move(map<unsigned int, Player>& mapPlayers) {
     74   // if the current target logs off, this method will run into problems
     75
     76   //cout << "Inside projectile move" << endl;
     77
    7478   unsigned long long curTime = getCurrentMillis();
    7579   Player targetP = mapPlayers[target];
     
    8589   float dist = sqrt(pow(targetP.pos.x-pos.x, 2) + pow(targetP.pos.y-pos.y, 2));
    8690
     91   //cout << "About to finish projectile move" << endl;
     92
    8793   if (dist <= pixels) {
    8894      pos.x = targetP.pos.x;
  • server/server.cpp

    r11d21ee rff2133a  
    149149         // set targets for all chasing players (or make them attack if they're close enough)
    150150         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
    151             Player* p = &it->second;
    152 
     151            //Player* p = &it->second;
     152            it->second.updateTarget(mapPlayers);
     153
     154            /*
    153155            if (p->isChasing) {
    154156               p->target.x = mapPlayers[p->targetPlayer].pos.x;
     
    164166               }
    165167            }
     168            */
    166169         }
    167170
     
    171174         bool broadcastMove = false;
    172175         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
     176            cout << "Starting new for loop iteration" << endl;
    173177            oldPos = it->second.pos;
    174178            if (it->second.move(gameMap)) {
     
    362366
    363367               if (it->second.attackType == Player::ATTACK_MELEE) {
     368                  cout << "Melee attack" << endl;
     369
    364370                  Player* target = &mapPlayers[it->second.targetPlayer];
    365371
     
    371377                  target->serialize(serverMsg.buffer);
    372378               }else if (it->second.attackType == Player::ATTACK_RANGED) {
     379                  cout << "Ranged attack" << endl;
     380
    373381                  Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);
    374382                  proj.id = unusedProjectileId;
     
    389397
    390398               // broadcast either a PLAYER or PROJECTILE message
     399               cout << "Broadcasting player or projectile message" << endl;
     400               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
     401               {
     402                  if (sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     403                     error("sendMessage");
     404               }
     405               cout << "Done broadcasting" << endl;
     406            }
     407         }
     408
     409         cout << "Done with the for loop" << endl;
     410
     411         // move all projectiles
     412         cout << "Moving projectiles" << endl;
     413         map<unsigned int, Projectile>::iterator itProj;
     414         for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
     415            if (itProj->second.move(mapPlayers)) {
     416               // send a REMOVE_PROJECTILE message
     417               cout << "send a REMOVE_PROJECTILE message" << endl;
     418               serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
     419               memcpy(serverMsg.buffer, &itProj->second.id, 4);
     420               mapProjectiles.erase(itProj->second.id);
     421
     422               map<unsigned int, Player>::iterator it2;
     423               cout << "Broadcasting REMOVE_PROJECTILE" << endl;
    391424               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    392425               {
     
    394427                     error("sendMessage");
    395428               }
    396             }
    397          }
    398 
    399          // move all projectiles
    400          map<unsigned int, Projectile>::iterator itProj;
    401          for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
    402             if (itProj->second.move(mapPlayers)) {
    403                // send a REMOVE_PROJECTILE message
    404                serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
    405                memcpy(serverMsg.buffer, &itProj->second.id, 4);
    406                mapProjectiles.erase(itProj->second.id);
    407 
    408                map<unsigned int, Player>::iterator it2;
     429
     430               cout << "send a PLAYER message after dealing damage" << endl;
     431               // send a PLAYER message after dealing damage
     432               Player* target = &mapPlayers[itProj->second.target];
     433
     434               target->health -= itProj->second.damage;
     435               if (target->health < 0)
     436                  target->health = 0;
     437
     438               serverMsg.type = MSG_TYPE_PLAYER;
     439               target->serialize(serverMsg.buffer);
     440
     441               cout << "Sending a PLAYER message" << endl;
    409442               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    410443               {
     
    412445                     error("sendMessage");
    413446               }
    414 
    415                // send a PLAYER message after dealing damage
    416                Player* target = &mapPlayers[itProj->second.target];
    417 
    418                target->health -= itProj->second.damage;
    419                if (target->health < 0)
    420                   target->health = 0;
    421 
    422                serverMsg.type = MSG_TYPE_PLAYER;
    423                target->serialize(serverMsg.buffer);
    424 
    425                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
    426                {
    427                   if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
    428                      error("sendMessage");
    429                }
    430             }
    431          }
    432       }
     447            }
     448         }
     449      }
     450      cout << "Done moving projectiles" << endl;
    433451
    434452      n = receiveMessage(&clientMsg, sock, &from);
Note: See TracChangeset for help on using the changeset viewer.