-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2d_cyclic_colors.rs
64 lines (58 loc) · 1.91 KB
/
2d_cyclic_colors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use bevy::prelude::*;
use bevy_life::{CyclicColorCellState, CyclicColors2dPlugin, MooreCell2d, SimulationBatch};
use rand::Rng;
const N: usize = 9;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Cyclic Colors".to_string(),
resolution: [1200.0, 800.0].into(),
..default()
}),
..default()
}))
.add_plugins(CyclicColors2dPlugin::<N>::new().with_time_step(0.05))
.insert_resource(SimulationBatch)
.add_systems(Startup, (setup_camera, setup_map))
.run();
}
fn setup_camera(mut commands: Commands) {
// Camera
commands.spawn(Camera2d::default());
}
fn setup_map(mut commands: Commands) {
spawn_map(&mut commands);
}
fn spawn_map(commands: &mut Commands) {
let mut rng = rand::thread_rng();
let (size_x, size_y) = (300, 200);
let sprite_size = 4.;
commands
.spawn((
Transform::from_xyz(
-(size_x as f32 * sprite_size) / 2.,
-(size_y as f32 * sprite_size) / 2.,
0.,
),
Visibility::default(),
))
.with_children(|builder| {
for y in 0..=size_y {
for x in 0..=size_x {
let color_index = rng.gen_range(0..N);
let state = CyclicColorCellState::<N>(color_index);
builder.spawn((
Sprite {
custom_size: Some(Vec2::splat(sprite_size)),
..default()
},
Transform::from_xyz(sprite_size * x as f32, sprite_size * y as f32, 0.),
MooreCell2d::new(IVec2::new(x, y)),
state,
));
}
}
});
println!("map generated");
}