From b8acb539a3580bf0665fe435c55f33ba16cb507e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 09:58:34 +0100 Subject: WIP --- game/Random.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 game/Random.cpp (limited to 'game/Random.cpp') diff --git a/game/Random.cpp b/game/Random.cpp new file mode 100644 index 0000000..59be3c5 --- /dev/null +++ b/game/Random.cpp @@ -0,0 +1,28 @@ +#include + +#include "Random.h" + +float Random::f(float upper, float lower) { + float range = upper - lower; + float x = ((float) rand() / (float) (RAND_MAX)) * range; + return x + lower; +} + +double Random::d(double upper, double lower) { + double range = upper - lower; + double x = ((double) rand() / (double) (RAND_MAX)) * range; + return x + lower; +} + +int Random::i(int upper, int lower) { + int range = upper - lower; + int x = rand() % range; + return x + lower; +} + +unsigned Random::u(unsigned upper, unsigned lower) { + unsigned range = upper - lower; + unsigned x = rand() % range; + return x + lower; +} + -- cgit v1.2.3 From 46c2cbdcbe5e7dbc4d86a5d43b3bd7275b7a1c33 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 11:59:51 +0100 Subject: WIP --- game/GameScene.cpp | 33 --------------------------------- game/Random.cpp | 4 ++++ game/Random.h | 1 + game/prefab/ZapperObject.cpp | 2 -- game/prefab/ZapperPoolScript.cpp | 12 +++++++++--- game/prefab/ZapperPoolScript.h | 3 +++ game/prefab/ZapperPoolSubScene.cpp | 2 +- 7 files changed, 18 insertions(+), 39 deletions(-) (limited to 'game/Random.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 8bd6c74..6c21843 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -103,39 +103,6 @@ void GameScene::load_scene() { Asset boom_audio_asset {"asset/sfx/window_smash.ogg"}; boom_audio.add_component(boom_audio_asset); - GameObject laser = new_object("laser", "laser", vec2(2000, 0)); - Asset laser_asset {"asset/obstacles/laser/laserPower.png"}; - Sprite & laser_sprite = laser.add_component( - laser_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 0, - .size = vec2(100, 100), - } - ); - laser.add_component(Rigidbody::Data { - .body_type = Rigidbody::BodyType::KINEMATIC, - .kinematic_collision = false, - .collision_layer = COLL_LAY_LASER, - }); - laser.add_component(vec2(100, 100)); - GameObject missile = new_object("missile", "missile", vec2(4000, 0)); - Asset missile_asset {"asset/obstacles/missile/missile.png"}; - Sprite & missile_sprite = missile.add_component( - missile_asset, - Sprite::Data { - .sorting_in_layer = SORT_IN_LAY_OBSTACLES, - .order_in_layer = 0, - .size = vec2(100, 100), - } - ); - missile.add_component(Rigidbody::Data { - .body_type = Rigidbody::BodyType::KINEMATIC, - .kinematic_collision = false, - .collision_layer = COLL_LAY_MISSILE, - }); - missile.add_component(vec2(100, 100)); - EndGameSubScene endgamewindow; endgamewindow.create(*this); } diff --git a/game/Random.cpp b/game/Random.cpp index 59be3c5..821ddc8 100644 --- a/game/Random.cpp +++ b/game/Random.cpp @@ -26,3 +26,7 @@ unsigned Random::u(unsigned upper, unsigned lower) { return x + lower; } +bool Random::b() { + return rand() % 2; +} + diff --git a/game/Random.h b/game/Random.h index cf05e87..8db616c 100644 --- a/game/Random.h +++ b/game/Random.h @@ -6,6 +6,7 @@ public: static double d(double upper = 1.0, double lower = 0.0); static int i(int upper, int lower = 0); static unsigned u(unsigned upper, unsigned lower = 0); + static bool b(); }; diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 712eca9..0a290e0 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -100,8 +100,6 @@ void ZapperObject::place(const crepe::vec2 & position, float rotation, float len this->sprite.beam.data.size.x = length; this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE; - - this->set_active(true); } void ZapperObject::set_active(bool active) { diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index 812024b..e201149 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -23,10 +23,11 @@ void ZapperPoolScript::init() { } void ZapperPoolScript::fixed_update(crepe::duration_t) { + float threshold = camera_transform->position.x - camera_camera->viewport_size.x / 2 - OFFSCREEN_MARGIN; for (ZapperObject & zapper : this->pool) { if (!zapper.active) continue; - if (zapper.transform.position.x < camera_transform->position.x) + if (zapper.transform.position.x < threshold) zapper.set_active(false); } @@ -40,11 +41,16 @@ void ZapperPoolScript::spawn_random() { if (!zapper) return; // pool exhausted vec2 pos = { - .x = camera_transform->position.x + camera_camera->viewport_size.x / 2, + .x = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN, .y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT, }; - zapper->place(pos, 0, Random::f(400, 200)); + bool horizontal = Random::b(); + float rotation = 90.0 * horizontal; + float length = horizontal ? Random::f(400, 200) : Random::f(200, 50); + + zapper->place(pos, rotation, length); + zapper->set_active(true); } OptionalRef ZapperPoolScript::get_next_zapper() { diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h index 5daba79..6aee8b2 100644 --- a/game/prefab/ZapperPoolScript.h +++ b/game/prefab/ZapperPoolScript.h @@ -26,5 +26,8 @@ private: private: void spawn_random(); + +private: + static constexpr float OFFSCREEN_MARGIN = 40; }; diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp index 923ed57..e341090 100644 --- a/game/prefab/ZapperPoolSubScene.cpp +++ b/game/prefab/ZapperPoolSubScene.cpp @@ -12,7 +12,7 @@ ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) Log::logf(Log::DEBUG, "Building zapper pool..."); vector pool; for (size_t i = 0; i < this->POOL_SIZE; i++) - pool.emplace_back(scene.new_object("zapper")); + pool.emplace_back(scene.new_object("zapper", "zapper")); BehaviorScript & behavior = this->controller.add_component(); behavior.set_script(std::move(pool)); } -- cgit v1.2.3 From 12cf43dd062f41af226e0ac038b2f2838391015f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 15:24:38 +0100 Subject: `make format` --- game/Config.h | 3 +-- game/Random.cpp | 5 +---- game/Random.h | 1 - game/menus/mainmenu/ITransitionScript.cpp | 2 +- game/menus/shop/ShopMenuScene.cpp | 3 +-- game/prefab/ZapperObject.cpp | 1 - game/prefab/ZapperPoolScript.cpp | 12 ++++++------ game/prefab/ZapperPoolScript.h | 1 - game/prefab/ZapperPoolSubScene.cpp | 5 ++--- game/prefab/ZapperPoolSubScene.h | 4 ++-- src/crepe/api/Vector2.hpp | 4 ++-- 11 files changed, 16 insertions(+), 25 deletions(-) (limited to 'game/Random.cpp') diff --git a/game/Config.h b/game/Config.h index 64f2828..580e9ec 100644 --- a/game/Config.h +++ b/game/Config.h @@ -58,5 +58,4 @@ static constexpr const char * PLAYER_NAME = "player"; static constexpr int PLAYER_SPEED = 7500; // In game units static constexpr int PLAYER_GRAVITY_SCALE = 60; // In game units -static constexpr const char* CAMERA_NAME = "camera"; - +static constexpr const char * CAMERA_NAME = "camera"; diff --git a/game/Random.cpp b/game/Random.cpp index 821ddc8..64cf1f3 100644 --- a/game/Random.cpp +++ b/game/Random.cpp @@ -26,7 +26,4 @@ unsigned Random::u(unsigned upper, unsigned lower) { return x + lower; } -bool Random::b() { - return rand() % 2; -} - +bool Random::b() { return rand() % 2; } diff --git a/game/Random.h b/game/Random.h index 4a1108a..94f98d2 100644 --- a/game/Random.h +++ b/game/Random.h @@ -8,4 +8,3 @@ public: static unsigned u(unsigned upper, unsigned lower = 0); static bool b(); }; - diff --git a/game/menus/mainmenu/ITransitionScript.cpp b/game/menus/mainmenu/ITransitionScript.cpp index 3e51a90..54b0875 100644 --- a/game/menus/mainmenu/ITransitionScript.cpp +++ b/game/menus/mainmenu/ITransitionScript.cpp @@ -1,8 +1,8 @@ #include "ITransitionScript.h" #include "MainMenuConfig.h" -#include "../MenusConfig.h" #include "../../Config.h" +#include "../MenusConfig.h" #include #include diff --git a/game/menus/shop/ShopMenuScene.cpp b/game/menus/shop/ShopMenuScene.cpp index f4d5e76..d4542ba 100644 --- a/game/menus/shop/ShopMenuScene.cpp +++ b/game/menus/shop/ShopMenuScene.cpp @@ -1,11 +1,10 @@ #include "ShopMenuScene.h" +#include "../../Config.h" #include "../BannerSubScene.h" #include "../ButtonSubScene.h" #include "../MenusConfig.h" -#include "../BannerSubScene.h" -#include "../../Config.h" #include #include diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 6139d53..24bbbd2 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -116,4 +116,3 @@ void ZapperObject::set_active(bool active) { this->active = active; } - diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index 168637c..b832ddd 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -23,23 +23,24 @@ void ZapperPoolScript::init() { } void ZapperPoolScript::fixed_update(crepe::duration_t) { - float threshold = camera_transform->position.x - camera_camera->viewport_size.x / 2 - OFFSCREEN_MARGIN; + float threshold + = camera_transform->position.x - camera_camera->viewport_size.x / 2 - OFFSCREEN_MARGIN; for (ZapperObject & zapper : this->pool) { if (!zapper.active) continue; - if (zapper.transform.position.x < threshold) - zapper.set_active(false); + if (zapper.transform.position.x < threshold) zapper.set_active(false); } } void ZapperPoolScript::spawn_random() { OptionalRef zapper = this->get_next_zapper(); if (!zapper) return; // pool exhausted - + bool horizontal = Random::b(); vec2 pos; float rotation, length; - pos.x = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN; + pos.x + = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN; if (horizontal) { rotation = 90; @@ -67,4 +68,3 @@ OptionalRef ZapperPoolScript::get_next_zapper() { } return {}; } - diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h index dd60071..2208c80 100644 --- a/game/prefab/ZapperPoolScript.h +++ b/game/prefab/ZapperPoolScript.h @@ -31,4 +31,3 @@ private: static constexpr float MAX_LENGTH = 400; static constexpr float OFFSCREEN_MARGIN = 50 + MAX_LENGTH; }; - diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp index e341090..8422b8c 100644 --- a/game/prefab/ZapperPoolSubScene.cpp +++ b/game/prefab/ZapperPoolSubScene.cpp @@ -1,13 +1,13 @@ #include -#include "ZapperPoolSubScene.h" #include "ZapperPoolScript.h" +#include "ZapperPoolSubScene.h" using namespace crepe; using namespace std; ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) - : controller { scene.new_object("controller") } { + : controller {scene.new_object("controller")} { Log::logf(Log::DEBUG, "Building zapper pool..."); vector pool; @@ -16,4 +16,3 @@ ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) BehaviorScript & behavior = this->controller.add_component(); behavior.set_script(std::move(pool)); } - diff --git a/game/prefab/ZapperPoolSubScene.h b/game/prefab/ZapperPoolSubScene.h index f930e22..6f6e297 100644 --- a/game/prefab/ZapperPoolSubScene.h +++ b/game/prefab/ZapperPoolSubScene.h @@ -1,8 +1,8 @@ #pragma once -#include -#include #include +#include +#include #include class CreateZapperEvent : public crepe::Event {}; diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index e2f96ed..30441d2 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -180,7 +180,7 @@ Vector2 Vector2::rotate(float deg) const { } // namespace crepe template -std::format_context::iterator std::formatter>::format(crepe::Vector2 vec, format_context & ctx) const { +std::format_context::iterator +std::formatter>::format(crepe::Vector2 vec, format_context & ctx) const { return formatter::format(std::format("{{{}, {}}}", vec.x, vec.y), ctx); } - -- cgit v1.2.3