cs381/as6/Entities.cpp

272 lines
8.3 KiB
C++

#include "Entities.hpp"
#include "Components.hpp"
#include "EnergyBarRaygui.hpp"
#include "raylib.h"
#include <algorithm>
std::shared_ptr<Entity> CreateProbe() {
auto e = std::make_shared<Entity>();
auto &transform = e->AddComponent<TransformComponent>();
transform.x = 96.0f;
transform.y = 230.0f;
e->AddComponent<GravityReceiverComponent>();
auto &physics = e->AddComponent<PhysicsComponent>();
physics.vx = 16.0f;
physics.vy = 0.0f;
physics.speedCap = 8192.0f;
auto &collider = e->AddComponent<ColliderComponent>();
collider.radius = 7.0f;
collider.monitorable = true;
auto &probeState = e->AddComponent<ProbeStateComponent>();
probeState.spawnX = 96.0f;
probeState.spawnY = 230.0f;
probeState.spawnVx = 165.0f;
probeState.spawnVy = 0.0f;
e->AddComponent<TrailComponent>();
e->AddComponent<ProjectionComponent>();
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto transform = e->GetComponent<TransformComponent>();
if (!transform) {
return;
}
const Color PROBE_COLOR = Color{120, 210, 255, 255};
const Color PROBE_HIGHLIGHT = Color{255, 255, 255, 120};
DrawCircleV({transform->get().x, transform->get().y}, 7.0f, PROBE_COLOR);
DrawCircleV({transform->get().x - 1, transform->get().y - 1}, 3.0f, PROBE_HIGHLIGHT);
};
return e;
}
std::shared_ptr<Entity> CreateGravityWell() {
auto e = std::make_shared<Entity>();
auto &transform = e->AddComponent<TransformComponent>();
transform.x = 96.0f;
transform.y = 230.0f;
auto &well = e->AddComponent<GravityWellComponent>();
well.mass = 0.0f;
well.minDist = 28.0f;
return e;
}
std::shared_ptr<Entity> CreateBlackHole(float x, float y) {
auto e = std::make_shared<Entity>();
auto &transform = e->AddComponent<TransformComponent>();
transform.x = x;
transform.y = y;
auto &scrollable = e->AddComponent<ScrollableComponent>();
scrollable.worldX = x;
auto &well = e->AddComponent<GravityWellComponent>();
well.mass = static_cast<float>(1 << 21);
well.minDist = 24.0f;
well.controlledByMouse = false;
well.alwaysActive = true;
// shader removed: previously used for black hole lensing
auto &collider = e->AddComponent<ColliderComponent>();
collider.radius = 18.0f;
e->AddComponent<HazardComponent>();
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto transform = e->GetComponent<TransformComponent>();
if (!transform) {
return;
}
const int cx = static_cast<int>(transform->get().x);
const int cy = static_cast<int>(transform->get().y);
DrawCircle(cx, cy, 18.0f, Color{8, 10, 20, 255});
DrawCircleLines(cx, cy, 18.0f, Color{70, 95, 170, 240});
DrawCircleLines(cx, cy, 24.0f, Color{95, 120, 200, 120});
};
return e;
}
std::shared_ptr<Entity> CreatePlayerBlackHole(float worldX, float y, float scrollX, float ttl) {
auto e = std::make_shared<Entity>();
auto &transform = e->AddComponent<TransformComponent>();
transform.x = worldX - scrollX;
transform.y = y;
auto &scrollable = e->AddComponent<ScrollableComponent>();
scrollable.worldX = worldX;
auto &well = e->AddComponent<GravityWellComponent>();
well.mass = static_cast<float>(1 << 22);
well.minDist = 28.0f;
well.controlledByMouse = false;
well.alwaysActive = true;
// shader removed: previously used for black hole lensing
auto &lifetime = e->AddComponent<LifetimeComponent>();
lifetime.remaining = ttl;
// Mark this as a player-created black hole so it can be removed via right-click
e->AddComponent<PlayerBlackHoleComponent>();
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto transform = e->GetComponent<TransformComponent>();
if (!transform) {
return;
}
auto lifetime = e->GetComponent<LifetimeComponent>();
if (!lifetime) {
return;
}
const int cx = static_cast<int>(transform->get().x);
const int cy = static_cast<int>(transform->get().y);
float t = 360 * lifetime->get().remaining / 1;
DrawCircleSector({(float)cx, (float)cy}, 28.0f, 0.0f, t, 32, Color{70, 95, 170, 25});
DrawCircleLines(cx, cy, 18.0f, Color{70, 95, 170, 240});
DrawCircleLines(cx, cy, 24.0f, Color{95, 120, 200, 120});
};
return e;
}
std::shared_ptr<Entity> CreateStar(float x, float y) {
auto e = std::make_shared<Entity>();
auto &t = e->AddComponent<TransformComponent>();
t.x = x;
t.y = y;
auto &scrollable = e->AddComponent<ScrollableComponent>();
scrollable.worldX = x;
auto &collider = e->AddComponent<ColliderComponent>();
collider.radius = 20.0f;
e->AddComponent<CollectibleComponent>();
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto transform = e->GetComponent<TransformComponent>();
if (!transform) {
return;
}
Vector2 center = {transform->get().x, transform->get().y};
Color STAR_COLOR = Color{255, 223, 86, 255};
Color GLOW_COLOR = Color{255, 223, 86, 80};
DrawCircleV(center, 20.0f, GLOW_COLOR);
DrawPoly(center, 3, 10.0f, transform->get().x, STAR_COLOR);
DrawPoly(center, 3, 10.0f, transform->get().x + 180, STAR_COLOR);
};
return e;
}
std::shared_ptr<Entity> CreateAsteroid(float x, float y) {
auto e = std::make_shared<Entity>();
auto &t = e->AddComponent<TransformComponent>();
t.x = x;
t.y = y;
auto &scrollable = e->AddComponent<ScrollableComponent>();
scrollable.worldX = x;
auto &collider = e->AddComponent<ColliderComponent>();
collider.radius = 13.0f;
e->AddComponent<HazardComponent>();
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto transform = e->GetComponent<TransformComponent>();
if (!transform) {
return;
}
DrawCircleV({transform->get().x, transform->get().y}, 13.0f, Color{116, 126, 142, 255});
};
return e;
}
std::shared_ptr<Entity> CreateDebris(float x, float y, float vx, float vy, float ttl) {
auto e = std::make_shared<Entity>();
auto &t = e->AddComponent<TransformComponent>();
t.x = x;
t.y = y;
auto &physics = e->AddComponent<PhysicsComponent>();
physics.vx = vx;
physics.vy = vy;
physics.speedCap = 260.0f;
auto &lifetime = e->AddComponent<LifetimeComponent>();
lifetime.remaining = ttl;
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto transform = e->GetComponent<TransformComponent>();
if (!transform) {
return;
}
DrawCircleV({transform->get().x, transform->get().y}, 3.0f, Color{245, 196, 104, 220});
};
return e;
}
std::shared_ptr<Entity> CreateNullZone(float x, float width) {
auto e = std::make_shared<Entity>();
auto &t = e->AddComponent<TransformComponent>();
t.x = x;
auto &scrollable = e->AddComponent<ScrollableComponent>();
scrollable.worldX = x;
auto &nullZone = e->AddComponent<NullZoneComponent>();
nullZone.width = width;
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto transform = e->GetComponent<TransformComponent>();
auto zone = e->GetComponent<NullZoneComponent>();
if (!transform || !zone) {
return;
}
int x = (int)(transform->get().x);
DrawRectangle(x, 0, (int)(zone->get().width), GetScreenHeight(), Color{96, 64, 146, 80});
DrawText("Null Zone", x + 8, 12, 16, Color{96, 64, 146, 200});
};
return e;
}
std::shared_ptr<Entity> CreateWorld() {
auto e = std::make_shared<Entity>();
e->AddComponent<ScrollComponent>();
e->AddComponent<SpawnComponent>();
return e;
}
std::shared_ptr<Entity> CreateStats() {
auto e = std::make_shared<Entity>();
e->AddComponent<StatsComponent>();
auto &render = e->AddComponent<RenderComponent>();
render.draw = [e]() {
auto stats = e->GetComponent<StatsComponent>();
if (!stats) {
return;
}
const float ratio = std::clamp(stats->get().value / stats->get().maxValue, 0.0f, 1.0f);
DrawHud(ratio, stats->get().GetStarCount());
};
return e;
}