feat: gas simulation

feat/simulation
hheik 2022-12-26 00:29:55 +02:00
parent 4a4d40e0fa
commit 67512ff926
2 changed files with 40 additions and 3 deletions

View File

@ -20,7 +20,7 @@ struct TerrainBrush2D {
impl Default for TerrainBrush2D {
fn default() -> Self {
TerrainBrush2D { radius: 7, tile: 3 }
TerrainBrush2D { radius: 5, tile: 4 }
}
}
@ -35,7 +35,8 @@ fn debug_painter(
mut mouse_wheel: EventReader<MouseWheel>,
camera_query: Query<(&Camera, &GlobalTransform), With<GameCamera>>,
) {
let allow_painting = key_input.pressed(KeyCode::LControl);
// let allow_painting = key_input.pressed(KeyCode::LControl);
let allow_painting = true;
// Change brush
for event in mouse_wheel.iter() {

View File

@ -77,7 +77,7 @@ fn terrain_simulation(mut terrain: ResMut<Terrain2D>, frame_counter: Res<FrameCo
.clone();
for chunk_index in indices.iter() {
// Mark few chunks dirty in interval. Should help activate stale chunks
// Mark few chunks dirty in interval. Should help activate stale chunks
if let Some(chunk) = terrain.index_to_chunk_mut(&chunk_index) {
let interval = 1;
if frame_counter.frame % interval == 0 {
@ -124,6 +124,7 @@ fn terrain_simulation(mut terrain: ResMut<Terrain2D>, frame_counter: Res<FrameCo
continue;
};
// TODO: generalise "check for empty space and move" behaviour
match tb.form {
TexelForm::Liquid => {
// Check if there is space below
@ -160,6 +161,41 @@ fn terrain_simulation(mut terrain: ResMut<Terrain2D>, frame_counter: Res<FrameCo
};
}
}
TexelForm::Gas => {
// Check if there is space above
{
let above_pos = global + Vector2I::UP;
if terrain
.get_texel(&above_pos)
.map_or(true, |texel| TexelBehaviour2D::is_empty(&texel.id))
{
let above_id =
terrain.get_texel(&above_pos).map_or(0, |texel| texel.id);
terrain.set_texel(&above_pos, texel.id, Some(simulation_frame));
terrain.set_texel(&global, above_id, Some(simulation_frame));
continue;
}
}
// Check if there is space to the side
let mut dirs = vec![Vector2I::RIGHT, Vector2I::LEFT];
if ((frame_counter.frame / 73) % 2) as i32 == global.y % 2 {
dirs.reverse();
}
for dir in dirs.iter() {
let side_pos = global + *dir;
if terrain
.get_texel(&side_pos)
.map_or(true, |texel| TexelBehaviour2D::is_empty(&texel.id))
{
let side_id =
terrain.get_texel(&side_pos).map_or(0, |texel| texel.id);
terrain.set_texel(&side_pos, texel.id, Some(simulation_frame));
terrain.set_texel(&global, side_id, Some(simulation_frame));
continue 'texel_loop;
};
}
}
_ => (),
}
}