Add scrolling

master
John Montagu, the 4th Earl of Sandvich 2026-03-15 19:07:28 -07:00
parent d3b871f214
commit 5abb201ed3
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
3 changed files with 74 additions and 32 deletions

View File

@ -2,8 +2,8 @@
#include "Entity.hpp" #include "Entity.hpp"
// Placeholder component definitions inspired by the GDD. Logic will be filled #include <algorithm>
// in later, but the types exist now so other modules can include them and build. #include <cmath>
struct TransformComponent : public Component { struct TransformComponent : public Component {
float x = 0.0f; float x = 0.0f;
@ -18,7 +18,22 @@ struct PhysicsComponent : public Component {
float vy = 0.0f; float vy = 0.0f;
float speedCap = 400.0f; float speedCap = 400.0f;
void Setup() override {} void Setup() override {}
void Update(float) override {} void Update(float dt) override {
auto transform = entity->GetComponent<TransformComponent>();
if (!transform) {
return;
}
const float speed = std::sqrt(vx * vx + vy * vy);
if (speed > speedCap && speed > 0.0f) {
const float scale = speedCap / speed;
vx *= scale;
vy *= scale;
}
transform->get().x += vx * dt;
transform->get().y += vy * dt;
}
void Cleanup() override {} void Cleanup() override {}
}; };
@ -49,8 +64,12 @@ struct ColliderComponent : public Component {
struct ScrollComponent : public Component { struct ScrollComponent : public Component {
float scrollX = 0.0f; float scrollX = 0.0f;
float speed = 2.0f; float speed = 2.0f;
float accel = 0.018f;
void Setup() override {} void Setup() override {}
void Update(float) override {} void Update(float dt) override {
speed += accel * dt;
scrollX += speed * dt * 60.0f;
}
void Cleanup() override {} void Cleanup() override {}
}; };

View File

@ -6,69 +6,76 @@
std::shared_ptr<Entity> CreateProbe() { std::shared_ptr<Entity> CreateProbe() {
auto e = std::make_shared<Entity>(); auto e = std::make_shared<Entity>();
e->template AddComponent<TransformComponent>(); auto &transform = e->AddComponent<TransformComponent>();
e->template AddComponent<PhysicsComponent>(); transform.x = 96.0f;
e->template AddComponent<GravityReceiverComponent>(); transform.y = 230.0f;
e->template AddComponent<ColliderComponent>();
e->template AddComponent<TrailComponent>(); auto &physics = e->AddComponent<PhysicsComponent>();
e->template AddComponent<ProjectionComponent>(); physics.vx = 165.0f;
e->template AddComponent<RenderComponent>(); physics.vy = 0.0f;
physics.speedCap = 420.0f;
e->AddComponent<GravityReceiverComponent>();
e->AddComponent<ColliderComponent>();
e->AddComponent<TrailComponent>();
e->AddComponent<ProjectionComponent>();
e->AddComponent<RenderComponent>();
return e; return e;
} }
std::shared_ptr<Entity> CreateGravityWell() { std::shared_ptr<Entity> CreateGravityWell() {
auto e = std::make_shared<Entity>(); auto e = std::make_shared<Entity>();
e->template AddComponent<TransformComponent>(); e->AddComponent<TransformComponent>();
e->template AddComponent<GravityWellComponent>(); e->AddComponent<GravityWellComponent>();
e->template AddComponent<RenderComponent>(); e->AddComponent<RenderComponent>();
return e; return e;
} }
std::shared_ptr<Entity> CreateStar(float x, float y) { std::shared_ptr<Entity> CreateStar(float x, float y) {
auto e = std::make_shared<Entity>(); auto e = std::make_shared<Entity>();
auto &t = e->template AddComponent<TransformComponent>(); auto &t = e->AddComponent<TransformComponent>();
t.x = x; t.x = x;
t.y = y; t.y = y;
e->template AddComponent<ScrollableComponent>(); e->AddComponent<ScrollableComponent>();
e->template AddComponent<ColliderComponent>(); e->AddComponent<ColliderComponent>();
e->template AddComponent<CollectibleComponent>(); e->AddComponent<CollectibleComponent>();
e->template AddComponent<RenderComponent>(); e->AddComponent<RenderComponent>();
return e; return e;
} }
std::shared_ptr<Entity> CreateAsteroid(float x, float y) { std::shared_ptr<Entity> CreateAsteroid(float x, float y) {
auto e = std::make_shared<Entity>(); auto e = std::make_shared<Entity>();
auto &t = e->template AddComponent<TransformComponent>(); auto &t = e->AddComponent<TransformComponent>();
t.x = x; t.x = x;
t.y = y; t.y = y;
e->template AddComponent<ScrollableComponent>(); e->AddComponent<ScrollableComponent>();
e->template AddComponent<ColliderComponent>(); e->AddComponent<ColliderComponent>();
e->template AddComponent<RenderComponent>(); e->AddComponent<RenderComponent>();
return e; return e;
} }
std::shared_ptr<Entity> CreateNullZone(float x, float width) { std::shared_ptr<Entity> CreateNullZone(float x, float width) {
auto e = std::make_shared<Entity>(); auto e = std::make_shared<Entity>();
auto &t = e->template AddComponent<TransformComponent>(); auto &t = e->AddComponent<TransformComponent>();
t.x = x; t.x = x;
(void)width; (void)width;
e->template AddComponent<ScrollableComponent>(); e->AddComponent<ScrollableComponent>();
e->template AddComponent<NullZoneComponent>(); e->AddComponent<NullZoneComponent>();
e->template AddComponent<RenderComponent>(); e->AddComponent<RenderComponent>();
return e; return e;
} }
std::shared_ptr<Entity> CreateWorld() { std::shared_ptr<Entity> CreateWorld() {
auto e = std::make_shared<Entity>(); auto e = std::make_shared<Entity>();
e->template AddComponent<ScrollComponent>(); e->AddComponent<ScrollComponent>();
e->template AddComponent<SpawnComponent>(); e->AddComponent<SpawnComponent>();
return e; return e;
} }
std::shared_ptr<Entity> CreateHUD() { std::shared_ptr<Entity> CreateHUD() {
auto e = std::make_shared<Entity>(); auto e = std::make_shared<Entity>();
e->template AddComponent<MeterComponent>(); e->AddComponent<MeterComponent>();
e->template AddComponent<HudComponent>(); e->AddComponent<HudComponent>();
e->template AddComponent<RenderComponent>(); e->AddComponent<RenderComponent>();
return e; return e;
} }

View File

@ -24,6 +24,9 @@ int main() {
UpdateAllSystems(entities, dt); UpdateAllSystems(entities, dt);
auto worldScroll = entities[0]->GetComponent<ScrollComponent>();
auto probeTransform = entities[2]->GetComponent<TransformComponent>();
window.BeginDrawing(); window.BeginDrawing();
window.ClearBackground(raylib::Color(11, 15, 26, 255)); window.ClearBackground(raylib::Color(11, 15, 26, 255));
@ -31,6 +34,19 @@ int main() {
raylib::DrawText("Gravity Surfing", 14, 12, 20, raylib::Color::RayWhite()); raylib::DrawText("Gravity Surfing", 14, 12, 20, raylib::Color::RayWhite());
if (worldScroll) {
// debug readout for early loop validation
raylib::DrawText(TextFormat("scrollX: %.2f", worldScroll->get().scrollX), 14, 40, 16,
raylib::Color(125, 225, 205, 255));
}
if (probeTransform) {
const auto &probe = probeTransform->get();
::DrawCircleV({probe.x, probe.y}, 7.0f, raylib::Color(250, 244, 227, 255));
raylib::DrawText(TextFormat("probe: (%.1f, %.1f)", probe.x, probe.y), 14, 60, 16,
raylib::Color(235, 215, 125, 255));
}
window.EndDrawing(); window.EndDrawing();
} }