Skip to content

Commit

Permalink
Fixed game end bug + displaying text now on game end
Browse files Browse the repository at this point in the history
  • Loading branch information
TanishTuteja committed Apr 17, 2022
1 parent 296530e commit 98aa9c9
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 74 deletions.
110 changes: 78 additions & 32 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
LGame::LGame(LWindow &window, std::string playerName, std::string opponentName, int &sockfd, sockaddr_in &theiraddr) : LScreen(window), playerName(playerName), opponentName(opponentName), sockfd(sockfd), theirAddr(theiraddr)
{
window.maximize();
std::cout << "Name: " << playerName << " Opponent: " << opponentName << std::endl;
// std::cout << "Name: " << playerName << " Opponent: " << opponentName << std::endl;
backGroundMusic = Mix_LoadMUS("resources/music.mpeg");
introMusic = Mix_LoadWAV("resources/intro.wav");
Mix_PlayChannel(-1, introMusic, 0);
Expand Down Expand Up @@ -54,6 +54,29 @@ void LGame::initTasks()

void LGame::handleEvent(SDL_Event &e)
{
if (waitingEnd)
{
return;
}

if (gameEnded)
{
switch (e.type)
{
case SDL_KEYDOWN:
{
MainMenu *mainMenu = new MainMenu(window, sockfd, theirAddr);
window.setCurrScreen(mainMenu);
break;
}
default:
{
break;
}
}
return;
}

players[0].handleEvent(e);
int lastTileType = players[0].getLastTileType();
if (lastTileType != -1)
Expand All @@ -64,6 +87,31 @@ void LGame::handleEvent(SDL_Event &e)

void LGame::update()
{

if (gameEnded)
{
return;
}

if (waitingEnd)
{

unsigned int len = sizeof(theirAddr);
int n = recvfrom(sockfd, (char *)recBuf, 512, MSG_WAITALL, (struct sockaddr *)&theirAddr, &len);
if (n != -1)
{
Message *msg = deserialize(recBuf);
if (msg->type == 4)
{
GameResultMessage *gameResultMessage = dynamic_cast<GameResultMessage *>(msg);
result = gameResultMessage->won;
gameEnded = true;
}
delete msg;
}
return;
}

camera = {camera.x, camera.y, window.getWidth() - tasksVPWidth, window.getHeight() - 3 * gyRenderOffset - 5 * gyPadding};
// Move the dot
for (int i = 0; i < players.size(); i++)
Expand All @@ -86,7 +134,7 @@ void LGame::update()
SDL_Rect NPCbox = NPCs[i].getBox();
int NPCtileX = (NPCbox.x + NPCbox.w / 2) / mTileWidth;
int NPCtileY = (NPCbox.y + NPCbox.h / 2) / mTileHeight;
std::cout << NPCtileX << " " << NPCtileY << "\n";
// std::cout << NPCtileX << " " << NPCtileY << "\n";
if (((tileX >= NPCtileX - 1) && (tileX <= NPCtileX + 1)) && ((tileY >= NPCtileY - 1) && (tileY <= NPCtileY + 1)))
{
std ::cout << "collision with " << NPCs[i].getName() << "\n";
Expand Down Expand Up @@ -138,7 +186,7 @@ void LGame::update()
gameUpdateMsg.money = players[0].getMoney();
gameUpdateMsg.points = players[0].getPoints();
gameUpdateMsg.health = players[0].getHealth();
std::cout << "Sending health " << gameUpdateMsg.health << std::endl;
// std::cout << "Sending health " << gameUpdateMsg.health << std::endl;
int bytesUsed = serialize(&gameUpdateMsg, buf);

sendto(sockfd, buf, bytesUsed, 0, (const struct sockaddr *)&theirAddr,
Expand All @@ -162,7 +210,7 @@ void LGame::update()
players[1].setPoints(updateMsg->points);
players[1].setMoney(updateMsg->money);
players[1].setHealth(updateMsg->health);
std::cout << "Receiving health " << gameUpdateMsg.health << std::endl;
// std::cout << "Receiving health " << gameUpdateMsg.health << std::endl;
}
delete msg;
}
Expand All @@ -172,9 +220,11 @@ void LGame::update()
}
pointsTextOpp->setText("POINTS: " + std::to_string(players[1].getPoints()));
moneyTextOpp->setText("MONEY: " + std::to_string(players[1].getMoney()));

if (secs / secsPerHour >= 24)
{
gameEnd();
return;
}
}

Expand All @@ -189,32 +239,9 @@ void LGame::gameEnd()
sendto(sockfd, buf, bytesUsed, 0, (const struct sockaddr *)&theirAddr,
sizeof(theirAddr));

unsigned int len = sizeof(theirAddr);
int type;
Message *msg;
do
{
int n;
do
{
n = recvfrom(sockfd, (char *)recBuf, 512, MSG_WAITALL, (struct sockaddr *)&theirAddr, &len);
} while (n == -1);
msg = deserialize(recBuf);
} while (msg->type != 4);
GameResultMessage *gameResultMessage = dynamic_cast<GameResultMessage *>(msg);
if (gameResultMessage->won)
{
std::cout << "YOU WON!" << std::endl;
}
else
{
std::cout << "YOU LOST!" << std::endl;
}
std::cout << "Going back to main menu in 5 secs..." << std::endl;
delete msg;
SDL_Delay(5000);
MainMenu *mainMenu = new MainMenu(window, sockfd, theirAddr);
window.setCurrScreen(mainMenu);
players[0].resetPlayer();
players[1].resetPlayer();
waitingEnd = true;
}

void LGame::render(SDL_Renderer *renderer)
Expand Down Expand Up @@ -326,6 +353,22 @@ void LGame::render(SDL_Renderer *renderer)
{
taskTexts[i]->render(renderer, 8, (16 + maxHeight) * i + 20 + tasksText->getHeight() + 16);
}

if (gameEnded)
{
SDL_Rect defViewport = {0, 0, window.getWidth(), window.getHeight()};
SDL_RenderSetViewport(renderer, &defViewport);
if (result)
{
winText->render(renderer, (window.getWidth() - winText->getWidth()) / 2, (window.getHeight() - winText->getHeight()) / 2);
pressAnyKey->render(renderer, (window.getWidth() - pressAnyKey->getWidth()) / 2, (window.getHeight() + winText->getHeight()) / 2 + 32);
}
else
{
loseText->render(renderer, (window.getWidth() - loseText->getWidth()) / 2, (window.getHeight() - loseText->getHeight()) / 2);
pressAnyKey->render(renderer, (window.getWidth() - pressAnyKey->getWidth()) / 2, (window.getHeight() + loseText->getHeight()) / 2 + 32);
}
}
}

void LGame::cleanUp()
Expand Down Expand Up @@ -461,6 +504,7 @@ bool LGame::initObjs()
SDL_Color txtColor = {0, 0, 0, 255};
TTF_Font *font = TTF_OpenFont("resources/FrostbiteBossFight-dL0Z.ttf", 28);
TTF_Font *fontSmall = TTF_OpenFont("resources/FrostbiteBossFight-dL0Z.ttf", 26);
TTF_Font *fontLarge = TTF_OpenFont("resources/FrostbiteBossFight-dL0Z.ttf", 32);

timeText = new Text(window, "00:00", font, txtColor);
healthText = new Text(window, "HEALTH: ", font, txtColor);
Expand All @@ -471,6 +515,9 @@ bool LGame::initObjs()
nameText = new Text(window, playerName, font, txtColor);
oppText = new Text(window, opponentName, font, txtColor);
tasksText = new Text(window, "TASKS", font, txtColor);
winText = new Text(window, "YOU WON!", fontLarge, txtColor);
loseText = new Text(window, "YOU LOST!", fontLarge, txtColor);
pressAnyKey = new Text(window, "Press any key to go back to main menu", font, txtColor);

for (int i = 0; i < tasksNum; i++)
{
Expand All @@ -492,8 +539,7 @@ bool LGame::initObjs()
{
renderables.push_back(&NPCs[i]);
}
std::cout << "OBJECTS INITIALIZED"
<< "\n";
// std::cout << "OBJECTS INITIALIZED" << "\n";
globalTime.start();
return true;
}
Expand Down
7 changes: 5 additions & 2 deletions Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class LGame : public LScreen
char recBuf[512];
void initSocket();

bool firstTime = true;

std::vector<Tile> tiles;
std::vector<Player> players;
std::vector<Entity> entities;
Expand Down Expand Up @@ -86,10 +84,15 @@ class LGame : public LScreen
// for instructions on object collision
Text *prompText;
Text *tasksText;
Text *winText;
Text *loseText;
Text *pressAnyKey;

std::vector<Text *> taskTexts;

bool gameEnded = false;
bool waitingEnd = false;
bool result = false;

public:
LGame(LWindow &window, std::string playerName, std::string opponentName, int &sockfd, sockaddr_in &theiraddr);
Expand Down
3 changes: 3 additions & 0 deletions MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ void MainMenu::update()

void MainMenu::render(SDL_Renderer *renderer)
{
SDL_Rect defViewport = {0, 0, window.getWidth(), window.getHeight()};
SDL_RenderSetViewport(renderer, &defViewport);

SDL_SetRenderDrawColor(renderer, 3, 211, 252, 255);
SDL_RenderClear(renderer);
for (int i = 0; i < buttons.size(); i++)
Expand Down
2 changes: 1 addition & 1 deletion Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void Player ::resetPlayer()
mVelY = 0;
moveFactor = 1;
mframes = 0;
mBox = {0, 0};
// mBox = {0, 0};
currentTaskTime = 0;
currentTaskTimer.stop();
direction = 'D';
Expand Down
75 changes: 38 additions & 37 deletions SoundEffect.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#include"SoundEffect.h"
#include<SDL2/SDL.h>
#include<SDL2/SDL_mixer.h>
#include<string>
#include<iostream>
#include<memory>
#include "SoundEffect.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <string>
#include <iostream>
#include <memory>

SoundEffect::SoundEffect ()
SoundEffect::SoundEffect()
{
// initialize variables
mMusic = NULL ;
// initialize variables
mMusic = NULL;
}

SoundEffect :: SoundEffect( std :: string filename )
SoundEffect ::SoundEffect(std ::string filename)
{
// initialize variables
mMusic = NULL ;
loadMusic( filename ) ;

// initialize variables
mMusic = NULL;
loadMusic(filename);
}

SoundEffect::~SoundEffect()
Expand All @@ -34,43 +33,45 @@ SoundEffect::~SoundEffect()
// Mix_VolumeChunk(chunk.get(), volume);
// }

bool SoundEffect::loadMusic( std :: string filename )
bool SoundEffect::loadMusic(std ::string filename)
{
// get rid of pre-existing music
free() ;
// std::cout << "loading music...\n" ;
mMusic = Mix_LoadWAV( filename.c_str() ) ;
if( mMusic == NULL ) {
printf( "Failed to load music!\n" );
std::cout << Mix_GetError() << "\n" ;
return false ;
// get rid of pre-existing music
free();
// std::cout << "loading music...\n" ;
mMusic = Mix_LoadWAV(filename.c_str());
if (mMusic == NULL)
{
printf("Failed to load music!\n");
std::cout << Mix_GetError() << "\n";
return false;
}
// std::cout << "loaded music...\n" ;
return true ;

// std::cout << "loaded music...\n" ;
return true;
}

void SoundEffect::free()
{
// Free music if it exists
if (mMusic != NULL)
{
//Free the sound effects
Mix_FreeChunk( mMusic ) ;
mMusic = NULL ;

// Free the sound effects
Mix_FreeChunk(mMusic);
mMusic = NULL;
}
}

void SoundEffect::play()
{
int channel = Mix_PlayChannel( -1, mMusic , 0 ) ;
std::cout << ( mMusic == NULL ) << "\n" ;
if( channel == -1 ){
printf("Mix_PlayChannel: %s\n",Mix_GetError());
}else{
std::cout << "playing on channel " << channel << "\n" ;
int channel = Mix_PlayChannel(-1, mMusic, 0);
// std::cout << ( mMusic == NULL ) << "\n" ;
if (channel == -1)
{
printf("Mix_PlayChannel: %s\n", Mix_GetError());
}
else
{
// std::cout << "playing on channel " << channel << "\n";
}
// SDL_Delay(1000) ;
// SDL_Delay(1000) ;
// Mix_PlayChannel(-1, chunk.get(), 0);
}
4 changes: 2 additions & 2 deletions server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ int main()
int bytesUsed = serialize(&gameBeginMessage, msgBuffer);
sendto(sockfd, msgBuffer, bytesUsed, 0, (const struct sockaddr *)&cliaddr,
sizeof(cliaddr));
std::cout << "Sending to " << cliaddr.sin_addr.s_addr << std::endl;
// std::cout << "Sending to " << cliaddr.sin_addr.s_addr << std::endl;
gameBeginMessage.opponentName = newClientMsg->name;
bytesUsed = serialize(&gameBeginMessage, msgBuffer);
sendto(sockfd, msgBuffer, bytesUsed, 0, (const struct sockaddr *)&waitaddr,
sizeof(waitaddr));
std::cout << "Sending to " << waitaddr.sin_addr.s_addr << std::endl;
// std::cout << "Sending to " << waitaddr.sin_addr.s_addr << std::endl;
opponents.insert(std::pair<in_addr_t, in_addr_t>(cliaddr.sin_addr.s_addr, waitaddr.sin_addr.s_addr));
opponents.insert(std::pair<in_addr_t, in_addr_t>(waitaddr.sin_addr.s_addr, cliaddr.sin_addr.s_addr));

Expand Down

0 comments on commit 98aa9c9

Please sign in to comment.