Created
August 25, 2020 15:04
-
-
Save Telzhaak/9cc256b3c72f547c6587d992a170bcb5 to your computer and use it in GitHub Desktop.
Color changing Rects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::prelude::*; | |
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, PrintDiagnosticsPlugin}; | |
use rand::Rng; | |
struct Fluxel2D { | |
width: u32, | |
height: u32, | |
} | |
impl Plugin for Fluxel2D { | |
fn build(&self, app: &mut AppBuilder) | |
{ | |
app.add_startup_system(create_fluxel_field.system()) | |
.add_system(tick_fluxels.system()); | |
} | |
} | |
fn create_fluxel_field(mut commands: Commands, | |
mut materials: ResMut<Assets<ColorMaterial>>, | |
) { | |
commands.spawn(Camera2dComponents::default()); | |
let start = -(32f32 * 16f32); | |
for idx in 0..=32 { | |
for idy in 0..=32 { | |
commands.spawn(SpriteComponents { | |
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()), | |
translation: Translation(Vec3::new(start + idx as f32 * 32.0, start + idy as f32 * 32.0, 0.0)), | |
sprite: Sprite { | |
size: Vec2::new(32.0, 32.0), | |
}, | |
..Default::default() | |
}).with(Fluxel{idx, idy}); | |
} | |
} | |
} | |
struct Fluxel { | |
idx: u32, | |
idy: u32, | |
} | |
struct FluidMaterials { | |
low: Handle<ColorMaterial>, | |
mid: Handle<ColorMaterial>, | |
high: Handle<ColorMaterial>, | |
} | |
impl FromResources for FluidMaterials { | |
fn from_resources(resources: &Resources) -> Self { | |
let mut materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap(); | |
FluidMaterials { | |
low: materials.add(Color::rgb(0.0, 0.0, 1.0).into()), | |
mid: materials.add(Color::rgb(0.5, 0.0, 0.5).into()), | |
high: materials.add(Color::rgb(1.0, 0.0, 0.0).into()), | |
} | |
} | |
} | |
// mut fluxel: Mut<Fluxel>, | |
fn tick_fluxels( | |
fluid_materials: Res<FluidMaterials>, | |
mut mat_handle: Mut<Handle<ColorMaterial>> | |
) { | |
let mut rng = rand::thread_rng(); | |
let fluid_amount = rng.gen_range(0.0, 1.0); | |
let color = match fluid_amount { | |
f if f < 0.33 => fluid_materials.low, | |
f if f >= 0.33 && f < 0.66 => fluid_materials.mid, | |
_ => fluid_materials.high, | |
}; | |
*mat_handle = color; | |
println!("Yahaha"); | |
} | |
fn main() { | |
App::build() | |
.add_default_plugins() | |
.init_resource::<FluidMaterials>() | |
// .add_plugin(FrameTimeDiagnosticsPlugin::default()) | |
// .add_plugin(PrintDiagnosticsPlugin::default()) | |
.add_plugin(Fluxel2D{width:32, height:32}) | |
.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment