#include "components/SpawnComponent.hpp" #include "Entities.hpp" #include "Entity.hpp" #include "components/CollectibleComponent.hpp" #include "components/HazardComponent.hpp" #include "components/NullZoneComponent.hpp" #include "components/ScrollComponent.hpp" #include "components/ScrollableComponent.hpp" #include "components/TransformComponent.hpp" #include "raylib.h" #include void SpawnComponent::Setup() { // Start a bit beyond the initial handcrafted segment. cursorWX = 1500.0f; } void SpawnComponent::Update(float) { if (!context || !context->entities) { return; } auto scroll = entity->GetComponent(); if (!scroll) { return; } const float cameraX = scroll->get().scrollX; const float spawnLimit = cameraX + spawnAheadDistance; while (cursorWX < spawnLimit) { const float r = static_cast(GetRandomValue(0, 99)); if (r < 55.0f) { const float y = static_cast(GetRandomValue(48, GetScreenHeight() - 48)); auto star = CreateStar(cursorWX, y); star->SetContext(context); context->entities->push_back(star); } else if (r < 88.0f) { const float y = static_cast(GetRandomValue(42, GetScreenHeight() - 42)); auto asteroid = CreateAsteroid(cursorWX, y); asteroid->SetContext(context); context->entities->push_back(asteroid); } else { const float width = static_cast(GetRandomValue(70, 140)); auto zone = CreateNullZone(cursorWX, width); zone->SetContext(context); context->entities->push_back(zone); } const float gap = static_cast(GetRandomValue(static_cast(minGap), static_cast(maxGap))); cursorWX += gap; } const float cullX = cameraX - despawnBehindDistance; for (auto &candidate : *context->entities) { if (!candidate || candidate.get() == entity || candidate->queuedForFree) { continue; } const bool isSpawnedGameplayObject = candidate->GetComponent().has_value() || candidate->GetComponent().has_value() || candidate->GetComponent().has_value(); if (!isSpawnedGameplayObject) { continue; } auto scrollable = candidate->GetComponent(); if (scrollable) { if (scrollable->get().worldX < cullX) { candidate->QueueFree(); } continue; } auto transform = candidate->GetComponent(); if (transform && transform->get().x < cullX) { candidate->QueueFree(); } } } void SpawnComponent::Cleanup() {}