updated stars in game play grid
This commit is contained in:
@ -23,6 +23,22 @@ void Starfield3D::resize(int w, int h) {
|
||||
centerY = height * 0.5f;
|
||||
}
|
||||
|
||||
void Starfield3D::setMagnetTarget(float localX, float localY, float strength) {
|
||||
if (strength <= 0.0f) {
|
||||
clearMagnetTarget();
|
||||
return;
|
||||
}
|
||||
magnetActive = true;
|
||||
magnetStrength = strength;
|
||||
magnetX = std::clamp(localX, 0.0f, static_cast<float>(width));
|
||||
magnetY = std::clamp(localY, 0.0f, static_cast<float>(height));
|
||||
}
|
||||
|
||||
void Starfield3D::clearMagnetTarget() {
|
||||
magnetActive = false;
|
||||
magnetStrength = 0.0f;
|
||||
}
|
||||
|
||||
float Starfield3D::randomFloat(float min, float max) {
|
||||
std::uniform_real_distribution<float> dist(min, max);
|
||||
return dist(rng);
|
||||
@ -147,13 +163,24 @@ void Starfield3D::drawStar(SDL_Renderer* renderer, float x, float y, SDL_Color c
|
||||
}
|
||||
|
||||
void Starfield3D::draw(SDL_Renderer* renderer, float offsetX, float offsetY, float alphaScale, bool grayscale) {
|
||||
const bool useMagnet = magnetActive && magnetStrength > 0.0f;
|
||||
for (const Star3D& star : stars) {
|
||||
// Calculate perspective projection factor
|
||||
const float k = DEPTH_FACTOR / star.z;
|
||||
|
||||
// Calculate screen position with perspective
|
||||
const float px = star.x * k + centerX;
|
||||
const float py = star.y * k + centerY;
|
||||
float px = star.x * k + centerX;
|
||||
float py = star.y * k + centerY;
|
||||
|
||||
if (useMagnet) {
|
||||
float dx = magnetX - px;
|
||||
float dy = magnetY - py;
|
||||
float dist = std::sqrt(dx * dx + dy * dy);
|
||||
float pull = magnetStrength / (magnetStrength + dist + 1.0f);
|
||||
pull = std::clamp(pull, 0.0f, 0.35f);
|
||||
px += dx * pull;
|
||||
py += dy * pull;
|
||||
}
|
||||
|
||||
// Only draw stars that are within the viewport
|
||||
if (px >= 0.0f && px <= static_cast<float>(width) &&
|
||||
|
||||
Reference in New Issue
Block a user