cs381/as6/components/ProjectionComponent.cpp

117 lines
4.0 KiB
C++

#include "components/ProjectionComponent.hpp"
#include "Entity.hpp"
#include "components/GravityWellComponent.hpp"
#include "components/PhysicsComponent.hpp"
#include "components/StatsComponent.hpp"
#include "components/TransformComponent.hpp"
#include <algorithm>
#include <cmath>
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>();
if (!transform || !physics || !context->entities) {
return;
}
float placementCost = 10.0f;
float previewMass = static_cast<float>(1 << 22);
float previewMinDist = 28.0f;
if (context->wellEntity) {
auto controllerWell = context->wellEntity->GetComponent<GravityWellComponent>();
if (controllerWell) {
placementCost = controllerWell->get().placementCost;
if (controllerWell->get().mass > 0.0f) {
previewMass = controllerWell->get().mass;
}
previewMinDist = controllerWell->get().minDist;
}
}
bool canPreviewPlacement = false;
if (context->statsEntity) {
auto stats = context->statsEntity->GetComponent<StatsComponent>();
canPreviewPlacement = stats && stats->get().value >= placementCost;
}
const Vector2 mouse = GetMousePosition();
const double previewX = static_cast<double>(mouse.x);
const double previewY = static_cast<double>(mouse.y);
for (auto &other : *context->entities) {
if (!other || other.get() == entity) {
continue;
}
auto wellGravity = other->GetComponent<GravityWellComponent>();
if (wellGravity && wellGravity->get().active) {
// highlight arc when another well is active so the player knows the projection is
// influenced
highlightActive = true;
break;
}
}
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);
if (canPreviewPlacement && !physics->get().IsInsideNullZone(px)) {
const double dx = previewX - px;
const double dy = previewY - py;
const double dist = std::sqrt(dx * dx + dy * dy);
if (dist > 0.0001) {
const double clampedDist = std::max(dist, static_cast<double>(previewMinDist));
const double force = static_cast<double>(previewMass) / (clampedDist * clampedDist);
const double dt = static_cast<double>(stepDt);
vx += (dx / dist) * force * dt;
vy += (dy / dist) * force * dt;
physics->get().ClampVelocity(vx, vy);
}
}
// storing each simulated position gives the hud the full trajectory so the player can line
// up placements
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);
}
}