diff options
-rw-r--r-- | game/CMakeLists.txt | 7 | ||||
-rw-r--r-- | game/GameScene.cpp | 12 | ||||
-rw-r--r-- | game/coins/CoinData.h | 11 | ||||
-rw-r--r-- | game/coins/CoinSubScene.cpp | 35 | ||||
-rw-r--r-- | game/coins/CoinSubScene.h | 12 | ||||
-rw-r--r-- | game/coins/CoinSystem.cpp | 81 | ||||
-rw-r--r-- | game/coins/CoinSystem.h | 22 | ||||
-rw-r--r-- | game/coins/CoinSystemScript.cpp | 18 | ||||
-rw-r--r-- | game/coins/CoinSystemScript.h | 13 | ||||
-rw-r--r-- | game/coins/coinSubScene.cpp | 0 | ||||
-rw-r--r-- | game/coins/coinSubScene.h | 0 | ||||
-rw-r--r-- | game/mainmenu/MainMenuConfig.h | 1 | ||||
-rw-r--r-- | game/mainmenu/MainMenuScene.cpp | 6 |
13 files changed, 217 insertions, 1 deletions
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 <cmath> #include <crepe/api/Animator.h> @@ -33,6 +36,7 @@ void GameScene::load_scene() { .bg_color = Color::RED, }); camera.add_component<BehaviorScript>().set_script<MoveCameraManualyScript>(); + camera.add_component<BehaviorScript>().set_script<CoinSystemScript>(); camera.add_component<Rigidbody>(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<BehaviorScript>().set_script<StartGameScript>(); + + 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 <string> + +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 <concepts> + +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>(Rigidbody::Data{}); + coin.add_component<CircleCollider>(size.x / 2).active = false; + crepe::OptionalRef<crepe::Sprite> coin_sprite = coin.add_component<Sprite>(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<Animator>(coin_sprite, ivec2{32, 32}, uvec2{8, 1}, + Animator::Data{ + .fps = 15, + .looping = true, + }); + coin.add_component<AudioSource>(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 <crepe/api/GameObject.h> + +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<CoinData> 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<Sprite> coin_sprites, RefVector<CircleCollider> coin_colliders, RefVector<Metadata> 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 <string> +#include <vector> + +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<crepe::Sprite> coin_sprites,crepe::RefVector<crepe::CircleCollider> coin_colliders,crepe::RefVector<crepe::Metadata> coin_metadatas); +private: + static constexpr float SPAWN_DISTANCE = 200; + static constexpr float SPAWN_AREA = 50; + static constexpr int MAXIMUM_AMOUNT = 100; + static std::vector<CoinData> 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<Transform>().position.x; + RefVector<Sprite> coin_sprites = this->get_components_by_tag<Sprite>("coin"); + RefVector<CircleCollider> coin_colliders = this->get_components_by_tag<CircleCollider>("coin"); + RefVector<Metadata> coin_metadatas = this->get_components_by_tag<Metadata>("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 --- a/game/coins/coinSubScene.cpp +++ /dev/null diff --git a/game/coins/coinSubScene.h b/game/coins/coinSubScene.h deleted file mode 100644 index e69de29..0000000 --- a/game/coins/coinSubScene.h +++ /dev/null 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 <string> + 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); |