Skip to content

Commit

Permalink
merge: pull request #123 from G-Epitech/raycasting
Browse files Browse the repository at this point in the history
feat(graphic): add interactions with tails and players
  • Loading branch information
TekMath authored Jun 26, 2024
2 parents 2bf9241 + c002473 commit 05d611b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 22 deletions.
129 changes: 107 additions & 22 deletions graphic/src/app/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <iostream>
#include <OgreCameraMan.h>
#include <OgreViewport.h>
#include <OgreTextAreaOverlayElement.h>
#include <OgreFontManager.h>
#include <OgreBorderPanelOverlayElement.h>
#include <getopt.h>
#include "App.hpp"

Expand All @@ -21,7 +24,9 @@ App::App() :
_scnMgr(nullptr),
_map(),
_commands(_client, _map, nullptr),
_options() {}
_options(),
_infosLabel(nullptr),
_infosPanel(nullptr) {}

void App::setup() {
ApplicationContext::setup();
Expand Down Expand Up @@ -221,44 +226,124 @@ void App::itemSelected(OgreBites::SelectMenu *menu) {
}
}

bool App::_isBroadcastNode(Ogre::Node *node) {
for (auto &circle: _map.broadcastCircles) {
if (circle.node == node) {
return true;
}
}
return false;
}

bool App::mousePressed(const MouseButtonEvent &evt) {
if (evt.button == OgreBites::BUTTON_LEFT)
{
// Convert to ray
Ogre::Ray ray = this->_getMouseRay(evt);

// Execute ray query
_raySceneQuery->setRay(ray);
_raySceneQuery->setSortByDistance(true);

Ogre::RaySceneQueryResult& result = _raySceneQuery->execute();
for (auto it = result.begin(); it != result.end(); ++it)
{
if (it->movable)
{
Ogre::MovableObject* object = it->movable;
_handleObjectSelection(object->getParentSceneNode());
break;
if (evt.button == OgreBites::BUTTON_LEFT) {
// Convert to ray
Ogre::Ray ray = this->_getMouseRay(evt);

// Execute ray query
_raySceneQuery->setRay(ray);
_raySceneQuery->setSortByDistance(true);

Ogre::RaySceneQueryResult &result = _raySceneQuery->execute();
for (auto it = result.begin(); it != result.end(); ++it) {
if (it->movable) {
Ogre::MovableObject *object = it->movable;
Ogre::Node *node = object->getParentSceneNode();

if (_isBroadcastNode(node)) {
continue;
}

_handleObjectSelection(node);
break;
}
}
}
return true;
}

void App::_handleObjectSelection(Ogre::Node *node)
{
void App::_handleObjectSelection(Ogre::Node *node) {
Vector2 position(0, 0);

if (_infosLabel && _infosPanel) {
trayMgr->destroyWidget(_infosLabel);
trayMgr->destroyWidget(_infosPanel);
_infosLabel = nullptr;
_infosPanel = nullptr;
}

// Tiles management
for (auto &row: _map.tiles) {
for (auto &tile : row) {
for (auto &tile: row) {
if (tile.node == node) {
std::cout << "Tile selected: " << position.x << ", " << position.y << std::endl;
Ogre::StringVector stats;
Ogre::StringVector values;

stats.push_back("Position");
values.push_back("x:" + std::to_string(static_cast<int>(position.x)) + ", y:" +
std::to_string(static_cast<int>(position.y)));
std::for_each(tile.items.begin(), tile.items.end(), [&stats, &values](const auto &item) {
stats.push_back(item.first);
values.push_back(std::to_string(item.second.size()));
});

_infosLabel = trayMgr->createLabel(TL_NONE, "Infos/StatsLabel", "Infos tile", 180);
_infosLabel->_assignListener(this);
_infosPanel = trayMgr->createParamsPanel(TL_NONE, "Infos/StatsPanel", 180, stats);
_infosPanel->setAllParamValues(values);

trayMgr->moveWidgetToTray(_infosLabel, TL_TOPRIGHT, -1);
trayMgr->moveWidgetToTray(_infosPanel, TL_TOPRIGHT, trayMgr->locateWidgetInTray(_infosLabel) + 1);

return;
}
position.x++;
}
position.y++;
position.x = 0;
}

// Player management
for (auto &player: _map.players) {
if (player.node == node) {
Ogre::StringVector stats;
Ogre::StringVector values;

stats.emplace_back("Position");
values.push_back("x:" + std::to_string(static_cast<int>(player.position.x)) + ", y:" +
std::to_string(static_cast<int>(player.position.y)));

stats.emplace_back("Team");
values.push_back(player.team);

stats.emplace_back("Level");
values.push_back(std::to_string(player.level));

stats.emplace_back("Inventory");
values.emplace_back("");

stats.emplace_back("Food");
values.push_back(std::to_string(player.inventory.food));

stats.emplace_back("Stones");
values.emplace_back(std::to_string(_getPlayerStonesNumber(player)));

_infosLabel = trayMgr->createLabel(TL_NONE, "Infos/PlayerLabel", "Infos player", 180);
_infosLabel->_assignListener(this);
_infosPanel = trayMgr->createParamsPanel(TL_NONE, "Infos/PlayerPanel", 180, stats);
_infosPanel->setAllParamValues(values);

trayMgr->moveWidgetToTray(_infosLabel, TL_TOPRIGHT, -1);
trayMgr->moveWidgetToTray(_infosPanel, TL_TOPRIGHT, trayMgr->locateWidgetInTray(_infosLabel) + 1);

return;
}
}
}

int App::_getPlayerStonesNumber(const Player &player) {
return player.inventory.linemate + player.inventory.deraumere + player.inventory.sibur +
player.inventory.mendiane + player.inventory.phiras + player.inventory.thystame;
}

Ogre::Ray App::_getMouseRay(const OgreBites::MouseButtonEvent &evt) {
Expand Down
18 changes: 18 additions & 0 deletions graphic/src/app/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class App : public OgreBites::ApplicationContext, public OgreBites::InputListene

sf::Music _background_music;

OgreBites::Label* _infosLabel;

OgreBites::ParamsPanel* _infosPanel;

/**
* @brief Load resources of the application
*/
Expand Down Expand Up @@ -213,4 +217,18 @@ class App : public OgreBites::ApplicationContext, public OgreBites::InputListene
* @param node Node of the object
*/
void _handleObjectSelection(Ogre::Node *node);

/**
* @brief Check if the node is a broadcast node
* @param node
* @return
*/
bool _isBroadcastNode(Ogre::Node *node);

/**
* @brief Get the player stones number
* @param player
* @return
*/
int _getPlayerStonesNumber(const Player &player);
};

0 comments on commit 05d611b

Please sign in to comment.