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/prefab/ZapperPoolScript.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'game/prefab/ZapperPoolScript.cpp') diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index e42adc9..c6ce1e7 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -1,9 +1,11 @@ #include #include "../Config.h" +#include "../Random.h" #include "ZapperPoolScript.h" #include "ZapperPoolSubScene.h" +#include "util/OptionalRef.h" using namespace crepe; using namespace std; @@ -24,9 +26,14 @@ void ZapperPoolScript::fixed_update(crepe::duration_t) { } void ZapperPoolScript::spawn_random() { + OptionalRef zapper = pool.get_next_zapper(); + if (!zapper) return; // pool exhausted + vec2 pos = this->get_camera_pos(); - logf(Log::DEBUG, "Spawning random zapper at {}", pos); + pos.y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT; + zapper->place(pos, 0, Random::f(400, 200)); + logf(Log::DEBUG, "Spawning random zapper at {}", pos); } vec2 ZapperPoolScript::get_camera_pos() { -- cgit v1.2.3 From 06d41eb2d58de21a8340ca59952b96cabc8caee6 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 11:13:40 +0100 Subject: pooling working --- game/prefab/ZapperObject.cpp | 10 +++++++--- game/prefab/ZapperPoolScript.cpp | 37 ++++++++++++++++++++++++------------- game/prefab/ZapperPoolScript.h | 11 ++++++++--- game/prefab/ZapperPoolSubScene.cpp | 14 ++++---------- game/prefab/ZapperPoolSubScene.h | 8 -------- 5 files changed, 43 insertions(+), 37 deletions(-) (limited to 'game/prefab/ZapperPoolScript.cpp') diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp index 61681ae..712eca9 100644 --- a/game/prefab/ZapperObject.cpp +++ b/game/prefab/ZapperObject.cpp @@ -13,7 +13,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) Sprite::Data { .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = 1, - .size = vec2(0, 42) * SCALE, + .size = vec2(0, 50) * SCALE, } ), .orb_end = add_component( @@ -22,7 +22,7 @@ ZapperObject::ZapperObject(crepe::GameObject && base) .flip = {true, true}, .sorting_in_layer = SORT_IN_LAY_OBSTACLES, .order_in_layer = 1, - .size = vec2(0, 42) * SCALE, + .size = vec2(0, 50) * SCALE, } ), .glow_start = add_component( @@ -81,10 +81,12 @@ ZapperObject::ZapperObject(crepe::GameObject && base) })}, collider {add_component(vec2(0, 0))} { this->set_active(false); - Log::logf(Log::DEBUG, "creating zapper"); + Log::logf(Log::DEBUG, "Creating zapper"); } void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) { + Log::logf(Log::DEBUG, "Placing zapper [position = {}, rotation = {}, length = {}]", position, rotation, length); + this->transform.position = position; this->transform.rotation = rotation; @@ -98,6 +100,8 @@ 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 c6ce1e7..812024b 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -10,37 +10,48 @@ using namespace crepe; using namespace std; -ZapperPoolScript::ZapperPoolScript(ZapperPoolSubScene & pool) : pool(pool) {} +ZapperPoolScript::ZapperPoolScript(std::vector && pool) : pool(pool) {} void ZapperPoolScript::init() { subscribe([this](const CreateZapperEvent &) { this->spawn_random(); return true; }); + + camera_transform = get_components_by_name(CAMERA_NAME).back(); + camera_camera = get_components_by_name(CAMERA_NAME).back(); } void ZapperPoolScript::fixed_update(crepe::duration_t) { - if (i++ < 80) return; - i = 0; + for (ZapperObject & zapper : this->pool) { + if (!zapper.active) continue; + + if (zapper.transform.position.x < camera_transform->position.x) + zapper.set_active(false); + } + + if (i-- > 0) return; + i = 200; queue_event(); } void ZapperPoolScript::spawn_random() { - OptionalRef zapper = pool.get_next_zapper(); + OptionalRef zapper = this->get_next_zapper(); if (!zapper) return; // pool exhausted - vec2 pos = this->get_camera_pos(); - pos.y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT; + vec2 pos = { + .x = camera_transform->position.x + camera_camera->viewport_size.x / 2, + .y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT, + }; zapper->place(pos, 0, Random::f(400, 200)); - logf(Log::DEBUG, "Spawning random zapper at {}", pos); } -vec2 ZapperPoolScript::get_camera_pos() { - Transform & transform = get_components_by_name(CAMERA_NAME).back(); - Camera & camera = get_components_by_name(CAMERA_NAME).back(); - - // right middle edge position - return transform.position + vec2(camera.viewport_size.x / 2, 0); +OptionalRef ZapperPoolScript::get_next_zapper() { + for (ZapperObject & zapper : this->pool) { + if (zapper.active) continue; + return zapper; + } + return {}; } diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h index 7c4701c..5daba79 100644 --- a/game/prefab/ZapperPoolScript.h +++ b/game/prefab/ZapperPoolScript.h @@ -2,11 +2,14 @@ #include +#include "ZapperObject.h" +#include "util/OptionalRef.h" + class ZapperPoolSubScene; class ZapperPoolScript : public crepe::Script { public: - ZapperPoolScript(ZapperPoolSubScene & pool); + ZapperPoolScript(std::vector && pool); void init(); void fixed_update(crepe::duration_t); @@ -14,10 +17,12 @@ public: unsigned i = 0; private: - ZapperPoolSubScene & pool; + std::vector pool; private: - crepe::vec2 get_camera_pos(); + crepe::OptionalRef camera_transform; + crepe::OptionalRef camera_camera; + crepe::OptionalRef get_next_zapper(); private: void spawn_random(); diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp index d7d30ea..923ed57 100644 --- a/game/prefab/ZapperPoolSubScene.cpp +++ b/game/prefab/ZapperPoolSubScene.cpp @@ -8,18 +8,12 @@ using namespace std; ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene) : controller { scene.new_object("controller") } { - this->controller.add_component().set_script(*this); Log::logf(Log::DEBUG, "Building zapper pool..."); + vector pool; for (size_t i = 0; i < this->POOL_SIZE; i++) - zappers.emplace_back(scene.new_object("zapper")); -} - -OptionalRef ZapperPoolSubScene::get_next_zapper() { - for (ZapperObject & zapper : this->zappers) { - if (!zapper.active) continue; - return zapper; - } - return {}; + pool.emplace_back(scene.new_object("zapper")); + 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 25328ee..f930e22 100644 --- a/game/prefab/ZapperPoolSubScene.h +++ b/game/prefab/ZapperPoolSubScene.h @@ -1,14 +1,10 @@ #pragma once -#include - #include #include #include #include -#include "ZapperObject.h" - class CreateZapperEvent : public crepe::Event {}; class ZapperPoolSubScene { @@ -17,11 +13,7 @@ public: private: crepe::GameObject controller; - std::vector zappers; private: static constexpr size_t POOL_SIZE = 4; - -public: - crepe::OptionalRef get_next_zapper(); }; -- 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/prefab/ZapperPoolScript.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 d9c67da9b8c2d8d25ef4dd2c700ddc78573d3a60 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 8 Jan 2025 12:11:14 +0100 Subject: WIP --- game/prefab/ZapperPoolScript.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'game/prefab/ZapperPoolScript.cpp') diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp index e201149..ac6ce96 100644 --- a/game/prefab/ZapperPoolScript.cpp +++ b/game/prefab/ZapperPoolScript.cpp @@ -46,8 +46,18 @@ void ZapperPoolScript::spawn_random() { }; bool horizontal = Random::b(); - float rotation = 90.0 * horizontal; - float length = horizontal ? Random::f(400, 200) : Random::f(200, 50); + float rotation, length; + + if (horizontal) { + rotation = 90; + length = Random::f(400, 200); + } else { + rotation = 0; + length = Random::f(200, 50); + if (abs(pos.y) + length / 2 > HALLWAY_HEIGHT / 2) { + // TODO: fix offset + } + } zapper->place(pos, rotation, length); zapper->set_active(true); -- cgit v1.2.3