Skip to content

Commit

Permalink
Merge pull request #71 from neo-jgrec/feat/gui/code-improve
Browse files Browse the repository at this point in the history
Feat/gui/code improve
  • Loading branch information
tomkawohl authored Jun 23, 2024
2 parents 92c91aa + fc351e1 commit 48bc56a
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 6 deletions.
Binary file modified assets/bousolle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/flags/lookButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions src/gui/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "utils/CommandLineParser.hpp"
#include "utils/GuiException.hpp"

#include "error/ErrorWindow.hpp"

int main(int argc, char **argv) {
int port = 0;
std::string ip = "";
Expand Down Expand Up @@ -49,8 +51,19 @@ int main(int argc, char **argv) {

try {
debug_print << "Starting GUI" << std::endl;
Core core(port, ip);
core.run();
try {
Core core(port, ip);
try {
core.run();
} catch (const std::exception &e) {
ErrorWindow errorWindow(1);
errorWindow.run();
}
} catch (const std::exception &e) {
ErrorWindow errorWindow(0);
errorWindow.run();
}

} catch (const guiException& e) {
std::cerr << e.what() << std::endl;
return 84;
Expand Down
75 changes: 75 additions & 0 deletions src/gui/src/error/ErrorWindow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
** EPITECH PROJECT, 2024
** zappy
** File description:
** ErrorWindow
*/

#ifndef ERRORWINDOW_HPP_
#define ERRORWINDOW_HPP_

#include <SFML/Graphics.hpp>
#include <vector>
#include <string>


static const std::vector<std::string> errorTitles = {
"Error: Initialization failed",
"Error: Runtime error",
};

static const std::vector<std::string> errorMessages = {
"uh oh, something went wrong during \nthe initialization of the game, \nplease check the logs\n or reinstall the game",
"uh oh, something went wrong during \nthe runtime of the game, \nplease check the logs\n or reinstall the game",
};

class ErrorWindow {

public:
ErrorWindow(int errorType) {
if (errorType < 0 || errorType > 1)
errorType = 0;
_errorType = errorType;
_window.create(sf::VideoMode(400, 225), errorTitles[_errorType], sf::Style::Close);
if (_font.loadFromFile("assets/BadComic-Regular.ttf") == false)
throw std::runtime_error("Error: Font not found");
_text.setFont(_font);
_text.setString(errorMessages[_errorType]);
_text.setCharacterSize(15);
_text.setPosition(0, 0);

if (_texture.loadFromFile("assets/error.png") == false)
throw std::runtime_error("Error: Texture not found");
_sprite.setTexture(_texture);
_sprite.setPosition(
400 / 2 - 140 / 2
, 225 - 140
);

};
~ErrorWindow() {};

void run() {
while (_window.isOpen()) {
sf::Event event;
while (_window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
_window.close();
}
_window.clear();
_window.draw(_text);
_window.draw(_sprite);
_window.display();
}
}
protected:
private:
int _errorType = 0;
sf::Font _font;
sf::Text _text;
sf::Texture _texture;
sf::Sprite _sprite;
sf::RenderWindow _window;
};

#endif /* !ERRORWINDOW_HPP_ */
17 changes: 16 additions & 1 deletion src/gui/src/render/scenes/world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ World::World(Core *core)
_sprites["hoverEgg"] = std::make_shared<Sprite>("./assets/hoverEgg.png");
_sprites["trantorian_run"] = std::make_shared<Sprite>("./assets/trantorian_run.png", 6, 0.1f);
_sprites["trantorian_spawn"] = std::make_shared<Sprite>("./assets/trantorian_spawn.png", 12, 0.1f);
_sprites["trantorian_death"] = std::make_shared<Sprite>("./assets/trantorian_death.png", 13, 0.1f);
_sprites["grass"] = std::make_shared<Sprite>("./assets/grass_detail(1).png");
_sprites["grass2"] = std::make_shared<Sprite>("./assets/stone(1).png");
_sprites["tree2"] = std::make_shared<Sprite>("./assets/trees/tree(2).png");
Expand All @@ -33,6 +34,7 @@ World::World(Core *core)
_sprites["starling"] = std::make_shared<Sprite>("assets/trantorian.png");
_sprites["starling"]->setScale(2);
_sprites["buildings"] = std::make_shared<Sprite>("./assets/houses.png", 8, 0.1f);
_sprites["lvlup"] = std::make_shared<Sprite>("./assets/lvlup.png", 8, 0.1f);
_view.setSize(sf::Vector2f(1920 , 1080));
_chat = std::make_shared<Chat>(_core->getFont(), 7);
_bubbleText = sf::Text("", _core->getFont(), 15);
Expand Down Expand Up @@ -131,4 +133,17 @@ void World::initSounds()
_sounds["wololo"].setVolume(100);
_sounds["interact"].openFromFile("./assets/audio/interact.ogg");
_sounds["interact"].setVolume(50);
}
}

void World::lookTrantorian(int index)
{
Trantorian trantorian = _trantorians[index];

sf::Vector2f pos = trantorian.getPos();
sf::Vector2f viewPos = _pos + _tmpOffset + _offset;
sf::Vector2f offset = sf::Vector2f(
pos.x - viewPos.x,
pos.y - viewPos.y
);
_offset += offset;
}
1 change: 1 addition & 0 deletions src/gui/src/render/scenes/world/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class World : public IScene {
sf::Vector2f _selectedTile = sf::Vector2f(-1, -1);
std::vector<std::vector<Chunck>> _chuncks;
std::vector<Ranking> _rankings;
void lookTrantorian(int index);
private:
void reset();
void getServerInit();
Expand Down
12 changes: 11 additions & 1 deletion src/gui/src/render/scenes/world/ui/WorldUi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ bool WorldUi::update(sf::Event event, sf::RenderWindow &window)
return false;
_idPlayer = (_idPlayer + 1) % _world->getNbTrantorian();
}
if (_world->getNbTrantorian() != 0)
if (_layer2["lookButton"]->update(event, window)) {
_world->lookTrantorian(_idPlayer);
}
}
if (_panelState == FLAG) {
if (_layer2["prevButton"]->update(event, window)) {
Expand Down Expand Up @@ -162,6 +166,7 @@ void WorldUi::drawTrantorian(sf::RenderWindow &window)
_sprites["bousolle"]->draw(window);
if (_world->getNbTrantorian() == 0)
return;
_layer2["lookButton"]->draw(window);
auto trantorian = _world->getTrantorian(_idPlayer);
_inventoryText.setPosition(sf::Vector2f(
_sprites["trantorianPanel"]->getPosition().x + + 96,
Expand All @@ -184,7 +189,12 @@ void WorldUi::drawTrantorian(sf::RenderWindow &window)
_sprites["trantorianPanel"]->getPosition().x + + 96,
_sprites["trantorianPanel"]->getPosition().y + 32 + 50
));
_inventoryText.setString("Facing " + orientation[trantorian._facing - 1]);
int facing = trantorian._facing - 1;
if (facing < 0)
facing = 3;
if (facing > 3)
facing = 0;
_inventoryText.setString("Facing " + orientation[facing]);
window.draw(_inventoryText);
}

Expand Down
5 changes: 3 additions & 2 deletions src/gui/src/render/scenes/world/ui/WorldUi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class WorldUi : public IScene {
_layer1.push_back(std::make_shared<AdvancedButton>("./assets/flags/flagButton.png", sf::Vector2f(0, 16*4 * 1), sf::Vector2f(16, 16), 4));
_layer1.push_back(std::make_shared<AdvancedButton>("./assets/flags/trantorianButton.png", sf::Vector2f(0, 16*4 * 2), sf::Vector2f(16, 16), 4));
_layer2["closeButton"] = std::make_shared<AdvancedButton>("./assets/flags/closeButton.png", sf::Vector2f(16 * 4 *7, 0), sf::Vector2f(16, 16), 4);
_layer2["prevButton"] = std::make_shared<AdvancedButton>("./assets/flags/prevButton.png", sf::Vector2f(16 * 4 + 8, 64), sf::Vector2f(16, 16), 4);
_layer2["nextButton"] = std::make_shared<AdvancedButton>("./assets/flags/nextButton.png", sf::Vector2f(16 * 4 * 6 - 8, 64), sf::Vector2f(16, 16), 4);
_layer2["prevButton"] = std::make_shared<AdvancedButton>("./assets/flags/prevButton.png", sf::Vector2f(16 * 4 + 8, 32), sf::Vector2f(16, 16), 4);
_layer2["nextButton"] = std::make_shared<AdvancedButton>("./assets/flags/nextButton.png", sf::Vector2f(16 * 4 * 6 - 8, 32), sf::Vector2f(16, 16), 4);
_layer2["lookButton"] = std::make_shared<AdvancedButton>("./assets/flags/lookButton.png", sf::Vector2f(16 * 4 * 6 - 8, 96), sf::Vector2f(16, 16), 4);
_sprites["inventory"] = std::make_shared<Sprite>("./assets/inventory.png");
_sprites["food"] = std::make_shared<Sprite>("./assets/ressources/food.png");
_sprites["trantorian"] = std::make_shared<Sprite>("./assets/trantorian.png");
Expand Down

0 comments on commit 48bc56a

Please sign in to comment.