Created
August 30, 2022 09:36
-
-
Save klemola/a2bb8a99f90578c456b7cb6e44c100ea to your computer and use it in GitHub Desktop.
vetovoima - update gravity visuals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn update_gravity_visuals( | |
mut visuals_query: Query<(&mut Path, &mut DrawMode, &mut GravityRing)>, | |
gravity_source: Res<GravitySource>, | |
) { | |
let level_bounds_radius_pixels = LEVEL_BOUNDS_RADIUS_METERS * PIXELS_PER_METER; | |
let gravity_source_radius_pixels = GRAVITY_SOURCE_RADIUS_METERS * PIXELS_PER_METER; | |
let min_radius = gravity_source_radius_pixels; | |
// fade to nothing just before the ring hits the terrain | |
let max_radius = level_bounds_radius_pixels; | |
for (mut path, mut draw_mode, mut ring) in visuals_query.iter_mut() { | |
let current_radius = ring.0; | |
// reset the radius at the inner or outer edge of the world bounds if outside the bounds | |
let bounded_radius = if current_radius < min_radius { | |
max_radius | |
} else if current_radius > max_radius { | |
min_radius | |
} else { | |
current_radius | |
}; | |
// grow or shrink the ring based on gravity | |
let radius_force_ratio = PIXELS_PER_METER / for; | |
let next_radius = bounded_radius + (gravity_source.force * radius_force_ratio); | |
// solid when close to the gravity source, transparent when far away | |
let opacity = (1.0 - (next_radius / max_radius)).max(0.0); | |
// dimmer when transparent | |
let lightness = 0.35 + (opacity / 2.0); | |
// a little subdued even when close to the gravity source | |
let capped_opacity = opacity * 0.8; | |
let next_shape = shapes::Circle { | |
radius: next_radius, | |
center: Vec2::ZERO, | |
}; | |
let next_draw_mode = DrawMode::Stroke(StrokeMode { | |
options: StrokeOptions::DEFAULT, | |
color: Color::hsla(220.0, 1.0, lightness, capped_opacity), | |
}); | |
ring.0 = next_radius; | |
*path = ShapePath::build_as(&next_shape); | |
*draw_mode = next_draw_mode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment