diff options
Diffstat (limited to 'game')
-rw-r--r-- | game/GameScene.cpp | 5 | ||||
-rw-r--r-- | game/coins/CoinSubScene.cpp | 9 | ||||
-rw-r--r-- | game/coins/CoinSystem.cpp | 71 | ||||
-rw-r--r-- | game/coins/CoinSystem.h | 14 | ||||
-rw-r--r-- | game/coins/CoinSystemScript.cpp | 134 | ||||
-rw-r--r-- | game/coins/CoinSystemScript.h | 16 | ||||
-rw-r--r-- | game/mainmenu/ITransitionScript.cpp | 2 | ||||
-rw-r--r-- | game/mainmenu/MainMenuScene.cpp | 6 |
8 files changed, 152 insertions, 105 deletions
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 <concepts> +#include <iostream> 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>(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{ @@ -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<Animator>(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<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 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 <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 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<CoinData> 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<Transform>().position.x; + + // Retrieve all active coin sprites tagged as "coin" 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); + + 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<Transform>(coin_sprite.game_object_id).front().get(); + Metadata& coin_metadata = this->get_components_by_id<Metadata>(coin_sprite.game_object_id).front().get(); + CircleCollider& coin_collider = this->get_components_by_id<CircleCollider>(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<Transform>().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<Sprite> coin_sprites = this->get_components_by_tag<Sprite>("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<Transform>(coin_sprite.game_object_id).front().get(); + Metadata & coin_metadata = this->get_components_by_id<Metadata>(coin_sprite.game_object_id).front().get(); + CircleCollider & coin_collider = this->get_components_by_id<CircleCollider>(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<CoinData> 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<Transform>(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; |