60 lines
1.7 KiB
C++
60 lines
1.7 KiB
C++
#include "components/GravityWellComponent.hpp"
|
|
|
|
#include "Entities.hpp"
|
|
#include "Entity.hpp"
|
|
#include "components/PlayerBlackHoleComponent.hpp"
|
|
#include "components/StatsComponent.hpp"
|
|
|
|
#include "raylib.h"
|
|
|
|
void GravityWellComponent::Setup() {}
|
|
|
|
void GravityWellComponent::Update(float) {
|
|
if (!controlledByMouse) {
|
|
active = alwaysActive;
|
|
return;
|
|
}
|
|
|
|
active = false;
|
|
|
|
if (!context || !context->entities) {
|
|
return;
|
|
}
|
|
|
|
// right click removes all player-created black holes so the player can clear the screen quickly
|
|
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
|
|
for (auto &ent : *context->entities) {
|
|
if (!ent || ent->queuedForFree) {
|
|
continue;
|
|
}
|
|
|
|
auto marker = ent->GetComponent<PlayerBlackHoleComponent>();
|
|
if (marker) {
|
|
ent->QueueFree();
|
|
}
|
|
}
|
|
// Do not early return; allow left-click placement to still happen in the same frame
|
|
}
|
|
|
|
// only spawn a new black hole when the player has enough meter and pressed left
|
|
if (!IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || !context->statsEntity) {
|
|
return;
|
|
}
|
|
|
|
auto stats = context->statsEntity->GetComponent<StatsComponent>();
|
|
if (!stats || stats->get().value < placementCost) {
|
|
return;
|
|
}
|
|
|
|
stats->get().Drain(placementCost);
|
|
|
|
const Vector2 mouse = GetMousePosition();
|
|
const float worldX = context->scrollX + mouse.x;
|
|
|
|
auto blackHole = CreatePlayerBlackHole(worldX, mouse.y, context->scrollX, placementTtl);
|
|
blackHole->SetContext(context);
|
|
context->entities->push_back(blackHole);
|
|
}
|
|
|
|
void GravityWellComponent::Cleanup() {}
|