From 98aa9c9ef6e16284f237653d773f94cd2ff71bc4 Mon Sep 17 00:00:00 2001 From: TanishTuteja Date: Sun, 17 Apr 2022 19:43:41 +0530 Subject: [PATCH] Fixed game end bug + displaying text now on game end --- Game.cpp | 110 ++++++++++++++++++++++++++++++++-------------- Game.h | 7 ++- MainMenu.cpp | 3 ++ Player.cpp | 2 +- SoundEffect.cpp | 75 +++++++++++++++---------------- server/server.cpp | 4 +- 6 files changed, 127 insertions(+), 74 deletions(-) diff --git a/Game.cpp b/Game.cpp index e5ea06e..3a5beb4 100644 --- a/Game.cpp +++ b/Game.cpp @@ -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); @@ -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) @@ -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(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++) @@ -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"; @@ -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, @@ -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; } @@ -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; } } @@ -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(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) @@ -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() @@ -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); @@ -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++) { @@ -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; } diff --git a/Game.h b/Game.h index 175fcc7..5403a90 100644 --- a/Game.h +++ b/Game.h @@ -45,8 +45,6 @@ class LGame : public LScreen char recBuf[512]; void initSocket(); - bool firstTime = true; - std::vector tiles; std::vector players; std::vector entities; @@ -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 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); diff --git a/MainMenu.cpp b/MainMenu.cpp index 9195f7d..e331bf5 100644 --- a/MainMenu.cpp +++ b/MainMenu.cpp @@ -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++) diff --git a/Player.cpp b/Player.cpp index c0f82b0..95639f5 100644 --- a/Player.cpp +++ b/Player.cpp @@ -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'; diff --git a/SoundEffect.cpp b/SoundEffect.cpp index 75e121b..69e624d 100644 --- a/SoundEffect.cpp +++ b/SoundEffect.cpp @@ -1,22 +1,21 @@ -#include"SoundEffect.h" -#include -#include -#include -#include -#include +#include "SoundEffect.h" +#include +#include +#include +#include +#include -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() @@ -34,20 +33,20 @@ 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() @@ -55,22 +54,24 @@ 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); } \ No newline at end of file diff --git a/server/server.cpp b/server/server.cpp index eb0846e..4eae0ae 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -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(cliaddr.sin_addr.s_addr, waitaddr.sin_addr.s_addr)); opponents.insert(std::pair(waitaddr.sin_addr.s_addr, cliaddr.sin_addr.s_addr));