From 5c3c3555c9adc29aa667df52f899d337d9ad6583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sat, 19 Jun 2021 15:26:59 +0200 Subject: [PATCH] dding a timer delay but linak issue --- sources/Parser/ParserYaml.cpp | 11 ++++++++++- sources/Runner/Runner.cpp | 1 + sources/Runner/Runner.hpp | 3 +++ sources/System/Renderer/CameraSystem.cpp | 2 +- sources/Utils/Utils.cpp | 8 ++++++++ sources/Utils/Utils.hpp | 9 ++++++++- 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index ad5a4dbe..8a3900fb 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -127,10 +127,14 @@ namespace BBM { std::ofstream blockFile(block); std::ofstream playerFile(player); std::ofstream bonusFile(bonus); + auto &ret = scene->view, TimerComponent>(); + for (auto &[myEntity, tag, timerComponent] : ret) { + _block << "timer: " << timerComponent.ringIn.count(); + } _player << "players:"; _bonus << "bonuses:"; - _block << "width: " << std::to_string(Runner::mapWidth); + _block << std::endl << "width: " << std::to_string(Runner::mapWidth); _block << std::endl << "height: " + std::to_string(Runner::mapHeight); _block << std::endl << "blocks:"; for (const auto &entity : scene->getEntities()) { @@ -233,6 +237,11 @@ namespace BBM { throw ParserError("width property must be an int"); } Runner::mapHeight = size; + long timer = 0; + if (!Utils::tryParseLong(node.getProperty("timer"), timer)) { + throw ParserError("timer property parsing error (must be a long)"); + } + Runner::timerDelay = std::chrono::nanoseconds(timer); for (int i = 0; i < Runner::mapWidth; i++) for (int j = 0; j < Runner::mapHeight; j++) diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 99cc17d7..7f2db6ac 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -52,6 +52,7 @@ namespace BBM int Runner::mapWidth = 16; int Runner::mapHeight = 16; bool Runner::hasHeights = false; + std::chrono::nanoseconds timerDelay = std::chrono::minutes(3); void Runner::updateState(WAL::Wal &engine, GameState &state) { diff --git a/sources/Runner/Runner.hpp b/sources/Runner/Runner.hpp index da023c97..475ea15b 100644 --- a/sources/Runner/Runner.hpp +++ b/sources/Runner/Runner.hpp @@ -5,6 +5,7 @@ #pragma once #include "Models/GameState.hpp" #include "Wal.hpp" +#include #include #include "Component/Sound/SoundComponent.hpp" @@ -19,6 +20,8 @@ namespace BBM static int mapWidth; //! @brief the height of the map static int mapHeight; + //! @brief timer duration + static std::chrono::nanoseconds timerDelay; //! @brief store current scenes informations static GameState gameState; diff --git a/sources/System/Renderer/CameraSystem.cpp b/sources/System/Renderer/CameraSystem.cpp index 749bca2e..9339766f 100644 --- a/sources/System/Renderer/CameraSystem.cpp +++ b/sources/System/Renderer/CameraSystem.cpp @@ -32,7 +32,7 @@ namespace BBM .addComponent(1920 / 2 - 2 * 30 - 20, 28, 0) .addComponent(Vector2f(), Vector2f(150, 60), RAY::Color(BLACK).setA(150)); this->_wal.getScene()->scheduleNewEntity("Timer") - .addComponent(std::chrono::minutes (3), [](WAL::Entity &, WAL::Wal &engine) { + .addComponent(Runner::timerDelay, [](WAL::Entity &, WAL::Wal &engine) { engine.getSystem().hasEnded = false; Runner::gameState.nextScene = GameState::ScoreScene; }) diff --git a/sources/Utils/Utils.cpp b/sources/Utils/Utils.cpp index e7ac109b..e0d46c87 100644 --- a/sources/Utils/Utils.cpp +++ b/sources/Utils/Utils.cpp @@ -91,4 +91,12 @@ namespace BBM return strings; } + bool Utils::tryParseLong(const std::string &s, long &l) + { + std::istringstream iss(s); + + iss >> std::noskipws >> l; + return iss.eof() && !iss.fail(); + } + } \ No newline at end of file diff --git a/sources/Utils/Utils.hpp b/sources/Utils/Utils.hpp index d3220256..64f43e44 100644 --- a/sources/Utils/Utils.hpp +++ b/sources/Utils/Utils.hpp @@ -29,9 +29,16 @@ namespace BBM //! @brief find the frequency of a substring in a string static int findFrequency(const std::string &s, const std::string &pattern); - + //! @brief return true if parsing has been successful result ill be in i static bool tryParseInteger(const std::string &s, int &i); + + //! @brief return true if parsing has been successful result ill be in f static bool tryParseFloat(const std::string &s, float &f); + + //! @brief return true if parsing has been successful result ill be in l + static bool tryParseLong(const std::string &s, long &l); + + //! @brief split a string with a delim char static std::vector splitStr(const std::string &str, char delim); };