cs381/as6/scene/GameplayScene.cpp

105 lines
3.1 KiB
C++

#include "scene/GameplayScene.hpp"
#include "scene/DeathScene.hpp"
#include "scene/SceneManager.hpp"
void GameplayScene::Enter() {
entities.clear();
entities.reserve(20);
context = {};
wantsDeathScene = false;
collectedCount = 0;
meterValue = 60.0f;
context.onPlayerDeath = [this]() { wantsDeathScene = true; };
context.AddCollectiblePickedListener([this](Entity &collectible) {
++collectedCount;
auto transform = collectible.GetComponent<TransformComponent>();
if (!transform || !context.entities) {
return;
}
context.entities->emplace_back(
CreateDebris(transform->get().x - 4.0f, transform->get().y, -22.0f, -36.0f));
context.entities->emplace_back(
CreateDebris(transform->get().x + 4.0f, transform->get().y, 24.0f, -28.0f));
});
context.AddMeterChangedListener([this](float, float newValue) { meterValue = newValue; });
context.worldEntity = entities.emplace_back(CreateWorld()).get();
context.wellEntity = entities.emplace_back(CreateGravityWell()).get();
context.probeEntity = entities.emplace_back(CreateProbe()).get();
entities.emplace_back(CreateStar(900.0f, 230.0f));
entities.emplace_back(CreateNullZone(1280.0f, 120.0f));
context.hudEntity = entities.emplace_back(CreateHUD()).get();
if (context.hudEntity) {
auto meter = context.hudEntity->GetComponent<MeterComponent>();
if (meter) {
meterValue = meter->get().value;
}
}
context.entities = &entities;
for (auto &entity : entities) {
if (!entity) {
continue;
}
entity->SetContext(&context);
}
}
void GameplayScene::Update(float dt) {
UpdateAllSystems(entities, dt);
if (wantsDeathScene) {
manager.QueueSceneChange<DeathScene>();
}
}
void GameplayScene::Draw() {
DrawSceneOutline();
DrawText("Gravity Surfing", 14, 12, 20, Color{230, 238, 255, 255});
std::optional<std::reference_wrapper<ScrollComponent>> worldScroll;
if (context.worldEntity) {
worldScroll = context.worldEntity->GetComponent<ScrollComponent>();
}
std::optional<std::reference_wrapper<TransformComponent>> probeTransform;
if (context.probeEntity) {
probeTransform = context.probeEntity->GetComponent<TransformComponent>();
}
if (worldScroll) {
DrawText(TextFormat("scrollX: %.2f", worldScroll->get().scrollX), 14, 40, 16,
Color{125, 225, 205, 255});
}
if (probeTransform) {
const auto &probe = probeTransform->get();
DrawText(TextFormat("probe: (%.1f, %.1f)", probe.x, probe.y), 14, 60, 16,
Color{235, 215, 125, 255});
}
DrawText(TextFormat("meter: %.1f", meterValue), 14, 80, 16, Color{120, 210, 190, 255});
DrawText(TextFormat("stars: %i", collectedCount), 14, 100, 16, Color{255, 223, 86, 255});
for (auto &entity : entities) {
if (!entity || entity->queuedForFree) {
continue;
}
auto render = entity->GetComponent<RenderComponent>();
if (render) {
render->get().Draw();
}
}
}