Skip to content

Commit

Permalink
Network Agents are no longer created on the cosntructor
Browse files Browse the repository at this point in the history
  • Loading branch information
MartGon committed Jul 16, 2018
1 parent 0f9163c commit 7d26f9c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
37 changes: 20 additions & 17 deletions SDL-Pong/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ Game::Game()
{
}

Game::Game(SDL_Renderer * renderer, GameMode mode)
Game::Game(SDL_Renderer *renderer, GameMode mode)
{
this->renderer = renderer;
this->gameMode = mode;
gameState = GAME_RUNNING;

if (gameMode == ONLINE_SERVER)
networkAgent = new NetworkServer();
else if(gameMode == ONLINE_CLIENT)
networkAgent = new NetworkClient();
}

Game::~Game()
Expand Down Expand Up @@ -69,6 +64,12 @@ void Game::loadMedia()
// Set scoreboard
player->scoreBoard = scoreBoardOne;
playerTwo->scoreBoard = scoreBoardTwo;

// Creating networkAgents
if (gameMode == ONLINE_SERVER)
networkAgent = new NetworkServer();
else if (gameMode == ONLINE_CLIENT)
networkAgent = new NetworkClient();
}

void Game::startNewGame()
Expand Down Expand Up @@ -176,33 +177,25 @@ void Game::handlePlayersMovement()
if (gameMode != ONLINE_CLIENT)
{
if (currentKeyStates[SDL_SCANCODE_W])
{
player->move(player->MOVE_UP);
}
if (currentKeyStates[SDL_SCANCODE_S])
{
else if (currentKeyStates[SDL_SCANCODE_S])
player->move(player->MOVE_DOWN);
}
}
// If Online client
else
{
if (currentKeyStates[SDL_SCANCODE_W])
{
playerTwo->move(player->MOVE_UP);
}
if (currentKeyStates[SDL_SCANCODE_S])
{
else if (currentKeyStates[SDL_SCANCODE_S])
playerTwo->move(player->MOVE_DOWN);
}
}

switch (gameMode)
{
case TWO_PLAYERS:
if (currentKeyStates[SDL_SCANCODE_UP])
playerTwo->move(player->MOVE_UP);
if (currentKeyStates[SDL_SCANCODE_DOWN])
else if (currentKeyStates[SDL_SCANCODE_DOWN])
playerTwo->move(player->MOVE_DOWN);
break;
case ONLINE_CLIENT:
Expand Down Expand Up @@ -312,6 +305,13 @@ void Game::sendClientData()

void Game::handlePacket(PongPacket* packet)
{
if (!packet)
{
std::cout << "Se perdió la conexión con el otro jugador\n";
loadMainMenu();
return;
}

switch (packet->packetType)
{
case PongPacket::PACKET_BALL_POSITION:
Expand All @@ -331,5 +331,8 @@ void Game::handlePacket(PongPacket* packet)

void Game::destroy()
{
if (isOnline())
networkAgent->destroy();

Scene::destroy();
}
6 changes: 2 additions & 4 deletions SDL-Pong/NetworkAgent.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "NetworkAgent.h"

// Attributes
//bool NetworkAgent::networkEnabled = false;


// Constructors

Expand All @@ -25,7 +25,6 @@ void NetworkAgent::initNetworking()
return;
}

//networkEnabled = true;
}

bool NetworkAgent::readConfigFile()
Expand Down Expand Up @@ -101,7 +100,6 @@ void NetworkAgent::destroy()

// Reset flag
printf("Destroying network agent\n");
//networkEnabled = false;

// Calling SDL
SDLNet_Quit();
Expand All @@ -112,5 +110,5 @@ void NetworkAgent::destroy()

void NetworkAgent::beforeDestroy()
{
printf("Parent method");
printf("Parent method\n");
}
1 change: 0 additions & 1 deletion SDL-Pong/NetworkAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class NetworkAgent

// Methods
// Initializing
//static bool networkEnabled;
static void initNetworking();

// Configuration
Expand Down

0 comments on commit 7d26f9c

Please sign in to comment.