diff options
Diffstat (limited to 'game')
-rw-r--r-- | game/CMakeLists.txt | 2 | ||||
-rw-r--r-- | game/Config.h | 20 | ||||
-rw-r--r-- | game/GameScene.cpp | 10 | ||||
-rw-r--r-- | game/coins/CoinScript.cpp | 5 | ||||
-rw-r--r-- | game/coins/CoinScript.h | 2 | ||||
-rw-r--r-- | game/hud/HudConfig.h | 28 | ||||
-rw-r--r-- | game/hud/HudScript.cpp | 33 | ||||
-rw-r--r-- | game/hud/HudScript.h | 12 | ||||
-rw-r--r-- | game/hud/HudSubScene.cpp | 35 | ||||
-rw-r--r-- | game/hud/HudSubScene.h | 8 | ||||
-rw-r--r-- | game/mainmenu/BannerSubScene.cpp | 5 | ||||
-rw-r--r-- | game/mainmenu/ButtonSubScene.cpp | 5 | ||||
-rw-r--r-- | game/mainmenu/MainMenuConfig.h | 5 | ||||
-rw-r--r-- | game/mainmenu/MainMenuScene.cpp | 3 |
14 files changed, 157 insertions, 16 deletions
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index ece2a40..50c9b4d 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -35,6 +35,8 @@ add_executable(main coins/CoinPool.cpp coins/CoinSystemScript.cpp coins/CoinScript.cpp + hud/HudSubScene.cpp + hud/HudScript.cpp ) target_link_libraries(main PUBLIC crepe) diff --git a/game/Config.h b/game/Config.h index ec753df..210326e 100644 --- a/game/Config.h +++ b/game/Config.h @@ -1,4 +1,5 @@ #pragma once +#include "types.h" static constexpr int SORT_IN_LAY_BACK_BACKGROUND = 3; // For all scenes static constexpr int SORT_IN_LAY_BACKGROUND = 4; // For all scenes @@ -19,3 +20,22 @@ static constexpr int GAME_HEIGHT = 800; // In game units static constexpr int VIEWPORT_X = 1100; // In game units // 'GAME_HEIGHT' (below) should be replaced by '500' when game development is finished static constexpr int VIEWPORT_Y = GAME_HEIGHT; // In game units + +// Font settings +static constexpr const char* FONT = "Jetpackia"; +static constexpr crepe::vec2 FONTOFFSET = {0,0}; + +// Save data + +// Amount of coins in game +static constexpr const char* TOTAL_COINS_GAME = "total_coins_game"; + +// Amount of coins in current run +static constexpr const char* TOTAL_COINS_RUN = "total_coins_run"; + +// Distance +static constexpr const char* DISTANCE_GAME = "distance_game"; +static constexpr const char* DISTANCE_RUN = "distance_run"; + +// Global tags and names +static constexpr const char* PLAYER_NAME = "player"; diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 551f4f1..2073a1e 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -8,6 +8,8 @@ #include "coins/CoinSystemScript.h" #include "background/BackgroundSubScene.h" +#include "hud/HudScript.h" +#include "hud/HudSubScene.h" #include <cmath> #include <crepe/api/Animator.h> @@ -40,6 +42,7 @@ void GameScene::load_scene() { ); camera.add_component<BehaviorScript>().set_script<MoveCameraManualyScript>(); camera.add_component<BehaviorScript>().set_script<CoinSystemScript>(); + camera.add_component<BehaviorScript>().set_script<HudScript>(); camera.add_component<Rigidbody>(Rigidbody::Data{}); PlayerSubScene player(*this); @@ -71,12 +74,13 @@ void GameScene::load_scene() { GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0)); start_game_script.add_component<BehaviorScript>().set_script<StartGameScript>(); - CoinSubScene coin; - coin.create(*this); - + //create coin pool CoinPool coin_system; coin_system.create_coins(*this); + HudSubScene hud; + hud.create(*this); + } string GameScene::get_name() const { return "scene1"; } diff --git a/game/coins/CoinScript.cpp b/game/coins/CoinScript.cpp index 862e7f9..5a1e922 100644 --- a/game/coins/CoinScript.cpp +++ b/game/coins/CoinScript.cpp @@ -2,6 +2,7 @@ #include "api/CircleCollider.h" #include "api/Sprite.h" #include "manager/SaveManager.h" +#include "../Config.h" using namespace crepe; using namespace std; @@ -12,8 +13,8 @@ bool CoinScript::on_collision(const CollisionEvent & collisionData){ this->get_component<Sprite>().active = false; this->get_component<CircleCollider>().active = false; SaveManager & savemgr = this->get_save_manager(); - int amount = savemgr.get<int>(COIN_GAME_AMOUNT,0).get() + 1; - savemgr.set(COIN_GAME_AMOUNT, amount); + int amount = savemgr.get<int>(TOTAL_COINS_RUN,0).get() + 1; + savemgr.set(TOTAL_COINS_RUN, amount); return true; } diff --git a/game/coins/CoinScript.h b/game/coins/CoinScript.h index 3fcee6d..96e54fa 100644 --- a/game/coins/CoinScript.h +++ b/game/coins/CoinScript.h @@ -6,6 +6,4 @@ class CoinScript : public crepe::Script { public: void init() override; bool on_collision(const crepe::CollisionEvent & collisionData); - static constexpr const char* PLAYER_NAME = "player"; - static constexpr const char* COIN_GAME_AMOUNT = "coin_game_amount"; }; diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h new file mode 100644 index 0000000..91e3bfa --- /dev/null +++ b/game/hud/HudConfig.h @@ -0,0 +1,28 @@ +#pragma once +#include "types.h" + +static constexpr crepe::vec2 TOP_LEFT = {-500,-380}; +static constexpr const char* HUD_DISTANCE = "hud_distance"; +static constexpr const char* HUD_BEST = "hud_best"; +static constexpr const char* HUD_COINS = "hud_coins"; + +// Distance +static constexpr const char* DISTANCE_PLACEHOLDER = "0000m"; +static constexpr const char* DISTANCE_UNIT = "m"; +static constexpr int DISTANCE_LENGTH = 5; +static constexpr float DISTANCE_WIDTH = 60; +static constexpr float STEP_SIZE_DISTANCE = 100; + +// BEST +static constexpr const char* BEST = "BEST:"; +static constexpr int BEST_LENGTH = 5; +static constexpr float BEST_WIDTH = 40; +static constexpr crepe::vec2 BEST_OFFSET = {0,25}; + +// COINS +static constexpr const char* COINS = "0000"; +static constexpr int COINS_LENGTH = 4; +static constexpr float COINS_WIDTH = 40; +static constexpr crepe::vec2 COINS_OFFSET = {0,50}; + +
\ No newline at end of file diff --git a/game/hud/HudScript.cpp b/game/hud/HudScript.cpp new file mode 100644 index 0000000..42ff118 --- /dev/null +++ b/game/hud/HudScript.cpp @@ -0,0 +1,33 @@ +#include "HudScript.h" +#include "api/Text.h" +#include "api/Transform.h" +#include "manager/SaveManager.h" +#include "../Config.h" +#include "HudConfig.h" + +using namespace crepe; +using namespace std; + +void HudScript::init() { + savemgr = &this->get_save_manager(); + savemgr->set(TOTAL_COINS_RUN,0); + Text & txt = this->get_components_by_name<Text>(HUD_BEST).front(); + string record = BEST+to_string(savemgr->get<int>(DISTANCE_GAME,0).get())+DISTANCE_UNIT; + txt.text = record; +} + +void HudScript::frame_update(crepe::duration_t dt) { + + // string number = std::to_string(savemgr->get<int>(DISTANCE_RUN,0).get()); + + // Distance + Text & txt_dt = this->get_components_by_name<Text>(HUD_DISTANCE).front(); + Transform & tf = this->get_components_by_name<Transform>(PLAYER_NAME).front(); + string distance = to_string(static_cast<int>(tf.position.x/STEP_SIZE_DISTANCE)) + DISTANCE_UNIT; + txt_dt.text = distance; + + // Coins + Text & txt_co = this->get_components_by_name<Text>(HUD_COINS).front(); + string amount_of_coins = to_string(savemgr->get<int>(TOTAL_COINS_RUN,0).get()); + txt_co.text = amount_of_coins; +} diff --git a/game/hud/HudScript.h b/game/hud/HudScript.h new file mode 100644 index 0000000..aa92582 --- /dev/null +++ b/game/hud/HudScript.h @@ -0,0 +1,12 @@ +#pragma once + +#include "api/Script.h" +#include "manager/SaveManager.h" + +class HudScript : public crepe::Script { +public: + void init() override; + void frame_update(crepe::duration_t dt) override; +private: + crepe::SaveManager* savemgr; +}; diff --git a/game/hud/HudSubScene.cpp b/game/hud/HudSubScene.cpp new file mode 100644 index 0000000..e76623d --- /dev/null +++ b/game/hud/HudSubScene.cpp @@ -0,0 +1,35 @@ +#include "HudSubScene.h" +#include "api/GameObject.h" +#include "api/Text.h" +#include "../Config.h" +#include "HudConfig.h" + +using namespace crepe; +using namespace std; + +void HudSubScene::create(Scene & scn){ + + // Distance + GameObject hud_dis = scn.new_object(HUD_DISTANCE); + crepe::vec2 size_distance = {DISTANCE_WIDTH,(DISTANCE_WIDTH/DISTANCE_LENGTH)*2}; + hud_dis.add_component<Text>(size_distance, FONT,Text::Data{ + .world_space = false, + .text_color = Color::WHITE, + }, TOP_LEFT+FONTOFFSET, DISTANCE_PLACEHOLDER); + + // Best + GameObject hud_best = scn.new_object(HUD_BEST); + crepe::vec2 size_best = {BEST_WIDTH,(BEST_WIDTH/BEST_LENGTH)*2}; + hud_best.add_component<Text>(size_best, FONT,Text::Data{ + .world_space = false, + .text_color = Color::WHITE, + }, TOP_LEFT+FONTOFFSET+BEST_OFFSET, BEST); + + // Coins + GameObject hud_coin = scn.new_object(HUD_COINS); + crepe::vec2 size = {COINS_WIDTH,(COINS_WIDTH/COINS_LENGTH)*2}; + hud_coin.add_component<Text>(size, FONT,Text::Data{ + .world_space = false, + .text_color = Color::WHITE, + }, TOP_LEFT+FONTOFFSET+COINS_OFFSET, COINS); +} diff --git a/game/hud/HudSubScene.h b/game/hud/HudSubScene.h new file mode 100644 index 0000000..711a34d --- /dev/null +++ b/game/hud/HudSubScene.h @@ -0,0 +1,8 @@ +#pragma once + +#include "api/Scene.h" +class HudSubScene +{ +public: + void create(crepe::Scene & scn); +}; diff --git a/game/mainmenu/BannerSubScene.cpp b/game/mainmenu/BannerSubScene.cpp index ba4c126..0659d96 100644 --- a/game/mainmenu/BannerSubScene.cpp +++ b/game/mainmenu/BannerSubScene.cpp @@ -1,5 +1,6 @@ #include "BannerSubScene.h" #include "MainMenuConfig.h" +#include "../Config.h" #include <crepe/api/Sprite.h> @@ -33,9 +34,9 @@ void BannerSubScene::create(Scene & scn,const Data & data){ }); crepe::vec2 size = {data.banner_title_width,(data.banner_title_width/data.banner_title.size())*2}; - menu_banner.add_component<Text>( size, MainMenuConfig::FONT, Text::Data{ + menu_banner.add_component<Text>( size, FONT, Text::Data{ .world_space = true, .text_color = Color::WHITE, - }, data.banner_title_offset + MainMenuConfig::FONTOFFSET, data.banner_title); + }, data.banner_title_offset + FONTOFFSET, data.banner_title); } diff --git a/game/mainmenu/ButtonSubScene.cpp b/game/mainmenu/ButtonSubScene.cpp index 53ac8d0..760fc0d 100644 --- a/game/mainmenu/ButtonSubScene.cpp +++ b/game/mainmenu/ButtonSubScene.cpp @@ -4,6 +4,7 @@ #include "ButtonTransitionPreviewScript.h" #include "IButtonScript.h" #include "MainMenuConfig.h" +#include "../Config.h" #include "api/Color.h" #include <crepe/api/BehaviorScript.h> @@ -26,10 +27,10 @@ void ButtonSubScene::create(Scene & scn,const Data & data){ void ButtonSubScene::btn_text(crepe::GameObject & button_object,const Data & data){ crepe::vec2 size = {data.text_width,(data.text_width/data.text.size())*2}; - button_object.add_component<Text>(size, MainMenuConfig::FONT,Text::Data{ + button_object.add_component<Text>(size, FONT,Text::Data{ .world_space = data.worldspace, .text_color = Color::WHITE, - }, data.text_offset+MainMenuConfig::FONTOFFSET, data.text); + }, data.text_offset+FONTOFFSET, data.text); } void ButtonSubScene::set_script(crepe::GameObject & button_object,const Data & data){ diff --git a/game/mainmenu/MainMenuConfig.h b/game/mainmenu/MainMenuConfig.h index 99d29e8..0ce5980 100644 --- a/game/mainmenu/MainMenuConfig.h +++ b/game/mainmenu/MainMenuConfig.h @@ -29,14 +29,11 @@ struct MainMenuConfig { static constexpr float VELOCITY_STEP = 200; static constexpr float VELOCITY_INFO_UP = 30; //button config - static constexpr const char* FONT = "Jetpackia"; - static constexpr crepe::vec2 FONTOFFSET = {0,0}; static constexpr crepe::vec2 LARGE_OVERLAY_SIZE = {250,100}; static constexpr crepe::vec2 SMALL_OVERLAY_SIZE_RIGHT = {150,100}; static constexpr crepe::vec2 SMALL_OVERLAY_SIZE_LEFT = {50,100}; static constexpr crepe::vec2 SIDE_PANEL_SIZE = {50,150}; static constexpr crepe::vec2 ICON_SIZE = {50,50}; - //total coins (move to main config) - static constexpr const char* TOTAL_COINS = "total_coins"; + }; diff --git a/game/mainmenu/MainMenuScene.cpp b/game/mainmenu/MainMenuScene.cpp index e68696b..c2306b1 100644 --- a/game/mainmenu/MainMenuScene.cpp +++ b/game/mainmenu/MainMenuScene.cpp @@ -9,6 +9,7 @@ #include "api/GameObject.h" #include "api/Sprite.h" #include "manager/SaveManager.h" +#include "../Config.h" using namespace crepe; using namespace std; @@ -71,7 +72,7 @@ void MainMenuScene::load_scene(){ .world_space = false, }); SaveManager & savemgr = this->get_save_manager(); - std::string number = std::to_string(savemgr.get<int>(MainMenuConfig::TOTAL_COINS,123).get()); + string number = std::to_string(savemgr.get<int>(TOTAL_COINS_GAME,0).get()); float amount_number = static_cast<float>(number.size()); // savemgr.set(COIN_GAME_AMOUNT, amount); button.create(*this,ButtonSubScene::Data{ |