Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-a-tuto-hud-for-user-experience-zapp…
Browse files Browse the repository at this point in the history
…y-gui
  • Loading branch information
Thomaltarix committed Jun 22, 2024
2 parents 324c0f8 + b446ba2 commit f001180
Show file tree
Hide file tree
Showing 20 changed files with 306 additions and 39 deletions.
7 changes: 7 additions & 0 deletions gui/include/Event/Event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Gui::Event : public Gui::AEvent {
{KEY_K, [this](){decreaseRenderDistance();}},
{KEY_F5, [this](){changeActualPlayerPov();}},
{KEY_FOUR, [this](){changeActualPlayerPov();}},
{KEY_V, [this](){setPlayerVision();}},
{KEY_KP_ADD, [this](){increaseTimeUnit();}},
{KEY_KP_SUBTRACT, [this](){decreaseTimeUnit();}},
{KEY_H, [this](){displayHelpMenu();}},
Expand Down Expand Up @@ -210,4 +211,10 @@ class Gui::Event : public Gui::AEvent {
* @brief Close the window for Gamepad
*/
void closeWindowGamepad();

/**
* @brief Set the player vision.
*
*/
void setPlayerVision();
};
14 changes: 12 additions & 2 deletions gui/include/Network/ANetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ class Gui::ANetwork : public Gui::INetwork {
/**
* @brief Listen the server and return it message.
*
* @return std::string - Message of the server.
* @return BufferState - Buffer state.
*/
virtual const std::string listenServer() = 0;
virtual BufferState listenServer() = 0;

/**
* @brief Send a message to the Server.
Expand All @@ -88,7 +88,17 @@ class Gui::ANetwork : public Gui::INetwork {
*/
virtual void sendMessageServer(const std::string& message) = 0;

/**
* @brief Get the Buffer object.
* Be careful, this method will
* delete the current buffer.
*
* @return std::string - Buffer message.
*/
std::string getBuffer();

protected:
int _port; //!< Port of the server.
std::string _hostName; //!< Host name of the server.
std::string _buffer; //!< Buffer to receive server message.
};
19 changes: 17 additions & 2 deletions gui/include/Network/INetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class Gui::INetwork {

public:

enum BufferState {
NONE,
READY,
SERVER_ERROR
};

/**
* @brief Destroy the INetwork object.
*
Expand Down Expand Up @@ -69,14 +75,23 @@ class Gui::INetwork {
/**
* @brief Listen to the server.
*
* @return std::string Message from the server.
* @return BufferState - Buffer state.
*/
virtual const std::string listenServer() = 0;
virtual BufferState listenServer() = 0;

/**
* @brief Send a message to the server.
*
* @param message Message to send.
*/
virtual void sendMessageServer(const std::string &message) = 0;

/**
* @brief Get the Buffer object.
* Be careful, this method will
* delete the current buffer.
*
* @return std::string - Buffer message.
*/
virtual std::string getBuffer() = 0;
};
8 changes: 4 additions & 4 deletions gui/include/Network/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class Gui::Network : public Gui::ANetwork {
/**
* @brief Listen the server and return it message.
*
* @return std::string - Message of the server.
* @return BufferState - Buffer state.
*/
const std::string listenServer();
BufferState listenServer();

/**
* @brief Send a message to the Server.
Expand All @@ -72,9 +72,9 @@ class Gui::Network : public Gui::ANetwork {
/**
* @brief Read the server output.
*
* @return const std::string - Server message.
* @return BufferState - Buffer state.
*/
const std::string readInfoServer();
BufferState readInfoServer();

int _serverFd; //!< server file descriptor
fd_set _writeFd; //!< file descriptor for write access
Expand Down
13 changes: 12 additions & 1 deletion gui/include/Render/Decoration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class Gui::Decoration {
* @param mapSize Size of the map.
* @param renderDistance Distance to render.
* @param camPos Position of the camera.
* @param displayPos Positions to know what to display.
*/
void display(std::pair<std::size_t, std::size_t> mapSize, size_t renderDistance, std::pair<std::size_t, std::size_t> camPos);
void display(std::pair<std::size_t, std::size_t> mapSize, size_t renderDistance, std::pair<std::size_t, std::size_t> camPos, std::vector<Vector2> displayPos);

/**
* @brief Generate random emplacement for decorations.
Expand All @@ -74,4 +75,14 @@ class Gui::Decoration {
* @param posTile Position of a tile.
*/
void displayTree(size_t i, size_t j, Vector3 posTile);

/**
* @brief Check if a position is in player vision.
*
* @param position Position to check.
* @param _playerVisionPositions Positions of player vision.
* @return true - Position is in player vision.
* @return false - Position is not in player vision.
*/
bool isInArrayPlayerVision(std::pair<size_t, size_t> pos, std::vector<Vector2> _playerVisionPositions);
};
52 changes: 52 additions & 0 deletions gui/include/Render/Render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ class Gui::Render {
*/
void setTimeUnit(size_t timeUnit);

/**
* @brief Set the Player Vision value.
*
* @param isPlayerVision New player vision value.
* @note True to display player vision.
* @note False to not display player vision.
*/
void setPlayerVision(bool isPlayerVision);

/**
* @brief Get the Player Vision value.
*
* @return true - Display player vision.
* @return false - Do not display player vision.
*/
bool getPlayerVision() const;

/**
* @brief Set the Help Menu value.
*
Expand Down Expand Up @@ -236,6 +253,7 @@ class Gui::Render {
Model _thystameModel; //!< Model to display thystames.
Model _deraumereModel; //!< Model to display deraumeres.
Texture2D _cursorTexture; //!< Cursor texture.
std::vector<Vector2> _playerVisionPositions; //!< Player vision positions.

/**
* @brief Load the models to draw.
Expand Down Expand Up @@ -384,6 +402,40 @@ class Gui::Render {
*/
std::pair<std::size_t, std::size_t> getCameraTile();

/**
* @brief Get the positions of objects in player vision.
*
* @return size_t - Player id.
*/
std::vector<Vector2> getPositionsInPlayerVision(size_t playerId);

/**
* @brief Check if a position is in player vision.
*
* @param position Position to check.
* @return true - Position is in player vision.
* @return false - Position is not in player vision.
*/
bool isInArrayPlayerVision(std::pair<size_t, size_t> pos);

/**
* @brief Get the line of vision.
*
* @param pos Position to check.
* @param sizeOfHalf size of the half of the line of vision.
* @param orientation orientation of the vision.
*/
std::vector<Vector2> getLineOfVision(Vector2 pos, size_t sizeOfHalf, size_t orientation);

/**
* @brief Add a position to the vision.
*
* @param vision Vision to add the position.
* @param pos Positions to add.
* @return std::vector<Vector2> - Vision with the position added.
*/
std::vector<Vector2> addVisionPosition(std::vector<Vector2> vision, std::vector<Vector2> pos);

/**
* @brief Display the help menu.
*
Expand Down
16 changes: 16 additions & 0 deletions gui/include/Render/UserCamera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,26 @@ class Gui::UserCamera {
*/
bool isPlayerPov() const;

/**
* @brief Check if the camera is in player vision.
*
* @return true - Camera is in player vision.
* @return false - Camera is not in player vision.
*/
bool isPlayerVision() const;

/**
* @brief Set the Player Vision object.
*
* @param isPlayerVision Is player vision.
*/
void setPlayerVision(bool isPlayerVision);

private:

std::shared_ptr<Camera> _camera; //!< Camera raylib instance.
CameraType _type; //!< Type of camera.
size_t _playerId; //!< Player id.
std::pair<std::size_t, std::size_t> _tilePos; //!< Tile position.
bool _isPlayerVision; //!< Is player vision.
};
7 changes: 4 additions & 3 deletions gui/src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ void Gui::Engine::run()

void Gui::Engine::listenServer()
{
std::string command = _network.get()->listenServer();
Gui::INetwork::BufferState bufferState = _network.get()->listenServer();

if (command == "")
if (bufferState == Gui::INetwork::BufferState::NONE)
return;
if (command == SERVER_DOWN_MESSAGE) {
if (bufferState == Gui::INetwork::BufferState::SERVER_ERROR) {
std::cout << STR_RED << SERVER_DOWN_MESSAGE << STR_RESET << std::endl;
_gameData.get()->setIsEndGame(true);
return;
}
try {
std::string command = _network->getBuffer();
std::vector<std::string> arguments = _parser->parse(command);
std::istringstream stream(command);
std::string keyCommand;
Expand Down
5 changes: 5 additions & 0 deletions gui/src/Event/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,8 @@ void Gui::Event::closeWindowGamepad()
{
_gameData.get()->setIsEndGame(true);
}

void Gui::Event::setPlayerVision()
{
_render.get()->setPlayerVision(!_render.get()->getPlayerVision());
}
7 changes: 7 additions & 0 deletions gui/src/Network/ANetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ std::string Gui::ANetwork::getHostName() const
{
return this->_hostName;
}

std::string Gui::ANetwork::getBuffer()
{
std::string command = _buffer;
_buffer = "";
return command;
}
24 changes: 12 additions & 12 deletions gui/src/Network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,38 +47,38 @@ void Gui::Network::selectServer()
throw Errors::NetworkException("Select failed.");
}

const std::string Gui::Network::listenServer()
Gui::Network::BufferState Gui::Network::listenServer()
{
selectServer();
std::string data = readInfoServer();
if (!_isConnected && data == "WELCOME") {
BufferState bufferSate = readInfoServer();
if (!_isConnected && _buffer == "WELCOME" && bufferSate == READY) {
sendMessageServer("GRAPHIC\n");
sendMessageServer("sgt\n");
sendMessageServer("msz\n");
sendMessageServer("mct\n");
sendMessageServer("tna\n");
_isConnected = true;
return "";
_buffer = "";
return Gui::Network::NONE;
}
return data;
return bufferSate;
}

const std::string Gui::Network::readInfoServer()
Gui::Network::BufferState Gui::Network::readInfoServer()
{
std::string data;
char buffer;
int len;

if (!FD_ISSET(_serverFd, &_readFd))
return "";
return NONE;
while ((len = read(_serverFd, &buffer, 1)) > 0) {
if (buffer == '\n')
break;
data.append(&buffer, 1);
return READY;
_buffer.append(&buffer, 1);
}
if (len == 0)
return SERVER_DOWN_MESSAGE;
return data;
return SERVER_ERROR;
return NONE;
}

void Gui::Network::sendMessageServer(const std::string &message)
Expand Down
15 changes: 14 additions & 1 deletion gui/src/Render/Decoration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Map<bool> Gui::Decoration::getGenerationItem(std::size_t ratio)
return map;
}

void Gui::Decoration::display(std::pair<std::size_t, std::size_t> mapSize, size_t renderDistance, std::pair<std::size_t, std::size_t> camPos)
void Gui::Decoration::display(std::pair<std::size_t, std::size_t> mapSize, size_t renderDistance, std::pair<std::size_t, std::size_t> camPos, std::vector<Vector2> displayPos)
{
if (mapSize != _mapSize) {
_mapSize = mapSize;
Expand All @@ -39,6 +39,8 @@ void Gui::Decoration::display(std::pair<std::size_t, std::size_t> mapSize, size_

for (int i = 0; i < (int)_mapSize.first; i++) {
for (int j = 0; j < (int)_mapSize.second; j++) {
if (!isInArrayPlayerVision({i, j}, displayPos))
continue;
if (i > (int)(camPos.first - renderDistance) && i < (int)(camPos.first + renderDistance) && j > (int)(camPos.second - renderDistance) && j < (int)(camPos.second + renderDistance)) {
Vector3 posTile = {(float)(i * SIZE_TILE), 0.0f, (float)(j * SIZE_TILE)};
displayTree(i, j, posTile);
Expand All @@ -53,3 +55,14 @@ void Gui::Decoration::displayTree(size_t i, size_t j, Vector3 posTile)
if (_mapTree[i][j])
DrawModelEx(_treeModel, (Vector3){posTile.x + posTreeModel.x, posTile.y + posTreeModel.y, posTile.z + posTreeModel.z}, ROTATION_AXIS_TREE, ROTATION_ANGLE_TREE, SCALE_TREE, WHITE);
}

bool Gui::Decoration::isInArrayPlayerVision(std::pair<size_t, size_t> pos, std::vector<Vector2> _playerVisionPositions)
{
if (_playerVisionPositions.empty())
return true;
for (auto &position : _playerVisionPositions) {
if (position.x == pos.first && position.y == pos.second)
return true;
}
return false;
}
Loading

0 comments on commit f001180

Please sign in to comment.