Implement torch flickering

This commit is contained in:
crumblingstatue 2023-04-08 17:43:30 +02:00
parent 9b2701a5a1
commit 38b6a476d9
3 changed files with 35 additions and 3 deletions

View file

@ -1,5 +1,5 @@
use egui_inspect::derive::Inspect;
use num_traits::Signed;
use num_traits::{Num, Signed};
use crate::world::{TilePos, TilePosScalar};
@ -70,6 +70,29 @@ pub fn center_offset<N: From<u8> + Copy + Signed>(xw: N, yw: N) -> N {
diff / N::from(2)
}
/// A smooth triangle-wave like transform of the input value, oscillating between 0 and the ceiling.
pub fn smoothwave<T: Num + From<u8> + PartialOrd + Copy>(input: T, max: T) -> T {
let period = max * T::from(2);
let value = input % period;
if value < max {
value
} else {
period - value
}
}
#[test]
fn test_smooth_wave() {
assert_eq!(smoothwave(0, 100), 0);
assert_eq!(smoothwave(50, 100), 50);
assert_eq!(smoothwave(125, 100), 75);
assert_eq!(smoothwave(150, 100), 50);
assert_eq!(smoothwave(175, 100), 25);
assert_eq!(smoothwave(199, 100), 1);
assert_eq!(smoothwave(200, 100), 0);
assert_eq!(smoothwave(201, 100), 1);
}
#[test]
fn test_wp_to_tp() {
assert_eq!(wp_to_tp(0), 0);