diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-08 11:13:40 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-08 11:13:40 +0100 |
commit | 06d41eb2d58de21a8340ca59952b96cabc8caee6 (patch) | |
tree | 4daa9f74cb8a088ba17e54d52be307c5b917878f /game/prefab/ZapperPoolScript.cpp | |
parent | b8acb539a3580bf0665fe435c55f33ba16cb507e (diff) |
pooling working
Diffstat (limited to 'game/prefab/ZapperPoolScript.cpp')
-rw-r--r-- | game/prefab/ZapperPoolScript.cpp | 37 |
1 files changed, 24 insertions, 13 deletions
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<ZapperObject> && pool) : pool(pool) {} void ZapperPoolScript::init() { subscribe<CreateZapperEvent>([this](const CreateZapperEvent &) { this->spawn_random(); return true; }); + + camera_transform = get_components_by_name<Transform>(CAMERA_NAME).back(); + camera_camera = get_components_by_name<Camera>(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<CreateZapperEvent>(); } void ZapperPoolScript::spawn_random() { - OptionalRef<ZapperObject> zapper = pool.get_next_zapper(); + OptionalRef<ZapperObject> 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<Transform>(CAMERA_NAME).back(); - Camera & camera = get_components_by_name<Camera>(CAMERA_NAME).back(); - - // right middle edge position - return transform.position + vec2(camera.viewport_size.x / 2, 0); +OptionalRef<ZapperObject> ZapperPoolScript::get_next_zapper() { + for (ZapperObject & zapper : this->pool) { + if (zapper.active) continue; + return zapper; + } + return {}; } |