From 82a48e8f231dd8f9b369fd5f363e88959526aed0 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Wed, 31 Jan 2024 13:02:00 +0000 Subject: [PATCH] Extract rule stuffs to module --- src/falling_sand/mod.rs | 75 ++------------------------------------ src/falling_sand/rules.rs | 76 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 72 deletions(-) create mode 100644 src/falling_sand/rules.rs diff --git a/src/falling_sand/mod.rs b/src/falling_sand/mod.rs index 832f3a0..106c4a0 100644 --- a/src/falling_sand/mod.rs +++ b/src/falling_sand/mod.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +mod rules; use bevy::{ app::{Plugin, Startup}, @@ -17,6 +17,8 @@ use rand::Rng; use crate::util::DirtyRect; +use self::rules::{to_rule_state, FallingSandRules}; + pub struct FallingSandPlugin; impl Plugin for FallingSandPlugin { @@ -239,74 +241,3 @@ impl From for Element { } } } - -fn gen_rule( - input: (Element, Element, Element, Element), - output: (Element, Element, Element, Element), -) -> (u32, u32) { - (to_rule_state(input), to_rule_state(output)) -} - -fn to_rule_state(input: (Element, Element, Element, Element)) -> u32 { - ((input.0 as u32) << 24) + ((input.1 as u32) << 16) + ((input.2 as u32) << 8) + input.3 as u32 -} - -#[derive(Resource)] -pub struct FallingSandRules { - rules: HashMap, -} - -impl Default for FallingSandRules { - fn default() -> Self { - // Pre-computed air-sand rules - Self { - rules: HashMap::from([ - gen_rule( - (Element::Air, Element::Sand, Element::Air, Element::Air), - (Element::Air, Element::Air, Element::Air, Element::Sand), - ), - gen_rule( - (Element::Air, Element::Sand, Element::Air, Element::Sand), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Air, Element::Sand, Element::Sand, Element::Air), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Air, Element::Air, Element::Air), - (Element::Air, Element::Air, Element::Sand, Element::Air), - ), - gen_rule( - (Element::Sand, Element::Air, Element::Air, Element::Sand), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Air, Element::Sand, Element::Air), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Sand, Element::Air, Element::Air), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Sand, Element::Air, Element::Sand), - (Element::Air, Element::Sand, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Sand, Element::Sand, Element::Air), - (Element::Sand, Element::Air, Element::Sand, Element::Sand), - ), - ]), - } - } -} - -impl FallingSandRules { - pub fn get_result(&self, input_rule: u32) -> u32 { - match self.rules.get(&input_rule) { - Some(&result) => result, - None => input_rule, - } - } -} diff --git a/src/falling_sand/rules.rs b/src/falling_sand/rules.rs new file mode 100644 index 0000000..af7c841 --- /dev/null +++ b/src/falling_sand/rules.rs @@ -0,0 +1,76 @@ +use std::collections::HashMap; + +use bevy::ecs::system::Resource; + +use super::Element; + +#[derive(Resource)] +pub struct FallingSandRules { + rules: HashMap, +} + +impl Default for FallingSandRules { + fn default() -> Self { + // Pre-computed air-sand rules + Self { + rules: HashMap::from([ + gen_rule( + (Element::Air, Element::Sand, Element::Air, Element::Air), + (Element::Air, Element::Air, Element::Air, Element::Sand), + ), + gen_rule( + (Element::Air, Element::Sand, Element::Air, Element::Sand), + (Element::Air, Element::Air, Element::Sand, Element::Sand), + ), + gen_rule( + (Element::Air, Element::Sand, Element::Sand, Element::Air), + (Element::Air, Element::Air, Element::Sand, Element::Sand), + ), + gen_rule( + (Element::Sand, Element::Air, Element::Air, Element::Air), + (Element::Air, Element::Air, Element::Sand, Element::Air), + ), + gen_rule( + (Element::Sand, Element::Air, Element::Air, Element::Sand), + (Element::Air, Element::Air, Element::Sand, Element::Sand), + ), + gen_rule( + (Element::Sand, Element::Air, Element::Sand, Element::Air), + (Element::Air, Element::Air, Element::Sand, Element::Sand), + ), + gen_rule( + (Element::Sand, Element::Sand, Element::Air, Element::Air), + (Element::Air, Element::Air, Element::Sand, Element::Sand), + ), + gen_rule( + (Element::Sand, Element::Sand, Element::Air, Element::Sand), + (Element::Air, Element::Sand, Element::Sand, Element::Sand), + ), + gen_rule( + (Element::Sand, Element::Sand, Element::Sand, Element::Air), + (Element::Sand, Element::Air, Element::Sand, Element::Sand), + ), + ]), + } + } +} + +impl FallingSandRules { + pub fn get_result(&self, input_rule: u32) -> u32 { + match self.rules.get(&input_rule) { + Some(&result) => result, + None => input_rule, + } + } +} + +pub fn to_rule_state(input: (Element, Element, Element, Element)) -> u32 { + ((input.0 as u32) << 24) + ((input.1 as u32) << 16) + ((input.2 as u32) << 8) + input.3 as u32 +} + +fn gen_rule( + input: (Element, Element, Element, Element), + output: (Element, Element, Element, Element), +) -> (u32, u32) { + (to_rule_state(input), to_rule_state(output)) +}