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/coins/CoinScript.cpp | 19 ++++++++++ game/coins/CoinScript.h | 10 ++++++ game/coins/CoinSubScene.cpp | 6 +++- game/coins/CoinSystemScript.cpp | 78 ++++++++++++----------------------------- game/coins/CoinSystemScript.h | 28 +++++++++++---- 5 files changed, 78 insertions(+), 63 deletions(-) create mode 100644 game/coins/CoinScript.cpp create mode 100644 game/coins/CoinScript.h (limited to 'game/coins') 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; }; -- cgit v1.2.3