From 42f1cd86848c1bb9ba75559dc4fa216d79a44051 Mon Sep 17 00:00:00 2001 From: Gaya1858 Date: Tue, 5 Dec 2023 13:53:45 -0500 Subject: [PATCH 01/16] Checked and corrected for code format, uniformity, and documentation for the classes MainInterface.cpp, MainInterface.cpp, MessageBoard.cpp, MessageBoard.hpp, TextBox.cpp, TextBox.cpp and also updates Group3_README.md for these classes. --- assets/walls/{portal3,png.png => portal3.png} | Bin source/Interfaces/Group3_README.md | 69 +++++- source/Interfaces/MainInterface.cpp | 150 ++++++------ source/Interfaces/MainInterface.hpp | 113 +++++---- source/Interfaces/MessageBoard.cpp | 56 +++-- .../{MessageBoard.h => MessageBoard.hpp} | 16 +- source/Interfaces/TextBox.cpp | 226 +++++++++--------- source/Interfaces/TextBox.hpp | 46 ++-- source/Interfaces/TextureHolder.cpp | 4 + source/group_3_main.cpp | 1 + 10 files changed, 392 insertions(+), 289 deletions(-) rename assets/walls/{portal3,png.png => portal3.png} (100%) rename source/Interfaces/{MessageBoard.h => MessageBoard.hpp} (76%) diff --git a/assets/walls/portal3,png.png b/assets/walls/portal3.png similarity index 100% rename from assets/walls/portal3,png.png rename to assets/walls/portal3.png diff --git a/source/Interfaces/Group3_README.md b/source/Interfaces/Group3_README.md index 8f1533ae..09f0b247 100644 --- a/source/Interfaces/Group3_README.md +++ b/source/Interfaces/Group3_README.md @@ -94,4 +94,71 @@ The `GetTexture` function performs the following key tasks: The function allows for easy reference to any of the loaded sf::Textures without having to instantiate new ones for each use. - \ No newline at end of file + + +# MessageBoard Class + +## Description + +
Functionalities: + +### MessageBoard Constructor + +The `MessageBoard` class is responsible for managing and displaying messages on the interface entity. +Parameters: - **font:** The font used by the message board. + +### DrawTo Method + +Draws the message board onto the render window. If the elapsed time since the last message exceeds 10 seconds, the message board will be cleared. +Parameters:- **window:** The render window to be drawn on. + +
+ +# TextBox Class + +## Description + +
Functionalities: + +### TextBox Constructor + +The `TextBox` class enables the interface entity to send messages to the `MessageBoard` and `World`. It provides functionality for handling text input, setting the string, and drawing the text to the render window. + +#### Parameters: +- **font:** The font used by TextBox. +- **size:** The size of the text. +- **color:** The color of the text. +- **sel:** Set the activity of TextBox. + +### SetString Method + +Sets the string of the text in the TextBox. +Parameters:- **s:** The string to be set. + +### SetSelected Method + +Setter for isSelected, updates the text displayed in the box based on selection status. +Parameters:- **sel:** Whether the TextBox is to be set selected or not. + +### TypedOn Method + +TypedOn event handler, processes new text that has been typed into the TextBox. +Parameters:- **input:** The new text event. + +### InputLogic Method + +Determines what actions to take with different key presses. +Parameters:- **charTyped:** The character that has just been entered into the TextBox. + +### DeleteLastChar Method + +Removes the last character from the TextBox's current string. + +### DrawTo Method + +Draws the text and border to the render window. +Parameters:- **window:** The render window to be drawn on. + +
+ + diff --git a/source/Interfaces/MainInterface.cpp b/source/Interfaces/MainInterface.cpp index ef15b8a5..529d746b 100644 --- a/source/Interfaces/MainInterface.cpp +++ b/source/Interfaces/MainInterface.cpp @@ -1,15 +1,21 @@ /** - * @author : Team - 3 - * @date: 10/03/2023 - * MainInterface class creates a window and displays the default maze grid + * @file MainInterface.cpp + * @authors Gaya Kanagaraj, Vincenzo Felici, Mui Pham + * @date 10/03/2023 + * @brief MainInterface class manages the game's user interface, including the menu, textbox, + * message box, inventory, and texture holder. It serves as the main class responsible + * for creating and managing the game window, drawing the grid, handling player movements, + * and displaying menu and inventory details. + * */ + #include #include "MainInterface.hpp" namespace i_2D { - sf::Clock timer; - float elapsedTime = 0.0f; + sf::Clock timer; // To drawTimer function + float elapsedTime = 0.0f; // Sets elapsed time = 0.0 /** * @brief Constructs a `MainInterface` object. @@ -43,12 +49,14 @@ namespace i_2D { * @return A vector of strings representing the maze grid. */ std::vector MainInterface::CreateVectorMaze( - const WorldGrid &grid, const type_options_t &type_options, const item_map_t &item_map, const agent_map_t &agent_map) { + std::vector symbol_grid(grid.GetHeight()); + mGridHeight = grid.GetHeight(); mGridWidth = grid.GetWidth(); + // Load the world into the symbol_grid; for (size_t y = 0; y < grid.GetHeight(); ++y) { symbol_grid[y].resize(grid.GetWidth()); @@ -86,6 +94,7 @@ namespace i_2D { * @return sf::Vector2f The size of each cell as a 2D vector. */ sf::Vector2f MainInterface::CalculateCellSize(const WorldGrid &grid) { + float cellSizeWide, cellSizeTall; if (mGridSizeLarge) { cellSizeWide = mWindow.getSize().x / COL; @@ -154,6 +163,7 @@ namespace i_2D { renderTexture.display(); DrawTimer(); DrawHealthInfo(); + // Draw the texture to the window sf::Sprite sprite(renderTexture.getTexture()); sprite.setPosition({drawCenterX, drawCenterY}); @@ -167,31 +177,31 @@ namespace i_2D { } /** - * @brief this function draws timer and checks the elapsed time and - * shows remainder if the timer exceed above 5 seconds. and sestart the timer every move - * the player makes. - */ + * @brief this function draws timer and checks the elapsed time + * if the timer exceed above 0.5 seconds, it returns 0 to the netwrok interface + * and restart the timer every move player makes + */ void MainInterface::DrawTimer() { // Get elapsed time in seconds elapsedTime = timer.getElapsedTime().asSeconds(); - float testNum = 0.12345; - std::string testStr = std::to_string(testNum); - - // Set up font and location sf::Text timerText(mFont); timerText.setCharacterSize(24); timerText.setPosition({750.0f, 75.0f}); // Adjust position as needed + // Format the elapsed time with 2 decimal points + std::ostringstream stream; + stream << "Time: " << std::fixed << std::setprecision(2) << elapsedTime << " S"; + std::string formattedTime = stream.str(); + // Create text for current value - timerText.setString("Time: " + std::to_string(elapsedTime) + " S"); + timerText.setString(formattedTime); timerText.setFillColor(sf::Color::Blue); mWindow.draw(timerText); } - void MainInterface::DrawHealthInfo() - { + void MainInterface::DrawHealthInfo() { //TODO fix this // // Reference health property // int health = GetProperty("Health"); @@ -209,7 +219,7 @@ namespace i_2D { * @brief Creates a 9x23 window of the symbol grid centered around the player's position. * * @param symbol_grid The original symbol grid. - * @return A new symbol grid representing the 9x23 window. + * @return std::vector A new symbol grid representing the 9x23 window. */ std::vector MainInterface::LargeDisplayGrid(const std::vector &symbol_grid) { // Determine the top-left corner of the 9x23 window @@ -259,7 +269,6 @@ namespace i_2D { } /** - * @brief Handles user input for selecting actions. * * @param grid The WorldGrid representing the maze. @@ -306,8 +315,7 @@ namespace i_2D { } // Check if a valid action was taken and return that if so - if(action_id != 0) - { + if (action_id != 0) { return action_id; } @@ -331,65 +339,52 @@ namespace i_2D { * @return size_t The action ID corresponding to the key event. */ size_t MainInterface::HandleKeyEvent(const sf::Event &event) { - size_t action_id = 0; - switch (event.key.code) { - case sf::Keyboard::Enter: - if (!mTextBox->IsSelected()) { - mTextBox->SetSelected(true); - } else { + if (mTextBox->IsSelected()) { + // TextBox is selected, handle specific cases + switch (event.key.code) { + case sf::Keyboard::Enter: mMessageBoard->Send(mTextBox->GetText()); - mTextBox->SetString(""); + mTextBox->SetString("Please Enter!"); mTextBox->SetSelected(false); - } - break; - case sf::Keyboard::Escape: - if (mTextBox->IsSelected()) { + break; + case sf::Keyboard::Escape: + mTextBox->SetString("Please Enter!"); mTextBox->SetSelected(false); - mTextBox->SetString(""); - } - break; - case sf::Keyboard::W: - if (mTextBox->IsSelected())break; - action_id = GetActionID("up"); - break; - case sf::Keyboard::A: - if (mTextBox->IsSelected())break; - action_id = GetActionID("left"); - break; - case sf::Keyboard::S: - if (mTextBox->IsSelected())break; - action_id = GetActionID("down"); - break; - case sf::Keyboard::D: - if (mTextBox->IsSelected())break; - action_id = GetActionID("right"); - break; - case sf::Keyboard::Up: - action_id = GetActionID("up"); - break; - case sf::Keyboard::Left: - action_id = GetActionID("left"); - break; - case sf::Keyboard::Down: - action_id = GetActionID("down"); - break; - case sf::Keyboard::Right: - action_id = GetActionID("right"); - break; - default: - break; // The user pressed an unknown key. - } - // If we waited for input, but don't understand it, notify the user. - if (action_id == 0 && !mTextBox->IsSelected()) { - std::cout << "Unknown key." << std::endl; + break; + default: + break; + } + } else { + // TextBox is not selected, handle movement keys + switch (event.key.code) { + case sf::Keyboard::W: + return GetActionID("up"); + case sf::Keyboard::A: + return GetActionID("left"); + case sf::Keyboard::S: + return GetActionID("down"); + case sf::Keyboard::D: + return GetActionID("right"); + case sf::Keyboard::Up: + return GetActionID("up"); + case sf::Keyboard::Left: + return GetActionID("left"); + case sf::Keyboard::Down: + return GetActionID("down"); + case sf::Keyboard::Right: + return GetActionID("right"); + default: + std::cout << "Unknown key." << std::endl; + break; // The user pressed an unknown key. + } } - // Do the action! - return action_id; + // No action performed + return 0; } + /** * @brief Handles the window resize event - * * Restricts window resizing if below a minimum size. * Matches the window's view to the new size of the window. * @@ -434,10 +429,11 @@ namespace i_2D { // Calculate the actual position based on percentages float xPos = (widthWindow * xPosPercentage); - float yPos = (heightWindow* yPosPercentage); + float yPos = (heightWindow * yPosPercentage); // Set the position of your textbox - mTextBox->SetPosition({ xPos, yPos}); + mTextBox->SetPosition({xPos, yPos}); + } } @@ -480,7 +476,7 @@ namespace i_2D { } // Check if the mouse is over specific menu items - if (mMenu.GetMenu()[4]->isMouseOver(mWindow) or (mGridWidth == mGridHeight and mGridWidth > ROW)){ + if (mMenu.GetMenu()[4]->isMouseOver(mWindow) or (mGridWidth == mGridHeight and mGridWidth > ROW)) { mGridSizeLarge = true; } else if (mMenu.GetMenu()[3]->isMouseOver(mWindow)) { mGridSizeLarge = false; @@ -535,9 +531,9 @@ namespace i_2D { * @param wallTexture The texture for the wall. * @param renderTexture The Texture for the whole grid */ + void MainInterface::DrawWall(sf::RenderTexture &renderTexture, + sf::RectangleShape &cellRect, sf::Texture &wallTexture) { - void - MainInterface::DrawWall(sf::RenderTexture &renderTexture, sf::RectangleShape &cellRect, sf::Texture &wallTexture) { cellRect.setTexture(&wallTexture); renderTexture.draw(cellRect); } diff --git a/source/Interfaces/MainInterface.hpp b/source/Interfaces/MainInterface.hpp index 8783f4bd..a8b0e479 100644 --- a/source/Interfaces/MainInterface.hpp +++ b/source/Interfaces/MainInterface.hpp @@ -1,7 +1,11 @@ /** - * @author : Team - 3 + * @file MainInterface.hpp + * @author :Gaya Kanagaraj, Vincenzo Felici, Mui Pham * @date: 10/03/2023 - * MainInterface class creates a window and displays the default maze grid + * @brief MainInterface class manages the game's user interface, including the menu, textbox, + * message box, inventory, and texture holder. It serves as the main class responsible + * for creating and managing the game window, drawing the grid, handling player movements, + * and displaying menu and inventory details. */ #pragma once @@ -19,7 +23,7 @@ #include "../core/InterfaceBase.hpp" #include "TextureHolder.hpp" #include "TextBox.hpp" -#include "MessageBoard.h" +#include "MessageBoard.hpp" namespace i_2D { @@ -38,41 +42,68 @@ namespace i_2D { */ class MainInterface : public virtual InterfaceBase { - protected: - + private: sf::RenderWindow mWindow; ///< render window float const MIN_SIZE_CELL = 16; ///< Pixels // Menu and message vars Menu mMenu; ///< for menu class sf::Font mFont; ///< one font for all objects using font - std::unique_ptr mTextBox; /// for chatting and possible event handling by text - std::unique_ptr mMessageBoard; + std::unique_ptr mTextBox; ///< for chatting and possible event handling by text + std::unique_ptr mMessageBoard; ///< message box object // Texture vars TextureHolder mTextureHolder; ///< for the texture holder - std::map mTexturesDefault; - std::map mTexturesSecondWorld; - std::map mTexturesManualWorld; - std::map mTexturesGenerativeWorld; - std::map mTexturesCurrent; + std::map mTexturesDefault; ///< for the texture holder default grid + std::map mTexturesSecondWorld; ///< for the texture holder SecondWorld grid + std::map mTexturesManualWorld; ///< for the texture holder Manualworld grid + std::map mTexturesGenerativeWorld; ///< for the texture holder GenerativeWorld grid + std::map mTexturesCurrent; ///< for the texture holder current world grid // Render range vars - sf::Vector2i mPlayerPosition = sf::Vector2i(0,0); ///< xy world grid location of the player - bool mGridSizeLarge = false; - int const ROW = 9; - int const COL = 23; + sf::Vector2i mPlayerPosition = sf::Vector2i(0, 0); ///< xy world grid location of the player + bool mGridSizeLarge = false; ///< flag for the largegrid + int const ROW = 9; ///< row to enlarge te grid + int const COL = 23; ///< column to enlarge the grid - int mGridWidth = 0; - int mGridHeight = 0; + int mGridWidth = 0; ///< for the gridwidth + int mGridHeight = 0; ///< for the gridheight + + double mInputWaitTime = 0.5f; ///< for the waittime of the player + + size_t HandleKeyEvent(const sf::Event &event); + + void CalculateDrawSpace(const WorldGrid &grid, float cellSize, float &drawSpaceWidth, float &drawSpaceHeight, + float &drawCenterX, float &drawCenterY); + + sf::Vector2f CalculateCellSize(const WorldGrid &grid); + + void HandleResize(const sf::Event &event, const WorldGrid &grid); + + void ChooseTexture(); + + std::vector LargeDisplayGrid(const std::vector &symbol_grid); + + void MouseClickEvent(const sf::Event &event); + + void DrawAgentCell(sf::RenderTexture &renderTexture, sf::RectangleShape &cellRect, sf::RectangleShape &cell, + sf::Texture &agent); + + void + SwitchCellSelect(sf::RenderTexture &renderTexture, sf::RectangleShape &cellRect, sf::RectangleShape &cell, + char symbol); + + void DrawWall(sf::RenderTexture &renderTexture, sf::RectangleShape &cellRect, sf::Texture &wallTexture); + + void DrawTimer(); + + void DrawHealthInfo(); - double mInputWaitTime = 0.5f; - public: void setMInputWaitTime(double mInputWaitTime); - public: - MainInterface(size_t id, const std::string &name) ; + public: + MainInterface(size_t id, const std::string &name); /** * @brief Destructor for the `MainInterface` class. @@ -84,7 +115,7 @@ namespace i_2D { const WorldGrid &grid, const type_options_t &type_options, const item_map_t &item_map, - const agent_map_t &agent_map) ; + const agent_map_t &agent_map); void DrawGrid(const WorldGrid &grid, const type_options_t &type_options, const item_map_t &item_map, const agent_map_t &agent_map); @@ -97,44 +128,22 @@ namespace i_2D { bool Initialize() override { return true; } + size_t SelectAction(const WorldGrid &grid, const type_options_t &type_options, const item_map_t &item_map, const agent_map_t &agent_map) override; - size_t HandleKeyEvent(const sf::Event &event); - - void CalculateDrawSpace(const WorldGrid &grid, float cellSize, float &drawSpaceWidth, float &drawSpaceHeight, - float &drawCenterX, float &drawCenterY); - - sf::Vector2f CalculateCellSize(const WorldGrid &grid); - - void HandleResize(const sf::Event &event, const WorldGrid &grid); - - void ChooseTexture(); - void Notify(const std::string & message, - const std::string & /*msg_type*/="none") override - { + /** + * @brief notifies the world if the player have any progress message + * @param message that notifies the world + */ + void Notify(const std::string &message, + const std::string & /*msg_type*/= "none") override { std::cout << message << std::endl; mMessageBoard->Send(message); } - std::vector LargeDisplayGrid(const std::vector &symbol_grid); - - void MouseClickEvent(const sf::Event &event); - - void DrawAgentCell(sf::RenderTexture &renderTexture, sf::RectangleShape &cellRect, sf::RectangleShape &cell, - sf::Texture &agent); - - void - SwitchCellSelect(sf::RenderTexture &renderTexture, sf::RectangleShape &cellRect, sf::RectangleShape &cell, - char symbol); - - void DrawWall(sf::RenderTexture &renderTexture, sf::RectangleShape &cellRect, sf::Texture &wallTexture); - - void DrawTimer(); - - void DrawHealthInfo(); }; } // End of namespace 2D diff --git a/source/Interfaces/MessageBoard.cpp b/source/Interfaces/MessageBoard.cpp index 28cf7673..be317b02 100644 --- a/source/Interfaces/MessageBoard.cpp +++ b/source/Interfaces/MessageBoard.cpp @@ -1,35 +1,39 @@ /** - * @author : Team - 3 + * @file MessageBoard.cpp + * @authors Gaya Kanagaraj, Vincenzo Felici, Mui Pham * @date: 11/03/2023 * MessageBoard class communicate messages to the interface entity */ -#include "MessageBoard.h" +#include "MessageBoard.hpp" -/** - * @brief Construct the message board - * - * @param font The font used by message borad - */ -i_2D::MessageBoard::MessageBoard(sf::Font &font) { - mText = std::make_unique(font); - mText->setString("Hello World!"); - mText->setCharacterSize(35); - mText->setFillColor(sf::Color::Blue); - mText->setPosition({5,165}); - mStartTime = std::chrono::system_clock::now(); -} +namespace i_2D { -/** - * @brief Draws the message board onto the render window - * - * @param window The render window to be drawn on - */ -void i_2D::MessageBoard::DrawTo(sf::RenderWindow &window) { - if(std::chrono::duration_cast( - std::chrono::system_clock::now() - mStartTime).count() > 10000){ - mText->setString(""); - }else window.draw(*mText); -} + /** + * @brief Construct the message board + * + * @param font The font used by message borad + */ + MessageBoard::MessageBoard(sf::Font &font) { + mText = std::make_unique(font); + mText->setString("Welcome!"); + mText->setCharacterSize(35); + mText->setFillColor(sf::Color::Blue); + mText->setPosition({5, 125}); + mStartTime = std::chrono::system_clock::now(); + } + /** + * @brief Draws the message board onto the render window + * + * @param window The render window to be drawn on + */ + void MessageBoard::DrawTo(sf::RenderWindow &window) { + if (std::chrono::duration_cast( + std::chrono::system_clock::now() - mStartTime).count() > 10000) { + mText->setString(""); + } else window.draw(*mText); + } + +} diff --git a/source/Interfaces/MessageBoard.h b/source/Interfaces/MessageBoard.hpp similarity index 76% rename from source/Interfaces/MessageBoard.h rename to source/Interfaces/MessageBoard.hpp index 30648c2f..cfac03f5 100644 --- a/source/Interfaces/MessageBoard.h +++ b/source/Interfaces/MessageBoard.hpp @@ -1,10 +1,10 @@ /** - * @author : Team - 3 + * @file MessageBoard.cpp + * @authors Gaya Kanagaraj, Vincenzo Felici, Mui Pham * @date: 11/03/2023 * MessageBoard class communicate messages to the interface entity */ -#ifndef CSE_491_MESSAGEBOARD_H -#define CSE_491_MESSAGEBOARD_H +#pragma once #include #include @@ -13,10 +13,12 @@ #include namespace i_2D { + /** - * @class The MessagebBoard is where the backend and users communicate by words + * @class The MessageBoard is where the backend and users communicate by words */ class MessageBoard { + private: /// The text displayed std::unique_ptr mText; @@ -25,18 +27,18 @@ namespace i_2D { public: explicit MessageBoard(sf::Font &font); + void DrawTo(sf::RenderWindow &window); + /** * @brief Set the text of the message board * * @param message The message to set the text to */ - void Send(const std::string &message){ + void Send(const std::string &message) { mText->setString(message); mStartTime = std::chrono::system_clock::now(); } }; } - -#endif //CSE_491_MESSAGEBOARD_H diff --git a/source/Interfaces/TextBox.cpp b/source/Interfaces/TextBox.cpp index 9ca603d4..ad5825e4 100644 --- a/source/Interfaces/TextBox.cpp +++ b/source/Interfaces/TextBox.cpp @@ -1,139 +1,143 @@ /** - * @author : Team - 3 + * @file TextBox.cpp + * @authors Gaya Kanagaraj, Vincenzo Felici, Mui Pham * @date: 11/02/2023 * TextBox class enables interface entity to send message to the MessageBoard and World */ #include "TextBox.hpp" -/** - * @brief Constructor - * - * @param font The font used by TextBox - * @param size The size of the text - * @param color The color of the text - * @param sel Set the activity of TextBox - */ -i_2D::TextBox::TextBox(const sf::Font &font, int size, sf::Color color, bool sel) { +namespace i_2D { - mTextBox = std::make_unique(font); - mTextBox->setCharacterSize(size); - mTextBox->setFillColor(color); - mTextBox->setPosition({10,650}); - isSelected = sel; - if(!isSelected) - mTextBox->setString("Press Enter!"); + /** + * @brief Constructor + * + * @param font The font used by TextBox + * @param size The size of the text + * @param color The color of the text + * @param sel Set the activity of TextBox + */ + TextBox::TextBox(const sf::Font &font, int size, sf::Color color, bool sel) { -} -/** - * @brief Set the string of the text - * - * @param s The string to be set to - */ -void i_2D::TextBox::SetString(const std::string &s) { - mTextBox->setString(s); - mText.str(""); - mText << s; -} - -/*** - * @brief Setter for isSelected, updates the text displayed in the box - * @param sel whether the TextBox is to be set selected or not - */ -void i_2D::TextBox::SetSelected(bool sel) { - isSelected = sel; - if(!sel){ - std::string t = mText.str(); - std::string newT; - for(char i : t){ - newT += i; + mTextBox = std::make_unique(font); + mTextBox->setCharacterSize(size); + mTextBox->setFillColor(color); + mTextBox->setPosition({10, 650}); + isSelected = sel; + if (!isSelected) + mTextBox->setString("Press Enter!"); + + } + + /** + * @brief Set the string of the text + * + * @param s The string to be set to + */ + void TextBox::SetString(const std::string &s) { + mTextBox->setString(s); + mText.str(""); + mText << s; + } + + /** + * @brief Setter for isSelected, updates the text displayed in the box + * @param sel whether the TextBox is to be set selected or not + */ + void TextBox::SetSelected(bool sel) { + isSelected = sel; + if (!sel) { + std::string t = mText.str(); + std::string newT; + for (char i: t) { + newT += i; + } + mTextBox->setString((newT)); } - mTextBox->setString((newT)); } -} -/*** - * @brief TypedOn event handler - * @param input the new text that has been typed into the TextBox - */ -void i_2D::TextBox::TypedOn(sf::Event input) { - if(isSelected){ - int charTyped = static_cast(input.text.unicode); - if(charTyped < 128){ - if(hasLimit){ - if(static_cast(mText.str().length()) <= MAX_CHAR){ + /** + * @brief TypedOn event handler + * @param input the new text that has been typed into the TextBox + */ + void TextBox::TypedOn(sf::Event input) { + if (isSelected) { + int charTyped = static_cast(input.text.unicode); + if (charTyped < 128) { + if (hasLimit) { + if (static_cast(mText.str().length()) <= MAX_CHAR) { + InputLogic(charTyped); + } else if (static_cast(mText.str().length()) > MAX_CHAR && charTyped == DELETE_KEY) { + DeleteLastChar(); + } + } else { InputLogic(charTyped); - }else if( static_cast(mText.str().length()) > MAX_CHAR && charTyped == DELETE_KEY){ - DeleteLastChar(); } - }else{ - InputLogic(charTyped); } } } -} -/*** - * @brief Determines what actions to take with different key presses - * @param charTyped The character that has just been entered into the TextBox - */ -void i_2D::TextBox::InputLogic(int charTyped) { - if(isSelected) - { - if (charTyped != DELETE_KEY && charTyped != ENTER_KEY && charTyped != ESCAPE_KEY) { - if (mText.str().length() < MAX_CHAR) { - mText << static_cast(charTyped); - } - else if (mText.str().length() >= MAX_CHAR) { - hasLimit = true; - } - } else if (charTyped == ENTER_KEY) { - // Handle line breaks - if (mText.str().length() < MAX_CHAR) { - mText << '\n'; - hasLimit = false; - } - } else if (charTyped == DELETE_KEY) { - if (mText.str().length() > 0) { - DeleteLastChar(); + /** + * @brief Determines what actions to take with different key presses + * @param charTyped The character that has just been entered into the TextBox + */ + void TextBox::InputLogic(int charTyped) { + if (isSelected) { + if (charTyped != DELETE_KEY && charTyped != ENTER_KEY && charTyped != ESCAPE_KEY) { + if (mText.str().length() < MAX_CHAR) { + mText << static_cast(charTyped); + } else if (mText.str().length() >= MAX_CHAR) { + hasLimit = true; + } + } else if (charTyped == ENTER_KEY) { + // Handle line breaks + if (mText.str().length() < MAX_CHAR) { + mText << '\n'; + hasLimit = false; + } + } else if (charTyped == DELETE_KEY) { + if (mText.str().length() > 0) { + DeleteLastChar(); + } } } - } - mTextBox->setString(mText.str() + "_"); -} + mTextBox->setString(mText.str() + "_"); + } -/** - * @brief removes the last character from the TextBox's current string - */ -void i_2D::TextBox::DeleteLastChar() { - std::string t = mText.str(); - std::string newT; - for(unsigned int i = 0; i < t.length() - 1; i++){ - newT += t[i]; + /** + * @brief removes the last character from the TextBox's current string + */ + void TextBox::DeleteLastChar() { + std::string t = mText.str(); + std::string newT; + for (unsigned int i = 0; i < t.length() - 1; i++) { + newT += t[i]; + } + mText.str(""); + mText << newT; + mTextBox->setString(mText.str()); } - mText.str(""); - mText << newT; - mTextBox->setString(mText.str()); -} -/** -* @brief Draws the text to the render window -* -* @param window The render window to be drawn on -*/ -void i_2D::TextBox::DrawTo(sf::RenderWindow &window){ - mBorderRect.setSize(sf::Vector2f(800, 50)); - //Subtracts the vector (5, 5) from the position of mTextBox. This creates a new position - // slightly to the left and up from the original position, effectively creating a margin. + /** + * @brief Draws the text to the render window + * + * @param window The render window to be drawn on + */ + void TextBox::DrawTo(sf::RenderWindow &window) { - mBorderRect.setPosition(mTextBox->getPosition() - sf::Vector2f(5, 5)); - mBorderRect.setFillColor(sf::Color(200, 200, 200)); - mBorderRect.setOutlineThickness(2.0f); // thickness of the border - mBorderRect.setOutlineColor(sf::Color::Black); + mBorderRect.setSize(sf::Vector2f(800, 50)); + //Subtracts the vector (5, 5) from the position of mTextBox. This creates a new position + // slightly to the left and up from the original position, effectively creating a margin. - window.draw(mBorderRect); - window.draw(*mTextBox); + mBorderRect.setPosition(mTextBox->getPosition() - sf::Vector2f(5, 5)); + mBorderRect.setFillColor(sf::Color(200, 200, 200)); + mBorderRect.setOutlineThickness(2.0f); // thickness of the border + mBorderRect.setOutlineColor(sf::Color::Black); -} + window.draw(mBorderRect); + window.draw(*mTextBox); + + } +} \ No newline at end of file diff --git a/source/Interfaces/TextBox.hpp b/source/Interfaces/TextBox.hpp index 0701b8b5..7704b448 100644 --- a/source/Interfaces/TextBox.hpp +++ b/source/Interfaces/TextBox.hpp @@ -1,5 +1,6 @@ /** - * @author : Team - 3 + * @file TextBox.hpp + * @authors Gaya Kanagaraj, Vincenzo Felici, Mui Pham * @date: 11/02/2023 * TextBox class enables interface entity to send message to the MessageBoard and World */ @@ -11,14 +12,16 @@ #include namespace i_2D { -#define DELETE_KEY 8 -#define ENTER_KEY 13 -#define ESCAPE_KEY 27 + +#define DELETE_KEY 8 ///< value for delete key +#define ENTER_KEY 13 ///< value for the enter key +#define ESCAPE_KEY 27 ///< value for the escape key /** * @class The TextBox is where users write to interface and the world */ class TextBox { + private: /// The string of the text std::unique_ptr mTextBox; @@ -30,61 +33,76 @@ namespace i_2D { // Draw the border around the TextBox sf::RectangleShape mBorderRect; + void InputLogic(int charTyped); + + void DeleteLastChar(); + public: - TextBox()=default; - explicit TextBox(const sf::Font &font ,int size = 25, sf::Color color = sf::Color::Red, bool sel = false); + TextBox() = default; + + explicit TextBox(const sf::Font &font, int size = 25, sf::Color color = sf::Color::Red, bool sel = false); + void SetString(const std::string &s); + /** * @brief Set the font of the TextBox * * @param font The font to be set to */ - void SetFont(const sf::Font &font){ + void SetFont(const sf::Font &font) { mTextBox->setFont(font); } + /** * @brief The position of the TextBox * * @param pos The position to be set to */ - void SetPosition(sf::Vector2f pos){ + void SetPosition(sf::Vector2f pos) { mTextBox->setPosition(pos); } + /** * @brief Set the limit of the TextBox * * @param ToF True of False */ - void SetLimit(bool ToF){ + void SetLimit(bool ToF) { hasLimit = ToF; } + /** * @brief Set the limit of the TextBox * * @param ToF True or False * @param lim The limit of the TextBox */ - void SetLimit(bool ToF, int lim){ + void SetLimit(bool ToF, int lim) { hasLimit = ToF; limit = lim - 1; } + void SetSelected(bool sel); + /** * @brief Get the string that was input by user * * @return Return the string */ - std::string GetText(){ + std::string GetText() { return mText.str(); } + void DrawTo(sf::RenderWindow &window); + void TypedOn(sf::Event input); + /** * @brief Checks if TextBox is active * * @return True if active, else False */ - bool IsSelected() const{ + bool IsSelected() const { return isSelected; } @@ -97,9 +115,7 @@ namespace i_2D { return mBorderRect.getGlobalBounds().contains(point); } - private: - void InputLogic(int charTyped); - void DeleteLastChar(); + }; } diff --git a/source/Interfaces/TextureHolder.cpp b/source/Interfaces/TextureHolder.cpp index 6c2dc3b7..5e501e34 100644 --- a/source/Interfaces/TextureHolder.cpp +++ b/source/Interfaces/TextureHolder.cpp @@ -111,6 +111,8 @@ namespace i_2D LoadTexture("pathTexture", "../assets/Ground_tiles/Sand1.png"); LoadTexture("portal1Texture", "../assets/walls/portal1.png"); LoadTexture("portal2Texture", "../assets/walls/portal2.png"); +// LoadTexture("portal3Texture", "../assets/walls/portal3.png"); + LoadTexture("portal4Texture", "../assets/walls/portal4.png"); textures['P'] = GetTexture("axeTexture"); textures['U'] = GetTexture("boatTexture"); textures['#'] = GetTexture("wallTexture"); @@ -121,6 +123,8 @@ namespace i_2D textures[' '] = GetTexture("pathTexture"); textures['{'] = GetTexture("portal1Texture"); textures['}'] = GetTexture("portal2Texture"); +// textures['('] = GetTexture("portal3Texture"); + textures[')'] = GetTexture("portal4Texture"); return textures; } diff --git a/source/group_3_main.cpp b/source/group_3_main.cpp index 18b23a10..0487747c 100644 --- a/source/group_3_main.cpp +++ b/source/group_3_main.cpp @@ -14,6 +14,7 @@ #include "Worlds/GenerativeWorld.hpp" int main() { + // cse491::MazeWorld world; // world.AddAgent("Pacer 1").SetPosition(3, 1); // world.AddAgent("Pacer 2").SetPosition(6, 1); From df696439a40e8f653c6c4eb4eb64969911186670 Mon Sep 17 00:00:00 2001 From: Felicivi Date: Tue, 5 Dec 2023 15:43:32 -0500 Subject: [PATCH 02/16] Health display property check. Uncommented portal files in TextureHolder. --- source/Interfaces/MainInterface.cpp | 25 +++++++++++++++---------- source/Interfaces/TextureHolder.cpp | 4 ++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/source/Interfaces/MainInterface.cpp b/source/Interfaces/MainInterface.cpp index 529d746b..e307ca5e 100644 --- a/source/Interfaces/MainInterface.cpp +++ b/source/Interfaces/MainInterface.cpp @@ -203,16 +203,21 @@ namespace i_2D { void MainInterface::DrawHealthInfo() { //TODO fix this -// // Reference health property -// int health = GetProperty("Health"); -// -// // Set text properties and draw -// sf::Text healthText(mFont); -// healthText.setCharacterSize(24); -// healthText.setPosition({20.0f, 75.0f}); -// healthText.setFillColor(sf::Color::Green); -// healthText.setString("Hp: " + std::to_string(health)); -// mWindow.draw(healthText); + // Reference health property + if(!HasProperty("Health")) + { + return; + } + + int health = GetProperty("Health"); + + // Set text properties and draw + sf::Text healthText(mFont); + healthText.setCharacterSize(24); + healthText.setPosition({20.0f, 75.0f}); + healthText.setFillColor(sf::Color::Green); + healthText.setString("Hp: " + std::to_string(health)); + mWindow.draw(healthText); } /** diff --git a/source/Interfaces/TextureHolder.cpp b/source/Interfaces/TextureHolder.cpp index 5e501e34..ee9f5660 100644 --- a/source/Interfaces/TextureHolder.cpp +++ b/source/Interfaces/TextureHolder.cpp @@ -111,7 +111,7 @@ namespace i_2D LoadTexture("pathTexture", "../assets/Ground_tiles/Sand1.png"); LoadTexture("portal1Texture", "../assets/walls/portal1.png"); LoadTexture("portal2Texture", "../assets/walls/portal2.png"); -// LoadTexture("portal3Texture", "../assets/walls/portal3.png"); + LoadTexture("portal3Texture", "../assets/walls/portal3.png"); LoadTexture("portal4Texture", "../assets/walls/portal4.png"); textures['P'] = GetTexture("axeTexture"); textures['U'] = GetTexture("boatTexture"); @@ -123,7 +123,7 @@ namespace i_2D textures[' '] = GetTexture("pathTexture"); textures['{'] = GetTexture("portal1Texture"); textures['}'] = GetTexture("portal2Texture"); -// textures['('] = GetTexture("portal3Texture"); + textures['('] = GetTexture("portal3Texture"); textures[')'] = GetTexture("portal4Texture"); return textures; From c72e5614db2824c969805caf59938b7f87a92c6f Mon Sep 17 00:00:00 2001 From: Felicivi Date: Tue, 5 Dec 2023 15:43:47 -0500 Subject: [PATCH 03/16] Health display property check. Uncommented portal files in TextureHolder. --- source/Interfaces/MainInterface.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/Interfaces/MainInterface.cpp b/source/Interfaces/MainInterface.cpp index e307ca5e..7db098cc 100644 --- a/source/Interfaces/MainInterface.cpp +++ b/source/Interfaces/MainInterface.cpp @@ -202,8 +202,6 @@ namespace i_2D { } void MainInterface::DrawHealthInfo() { - //TODO fix this - // Reference health property if(!HasProperty("Health")) { return; From f33eef93358ff7b56707a9155cd0b135dd40e278 Mon Sep 17 00:00:00 2001 From: muiph Date: Tue, 5 Dec 2023 19:48:41 -0500 Subject: [PATCH 04/16] in theory, aftering iterating over itemmap and checking each item's owner's id against the interface id, we should be able to get a list of items in our inventory. but we can't pick up items. --- source/Interfaces/Inventory.cpp | 8 +++++++- source/Interfaces/Inventory.hpp | 2 +- source/Interfaces/MainInterface.cpp | 15 +++++++++++---- source/Interfaces/MainInterface.hpp | 3 ++- source/Interfaces/Menu.cpp | 5 +++-- source/Interfaces/Menu.hpp | 7 ++++--- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/source/Interfaces/Inventory.cpp b/source/Interfaces/Inventory.cpp index d67f4180..8fc6e0d6 100644 --- a/source/Interfaces/Inventory.cpp +++ b/source/Interfaces/Inventory.cpp @@ -11,7 +11,7 @@ * * @param font The font used by the inventory list */ -void i_2D::Inventory::ConstructInventory(sf::Font &font) { +void i_2D::Inventory::ConstructInventory(sf::Font &font,const std::vector &interfaceAgentInventory) { mInventoryWindow = std::make_unique(); mInventoryWindow->setSize({mWorldSize.x,mWorldSize.y/2}); mInventoryWindow->setFillColor(sf::Color::Black); @@ -26,12 +26,18 @@ void i_2D::Inventory::ConstructInventory(sf::Font &font) { }else mRow = 3; // Create 2d vector of buttons + size_t Index = 0; for(int i = 0; i < mRow; i++) { std::vector> v1; for (int j = 0; j < mCol; j++) { v1.push_back(std::make_unique