Add appearing and disappearing boxes

This commit is contained in:
Evan Pratten 2021-10-02 23:48:30 -04:00
parent e61f67ee30
commit f53dc8122e
5 changed files with 101 additions and 31 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -7,7 +7,20 @@
"height": 57 "height": 57
} }
], ],
"disappear": [], "disappear": [
{
"x": 792,
"y": 462,
"width": 54,
"height": 468
},
{
"x": 2005,
"y": 641,
"width": 141,
"height": 289
}
],
"win": { "win": {
"x": 3000, "x": 3000,
"y": 0, "y": 0,

View File

@ -46,6 +46,11 @@ pub fn load_all_levels(
thread, thread,
&format!("levels/{}/appearing_platforms.png", level_name), &format!("levels/{}/appearing_platforms.png", level_name),
)?, )?,
disappearing_platform_tex: load_texture_from_internal_data(
raylib_handle,
thread,
&format!("levels/{}/disappearing_platforms.png", level_name),
)?,
colliders: serde_json::from_str( colliders: serde_json::from_str(
&String::from_utf8( &String::from_utf8(
StaticGameData::get(&format!("levels/{}/colliders.json", level_name)) StaticGameData::get(&format!("levels/{}/colliders.json", level_name))

View File

@ -17,6 +17,7 @@ pub struct Level {
pub background_tex: WorldPaintTexture, pub background_tex: WorldPaintTexture,
pub platform_tex: Texture2D, pub platform_tex: Texture2D,
pub appearing_platform_tex: Texture2D, pub appearing_platform_tex: Texture2D,
pub disappearing_platform_tex: Texture2D,
pub colliders: Vec<Rectangle>, pub colliders: Vec<Rectangle>,
pub zones: LevelZones, pub zones: LevelZones,
} }

View File

@ -13,6 +13,7 @@ use tracing::trace;
pub const WORLD_LEVEL_X_OFFSET: f32 = 200.0; pub const WORLD_LEVEL_X_OFFSET: f32 = 200.0;
pub const APPEAR_FADE_DISTANCE: f32 = 16.0; pub const APPEAR_FADE_DISTANCE: f32 = 16.0;
pub const DISAPPEAR_FADE_DISTANCE: f32 = 18.0;
impl WorldSpaceRender for InGameScreen { impl WorldSpaceRender for InGameScreen {
fn render_world_space( fn render_world_space(
@ -37,37 +38,87 @@ impl WorldSpaceRender for InGameScreen {
Color::WHITE, Color::WHITE,
); );
// Calculate the distance between the player and the nearest appearing zone {
let appear_zone_dist = cur_level // Calculate the distance between the player and the nearest appearing zone
.zones let appear_zone_dist = cur_level
.appear .zones
.iter() .appear
.map(|zone| { .iter()
Vector2::new( .map(|zone| {
zone.x + WORLD_LEVEL_X_OFFSET + (zone.width / 2.0), Vector2::new(
zone.y - cur_level.platform_tex.height as f32, zone.x + WORLD_LEVEL_X_OFFSET + (zone.width / 2.0),
) zone.y - cur_level.platform_tex.height as f32,
.distance_to(self.player.position) as i32 )
}) .distance_to(self.player.position) as i32
.min() })
.unwrap(); .min()
let opacity = interpolate_exp( .unwrap_or(i32::MAX);
(appear_zone_dist as f32).sub(APPEAR_FADE_DISTANCE.div(2.0)).div(APPEAR_FADE_DISTANCE).mul(-1.0), let appear_opacity = interpolate_exp(
-APPEAR_FADE_DISTANCE..APPEAR_FADE_DISTANCE, (appear_zone_dist as f32)
0.0..1.0, .sub(APPEAR_FADE_DISTANCE.div(2.0))
8.0, .div(APPEAR_FADE_DISTANCE)
); .mul(-1.0),
trace!("Appearing values: ({}, {})", appear_zone_dist, opacity); -APPEAR_FADE_DISTANCE..APPEAR_FADE_DISTANCE,
0.0..1.0,
8.0,
);
trace!(
"Appearing values: ({}, {})",
appear_zone_dist,
appear_opacity
);
// Render the appearing layer // Render the appearing layer
raylib.draw_texture_v( raylib.draw_texture_v(
&cur_level.appearing_platform_tex, &cur_level.appearing_platform_tex,
Vector2::new( Vector2::new(
WORLD_LEVEL_X_OFFSET, WORLD_LEVEL_X_OFFSET,
-cur_level.appearing_platform_tex.height as f32, -cur_level.appearing_platform_tex.height as f32,
), ),
Color::WHITE.fade(opacity), Color::WHITE.fade(appear_opacity),
); );
}
{
// Calculate the distance between the player and the nearest disappearing zone
let disappear_zone_dist = cur_level
.zones
.disappear
.iter()
.map(|zone| {
Vector2::new(
zone.x + WORLD_LEVEL_X_OFFSET + (zone.width / 2.0),
zone.y - cur_level.platform_tex.height as f32,
)
.distance_to(self.player.position) as i32
})
.min()
.unwrap_or(i32::MAX);
let disappear_opacity = interpolate_exp(
(disappear_zone_dist as f32)
.sub(DISAPPEAR_FADE_DISTANCE.div(2.0))
.div(DISAPPEAR_FADE_DISTANCE)
.mul(-1.0),
-DISAPPEAR_FADE_DISTANCE..DISAPPEAR_FADE_DISTANCE,
0.0..1.0,
8.0,
);
trace!(
"Disappearing values: ({}, {})",
disappear_zone_dist,
disappear_opacity
);
// Render the appearing layer
raylib.draw_texture_v(
&cur_level.disappearing_platform_tex,
Vector2::new(
WORLD_LEVEL_X_OFFSET,
-cur_level.disappearing_platform_tex.height as f32,
),
Color::WHITE.fade(1.0 - disappear_opacity),
);
}
#[cfg(all(debug_assertions, feature = "collider_debug"))] #[cfg(all(debug_assertions, feature = "collider_debug"))]
{ {