diff --git a/src/texture/blinking.rs b/src/texture/blinking.rs deleted file mode 100644 index 10e72f7..0000000 --- a/src/texture/blinking.rs +++ /dev/null @@ -1,63 +0,0 @@ -#![allow(dead_code)] -use crate::texture::sprite::AtlasTile; - -#[derive(Clone)] -pub struct BlinkingTexture { - tile: AtlasTile, - interval_ticks: u32, - tick_timer: u32, - is_on: bool, -} - -impl BlinkingTexture { - pub fn new(tile: AtlasTile, interval_ticks: u32) -> Self { - Self { - tile, - interval_ticks, - tick_timer: 0, - is_on: true, - } - } - - pub fn tick(&mut self, delta_ticks: u32) { - self.tick_timer += delta_ticks; - - // Handle zero interval case (immediate toggling) - if self.interval_ticks == 0 { - // With zero interval, any positive ticks should toggle - if delta_ticks > 0 { - self.is_on = !self.is_on; - } - return; - } - - // Calculate how many complete intervals have passed - let complete_intervals = self.tick_timer / self.interval_ticks; - - // Update the timer to the remainder after complete intervals - self.tick_timer %= self.interval_ticks; - - // Toggle for each complete interval, but since toggling twice is a no-op, - // we only need to toggle if the count is odd - if complete_intervals % 2 == 1 { - self.is_on = !self.is_on; - } - } - - pub fn is_on(&self) -> bool { - self.is_on - } - - pub fn tile(&self) -> &AtlasTile { - &self.tile - } - - // Helper methods for testing - pub fn tick_timer(&self) -> u32 { - self.tick_timer - } - - pub fn interval_ticks(&self) -> u32 { - self.interval_ticks - } -} diff --git a/src/texture/mod.rs b/src/texture/mod.rs index b70145e..a946618 100644 --- a/src/texture/mod.rs +++ b/src/texture/mod.rs @@ -1,5 +1,4 @@ pub mod animated; -pub mod blinking; pub mod sprite; pub mod sprites; pub mod text; diff --git a/tests/blinking.rs b/tests/blinking.rs deleted file mode 100644 index 47395be..0000000 --- a/tests/blinking.rs +++ /dev/null @@ -1,205 +0,0 @@ -use pacman::texture::blinking::BlinkingTexture; -use speculoos::prelude::*; - -mod common; - -#[test] -fn test_blinking_texture() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 30); // 30 ticks = 0.5 seconds at 60 FPS - - assert_that(&texture.is_on()).is_true(); - - texture.tick(30); - assert_that(&texture.is_on()).is_false(); - - texture.tick(30); - assert_that(&texture.is_on()).is_true(); - - texture.tick(30); - assert_that(&texture.is_on()).is_false(); -} - -#[test] -fn test_blinking_texture_partial_duration() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 30); // 30 ticks - - texture.tick(37); // 37 ticks, should complete 1 interval (30 ticks) with 7 remaining - assert_that(&texture.is_on()).is_false(); - assert_that(&texture.tick_timer()).is_equal_to(7); -} - -#[test] -fn test_blinking_texture_zero_interval() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 0); - - assert_that(&texture.is_on()).is_true(); - assert_that(&texture.interval_ticks()).is_equal_to(0); - - // With zero interval, any positive ticks should toggle - texture.tick(1); - assert_that(&texture.is_on()).is_false(); - assert_that(&texture.tick_timer()).is_equal_to(1); - - texture.tick(1); - assert_that(&texture.is_on()).is_true(); - assert_that(&texture.tick_timer()).is_equal_to(2); -} - -#[test] -fn test_blinking_texture_multiple_cycles() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 15); // 15 ticks - - // Test multiple complete cycles - for i in 0..10 { - let expected_state = i % 2 == 0; - assert_that(&texture.is_on()).is_equal_to(expected_state); - texture.tick(15); - } -} - -#[test] -fn test_blinking_texture_accumulated_ticks() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 60); // 60 ticks = 1 second at 60 FPS - - // Test that ticks accumulate correctly - texture.tick(18); // 18 ticks - assert_that(&texture.tick_timer()).is_equal_to(18); - assert_that(&texture.is_on()).is_true(); - - texture.tick(18); // 36 total ticks - assert_that(&texture.tick_timer()).is_equal_to(36); - assert_that(&texture.is_on()).is_true(); - - texture.tick(18); // 54 total ticks - assert_that(&texture.tick_timer()).is_equal_to(54); - assert_that(&texture.is_on()).is_true(); - - texture.tick(12); // 66 total ticks, should complete 1 interval (60 ticks) with 6 remaining - assert_that(&texture.tick_timer()).is_equal_to(6); - assert_that(&texture.is_on()).is_false(); -} - -#[test] -fn test_blinking_texture_large_tick_step() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 30); // 30 ticks - - // Test with a tick step larger than the interval - texture.tick(90); // 90 ticks, should complete 3 intervals (90/30 = 3) - assert_that(&texture.is_on()).is_false(); // 3 toggles: true -> false -> true -> false - assert_that(&texture.tick_timer()).is_equal_to(0); // 90 % 30 = 0 - - texture.tick(60); // 60 ticks, should complete 2 intervals (60/30 = 2) - assert_that(&texture.is_on()).is_false(); // 2 more toggles: false -> true -> false (no change) - assert_that(&texture.tick_timer()).is_equal_to(0); // 60 % 30 = 0 -} - -#[test] -fn test_blinking_texture_small_steps() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 6); // 6 ticks - - // Test with small tick steps - for _ in 0..6 { - texture.tick(1); - } - - // After 6 ticks, should have completed one cycle - assert_that(&texture.tick_timer()).is_equal_to(0); - assert_that(&texture.is_on()).is_false(); -} - -#[test] -fn test_blinking_texture_clone() { - let tile = common::mock_atlas_tile(1); - let mut texture1 = BlinkingTexture::new(tile, 30); - let texture2 = texture1.clone(); - - // Both should have the same initial state - assert_that(&texture1.is_on()).is_equal_to(texture2.is_on()); - assert_that(&texture1.tick_timer()).is_equal_to(texture2.tick_timer()); - assert_that(&texture1.interval_ticks()).is_equal_to(texture2.interval_ticks()); - - // Modifying one shouldn't affect the other - texture1.tick(18); - assert_that(&texture1.tick_timer()).is_not_equal_to(texture2.tick_timer()); - assert_that(&texture2.tick_timer()).is_equal_to(0); -} - -#[test] -fn test_blinking_texture_edge_cases() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 60); // 60 ticks - - // Test exactly at the interval - texture.tick(60); - assert_that(&texture.is_on()).is_false(); - assert_that(&texture.tick_timer()).is_equal_to(0); - - // Test just under the interval - texture.tick(59); - assert_that(&texture.is_on()).is_false(); - assert_that(&texture.tick_timer()).is_equal_to(59); - - // Test just over the interval - texture.tick(2); - assert_that(&texture.is_on()).is_true(); - assert_that(&texture.tick_timer()).is_equal_to(1); -} - -#[test] -fn test_blinking_texture_very_small_interval() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 1); // 1 tick - - // Test with very small interval - texture.tick(1); - assert_that(&texture.is_on()).is_false(); - assert_that(&texture.tick_timer()).is_equal_to(0); - - texture.tick(1); - assert_that(&texture.is_on()).is_true(); - assert_that(&texture.tick_timer()).is_equal_to(0); -} - -#[test] -fn test_blinking_texture_very_large_interval() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 60000); // 60000 ticks = 1000 seconds at 60 FPS - - // Test with very large interval - texture.tick(30000); - assert_that(&texture.is_on()).is_true(); - assert_that(&texture.tick_timer()).is_equal_to(30000); - - texture.tick(30000); - assert_that(&texture.is_on()).is_false(); - assert_that(&texture.tick_timer()).is_equal_to(0); -} - -#[test] -fn test_blinking_texture_multiple_toggles_no_op() { - let tile = common::mock_atlas_tile(1); - let mut texture = BlinkingTexture::new(tile, 10); // 10 ticks - - // Test that multiple toggles work correctly (key feature) - // 2x ticks than the interval should do nothing at all, because toggling twice is a no-op - texture.tick(20); // 20 ticks = 2 complete intervals - assert_that(&texture.is_on()).is_true(); // Should still be true (2 toggles = no-op) - assert_that(&texture.tick_timer()).is_equal_to(0); - - // 3x ticks should toggle once (3 toggles = 1 toggle) - texture.tick(30); // 30 ticks = 3 complete intervals - assert_that(&texture.is_on()).is_false(); // Should be false (3 toggles = 1 toggle) - assert_that(&texture.tick_timer()).is_equal_to(0); - - // 4x ticks should do nothing (4 toggles = no-op) - texture.tick(40); // 40 ticks = 4 complete intervals - assert_that(&texture.is_on()).is_false(); // Should still be false (4 toggles = no-op) - assert_that(&texture.tick_timer()).is_equal_to(0); -}