mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-17 00:12:36 -06:00
refactor: add ticks to DeltaTime, rewrite Blinking system for tick-based calculations with absolute calculations, rewrite Blinking/Direction tests
This commit is contained in:
@@ -4,25 +4,42 @@ use crate::texture::sprite::AtlasTile;
|
||||
#[derive(Clone)]
|
||||
pub struct BlinkingTexture {
|
||||
tile: AtlasTile,
|
||||
blink_duration: f32,
|
||||
time_bank: f32,
|
||||
interval_ticks: u32,
|
||||
tick_timer: u32,
|
||||
is_on: bool,
|
||||
}
|
||||
|
||||
impl BlinkingTexture {
|
||||
pub fn new(tile: AtlasTile, blink_duration: f32) -> Self {
|
||||
pub fn new(tile: AtlasTile, interval_ticks: u32) -> Self {
|
||||
Self {
|
||||
tile,
|
||||
blink_duration,
|
||||
time_bank: 0.0,
|
||||
interval_ticks,
|
||||
tick_timer: 0,
|
||||
is_on: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tick(&mut self, dt: f32) {
|
||||
self.time_bank += dt;
|
||||
if self.time_bank >= self.blink_duration {
|
||||
self.time_bank -= self.blink_duration;
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -36,11 +53,11 @@ impl BlinkingTexture {
|
||||
}
|
||||
|
||||
// Helper methods for testing
|
||||
pub fn time_bank(&self) -> f32 {
|
||||
self.time_bank
|
||||
pub fn tick_timer(&self) -> u32 {
|
||||
self.tick_timer
|
||||
}
|
||||
|
||||
pub fn blink_duration(&self) -> f32 {
|
||||
self.blink_duration
|
||||
pub fn interval_ticks(&self) -> u32 {
|
||||
self.interval_ticks
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user