Move DirtyRect to utility module

This commit is contained in:
Jarrod Doyle 2024-01-29 15:03:41 +00:00
parent 8cc95cce02
commit a3e8b1387b
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 53 additions and 51 deletions

View File

@ -13,6 +13,8 @@ use bevy::{
};
use rand::Rng;
use crate::util::DirtyRect;
pub struct FallingSandPlugin;
impl Plugin for FallingSandPlugin {
@ -58,57 +60,6 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
});
}
#[derive(Debug, Default)]
struct DirtyRect {
dirty: bool,
rect: (usize, usize, usize, usize),
}
impl DirtyRect {
fn new() -> Self {
Self {
dirty: false,
rect: (usize::MAX, usize::MAX, usize::MIN, usize::MIN),
}
}
fn range_x(&self) -> std::ops::RangeInclusive<usize> {
self.rect.0..=self.rect.2
}
fn range_y(&self) -> std::ops::RangeInclusive<usize> {
self.rect.1..=self.rect.3
}
fn reset(&mut self) {
self.dirty = false;
self.rect = (usize::MAX, usize::MAX, usize::MIN, usize::MIN);
}
fn add_point(&mut self, x: usize, y: usize) {
if x < self.rect.0 {
self.rect.0 = x;
self.dirty = true;
}
if x > self.rect.2 {
self.rect.2 = x;
self.dirty = true;
}
if y < self.rect.1 {
self.rect.1 = y;
self.dirty = true;
}
if y > self.rect.3 {
self.rect.3 = y;
self.dirty = true;
}
}
fn is_dirty(&self) -> bool {
self.dirty
}
}
#[derive(Component)]
pub struct Chunk {
width: usize,

View File

@ -1,4 +1,5 @@
mod falling_sand;
mod util;
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},

50
src/util.rs Normal file
View File

@ -0,0 +1,50 @@
#[derive(Debug, Default)]
pub struct DirtyRect {
dirty: bool,
rect: (usize, usize, usize, usize),
}
impl DirtyRect {
pub fn new() -> Self {
Self {
dirty: false,
rect: (usize::MAX, usize::MAX, usize::MIN, usize::MIN),
}
}
pub fn range_x(&self) -> std::ops::RangeInclusive<usize> {
self.rect.0..=self.rect.2
}
pub fn range_y(&self) -> std::ops::RangeInclusive<usize> {
self.rect.1..=self.rect.3
}
pub fn reset(&mut self) {
self.dirty = false;
self.rect = (usize::MAX, usize::MAX, usize::MIN, usize::MIN);
}
pub fn add_point(&mut self, x: usize, y: usize) {
if x < self.rect.0 {
self.rect.0 = x;
self.dirty = true;
}
if x > self.rect.2 {
self.rect.2 = x;
self.dirty = true;
}
if y < self.rect.1 {
self.rect.1 = y;
self.dirty = true;
}
if y > self.rect.3 {
self.rect.3 = y;
self.dirty = true;
}
}
pub fn is_dirty(&self) -> bool {
self.dirty
}
}