cs381/as6/components/ProjectionComponent.cpp

75 lines
2.4 KiB
C++

#include "components/ProjectionComponent.hpp"
#include "Entity.hpp"
#include "components/GravityReceiverComponent.hpp"
#include "components/GravityWellComponent.hpp"
#include "components/PhysicsComponent.hpp"
#include "components/TransformComponent.hpp"
void ProjectionComponent::Setup() { points.clear(); }
void ProjectionComponent::Update(float) {
points.clear();
highlightActive = false;
if (!context || !context->probeEntity || entity != context->probeEntity) {
return;
}
auto transform = entity->GetComponent<TransformComponent>();
auto physics = entity->GetComponent<PhysicsComponent>();
auto receiver = entity->GetComponent<GravityReceiverComponent>();
if (!transform || !physics || !receiver) {
return;
}
Entity *wellEntity = receiver->get().well ? receiver->get().well : context->wellEntity;
if (!wellEntity) {
return;
}
auto wellTransform = wellEntity->GetComponent<TransformComponent>();
auto wellGravity = wellEntity->GetComponent<GravityWellComponent>();
if (!wellTransform || !wellGravity) {
return;
}
// only highlight if the well is active, inactive will be a lighter color to show the potential
// projection if the player were to activate it
highlightActive = wellGravity->get().active;
double px = static_cast<double>(transform->get().x);
double py = static_cast<double>(transform->get().y);
double vx = static_cast<double>(physics->get().vx);
double vy = static_cast<double>(physics->get().vy);
points.reserve(static_cast<size_t>(steps));
for (int i = 0; i < steps; ++i) {
physics->get().SimulateStep(px, py, vx, vy, static_cast<double>(stepDt), true);
points.push_back({static_cast<float>(px), static_cast<float>(py)});
}
Draw();
}
void ProjectionComponent::Cleanup() { points.clear(); }
void ProjectionComponent::Draw() const {
if (points.empty()) {
return;
}
const Color base = highlightActive ? activeColor : inactiveColor;
for (size_t i = 0; i < points.size(); ++i) {
// t goes from 0 to 1 across the points, used for fading effect
const float t = (points.size() <= 1) ? 0.0f : (float)i / (points.size() - 1);
Color c = base;
float alphaScale = 1.0f - t;
// multiply alpha to make it fade towards the end
c.a = (unsigned char)((float)(base.a) * alphaScale);
DrawCircleV(points[i], pointRadius, c);
}
}