cs381/as6/scene/GameplayScene.cpp

101 lines
3.5 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->push_back(
CreateDebris(transform->get().x - 4.0f, transform->get().y, -22.0f, -36.0f));
context.entities->push_back(
CreateDebris(transform->get().x + 4.0f, transform->get().y, 24.0f, -28.0f));
});
context.AddMeterChangedListener([this](float, float newValue) { meterValue = newValue; });
entities.push_back(CreateWorld());
entities.push_back(CreateGravityWell());
entities.push_back(CreateProbe());
entities.push_back(CreateStar(900.0f, 120.0f));
entities.push_back(CreateAsteroid(1100.0f, 330.0f));
entities.push_back(CreateNullZone(1280.0f, 120.0f));
entities.push_back(CreateHUD());
if (auto meter = entities.back()->GetComponent<MeterComponent>()) {
meterValue = meter->get().value;
}
context.entities = &entities;
context.worldEntity = (entities.size() > 0 && entities[0]) ? entities[0].get() : nullptr;
context.wellEntity = (entities.size() > 1 && entities[1]) ? entities[1].get() : nullptr;
context.probeEntity = (entities.size() > 2 && entities[2]) ? entities[2].get() : nullptr;
context.hudEntity = (!entities.empty() && entities.back()) ? entities.back().get() : nullptr;
for (auto &entity : entities) {
if (!entity) {
continue;
}
entity->SetContext(&context);
}
}
void GameplayScene::Update(float dt) {
UpdateAllSystems(entities, dt);
context.worldEntity = (entities.size() > 0 && entities[0]) ? entities[0].get() : nullptr;
context.wellEntity = (entities.size() > 1 && entities[1]) ? entities[1].get() : nullptr;
context.probeEntity = (entities.size() > 2 && entities[2]) ? entities[2].get() : nullptr;
context.hudEntity = (!entities.empty() && entities.back()) ? entities.back().get() : nullptr;
if (wantsDeathScene) {
manager.QueueSceneChange<DeathScene>();
}
}
void GameplayScene::Draw() {
DrawSceneOutline();
DrawText("Gravity Surfing", 14, 12, 20, Color{230, 238, 255, 255});
auto worldScroll = entities[0]->GetComponent<ScrollComponent>();
auto probeTransform = entities[2]->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();
}
}
}