aboutsummaryrefslogtreecommitdiff
path: root/game/coins
diff options
context:
space:
mode:
authorMax-001 <maxsmits21@kpnmail.nl>2025-01-08 09:03:30 +0100
committerMax-001 <maxsmits21@kpnmail.nl>2025-01-08 09:03:30 +0100
commit93aa94921e027e05d7cb9908305af225c64b1ff2 (patch)
tree8beacc3b049a7150aa2cb5af501b158d11158a9e /game/coins
parent1a61140ab5be8ede0aa19644cd29fb6770261f91 (diff)
Make format
Diffstat (limited to 'game/coins')
-rw-r--r--game/coins/CoinPoolSubScene.cpp4
-rw-r--r--game/coins/CoinPoolSubScene.h2
-rw-r--r--game/coins/CoinScript.cpp26
-rw-r--r--game/coins/CoinScript.h1
-rw-r--r--game/coins/CoinSubScene.cpp37
-rw-r--r--game/coins/CoinSubScene.h2
-rw-r--r--game/coins/CoinSystemScript.cpp127
-rw-r--r--game/coins/CoinSystemScript.h92
8 files changed, 160 insertions, 131 deletions
diff --git a/game/coins/CoinPoolSubScene.cpp b/game/coins/CoinPoolSubScene.cpp
index 05f6bfc..f8b5b70 100644
--- a/game/coins/CoinPoolSubScene.cpp
+++ b/game/coins/CoinPoolSubScene.cpp
@@ -7,7 +7,7 @@ using namespace std;
void CoinPoolSubScene::create_coins(crepe::Scene & scn) {
int amount = 0;
CoinSubScene coin;
- while(amount < this->MAXIMUM_AMOUNT){
- amount = coin.create(scn,amount);
+ while (amount < this->MAXIMUM_AMOUNT) {
+ amount = coin.create(scn, amount);
}
}
diff --git a/game/coins/CoinPoolSubScene.h b/game/coins/CoinPoolSubScene.h
index 7360051..07626d6 100644
--- a/game/coins/CoinPoolSubScene.h
+++ b/game/coins/CoinPoolSubScene.h
@@ -5,7 +5,7 @@
class CoinPoolSubScene {
public:
void create_coins(crepe::Scene & scn);
+
private:
static constexpr int MAXIMUM_AMOUNT = 100;
};
-
diff --git a/game/coins/CoinScript.cpp b/game/coins/CoinScript.cpp
index 3d18780..3a2b5b0 100644
--- a/game/coins/CoinScript.cpp
+++ b/game/coins/CoinScript.cpp
@@ -11,25 +11,35 @@
using namespace crepe;
using namespace std;
-bool CoinScript::on_collision(const CollisionEvent & collisionData){
- if(collisionData.info.other.metadata.tag != "coin") return false;
- this->get_components_by_name<Sprite>(collisionData.info.other.metadata.name).front().get().active = false;
- this->get_components_by_name<CircleCollider>(collisionData.info.other.metadata.name).front().get().active = false;
+bool CoinScript::on_collision(const CollisionEvent & collisionData) {
+ if (collisionData.info.other.metadata.tag != "coin") return false;
+ this->get_components_by_name<Sprite>(collisionData.info.other.metadata.name)
+ .front()
+ .get()
+ .active
+ = false;
+ this->get_components_by_name<CircleCollider>(collisionData.info.other.metadata.name)
+ .front()
+ .get()
+ .active
+ = false;
this->amount++;
return false;
}
-void CoinScript::init(){
- this->subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); });
+void CoinScript::init() {
+ this->subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {
+ return this->on_collision(ev);
+ });
}
void CoinScript::fixed_update(crepe::duration_t dt) {
- this->trigger_event(GetCoinEvent{
+ this->trigger_event(GetCoinEvent {
.amount_of_coins = this->amount,
});
}
-bool CoinScript::save(){
+bool CoinScript::save() {
SaveManager & savemgr = this->get_save_manager();
savemgr.set(TOTAL_COINS_RUN, this->amount);
return false;
diff --git a/game/coins/CoinScript.h b/game/coins/CoinScript.h
index 6fcb64e..5718025 100644
--- a/game/coins/CoinScript.h
+++ b/game/coins/CoinScript.h
@@ -8,6 +8,7 @@ public:
void fixed_update(crepe::duration_t dt) override;
bool on_collision(const crepe::CollisionEvent & collisionData);
bool save();
+
private:
int amount = 0;
};
diff --git a/game/coins/CoinSubScene.cpp b/game/coins/CoinSubScene.cpp
index 63f9e89..2c9feb6 100644
--- a/game/coins/CoinSubScene.cpp
+++ b/game/coins/CoinSubScene.cpp
@@ -3,37 +3,42 @@
#include "../Config.h"
#include <crepe/api/Animator.h>
+#include <crepe/api/AudioSource.h>
#include <crepe/api/CircleCollider.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Scene.h>
-#include <crepe/api/AudioSource.h>
using namespace crepe;
using namespace std;
-int CoinSubScene::create(Scene & scn,int coin_counter){
+int CoinSubScene::create(Scene & scn, int coin_counter) {
vec2 size = {20, 20};
string unique_name = "coin_" + to_string(coin_counter++);
- GameObject coin = scn.new_object(unique_name.c_str(),"coin",vec2{650,0},0,1);
- coin.add_component<Rigidbody>(Rigidbody::Data{
+ GameObject coin = scn.new_object(unique_name.c_str(), "coin", vec2 {650, 0}, 0, 1);
+ coin.add_component<Rigidbody>(Rigidbody::Data {
.body_type = Rigidbody::BodyType::KINEMATIC,
.kinematic_collision = false,
.collision_layers = {COLL_LAY_PLAYER},
});
- coin.add_component<CircleCollider>((size.x / 2)-3).active = false;
- crepe::OptionalRef<crepe::Sprite> coin_sprite = coin.add_component<Sprite>(Asset{"asset/coin/coin1_TVOS.png"}, Sprite::Data{
- .sorting_in_layer = SORT_IN_LAY_COINS,
- .order_in_layer = 0,
- .size = size,
- });
+ coin.add_component<CircleCollider>((size.x / 2) - 3).active = false;
+ crepe::OptionalRef<crepe::Sprite> coin_sprite = coin.add_component<Sprite>(
+ Asset {"asset/coin/coin1_TVOS.png"},
+ Sprite::Data {
+ .sorting_in_layer = SORT_IN_LAY_COINS,
+ .order_in_layer = 0,
+ .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"});
+ 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
index ddb6450..7a1c60a 100644
--- a/game/coins/CoinSubScene.h
+++ b/game/coins/CoinSubScene.h
@@ -8,5 +8,5 @@ class Scene;
class CoinSubScene {
public:
- int create(crepe::Scene & scn,int coin_counter);
+ int create(crepe::Scene & scn, int coin_counter);
};
diff --git a/game/coins/CoinSystemScript.cpp b/game/coins/CoinSystemScript.cpp
index a58e3fd..1634aa9 100644
--- a/game/coins/CoinSystemScript.cpp
+++ b/game/coins/CoinSystemScript.cpp
@@ -10,17 +10,13 @@
using namespace crepe;
using namespace std;
+void CoinSystemScript::init() { engine.seed(rd()); }
-
-void CoinSystemScript::init() {
- engine.seed(rd());
-}
-
-void CoinSystemScript::add_location(const crepe::vec2& location){
+void CoinSystemScript::add_location(const crepe::vec2 & location) {
coin_locations.push_back(CoinData(location));
}
-float CoinSystemScript::preset_1(const vec2 & begin_position){
+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)};
@@ -31,7 +27,7 @@ float CoinSystemScript::preset_1(const vec2 & begin_position){
}
// Add locations for the bottom row
- bottom.x +=this->COLUM_OFFSET_1 * COLUM_AMOUNT_1;
+ bottom.x += this->COLUM_OFFSET_1 * COLUM_AMOUNT_1;
for (int i = 0; i < COLUM_AMOUNT_1; ++i) {
add_location(bottom);
bottom.x += this->COLUM_OFFSET_1;
@@ -45,22 +41,24 @@ float CoinSystemScript::preset_1(const vec2 & begin_position){
}
// Add locations for the next set of the bottom row
- bottom.x +=this->COLUM_OFFSET_1 * COLUM_AMOUNT_1;
+ bottom.x += this->COLUM_OFFSET_1 * COLUM_AMOUNT_1;
for (int i = 0; i < COLUM_AMOUNT_1; ++i) {
add_location(bottom);
bottom.x += this->COLUM_OFFSET_1;
}
- return bottom.x-begin_position.x;
+ return bottom.x - begin_position.x;
}
-float CoinSystemScript::preset_2(const vec2 & begin_position){
- vec2 top = {begin_position.x+this->COLUM_OFFSET_2, begin_position.y - this->ROW_OFFSET_2};
+float CoinSystemScript::preset_2(const vec2 & begin_position) {
+ vec2 top
+ = {begin_position.x + this->COLUM_OFFSET_2, begin_position.y - this->ROW_OFFSET_2};
vec2 middle = begin_position;
- vec2 bottom = {begin_position.x+this->COLUM_OFFSET_2, begin_position.y + this->ROW_OFFSET_2};
+ vec2 bottom
+ = {begin_position.x + this->COLUM_OFFSET_2, begin_position.y + this->ROW_OFFSET_2};
// Add locations for the next set of the bottom row
- for (int i = 0; i < COLUM_AMOUNT_2-2; ++i) {
+ for (int i = 0; i < COLUM_AMOUNT_2 - 2; ++i) {
add_location(bottom);
bottom.x += this->COLUM_OFFSET_2;
}
@@ -72,18 +70,17 @@ float CoinSystemScript::preset_2(const vec2 & begin_position){
}
// Add locations for the next set of the top row
- for (int i = 0; i < COLUM_AMOUNT_2-2; ++i) {
+ for (int i = 0; i < COLUM_AMOUNT_2 - 2; ++i) {
add_location(top);
top.x += this->COLUM_OFFSET_2;
}
- return middle.x-begin_position.x;
+ return middle.x - begin_position.x;
}
-float CoinSystemScript::preset_3(const vec2 & begin_position){
+float CoinSystemScript::preset_3(const vec2 & begin_position) {
vec2 location = {begin_position.x, begin_position.y - (this->ROW_OFFSET_3)};
-
// Add locations for the top row
for (int i = 0; i < COLUM_AMOUNT_3; ++i) {
add_location(location);
@@ -91,7 +88,7 @@ float CoinSystemScript::preset_3(const vec2 & begin_position){
}
// Add locations for the bottom row
- location.y +=this->ROW_OFFSET_3;
+ location.y += this->ROW_OFFSET_3;
location.x += this->COLUM_OFFSET_3;
for (int i = 0; i < COLUM_AMOUNT_3; ++i) {
add_location(location);
@@ -99,20 +96,19 @@ float CoinSystemScript::preset_3(const vec2 & begin_position){
}
// Add locations for the next set of the top row
- location.y +=this->ROW_OFFSET_3;
+ location.y += this->ROW_OFFSET_3;
location.x += this->COLUM_OFFSET_3;
for (int i = 0; i < COLUM_AMOUNT_3; ++i) {
add_location(location);
location.x += this->COLUM_OFFSET_3;
}
- return location.x-begin_position.x;
+ return location.x - begin_position.x;
}
-float CoinSystemScript::preset_4(const vec2 & begin_position){
+float CoinSystemScript::preset_4(const vec2 & begin_position) {
vec2 location = {begin_position.x, begin_position.y + (this->ROW_OFFSET_4)};
-
// Add locations for the top row
for (int i = 0; i < COLUM_AMOUNT_4; ++i) {
add_location(location);
@@ -120,7 +116,7 @@ float CoinSystemScript::preset_4(const vec2 & begin_position){
}
// Add locations for the bottom row
- location.y -=this->ROW_OFFSET_4;
+ location.y -= this->ROW_OFFSET_4;
location.x += this->COLUM_OFFSET_4;
for (int i = 0; i < COLUM_AMOUNT_4; ++i) {
add_location(location);
@@ -128,32 +124,29 @@ float CoinSystemScript::preset_4(const vec2 & begin_position){
}
// Add locations for the next set of the top row
- location.y -=this->ROW_OFFSET_4;
+ location.y -= this->ROW_OFFSET_4;
location.x += this->COLUM_OFFSET_4;
for (int i = 0; i < COLUM_AMOUNT_4; ++i) {
add_location(location);
location.x += this->COLUM_OFFSET_4;
}
- return location.x-begin_position.x;
+ return location.x - begin_position.x;
}
-float CoinSystemScript::preset_5(const vec2 & begin_position){
- vec2 location = {begin_position.x, begin_position.y-ROW_OFFSET_5/2};
- for (int i = 0; i < COLUM_AMOUNT_5; ++i){
+float CoinSystemScript::preset_5(const vec2 & begin_position) {
+ vec2 location = {begin_position.x, begin_position.y - ROW_OFFSET_5 / 2};
+ for (int i = 0; i < COLUM_AMOUNT_5; ++i) {
add_location(location);
location.x += this->COLUM_OFFSET_5;
}
- return location.x-begin_position.x;
+ return location.x - begin_position.x;
}
-
-
-void CoinSystemScript::frame_update(crepe::duration_t dt)
-{
+void CoinSystemScript::frame_update(crepe::duration_t dt) {
this->despawn_coins();
this->generate_locations();
- this->spawn_coins();
+ this->spawn_coins();
}
void CoinSystemScript::despawn_coins() {
@@ -163,22 +156,26 @@ void CoinSystemScript::despawn_coins() {
// Retrieve all active coin sprites tagged as "coin"
RefVector<Sprite> coin_sprites = this->get_components_by_tag<Sprite>("coin");
- for (Sprite& coin_sprite : coin_sprites) {
+ 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();
+ 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;
+ coin_locations.begin(), coin_locations.end(),
+ [&coin_metadata](const CoinData & data) {
+ return data.name == coin_metadata.name;
}
);
@@ -192,31 +189,42 @@ void CoinSystemScript::despawn_coins() {
}
}
-void CoinSystemScript::spawn_coins(){
+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) {
+ for (auto & coin : coin_locations) {
// Skip this coin if it is already active
- if (coin.active)continue;
+ 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;
-
+ 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) {
+ 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();
-
+ 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;
@@ -224,26 +232,23 @@ void CoinSystemScript::spawn_coins(){
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 out of the inner loop since we've assigned this coin to an available sprite
break;
}
}
}
-void CoinSystemScript::generate_locations(){
+void CoinSystemScript::generate_locations() {
float position = this->get_component<Transform>().position.x;
- if(position + SPAWN_DISTANCE + SYSTEM_POSITION_OFFSET < this->system_position) return;
+ if (position + SPAWN_DISTANCE + SYSTEM_POSITION_OFFSET < this->system_position) return;
std::discrete_distribution<int> dist(weights.begin(), weights.end());
int selected_index = dist(engine);
std::uniform_real_distribution<float> space_dist(SPAWN_SPACING_MIN, SPAWN_SPACING_MAX);
float spacing = space_dist(engine);
-
+
// Call the corresponding function and return the new x position
- this->system_position += functions[selected_index]({this->system_position,0});
+ 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 6422807..5c94273 100644
--- a/game/coins/CoinSystemScript.h
+++ b/game/coins/CoinSystemScript.h
@@ -1,28 +1,33 @@
#pragma once
-#include <string>
#include <random>
+#include <string>
-#include <crepe/types.h>
#include <crepe/api/CircleCollider.h>
#include <crepe/api/Script.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Transform.h>
+#include <crepe/types.h>
class CoinSystemScript : public crepe::Script {
private:
- struct CoinData{
- crepe::vec2 start_location = {0,0};
+ 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) {}
+ CoinData(crepe::vec2 start_location)
+ : start_location(start_location),
+ name(""),
+ active(false) {}
};
+
public:
CoinSystemScript() {};
void init() override;
void frame_update(crepe::duration_t dt) override;
+
private:
- void add_location(const crepe::vec2& location);
+ void add_location(const crepe::vec2 & location);
void despawn_coins();
void spawn_coins();
void generate_locations();
@@ -31,19 +36,20 @@ private:
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<std::function<float(const crepe::vec2&)>> 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<int> weights = {20, 20,20,20, 20};
+ std::vector<std::function<float(const crepe::vec2 &)>> 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<int> weights = {20, 20, 20, 20, 20};
std::random_device rd;
std::default_random_engine engine;
float system_position = 1400;
static constexpr float SYSTEM_POSITION_OFFSET = 200;
+
private:
static constexpr float SPAWN_SPACING_MIN = 400;
static constexpr float SPAWN_SPACING_MAX = 1000;
@@ -51,48 +57,50 @@ private:
static constexpr float DESPAWN_DISTANCE = 600;
static constexpr float SPAWN_AREA = 50;
std::vector<CoinData> coin_locations;
+
private:
-// preset one settings
-// ***** *****
-//
-//
-//
-// ***** *****
+ // preset one settings
+ // ***** *****
+ //
+ //
+ //
+ // ***** *****
static constexpr float ROW_OFFSET_1 = 100;
static constexpr float COLUM_OFFSET_1 = 25;
static constexpr int COLUM_AMOUNT_1 = 5;
+
private:
-// preset two settings
-//
-// ********
-// **********
-// ********
-//
+ // preset two settings
+ //
+ // ********
+ // **********
+ // ********
+ //
static constexpr float ROW_OFFSET_2 = 25;
static constexpr float COLUM_OFFSET_2 = 25;
static constexpr int COLUM_AMOUNT_2 = 10;
-// preset three settings
-// ***
-//
-// ***
-//
-// ***
+ // preset three settings
+ // ***
+ //
+ // ***
+ //
+ // ***
static constexpr float ROW_OFFSET_3 = 100;
static constexpr float COLUM_OFFSET_3 = 25;
static constexpr int COLUM_AMOUNT_3 = 3;
-// preset four settings
-// ***
-//
-// ***
-//
-// ***
+ // preset four settings
+ // ***
+ //
+ // ***
+ //
+ // ***
static constexpr float ROW_OFFSET_4 = 100;
static constexpr float COLUM_OFFSET_4 = 25;
static constexpr int COLUM_AMOUNT_4 = 3;
-// preset five settings
-//
-// ***
-//
+ // preset five settings
+ //
+ // ***
+ //
static constexpr float ROW_OFFSET_5 = 25;
static constexpr float COLUM_OFFSET_5 = 25;
static constexpr int COLUM_AMOUNT_5 = 3;