Extract rule stuffs to module

This commit is contained in:
Jarrod Doyle 2024-01-31 13:02:00 +00:00
parent af6fdb085b
commit 82a48e8f23
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 79 additions and 72 deletions

View File

@ -1,4 +1,4 @@
use std::collections::HashMap; mod rules;
use bevy::{ use bevy::{
app::{Plugin, Startup}, app::{Plugin, Startup},
@ -17,6 +17,8 @@ use rand::Rng;
use crate::util::DirtyRect; use crate::util::DirtyRect;
use self::rules::{to_rule_state, FallingSandRules};
pub struct FallingSandPlugin; pub struct FallingSandPlugin;
impl Plugin for FallingSandPlugin { impl Plugin for FallingSandPlugin {
@ -239,74 +241,3 @@ impl From<u32> 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<u32, u32>,
}
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,
}
}
}

76
src/falling_sand/rules.rs Normal file
View File

@ -0,0 +1,76 @@
use std::collections::HashMap;
use bevy::ecs::system::Resource;
use super::Element;
#[derive(Resource)]
pub struct FallingSandRules {
rules: HashMap<u32, u32>,
}
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))
}