diff options
Diffstat (limited to 'game/coins')
-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 |
9 files changed, 192 insertions, 0 deletions
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 |