stars directions
This commit is contained in:
@ -11,6 +11,7 @@ constexpr float MIN_ASPECT = 0.001f;
|
||||
SpaceWarp::SpaceWarp() {
|
||||
std::random_device rd;
|
||||
rng.seed(rd());
|
||||
setFlightMode(SpaceWarpFlightMode::Forward);
|
||||
}
|
||||
|
||||
void SpaceWarp::init(int w, int h, int starCount) {
|
||||
@ -34,6 +35,53 @@ void SpaceWarp::setSettings(const SpaceWarpSettings& newSettings) {
|
||||
warpFactor = std::max(width, height) * settings.warpFactorScale;
|
||||
}
|
||||
|
||||
void SpaceWarp::setFlightMode(SpaceWarpFlightMode mode) {
|
||||
flightMode = mode;
|
||||
autoPilotEnabled = false;
|
||||
switch (mode) {
|
||||
case SpaceWarpFlightMode::Forward:
|
||||
motion = {1.0f, 0.0f, 0.0f};
|
||||
break;
|
||||
case SpaceWarpFlightMode::BankLeft:
|
||||
motion = {1.05f, -0.85f, 0.0f};
|
||||
break;
|
||||
case SpaceWarpFlightMode::BankRight:
|
||||
motion = {1.05f, 0.85f, 0.0f};
|
||||
break;
|
||||
case SpaceWarpFlightMode::Reverse:
|
||||
motion = {-0.6f, 0.0f, 0.0f};
|
||||
break;
|
||||
case SpaceWarpFlightMode::Custom:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SpaceWarp::setFlightMotion(const SpaceWarpFlightMotion& newMotion) {
|
||||
motion = newMotion;
|
||||
flightMode = SpaceWarpFlightMode::Custom;
|
||||
autoPilotEnabled = false;
|
||||
}
|
||||
|
||||
void SpaceWarp::setAutoPilotEnabled(bool enabled) {
|
||||
autoPilotEnabled = enabled;
|
||||
if (enabled) {
|
||||
flightMode = SpaceWarpFlightMode::Custom;
|
||||
motionTarget = motion;
|
||||
autoTimer = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void SpaceWarp::scheduleNewAutoTarget() {
|
||||
motionTarget.forwardScale = randomRange(0.82f, 1.28f);
|
||||
if (randomRange(0.0f, 1.0f) < 0.12f) {
|
||||
motionTarget.forwardScale = -randomRange(0.35f, 0.85f);
|
||||
}
|
||||
motionTarget.lateralSpeed = randomRange(-1.35f, 1.35f);
|
||||
motionTarget.verticalSpeed = randomRange(-0.75f, 0.75f);
|
||||
autoTimer = randomRange(autoMinInterval, autoMaxInterval);
|
||||
}
|
||||
|
||||
float SpaceWarp::randomRange(float min, float max) {
|
||||
std::uniform_real_distribution<float> dist(min, max);
|
||||
return dist(rng);
|
||||
@ -79,12 +127,43 @@ void SpaceWarp::update(float deltaSeconds) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& star : stars) {
|
||||
star.z -= star.speed * deltaSeconds;
|
||||
if (star.z <= minDepth) {
|
||||
respawn(star, true);
|
||||
continue;
|
||||
if (autoPilotEnabled) {
|
||||
autoTimer -= deltaSeconds;
|
||||
if (autoTimer <= 0.0f) {
|
||||
scheduleNewAutoTarget();
|
||||
}
|
||||
auto follow = std::clamp(deltaSeconds * 0.45f, 0.0f, 1.0f);
|
||||
motion.forwardScale = std::lerp(motion.forwardScale, motionTarget.forwardScale, follow);
|
||||
motion.lateralSpeed = std::lerp(motion.lateralSpeed, motionTarget.lateralSpeed, follow);
|
||||
motion.verticalSpeed = std::lerp(motion.verticalSpeed, motionTarget.verticalSpeed, follow);
|
||||
}
|
||||
|
||||
const float forwardScale = (std::abs(motion.forwardScale) < 0.01f)
|
||||
? (motion.forwardScale >= 0.0f ? 0.01f : -0.01f)
|
||||
: motion.forwardScale;
|
||||
const bool movingBackward = forwardScale < 0.0f;
|
||||
const float lateralSpeed = motion.lateralSpeed;
|
||||
const float verticalSpeed = motion.verticalSpeed;
|
||||
|
||||
for (auto& star : stars) {
|
||||
star.z -= star.speed * deltaSeconds * forwardScale;
|
||||
if (!movingBackward) {
|
||||
if (star.z <= minDepth) {
|
||||
respawn(star, true);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (star.z >= maxDepth) {
|
||||
respawn(star, true);
|
||||
star.z = minDepth + randomRange(0.25f, 24.0f);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
float closeness = 1.0f - std::clamp(star.z / maxDepth, 0.0f, 1.0f);
|
||||
float driftScale = (0.35f + closeness * 1.25f);
|
||||
star.x += lateralSpeed * deltaSeconds * driftScale;
|
||||
star.y += verticalSpeed * deltaSeconds * driftScale;
|
||||
|
||||
float sx = 0.0f;
|
||||
float sy = 0.0f;
|
||||
|
||||
Reference in New Issue
Block a user