From 6a3063e6012bae44457b668ce48c48b8af90a95d Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 28 Dec 2024 12:36:26 +0100 Subject: improved naming --- game/coins/coinSubScene.cpp | 0 game/coins/coinSubScene.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 game/coins/coinSubScene.cpp create mode 100644 game/coins/coinSubScene.h (limited to 'game/coins') diff --git a/game/coins/coinSubScene.cpp b/game/coins/coinSubScene.cpp new file mode 100644 index 0000000..e69de29 diff --git a/game/coins/coinSubScene.h b/game/coins/coinSubScene.h new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From 26d66b7a73ac8d172d342be03bd75bd5bea221e1 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 28 Dec 2024 16:22:58 +0100 Subject: coin system --- game/CMakeLists.txt | 7 ++++ game/GameScene.cpp | 12 ++++++ game/coins/CoinData.h | 11 ++++++ game/coins/CoinSubScene.cpp | 35 ++++++++++++++++++ game/coins/CoinSubScene.h | 12 ++++++ game/coins/CoinSystem.cpp | 81 +++++++++++++++++++++++++++++++++++++++++ game/coins/CoinSystem.h | 22 +++++++++++ game/coins/CoinSystemScript.cpp | 18 +++++++++ game/coins/CoinSystemScript.h | 13 +++++++ game/coins/coinSubScene.cpp | 0 game/coins/coinSubScene.h | 0 game/mainmenu/MainMenuConfig.h | 1 + game/mainmenu/MainMenuScene.cpp | 6 ++- 13 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 game/coins/CoinData.h create mode 100644 game/coins/CoinSubScene.cpp create mode 100644 game/coins/CoinSubScene.h create mode 100644 game/coins/CoinSystem.cpp create mode 100644 game/coins/CoinSystem.h create mode 100644 game/coins/CoinSystemScript.cpp create mode 100644 game/coins/CoinSystemScript.h delete mode 100644 game/coins/coinSubScene.cpp delete mode 100644 game/coins/coinSubScene.h (limited to 'game/coins') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 60ac8e6..6f3beb5 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -31,6 +31,9 @@ set(SOURCES mainmenu/ShopMenuScene.cpp mainmenu/BannerSubScene.cpp mainmenu/ButtonSetShopScript.cpp + coins/CoinSubScene.cpp + coins/CoinSystem.cpp + coins/CoinSystemScript.cpp ) set(HEADERS @@ -55,6 +58,10 @@ set(HEADERS mainmenu/ShopMenuScene.h mainmenu/BannerSubScene.h mainmenu/ButtonSetShopScript.h + coins/CoinData.h + coins/CoinSubScene.h + coins/CoinSystem.h + coins/CoinSystemScript.h ) add_executable(main ${SOURCES} ${HEADERS}) diff --git a/game/GameScene.cpp b/game/GameScene.cpp index c280474..3d692f8 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -3,6 +3,9 @@ #include "MoveCameraManualyScript.h" #include "PlayerSubScene.h" #include "StartGameScript.h" +#include "coins/CoinSubScene.h" +#include "coins/CoinSystem.h" +#include "coins/CoinSystemScript.h" #include #include @@ -33,6 +36,7 @@ void GameScene::load_scene() { .bg_color = Color::RED, }); camera.add_component().set_script(); + camera.add_component().set_script(); camera.add_component(Rigidbody::Data{}); PlayerSubScene player(*this); @@ -50,6 +54,14 @@ void GameScene::load_scene() { GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0)); start_game_script.add_component().set_script(); + + CoinSubScene coin; + coin.create(*this); + + // CoinSystem coin_system; + // coin_system.create_coins(*this); + // coin_system.add_location({900,0}); + } string GameScene::get_name() const { return "scene1"; } diff --git a/game/coins/CoinData.h b/game/coins/CoinData.h new file mode 100644 index 0000000..09b3448 --- /dev/null +++ b/game/coins/CoinData.h @@ -0,0 +1,11 @@ +#pragma once + +#include "types.h" +#include + +struct CoinData{ + crepe::vec2 start_location = {0,0}; + std::string name = ""; + bool active = false; + CoinData(crepe::vec2 start_location) : start_location(start_location),name(""), active(false) {} +}; diff --git a/game/coins/CoinSubScene.cpp b/game/coins/CoinSubScene.cpp new file mode 100644 index 0000000..12ddb7a --- /dev/null +++ b/game/coins/CoinSubScene.cpp @@ -0,0 +1,35 @@ +#include "CoinSubScene.h" +#include "api/Animator.h" +#include "api/CircleCollider.h" +#include "api/Rigidbody.h" +#include "api/Scene.h" +#include "api/AudioSource.h" +#include + +using namespace crepe; +using namespace std; + +int CoinSubScene::create(Scene & scn){ + vec2 size = {20, 20}; + + static int coin_counter = 0; + std::string unique_name = "coin_" + std::to_string(coin_counter++); + + + GameObject coin = scn.new_object(unique_name.c_str(),"coin",vec2{0,0},0,1); + coin.add_component(Rigidbody::Data{}); + coin.add_component(size.x / 2).active = false; + crepe::OptionalRef coin_sprite = coin.add_component(Asset{"asset/coin/coin1_TVOS.png"}, Sprite::Data{ + .sorting_in_layer = 100, + .order_in_layer = 100, + .size = size, + }); + // coin_sprite->active = false; + coin.add_component(coin_sprite, ivec2{32, 32}, uvec2{8, 1}, + Animator::Data{ + .fps = 15, + .looping = true, + }); + coin.add_component(Asset{"asset/sfx/coin_pickup_1.ogg"}); + return coin_counter; +} diff --git a/game/coins/CoinSubScene.h b/game/coins/CoinSubScene.h new file mode 100644 index 0000000..f85f427 --- /dev/null +++ b/game/coins/CoinSubScene.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace crepe { +class Scene; +} + +class CoinSubScene { +public: + int create(crepe::Scene & scn); +}; diff --git a/game/coins/CoinSystem.cpp b/game/coins/CoinSystem.cpp new file mode 100644 index 0000000..4474195 --- /dev/null +++ b/game/coins/CoinSystem.cpp @@ -0,0 +1,81 @@ +#include "CoinSystem.h" +#include "CoinSubScene.h" +#include "api/CircleCollider.h" +#include "api/Metadata.h" +#include "api/Script.h" +#include "api/Sprite.h" +#include "api/Transform.h" +#include "types.h" + +using namespace crepe; +using namespace std; + +std::vector CoinSystem::coin_locations; + +void CoinSystem::create_coins(crepe::Scene & scn) { + CoinSubScene coin; + while(coin.create(scn) < this->MAXIMUM_AMOUNT); +} + +void CoinSystem::add_location(const crepe::vec2& location){ + coin_locations.push_back(CoinData(location)); +} + +void CoinSystem::remove_location(const std::string& name){ + auto it = std::find_if(coin_locations.begin(), coin_locations.end(), + [&name](const CoinData& data) { + return data.name == name; + }); + + // If a match is found, erase + if (it != coin_locations.end()) { + coin_locations.erase(it); + } +} + + +void CoinSystem::update(float position, RefVector coin_sprites, RefVector coin_colliders, RefVector coin_metadatas) { + for (auto& coin : coin_locations) { + if (!coin.active) { + if (coin.start_location.x > SPAWN_DISTANCE + position && coin.start_location.x < SPAWN_AREA + SPAWN_DISTANCE + position) { + + // Local variables to hold the matching coin data + Metadata* coin_metadata = nullptr; + CircleCollider* coin_collider = nullptr; + + // Iterate over the sprites to find the matching one + for (Sprite& coin_sprite : coin_sprites) { + if (!coin_sprite.active) { + // Find the matching Metadata + for (Metadata& coin_metadata_unkown : coin_metadatas) { + if (coin_metadata_unkown.game_object_id == coin_sprite.game_object_id) { + coin_metadata = &coin_metadata_unkown; + // No need to continue searching + break; + } + } + + // Find the matching CircleCollider + for (CircleCollider& coin_collider_unkown : coin_colliders) { + if (coin_collider_unkown.game_object_id == coin_sprite.game_object_id) { + coin_collider = &coin_collider_unkown; + // No need to continue searching + break; + } + } + + // If we found both the metadata and collider, activate the coin + if (coin_metadata && coin_collider) { + coin.name = coin_metadata->name; + coin.active = true; + coin_sprite.active = true; + coin_collider->active = true; + break; + } + } + } + } + } + } +} + diff --git a/game/coins/CoinSystem.h b/game/coins/CoinSystem.h new file mode 100644 index 0000000..93cbf98 --- /dev/null +++ b/game/coins/CoinSystem.h @@ -0,0 +1,22 @@ +#pragma once + +#include "CoinData.h" +#include "api/Scene.h" +#include "api/Script.h" +#include "api/Sprite.h" +#include "types.h" +#include +#include + +class CoinSystem { +public: + void create_coins(crepe::Scene & scn); + void add_location(const crepe::vec2& location); + void remove_location(const std::string& name); + void update(float position,crepe::RefVector coin_sprites,crepe::RefVector coin_colliders,crepe::RefVector coin_metadatas); +private: + static constexpr float SPAWN_DISTANCE = 200; + static constexpr float SPAWN_AREA = 50; + static constexpr int MAXIMUM_AMOUNT = 100; + static std::vector coin_locations; +}; diff --git a/game/coins/CoinSystemScript.cpp b/game/coins/CoinSystemScript.cpp new file mode 100644 index 0000000..90405a8 --- /dev/null +++ b/game/coins/CoinSystemScript.cpp @@ -0,0 +1,18 @@ +#include "CoinSystemScript.h" +#include "CoinSystem.h" +#include "api/CircleCollider.h" +#include "api/Metadata.h" +#include "api/Transform.h" + +using namespace crepe; +using namespace std; + +void CoinSystemScript::frame_update(crepe::duration_t dt) +{ + float position = this->get_component().position.x; + RefVector coin_sprites = this->get_components_by_tag("coin"); + RefVector coin_colliders = this->get_components_by_tag("coin"); + RefVector coin_metadatas = this->get_components_by_tag("coin"); + this->coin_system.update(position, coin_sprites, coin_colliders, coin_metadatas); +} + diff --git a/game/coins/CoinSystemScript.h b/game/coins/CoinSystemScript.h new file mode 100644 index 0000000..96744f1 --- /dev/null +++ b/game/coins/CoinSystemScript.h @@ -0,0 +1,13 @@ +#pragma once + +#include "CoinSystem.h" +#include "api/Script.h" + +class CoinSystemScript : public crepe::Script { +public: + CoinSystemScript() {}; + // void init() override; + void frame_update(crepe::duration_t dt) override; +private: + CoinSystem coin_system; +}; diff --git a/game/coins/coinSubScene.cpp b/game/coins/coinSubScene.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/game/coins/coinSubScene.h b/game/coins/coinSubScene.h deleted file mode 100644 index e69de29..0000000 diff --git a/game/mainmenu/MainMenuConfig.h b/game/mainmenu/MainMenuConfig.h index e2ace30..fd7e922 100644 --- a/game/mainmenu/MainMenuConfig.h +++ b/game/mainmenu/MainMenuConfig.h @@ -2,6 +2,7 @@ #include "types.h" #include + struct MainMenuConfig { //generic menu config static constexpr unsigned int STARTING_SORTING_IN_LAYER = 7; diff --git a/game/mainmenu/MainMenuScene.cpp b/game/mainmenu/MainMenuScene.cpp index 8ffea70..472f190 100644 --- a/game/mainmenu/MainMenuScene.cpp +++ b/game/mainmenu/MainMenuScene.cpp @@ -5,6 +5,7 @@ #include "api/BehaviorScript.h" #include "api/Camera.h" #include "../StartSubScene.h" +#include "../coins/CoinSubScene.h" #include "MainMenuConfig.h" #include "api/GameObject.h" #include "api/Rigidbody.h" @@ -48,8 +49,11 @@ void MainMenuScene::load_scene(){ .position = pos, .script = ButtonSubScene::ScriptSelect::SHOP, }); - + CoinSubScene coin; + coin.create(*this); + coin.create(*this); + //Start of map StartSubScene start; start.create(*this, MainMenuConfig::STARTMAP_OFFSET); -- cgit v1.2.3 From fb096afbdf3d24f5b9f31626ddf8581033346f88 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 28 Dec 2024 19:33:06 +0100 Subject: coins with system pooling --- game/GameScene.cpp | 5 +- game/coins/CoinSubScene.cpp | 9 +-- game/coins/CoinSystem.cpp | 71 ------------------- game/coins/CoinSystem.h | 14 +--- game/coins/CoinSystemScript.cpp | 134 ++++++++++++++++++++++++++++++++++-- game/coins/CoinSystemScript.h | 16 ++++- game/mainmenu/ITransitionScript.cpp | 2 +- game/mainmenu/MainMenuScene.cpp | 6 -- 8 files changed, 152 insertions(+), 105 deletions(-) (limited to 'game/coins') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 3d692f8..ad42421 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -58,9 +58,8 @@ void GameScene::load_scene() { CoinSubScene coin; coin.create(*this); - // CoinSystem coin_system; - // coin_system.create_coins(*this); - // coin_system.add_location({900,0}); + CoinSystem coin_system; + coin_system.create_coins(*this); } diff --git a/game/coins/CoinSubScene.cpp b/game/coins/CoinSubScene.cpp index 12ddb7a..97f5877 100644 --- a/game/coins/CoinSubScene.cpp +++ b/game/coins/CoinSubScene.cpp @@ -5,6 +5,7 @@ #include "api/Scene.h" #include "api/AudioSource.h" #include +#include using namespace crepe; using namespace std; @@ -13,10 +14,10 @@ int CoinSubScene::create(Scene & scn){ vec2 size = {20, 20}; static int coin_counter = 0; - std::string unique_name = "coin_" + std::to_string(coin_counter++); + string unique_name = "coin_" + to_string(coin_counter++); + cout << "new coin: "<< unique_name << endl; - - GameObject coin = scn.new_object(unique_name.c_str(),"coin",vec2{0,0},0,1); + GameObject coin = scn.new_object(unique_name.c_str(),"coin",vec2{650,0},0,1); coin.add_component(Rigidbody::Data{}); coin.add_component(size.x / 2).active = false; crepe::OptionalRef coin_sprite = coin.add_component(Asset{"asset/coin/coin1_TVOS.png"}, Sprite::Data{ @@ -24,7 +25,7 @@ int CoinSubScene::create(Scene & scn){ .order_in_layer = 100, .size = size, }); - // coin_sprite->active = false; + coin_sprite->active = false; coin.add_component(coin_sprite, ivec2{32, 32}, uvec2{8, 1}, Animator::Data{ .fps = 15, diff --git a/game/coins/CoinSystem.cpp b/game/coins/CoinSystem.cpp index 4474195..60d558b 100644 --- a/game/coins/CoinSystem.cpp +++ b/game/coins/CoinSystem.cpp @@ -1,81 +1,10 @@ #include "CoinSystem.h" #include "CoinSubScene.h" -#include "api/CircleCollider.h" -#include "api/Metadata.h" -#include "api/Script.h" -#include "api/Sprite.h" -#include "api/Transform.h" -#include "types.h" using namespace crepe; using namespace std; -std::vector CoinSystem::coin_locations; - void CoinSystem::create_coins(crepe::Scene & scn) { CoinSubScene coin; while(coin.create(scn) < this->MAXIMUM_AMOUNT); } - -void CoinSystem::add_location(const crepe::vec2& location){ - coin_locations.push_back(CoinData(location)); -} - -void CoinSystem::remove_location(const std::string& name){ - auto it = std::find_if(coin_locations.begin(), coin_locations.end(), - [&name](const CoinData& data) { - return data.name == name; - }); - - // If a match is found, erase - if (it != coin_locations.end()) { - coin_locations.erase(it); - } -} - - -void CoinSystem::update(float position, RefVector coin_sprites, RefVector coin_colliders, RefVector coin_metadatas) { - for (auto& coin : coin_locations) { - if (!coin.active) { - if (coin.start_location.x > SPAWN_DISTANCE + position && coin.start_location.x < SPAWN_AREA + SPAWN_DISTANCE + position) { - - // Local variables to hold the matching coin data - Metadata* coin_metadata = nullptr; - CircleCollider* coin_collider = nullptr; - - // Iterate over the sprites to find the matching one - for (Sprite& coin_sprite : coin_sprites) { - if (!coin_sprite.active) { - // Find the matching Metadata - for (Metadata& coin_metadata_unkown : coin_metadatas) { - if (coin_metadata_unkown.game_object_id == coin_sprite.game_object_id) { - coin_metadata = &coin_metadata_unkown; - // No need to continue searching - break; - } - } - - // Find the matching CircleCollider - for (CircleCollider& coin_collider_unkown : coin_colliders) { - if (coin_collider_unkown.game_object_id == coin_sprite.game_object_id) { - coin_collider = &coin_collider_unkown; - // No need to continue searching - break; - } - } - - // If we found both the metadata and collider, activate the coin - if (coin_metadata && coin_collider) { - coin.name = coin_metadata->name; - coin.active = true; - coin_sprite.active = true; - coin_collider->active = true; - break; - } - } - } - } - } - } -} - diff --git a/game/coins/CoinSystem.h b/game/coins/CoinSystem.h index 93cbf98..31e72f1 100644 --- a/game/coins/CoinSystem.h +++ b/game/coins/CoinSystem.h @@ -1,22 +1,12 @@ #pragma once -#include "CoinData.h" #include "api/Scene.h" -#include "api/Script.h" -#include "api/Sprite.h" -#include "types.h" -#include -#include + class CoinSystem { public: void create_coins(crepe::Scene & scn); - void add_location(const crepe::vec2& location); - void remove_location(const std::string& name); - void update(float position,crepe::RefVector coin_sprites,crepe::RefVector coin_colliders,crepe::RefVector coin_metadatas); private: - static constexpr float SPAWN_DISTANCE = 200; - static constexpr float SPAWN_AREA = 50; static constexpr int MAXIMUM_AMOUNT = 100; - static std::vector coin_locations; }; + diff --git a/game/coins/CoinSystemScript.cpp b/game/coins/CoinSystemScript.cpp index 90405a8..da1fc65 100644 --- a/game/coins/CoinSystemScript.cpp +++ b/game/coins/CoinSystemScript.cpp @@ -2,17 +2,141 @@ #include "CoinSystem.h" #include "api/CircleCollider.h" #include "api/Metadata.h" +#include "api/Sprite.h" #include "api/Transform.h" using namespace crepe; using namespace std; -void CoinSystemScript::frame_update(crepe::duration_t dt) -{ +std::vector CoinSystemScript::coin_locations; + +void CoinSystemScript::init() { + this->add_location(vec2{1500,-30}); + this->add_location(vec2{1500,0}); + this->add_location(vec2{1500,30}); + this->add_location(vec2{1500,60}); + this->add_location(vec2{1550,-30}); + this->add_location(vec2{1550,0}); + this->add_location(vec2{1550,30}); + this->add_location(vec2{1550,60}); + + this->add_location(vec2{2500,-30}); + this->add_location(vec2{2500,0}); + this->add_location(vec2{2500,30}); + this->add_location(vec2{2500,60}); + this->add_location(vec2{2550,-30}); + this->add_location(vec2{2550,0}); + this->add_location(vec2{2550,30}); + this->add_location(vec2{2550,60}); + this->add_location(vec2{2600,-30}); + this->add_location(vec2{2600,0}); + this->add_location(vec2{2600,30}); + this->add_location(vec2{2600,60}); + this->add_location(vec2{2650,-30}); + this->add_location(vec2{2650,0}); + this->add_location(vec2{2650,30}); + this->add_location(vec2{2650,60}); + this->add_location(vec2{2700,-30}); + this->add_location(vec2{2700,0}); + this->add_location(vec2{2700,30}); + this->add_location(vec2{2700,60}); + this->add_location(vec2{2750,-30}); + this->add_location(vec2{2750,0}); + this->add_location(vec2{2750,30}); + this->add_location(vec2{2750,60}); + this->add_location(vec2{2800,-30}); + this->add_location(vec2{2800,0}); + this->add_location(vec2{2800,30}); + this->add_location(vec2{2800,60}); + this->add_location(vec2{2850,-30}); + this->add_location(vec2{2850,0}); + this->add_location(vec2{2850,30}); + this->add_location(vec2{2850,60}); +} + +void CoinSystemScript::add_location(const crepe::vec2& location){ + coin_locations.push_back(CoinData(location)); +} + +void CoinSystemScript::despawn_coins() { + // Get the current x-position of the CoinSystem's Transform component float position = this->get_component().position.x; + + // Retrieve all active coin sprites tagged as "coin" RefVector coin_sprites = this->get_components_by_tag("coin"); - RefVector coin_colliders = this->get_components_by_tag("coin"); - RefVector coin_metadatas = this->get_components_by_tag("coin"); - this->coin_system.update(position, coin_sprites, coin_colliders, coin_metadatas); + + for (Sprite& coin_sprite : coin_sprites) { + if (!coin_sprite.active) continue; // Skip inactive sprites + + // Retrieve the corresponding Transform, Metadata, and CircleCollider components + Transform& coin_transform = this->get_components_by_id(coin_sprite.game_object_id).front().get(); + Metadata& coin_metadata = this->get_components_by_id(coin_sprite.game_object_id).front().get(); + CircleCollider& coin_collider = this->get_components_by_id(coin_sprite.game_object_id).front().get(); + + // Check if the coin is out of bounds based on DESPAWN_DISTANCE + if (coin_transform.position.x < position - this->DESPAWN_DISTANCE) { + // Find the coin in the coin_locations vector using its name + auto it = std::find_if( + coin_locations.begin(), + coin_locations.end(), + [&coin_metadata](const CoinData& data) { + return data.name == coin_metadata.name; + } + ); + + // If a match is found, erase it from coin_locations + if (it != coin_locations.end()) { + coin_locations.erase(it); + coin_sprite.active = false; + coin_collider.active = false; + } + } + } } +void CoinSystemScript::spawn_coins(){ + // Get the current x-position of the CoinSystem's Transform component + float position = this->get_component().position.x; + + // Iterate through the list of coin locations + for (auto& coin : coin_locations) { + // Skip this coin if it is already active + if (coin.active)continue; + // Skip this coin if it is not within the defined spawn area + if (coin.start_location.x < this->SPAWN_DISTANCE + position || coin.start_location.x > this->SPAWN_AREA + this->SPAWN_DISTANCE + position) continue; + + // Retrieve all sprites tagged as "coin" + RefVector coin_sprites = this->get_components_by_tag("coin"); + + // Check for an available (inactive) coin sprite + for (Sprite& coin_sprite : coin_sprites) { + // Skip this sprite if it is already active + if (coin_sprite.active) continue; + + // Found an available (inactive) coin sprite + // Retrieve its associated components + Transform & coin_transform = this->get_components_by_id(coin_sprite.game_object_id).front().get(); + Metadata & coin_metadata = this->get_components_by_id(coin_sprite.game_object_id).front().get(); + CircleCollider & coin_collider = this->get_components_by_id(coin_sprite.game_object_id).front().get(); + + // Assign data and set active + coin.name = coin_metadata.name; + coin.active = true; + coin_sprite.active = true; + coin_collider.active = true; + coin_transform.position = coin.start_location; + + // Break out of the inner loop since we've assigned this coin to an available sprite + break; + } + } +} + +void CoinSystemScript::frame_update(crepe::duration_t dt) +{ + this->despawn_coins(); + this->spawn_coins(); +} + + + diff --git a/game/coins/CoinSystemScript.h b/game/coins/CoinSystemScript.h index 96744f1..6cf613a 100644 --- a/game/coins/CoinSystemScript.h +++ b/game/coins/CoinSystemScript.h @@ -1,13 +1,23 @@ #pragma once -#include "CoinSystem.h" +#include "CoinData.h" +#include "api/CircleCollider.h" #include "api/Script.h" +#include "api/Sprite.h" +#include "api/Transform.h" class CoinSystemScript : public crepe::Script { public: CoinSystemScript() {}; - // void init() override; + void init() override; void frame_update(crepe::duration_t dt) override; private: - CoinSystem coin_system; + void add_location(const crepe::vec2& location); + void despawn_coins(); + void spawn_coins(); +private: + static constexpr float SPAWN_DISTANCE = 400; + static constexpr float DESPAWN_DISTANCE = 400; + static constexpr float SPAWN_AREA = 50; + static std::vector coin_locations; }; diff --git a/game/mainmenu/ITransitionScript.cpp b/game/mainmenu/ITransitionScript.cpp index a7f5a0d..433f9f7 100644 --- a/game/mainmenu/ITransitionScript.cpp +++ b/game/mainmenu/ITransitionScript.cpp @@ -11,7 +11,7 @@ using namespace std; void ITransitionScript::frame_update(crepe::duration_t delta_time){ if(this->transition) { - cout << "transition:" << velocity << std::endl; + // cout << "transition:" << velocity << std::endl; Transform & cam = this->get_components_by_name(MainMenuConfig::CAMERA_NAME).front(); if(velocity < MainMenuConfig::VELOCITY_MAX && cam.position.x < MainMenuConfig::SLOW_DOWN) velocity += MainMenuConfig::VELOCITY_STEP * delta_time.count(); else if(velocity > 20) velocity -= MainMenuConfig::VELOCITY_STEP * delta_time.count(); diff --git a/game/mainmenu/MainMenuScene.cpp b/game/mainmenu/MainMenuScene.cpp index 472f190..80c945b 100644 --- a/game/mainmenu/MainMenuScene.cpp +++ b/game/mainmenu/MainMenuScene.cpp @@ -5,10 +5,8 @@ #include "api/BehaviorScript.h" #include "api/Camera.h" #include "../StartSubScene.h" -#include "../coins/CoinSubScene.h" #include "MainMenuConfig.h" #include "api/GameObject.h" -#include "api/Rigidbody.h" #include "api/Sprite.h" using namespace crepe; @@ -49,10 +47,6 @@ void MainMenuScene::load_scene(){ .position = pos, .script = ButtonSubScene::ScriptSelect::SHOP, }); - - CoinSubScene coin; - coin.create(*this); - coin.create(*this); //Start of map StartSubScene start; -- cgit v1.2.3 From 9afadbd2b2d2c5ec730154a426e83722e4c3da32 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 28 Dec 2024 19:34:07 +0100 Subject: renamed file --- game/GameScene.cpp | 4 ++-- game/coins/CoinPool.cpp | 10 ++++++++++ game/coins/CoinPool.h | 12 ++++++++++++ game/coins/CoinSystem.cpp | 10 ---------- game/coins/CoinSystem.h | 12 ------------ 5 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 game/coins/CoinPool.cpp create mode 100644 game/coins/CoinPool.h delete mode 100644 game/coins/CoinSystem.cpp delete mode 100644 game/coins/CoinSystem.h (limited to 'game/coins') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index ad42421..bb64d08 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -4,7 +4,7 @@ #include "PlayerSubScene.h" #include "StartGameScript.h" #include "coins/CoinSubScene.h" -#include "coins/CoinSystem.h" +#include "coins/CoinPool.h" #include "coins/CoinSystemScript.h" #include @@ -58,7 +58,7 @@ void GameScene::load_scene() { CoinSubScene coin; coin.create(*this); - CoinSystem coin_system; + CoinPool coin_system; coin_system.create_coins(*this); } diff --git a/game/coins/CoinPool.cpp b/game/coins/CoinPool.cpp new file mode 100644 index 0000000..5720c2f --- /dev/null +++ b/game/coins/CoinPool.cpp @@ -0,0 +1,10 @@ +#include "CoinPool.h" +#include "CoinSubScene.h" + +using namespace crepe; +using namespace std; + +void CoinPool::create_coins(crepe::Scene & scn) { + CoinSubScene coin; + while(coin.create(scn) < this->MAXIMUM_AMOUNT); +} diff --git a/game/coins/CoinPool.h b/game/coins/CoinPool.h new file mode 100644 index 0000000..83058f7 --- /dev/null +++ b/game/coins/CoinPool.h @@ -0,0 +1,12 @@ +#pragma once + +#include "api/Scene.h" + + +class CoinPool { +public: + void create_coins(crepe::Scene & scn); +private: + static constexpr int MAXIMUM_AMOUNT = 100; +}; + diff --git a/game/coins/CoinSystem.cpp b/game/coins/CoinSystem.cpp deleted file mode 100644 index 60d558b..0000000 --- a/game/coins/CoinSystem.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "CoinSystem.h" -#include "CoinSubScene.h" - -using namespace crepe; -using namespace std; - -void CoinSystem::create_coins(crepe::Scene & scn) { - CoinSubScene coin; - while(coin.create(scn) < this->MAXIMUM_AMOUNT); -} diff --git a/game/coins/CoinSystem.h b/game/coins/CoinSystem.h deleted file mode 100644 index 31e72f1..0000000 --- a/game/coins/CoinSystem.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "api/Scene.h" - - -class CoinSystem { -public: - void create_coins(crepe::Scene & scn); -private: - static constexpr int MAXIMUM_AMOUNT = 100; -}; - -- cgit v1.2.3 From e007e8f467349eeddaf503e31897d4135ba6fed4 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 28 Dec 2024 20:46:33 +0100 Subject: added presets --- game/CMakeLists.txt | 4 +- game/coins/CoinSystemScript.cpp | 124 +++++++++++++++++++++++++--------------- game/coins/CoinSystemScript.h | 22 +++++++ 3 files changed, 101 insertions(+), 49 deletions(-) (limited to 'game/coins') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 6f3beb5..d3608d1 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -32,7 +32,7 @@ set(SOURCES mainmenu/BannerSubScene.cpp mainmenu/ButtonSetShopScript.cpp coins/CoinSubScene.cpp - coins/CoinSystem.cpp + coins/CoinPool.cpp coins/CoinSystemScript.cpp ) @@ -60,7 +60,7 @@ set(HEADERS mainmenu/ButtonSetShopScript.h coins/CoinData.h coins/CoinSubScene.h - coins/CoinSystem.h + coins/CoinPool.h coins/CoinSystemScript.h ) diff --git a/game/coins/CoinSystemScript.cpp b/game/coins/CoinSystemScript.cpp index da1fc65..6cab12d 100644 --- a/game/coins/CoinSystemScript.cpp +++ b/game/coins/CoinSystemScript.cpp @@ -1,5 +1,5 @@ #include "CoinSystemScript.h" -#include "CoinSystem.h" +#include "CoinPool.h" #include "api/CircleCollider.h" #include "api/Metadata.h" #include "api/Sprite.h" @@ -11,53 +11,87 @@ using namespace std; std::vector CoinSystemScript::coin_locations; void CoinSystemScript::init() { - this->add_location(vec2{1500,-30}); - this->add_location(vec2{1500,0}); - this->add_location(vec2{1500,30}); - this->add_location(vec2{1500,60}); - this->add_location(vec2{1550,-30}); - this->add_location(vec2{1550,0}); - this->add_location(vec2{1550,30}); - this->add_location(vec2{1550,60}); - - this->add_location(vec2{2500,-30}); - this->add_location(vec2{2500,0}); - this->add_location(vec2{2500,30}); - this->add_location(vec2{2500,60}); - this->add_location(vec2{2550,-30}); - this->add_location(vec2{2550,0}); - this->add_location(vec2{2550,30}); - this->add_location(vec2{2550,60}); - this->add_location(vec2{2600,-30}); - this->add_location(vec2{2600,0}); - this->add_location(vec2{2600,30}); - this->add_location(vec2{2600,60}); - this->add_location(vec2{2650,-30}); - this->add_location(vec2{2650,0}); - this->add_location(vec2{2650,30}); - this->add_location(vec2{2650,60}); - this->add_location(vec2{2700,-30}); - this->add_location(vec2{2700,0}); - this->add_location(vec2{2700,30}); - this->add_location(vec2{2700,60}); - this->add_location(vec2{2750,-30}); - this->add_location(vec2{2750,0}); - this->add_location(vec2{2750,30}); - this->add_location(vec2{2750,60}); - this->add_location(vec2{2800,-30}); - this->add_location(vec2{2800,0}); - this->add_location(vec2{2800,30}); - this->add_location(vec2{2800,60}); - this->add_location(vec2{2850,-30}); - this->add_location(vec2{2850,0}); - this->add_location(vec2{2850,30}); - this->add_location(vec2{2850,60}); + float position = 1200; + position += this->preset_1({position,0}); + position += 100; + position += this->preset_2({position,0}); } + + + void CoinSystemScript::add_location(const crepe::vec2& location){ coin_locations.push_back(CoinData(location)); } + +float CoinSystemScript::preset_1(const vec2 & begin_position){ + vec2 top = {begin_position.x, begin_position.y - (this->ROW_OFFSET_1)}; + vec2 bottom = {begin_position.x, begin_position.y + (this->ROW_OFFSET_1)}; + + // Add locations for the top row + for (int i = 0; i < COLUM_AMOUNT_1; ++i) { + add_location(top); + top.x += this->COLUM_OFFSET_1; + } + + // Add locations for the bottom row + bottom.x +=this->COLUM_OFFSET_1 * COLUM_AMOUNT_1; + for (int i = 0; i < COLUM_AMOUNT_1; ++i) { + add_location(bottom); + bottom.x += this->COLUM_OFFSET_1; + } + + // Add locations for the next set of the top row + top.x += this->COLUM_OFFSET_1 * COLUM_AMOUNT_1; + for (int i = 0; i < COLUM_AMOUNT_1; ++i) { + add_location(top); + top.x += this->COLUM_OFFSET_1; + } + + // Add locations for the next set of the bottom row + bottom.x +=this->COLUM_OFFSET_1 * COLUM_AMOUNT_1; + for (int i = 0; i < COLUM_AMOUNT_1; ++i) { + add_location(bottom); + bottom.x += this->COLUM_OFFSET_1; + } + + return bottom.x-begin_position.x; +} + +float CoinSystemScript::preset_2(const vec2 & begin_position){ + vec2 top = {begin_position.x+this->COLUM_OFFSET_2, begin_position.y - this->ROW_OFFSET_2}; + vec2 middle = begin_position; + vec2 bottom = {begin_position.x+this->COLUM_OFFSET_2, begin_position.y + this->ROW_OFFSET_2}; + + // Add locations for the next set of the bottom row + for (int i = 0; i < COLUM_AMOUNT_2-2; ++i) { + add_location(bottom); + bottom.x += this->COLUM_OFFSET_2; + } + + // Add locations for the next set of the middle row + for (int i = 0; i < COLUM_AMOUNT_2; ++i) { + add_location(middle); + middle.x += this->COLUM_OFFSET_2; + } + + // Add locations for the next set of the top row + for (int i = 0; i < COLUM_AMOUNT_2-2; ++i) { + add_location(top); + top.x += this->COLUM_OFFSET_2; + } + + return middle.x-begin_position.x; +} + + +void CoinSystemScript::frame_update(crepe::duration_t dt) +{ + this->despawn_coins(); + this->spawn_coins(); +} + void CoinSystemScript::despawn_coins() { // Get the current x-position of the CoinSystem's Transform component float position = this->get_component().position.x; @@ -132,11 +166,7 @@ void CoinSystemScript::spawn_coins(){ } } -void CoinSystemScript::frame_update(crepe::duration_t dt) -{ - this->despawn_coins(); - this->spawn_coins(); -} + diff --git a/game/coins/CoinSystemScript.h b/game/coins/CoinSystemScript.h index 6cf613a..f17c160 100644 --- a/game/coins/CoinSystemScript.h +++ b/game/coins/CoinSystemScript.h @@ -15,9 +15,31 @@ private: void add_location(const crepe::vec2& location); void despawn_coins(); void spawn_coins(); + float preset_1(const crepe::vec2 & begin_position); + float preset_2(const crepe::vec2 & begin_position); private: static constexpr float SPAWN_DISTANCE = 400; static constexpr float DESPAWN_DISTANCE = 400; static constexpr float SPAWN_AREA = 50; static std::vector coin_locations; +private: +// preset one settings +// ***** ***** +// +// +// +// ***** ***** + static constexpr float ROW_OFFSET_1 = 100; + static constexpr float COLUM_OFFSET_1 = 25; + static constexpr int COLUM_AMOUNT_1 = 5; +private: +// preset two settings +// ***** ***** +// +// +// +// ***** ***** + static constexpr float ROW_OFFSET_2 = 25; + static constexpr float COLUM_OFFSET_2 = 25; + static constexpr int COLUM_AMOUNT_2 = 10; }; -- cgit v1.2.3 From b82c302e2eb5044ae966230a50aae9bdb698cf8c Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sun, 29 Dec 2024 15:34:23 +0100 Subject: added more presets --- game/coins/CoinSubScene.cpp | 4 +- game/coins/CoinSystemScript.cpp | 118 +++++++++++++++++++++++++++++++++++++++- game/coins/CoinSystemScript.h | 41 ++++++++++++-- 3 files changed, 154 insertions(+), 9 deletions(-) (limited to 'game/coins') diff --git a/game/coins/CoinSubScene.cpp b/game/coins/CoinSubScene.cpp index 97f5877..0a1cd68 100644 --- a/game/coins/CoinSubScene.cpp +++ b/game/coins/CoinSubScene.cpp @@ -18,7 +18,9 @@ int CoinSubScene::create(Scene & scn){ cout << "new coin: "<< unique_name << endl; GameObject coin = scn.new_object(unique_name.c_str(),"coin",vec2{650,0},0,1); - coin.add_component(Rigidbody::Data{}); + coin.add_component(Rigidbody::Data{ + .body_type = Rigidbody::BodyType::KINEMATIC + }); coin.add_component(size.x / 2).active = false; crepe::OptionalRef coin_sprite = coin.add_component(Asset{"asset/coin/coin1_TVOS.png"}, Sprite::Data{ .sorting_in_layer = 100, diff --git a/game/coins/CoinSystemScript.cpp b/game/coins/CoinSystemScript.cpp index 6cab12d..b28dab1 100644 --- a/game/coins/CoinSystemScript.cpp +++ b/game/coins/CoinSystemScript.cpp @@ -12,9 +12,11 @@ std::vector CoinSystemScript::coin_locations; void CoinSystemScript::init() { float position = 1200; - position += this->preset_1({position,0}); - position += 100; - position += this->preset_2({position,0}); + // position += this->preset_1({position,0}); + // position += 100; + // position += this->preset_2({position,0}); + // position += 100; + position += this->preset_5({position,0}); } @@ -85,6 +87,116 @@ float CoinSystemScript::preset_2(const vec2 & begin_position){ return middle.x-begin_position.x; } +float CoinSystemScript::preset_3(const vec2 & begin_position){ + vec2 location = {begin_position.x, begin_position.y - (this->ROW_OFFSET_3)}; + + + // Add locations for the top row + for (int i = 0; i < COLUM_AMOUNT_3; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_3; + } + + // Add locations for the bottom row + location.y +=this->ROW_OFFSET_3; + location.x += this->COLUM_OFFSET_3; + for (int i = 0; i < COLUM_AMOUNT_3; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_3; + } + + // Add locations for the next set of the top row + location.y +=this->ROW_OFFSET_3; + location.x += this->COLUM_OFFSET_3; + for (int i = 0; i < COLUM_AMOUNT_3; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_3; + } + + return location.x-begin_position.x; +} + +float CoinSystemScript::preset_4(const vec2 & begin_position){ + vec2 location = {begin_position.x, begin_position.y + (this->ROW_OFFSET_4)}; + + + // Add locations for the top row + for (int i = 0; i < COLUM_AMOUNT_4; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_4; + } + + // Add locations for the bottom row + location.y -=this->ROW_OFFSET_4; + location.x += this->COLUM_OFFSET_4; + for (int i = 0; i < COLUM_AMOUNT_4; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_4; + } + + // Add locations for the next set of the top row + location.y -=this->ROW_OFFSET_4; + location.x += this->COLUM_OFFSET_4; + for (int i = 0; i < COLUM_AMOUNT_4; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_4; + } + + return location.x-begin_position.x; +} + +float CoinSystemScript::preset_5(const vec2 & begin_position){ + vec2 location = {begin_position.x, begin_position.y-ROW_OFFSET_5/2}; + float locationx = 0; + for (int i = 0; i < 2; ++i) { + // Add locations for the top row + location.y -= ROW_OFFSET_5*6; + for (int i = 0; i < COLUM_AMOUNT_5; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_5; + } + location.x = begin_position.x; + location.y += ROW_OFFSET_5; + for (int i = 0; i < COLUM_AMOUNT_5; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_5; + } + + + // Add locations for the middle row + location.x = begin_position.x + COLUM_OFFSET_5*COLUM_AMOUNT_5*2; + location.y += ROW_OFFSET_5*6; + for (int i = 0; i < COLUM_AMOUNT_5; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_5; + } + location.x = begin_position.x + COLUM_OFFSET_5*COLUM_AMOUNT_5*2; + location.y += ROW_OFFSET_5; + for (int i = 0; i < COLUM_AMOUNT_5; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_5; + } + locationx = location.x; + + // Add locations for the bottom row + location.x = begin_position.x; + location.y += ROW_OFFSET_5*6; + for (int i = 0; i < COLUM_AMOUNT_5; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_5; + } + location.x = begin_position.x; + location.y += ROW_OFFSET_5; + for (int i = 0; i < COLUM_AMOUNT_5; ++i) { + add_location(location); + location.x += this->COLUM_OFFSET_5; + } + location.x = locationx + COLUM_OFFSET_5; + } + return location.x-begin_position.x; +} + + void CoinSystemScript::frame_update(crepe::duration_t dt) { diff --git a/game/coins/CoinSystemScript.h b/game/coins/CoinSystemScript.h index f17c160..8dcd793 100644 --- a/game/coins/CoinSystemScript.h +++ b/game/coins/CoinSystemScript.h @@ -17,6 +17,9 @@ private: void spawn_coins(); float preset_1(const crepe::vec2 & begin_position); float preset_2(const crepe::vec2 & begin_position); + float preset_3(const crepe::vec2 & begin_position); + float preset_4(const crepe::vec2 & begin_position); + float preset_5(const crepe::vec2 & begin_position); private: static constexpr float SPAWN_DISTANCE = 400; static constexpr float DESPAWN_DISTANCE = 400; @@ -34,12 +37,40 @@ private: static constexpr int COLUM_AMOUNT_1 = 5; private: // preset two settings -// ***** ***** -// -// -// -// ***** ***** +// +// ******** +// ********** +// ******** +// static constexpr float ROW_OFFSET_2 = 25; static constexpr float COLUM_OFFSET_2 = 25; static constexpr int COLUM_AMOUNT_2 = 10; +// preset three settings +// *** +// +// *** +// +// *** + static constexpr float ROW_OFFSET_3 = 100; + static constexpr float COLUM_OFFSET_3 = 25; + static constexpr int COLUM_AMOUNT_3 = 3; +// preset four settings +// *** +// +// *** +// +// *** + static constexpr float ROW_OFFSET_4 = 100; + static constexpr float COLUM_OFFSET_4 = 25; + static constexpr int COLUM_AMOUNT_4 = 3; +// preset five settings +// ** ** +// ** ** +// ** +// ** +// ** ** +// ** ** + static constexpr float ROW_OFFSET_5 = 25; + static constexpr float COLUM_OFFSET_5 = 25; + static constexpr int COLUM_AMOUNT_5 = 2; }; -- cgit v1.2.3 From f18635b6c73f753bd38343684134430a5ce9b22c Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 3 Jan 2025 16:12:14 +0100 Subject: added coins random and menu banner update --- game/CMakeLists.txt | 2 ++ game/coins/CoinScript.cpp | 19 ++++++++++ game/coins/CoinScript.h | 10 ++++++ game/coins/CoinSubScene.cpp | 6 +++- game/coins/CoinSystemScript.cpp | 78 ++++++++++++---------------------------- game/coins/CoinSystemScript.h | 28 +++++++++++---- game/mainmenu/BannerSubScene.cpp | 6 +++- game/mainmenu/BannerSubScene.h | 9 ++++- game/mainmenu/ShopMenuScene.cpp | 6 +++- 9 files changed, 98 insertions(+), 66 deletions(-) create mode 100644 game/coins/CoinScript.cpp create mode 100644 game/coins/CoinScript.h (limited to 'game/coins') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index c27dcc1..fbbe901 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -35,6 +35,7 @@ set(SOURCES coins/CoinSubScene.cpp coins/CoinPool.cpp coins/CoinSystemScript.cpp + coins/CoinScript.cpp ) set(HEADERS @@ -64,6 +65,7 @@ set(HEADERS coins/CoinSubScene.h coins/CoinPool.h coins/CoinSystemScript.h + coins/CoinScript.h ) add_executable(main ${SOURCES} ${HEADERS}) diff --git a/game/coins/CoinScript.cpp b/game/coins/CoinScript.cpp new file mode 100644 index 0000000..f067f04 --- /dev/null +++ b/game/coins/CoinScript.cpp @@ -0,0 +1,19 @@ +#include "CoinScript.h" +#include "api/CircleCollider.h" +#include "api/Sprite.h" + +using namespace crepe; +using namespace std; + +bool CoinScript::on_collision(const CollisionEvent & collisionData){ + if(collisionData.info.other.metadata.name != PLAYER_NAME) return true; + //collide with player + this->get_component().active = false; + this->get_component().active = false; + return true; +} + +void CoinScript::init(){ + subscribe([this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); }); +} + diff --git a/game/coins/CoinScript.h b/game/coins/CoinScript.h new file mode 100644 index 0000000..6c52733 --- /dev/null +++ b/game/coins/CoinScript.h @@ -0,0 +1,10 @@ +#pragma once + +#include "api/Script.h" + +class CoinScript : public crepe::Script { +public: + void init() override; + bool on_collision(const crepe::CollisionEvent & collisionData); + static constexpr const char* PLAYER_NAME = "player"; +}; diff --git a/game/coins/CoinSubScene.cpp b/game/coins/CoinSubScene.cpp index 0a1cd68..d30f5a4 100644 --- a/game/coins/CoinSubScene.cpp +++ b/game/coins/CoinSubScene.cpp @@ -1,11 +1,13 @@ #include "CoinSubScene.h" #include "api/Animator.h" +#include "api/BehaviorScript.h" #include "api/CircleCollider.h" #include "api/Rigidbody.h" #include "api/Scene.h" #include "api/AudioSource.h" #include #include +#include "CoinScript.h" using namespace crepe; using namespace std; @@ -19,7 +21,8 @@ int CoinSubScene::create(Scene & scn){ GameObject coin = scn.new_object(unique_name.c_str(),"coin",vec2{650,0},0,1); coin.add_component(Rigidbody::Data{ - .body_type = Rigidbody::BodyType::KINEMATIC + .body_type = Rigidbody::BodyType::KINEMATIC, + .kinematic_collision = false, }); coin.add_component(size.x / 2).active = false; crepe::OptionalRef coin_sprite = coin.add_component(Asset{"asset/coin/coin1_TVOS.png"}, Sprite::Data{ @@ -34,5 +37,6 @@ int CoinSubScene::create(Scene & scn){ .looping = true, }); coin.add_component(Asset{"asset/sfx/coin_pickup_1.ogg"}); + coin.add_component().set_script(); return coin_counter; } diff --git a/game/coins/CoinSystemScript.cpp b/game/coins/CoinSystemScript.cpp index b28dab1..41b1d3d 100644 --- a/game/coins/CoinSystemScript.cpp +++ b/game/coins/CoinSystemScript.cpp @@ -4,6 +4,8 @@ #include "api/Metadata.h" #include "api/Sprite.h" #include "api/Transform.h" +#include +#include "iostream" using namespace crepe; using namespace std; @@ -11,22 +13,13 @@ using namespace std; std::vector CoinSystemScript::coin_locations; void CoinSystemScript::init() { - float position = 1200; - // position += this->preset_1({position,0}); - // position += 100; - // position += this->preset_2({position,0}); - // position += 100; - position += this->preset_5({position,0}); + engine.seed(rd()); } - - - void CoinSystemScript::add_location(const crepe::vec2& location){ coin_locations.push_back(CoinData(location)); } - float CoinSystemScript::preset_1(const vec2 & begin_position){ vec2 top = {begin_position.x, begin_position.y - (this->ROW_OFFSET_1)}; vec2 bottom = {begin_position.x, begin_position.y + (this->ROW_OFFSET_1)}; @@ -147,51 +140,9 @@ float CoinSystemScript::preset_4(const vec2 & begin_position){ float CoinSystemScript::preset_5(const vec2 & begin_position){ vec2 location = {begin_position.x, begin_position.y-ROW_OFFSET_5/2}; - float locationx = 0; - for (int i = 0; i < 2; ++i) { - // Add locations for the top row - location.y -= ROW_OFFSET_5*6; - for (int i = 0; i < COLUM_AMOUNT_5; ++i) { - add_location(location); - location.x += this->COLUM_OFFSET_5; - } - location.x = begin_position.x; - location.y += ROW_OFFSET_5; - for (int i = 0; i < COLUM_AMOUNT_5; ++i) { - add_location(location); - location.x += this->COLUM_OFFSET_5; - } - - - // Add locations for the middle row - location.x = begin_position.x + COLUM_OFFSET_5*COLUM_AMOUNT_5*2; - location.y += ROW_OFFSET_5*6; - for (int i = 0; i < COLUM_AMOUNT_5; ++i) { - add_location(location); - location.x += this->COLUM_OFFSET_5; - } - location.x = begin_position.x + COLUM_OFFSET_5*COLUM_AMOUNT_5*2; - location.y += ROW_OFFSET_5; - for (int i = 0; i < COLUM_AMOUNT_5; ++i) { - add_location(location); - location.x += this->COLUM_OFFSET_5; - } - locationx = location.x; - - // Add locations for the bottom row - location.x = begin_position.x; - location.y += ROW_OFFSET_5*6; - for (int i = 0; i < COLUM_AMOUNT_5; ++i) { - add_location(location); - location.x += this->COLUM_OFFSET_5; - } - location.x = begin_position.x; - location.y += ROW_OFFSET_5; - for (int i = 0; i < COLUM_AMOUNT_5; ++i) { - add_location(location); - location.x += this->COLUM_OFFSET_5; - } - location.x = locationx + COLUM_OFFSET_5; + for (int i = 0; i < COLUM_AMOUNT_5; ++i){ + add_location(location); + location.x += this->COLUM_OFFSET_5; } return location.x-begin_position.x; } @@ -201,6 +152,7 @@ float CoinSystemScript::preset_5(const vec2 & begin_position){ void CoinSystemScript::frame_update(crepe::duration_t dt) { this->despawn_coins(); + this->generate_locations(); this->spawn_coins(); } @@ -278,7 +230,23 @@ void CoinSystemScript::spawn_coins(){ } } +void CoinSystemScript::generate_locations(){ + float position = this->get_component().position.x; + if(position + SPAWN_DISTANCE + SYSTEM_POSITION_OFFSET < this->system_position) return; + std::discrete_distribution dist(weights.begin(), weights.end()); + int selected_index = dist(engine); + + std::uniform_real_distribution space_dist(SPAWN_SPACING_MIN, SPAWN_SPACING_MAX); + float spacing = space_dist(engine); + + + cout << "selected " << selected_index << std::endl; + cout << "spacing " << spacing << std::endl; + // Call the corresponding function and return the new x position + this->system_position += functions[selected_index]({this->system_position,0}); + this->system_position += spacing; +} diff --git a/game/coins/CoinSystemScript.h b/game/coins/CoinSystemScript.h index 8dcd793..51739d6 100644 --- a/game/coins/CoinSystemScript.h +++ b/game/coins/CoinSystemScript.h @@ -5,6 +5,7 @@ #include "api/Script.h" #include "api/Sprite.h" #include "api/Transform.h" +#include class CoinSystemScript : public crepe::Script { public: @@ -15,12 +16,28 @@ private: void add_location(const crepe::vec2& location); void despawn_coins(); void spawn_coins(); + void generate_locations(); float preset_1(const crepe::vec2 & begin_position); float preset_2(const crepe::vec2 & begin_position); float preset_3(const crepe::vec2 & begin_position); float preset_4(const crepe::vec2 & begin_position); float preset_5(const crepe::vec2 & begin_position); private: + std::vector> functions = { + [this](const crepe::vec2& pos) { return preset_1(pos); }, + [this](const crepe::vec2& pos) { return preset_2(pos); }, + [this](const crepe::vec2& pos) { return preset_3(pos); }, + [this](const crepe::vec2& pos) { return preset_4(pos); }, + [this](const crepe::vec2& pos) { return preset_5(pos); } + }; + std::vector weights = {20, 20,20,20, 20}; + std::random_device rd; + std::default_random_engine engine; + float system_position = 1200; + static constexpr float SYSTEM_POSITION_OFFSET = 200; +private: + static constexpr float SPAWN_SPACING_MIN = 400; + static constexpr float SPAWN_SPACING_MAX = 1000; static constexpr float SPAWN_DISTANCE = 400; static constexpr float DESPAWN_DISTANCE = 400; static constexpr float SPAWN_AREA = 50; @@ -64,13 +81,10 @@ private: static constexpr float COLUM_OFFSET_4 = 25; static constexpr int COLUM_AMOUNT_4 = 3; // preset five settings -// ** ** -// ** ** -// ** -// ** -// ** ** -// ** ** +// +// *** +// static constexpr float ROW_OFFSET_5 = 25; static constexpr float COLUM_OFFSET_5 = 25; - static constexpr int COLUM_AMOUNT_5 = 2; + static constexpr int COLUM_AMOUNT_5 = 3; }; diff --git a/game/mainmenu/BannerSubScene.cpp b/game/mainmenu/BannerSubScene.cpp index 0f4f075..8de5711 100644 --- a/game/mainmenu/BannerSubScene.cpp +++ b/game/mainmenu/BannerSubScene.cpp @@ -9,7 +9,7 @@ using namespace crepe; using namespace std; -void BannerSubScene::create(Scene & scn){ +void BannerSubScene::create(Scene & scn,const Data & data){ GameObject menu_banner = scn.new_object("menu_banner","", {0,-414}); menu_banner.add_component( Asset("asset/ui/settings_container/top_middle_setting.png"), @@ -31,5 +31,9 @@ void BannerSubScene::create(Scene & scn){ .size = {1100,7}, .position_offset {0,113}, }); + crepe::vec2 size = {data.banner_title_width,(data.banner_title_width/data.banner_title.size())*2}; + menu_banner.add_component(size,data.banner_title_offset + MainMenuConfig::FONTOFFSET, MainMenuConfig::FONT, Text::Data{ + .text_color = Color::WHITE, + }, data.banner_title); } diff --git a/game/mainmenu/BannerSubScene.h b/game/mainmenu/BannerSubScene.h index 06cd1a4..341943d 100644 --- a/game/mainmenu/BannerSubScene.h +++ b/game/mainmenu/BannerSubScene.h @@ -1,5 +1,6 @@ #pragma once +#include "types.h" #include namespace crepe { @@ -8,5 +9,11 @@ class Scene; class BannerSubScene { public: - void create(crepe::Scene & scn); +struct Data{ + const std::string & banner_title = "NODATA"; + const float banner_title_width = 100; + const crepe::vec2 & banner_title_offset = {0,0}; + }; +public: + void create(crepe::Scene & scn,const Data & data); }; diff --git a/game/mainmenu/ShopMenuScene.cpp b/game/mainmenu/ShopMenuScene.cpp index be2039f..d6ad870 100644 --- a/game/mainmenu/ShopMenuScene.cpp +++ b/game/mainmenu/ShopMenuScene.cpp @@ -16,7 +16,11 @@ void ShopMenuScene::load_scene(){ .bg_color = Color::RED, }); BannerSubScene banner; - banner.create(*this); + banner.create(*this,{ + .banner_title = "SHOP", + .banner_title_width = 200, + .banner_title_offset = {0,65}, + }); GameObject menu_background = this->new_object("menu_background"); menu_background.add_component( Asset("asset/ui/background.png"), -- cgit v1.2.3 From d65e0ff31a75230fd1c18eaeab9cb25ab2b9c82a Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 3 Jan 2025 16:36:43 +0100 Subject: save amount of coins --- game/coins/CoinScript.cpp | 4 ++++ game/coins/CoinScript.h | 1 + 2 files changed, 5 insertions(+) (limited to 'game/coins') diff --git a/game/coins/CoinScript.cpp b/game/coins/CoinScript.cpp index f067f04..862e7f9 100644 --- a/game/coins/CoinScript.cpp +++ b/game/coins/CoinScript.cpp @@ -1,6 +1,7 @@ #include "CoinScript.h" #include "api/CircleCollider.h" #include "api/Sprite.h" +#include "manager/SaveManager.h" using namespace crepe; using namespace std; @@ -10,6 +11,9 @@ bool CoinScript::on_collision(const CollisionEvent & collisionData){ //collide with player this->get_component().active = false; this->get_component().active = false; + SaveManager & savemgr = this->get_save_manager(); + int amount = savemgr.get(COIN_GAME_AMOUNT,0).get() + 1; + savemgr.set(COIN_GAME_AMOUNT, amount); return true; } diff --git a/game/coins/CoinScript.h b/game/coins/CoinScript.h index 6c52733..3fcee6d 100644 --- a/game/coins/CoinScript.h +++ b/game/coins/CoinScript.h @@ -7,4 +7,5 @@ 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"; }; -- cgit v1.2.3 From 5ae4e3f0e3ff1d111eaea54b7b0a0788203d9d72 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 3 Jan 2025 21:02:48 +0100 Subject: bug fix --- game/coins/CoinSubScene.cpp | 2 ++ game/mainmenu/BannerSubScene.cpp | 5 +++- game/mainmenu/ButtonSubScene.cpp | 30 ++++++++++++++++----- game/mainmenu/ButtonSubScene.h | 11 +++++--- game/mainmenu/ITransitionScript.cpp | 5 ++++ game/mainmenu/MainMenuConfig.h | 12 +++++++-- game/mainmenu/MainMenuScene.cpp | 52 ++++++++++++++++++++++++++++++------- 7 files changed, 95 insertions(+), 22 deletions(-) (limited to 'game/coins') diff --git a/game/coins/CoinSubScene.cpp b/game/coins/CoinSubScene.cpp index d30f5a4..58f74ef 100644 --- a/game/coins/CoinSubScene.cpp +++ b/game/coins/CoinSubScene.cpp @@ -8,6 +8,7 @@ #include #include #include "CoinScript.h" +#include "../Config.h" using namespace crepe; using namespace std; @@ -23,6 +24,7 @@ int CoinSubScene::create(Scene & scn){ coin.add_component(Rigidbody::Data{ .body_type = Rigidbody::BodyType::KINEMATIC, .kinematic_collision = false, + .collision_layers = {COLL_LAY_PLAYER}, }); coin.add_component(size.x / 2).active = false; crepe::OptionalRef coin_sprite = coin.add_component(Asset{"asset/coin/coin1_TVOS.png"}, Sprite::Data{ diff --git a/game/mainmenu/BannerSubScene.cpp b/game/mainmenu/BannerSubScene.cpp index ed093aa..ba4c126 100644 --- a/game/mainmenu/BannerSubScene.cpp +++ b/game/mainmenu/BannerSubScene.cpp @@ -33,6 +33,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( size, MainMenuConfig::FONT, Text::Data{.text_color = Color::WHITE,}, data.banner_title_offset + MainMenuConfig::FONTOFFSET, data.banner_title); + menu_banner.add_component( size, MainMenuConfig::FONT, Text::Data{ + .world_space = true, + .text_color = Color::WHITE, + }, data.banner_title_offset + MainMenuConfig::FONTOFFSET, data.banner_title); } diff --git a/game/mainmenu/ButtonSubScene.cpp b/game/mainmenu/ButtonSubScene.cpp index 8890ccc..53ac8d0 100644 --- a/game/mainmenu/ButtonSubScene.cpp +++ b/game/mainmenu/ButtonSubScene.cpp @@ -16,7 +16,7 @@ using namespace crepe; using namespace std; void ButtonSubScene::create(Scene & scn,const Data & data){ - GameObject button_object = scn.new_object("button","",data.position,0,data.scale); + GameObject button_object = scn.new_object("button",data.tag,data.position,0,data.scale); this->set_button_overlay(button_object,data); this->btn_text(button_object,data); this->set_script(button_object,data); @@ -27,6 +27,7 @@ void ButtonSubScene::btn_text(crepe::GameObject & button_object,const Data & dat crepe::vec2 size = {data.text_width,(data.text_width/data.text.size())*2}; button_object.add_component(size, MainMenuConfig::FONT,Text::Data{ + .world_space = data.worldspace, .text_color = Color::WHITE, }, data.text_offset+MainMenuConfig::FONTOFFSET, data.text); } @@ -55,6 +56,15 @@ void ButtonSubScene::set_icon(crepe::GameObject & button_object,const Data & dat .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+3, .size = MainMenuConfig::ICON_SIZE, .position_offset = data.icon_offset, + .world_space = data.worldspace, + }); + break; + case IconSelect::COINS: + button_object.add_component(Asset("asset/ui/buttonCoinsSmall.png"),Sprite::Data{ + .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+3, + .size = MainMenuConfig::ICON_SIZE, + .position_offset = data.icon_offset, + .world_space = data.worldspace, }); break; case IconSelect::NONE: @@ -65,47 +75,53 @@ void ButtonSubScene::set_icon(crepe::GameObject & button_object,const Data & dat void ButtonSubScene::set_button_overlay(crepe::GameObject & button_object,const Data & data){ switch (data.button_type) { case ButtonSelect::LARGE: - this->large_btn_overlay(button_object); + this->large_btn_overlay(button_object,data); break; case ButtonSelect::SMALL: - this->small_btn_overlay(button_object); + this->small_btn_overlay(button_object,data); break; } } -void ButtonSubScene::large_btn_overlay(crepe::GameObject & button_object){ +void ButtonSubScene::large_btn_overlay(crepe::GameObject & button_object,const Data & data){ button_object.add_component(Asset("asset/ui/buttonBacking.png"),Sprite::Data{ .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+1, .size = MainMenuConfig::LARGE_OVERLAY_SIZE, + .world_space = data.worldspace, }); button_object.add_component