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 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