Support multiple non-interacting chunk entities
This commit is contained in:
parent
c31ddd9bea
commit
b1fd987955
|
@ -82,7 +82,7 @@ impl Chunk {
|
||||||
dirty_rect: DirtyRect::default(),
|
dirty_rect: DirtyRect::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let max_y = height / 10;
|
let max_y = height / rand::thread_rng().gen_range(2..10);
|
||||||
for y in 0..=max_y {
|
for y in 0..=max_y {
|
||||||
for x in 0..width {
|
for x in 0..width {
|
||||||
initial.set_cell(x, y, Element::Water);
|
initial.set_cell(x, y, Element::Water);
|
||||||
|
@ -121,73 +121,62 @@ impl Chunk {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn place_sand_system(mut chunk: Query<&mut Chunk>) {
|
pub fn place_sand_system(mut query: Query<&mut Chunk>) {
|
||||||
// We know for now there's only one chunk
|
for mut chunk in &mut query {
|
||||||
let chunk = chunk.get_single_mut();
|
let frac = chunk.width / 2;
|
||||||
if chunk.is_err() {
|
let x = (chunk.width - frac) / 2 + rand::thread_rng().gen_range(0..frac);
|
||||||
return;
|
let y = chunk.height - 1;
|
||||||
|
chunk.set_cell(x, y, Element::Sand);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut chunk = chunk.unwrap();
|
|
||||||
let frac = chunk.width / 2;
|
|
||||||
let x = (chunk.width - frac) / 2 + rand::thread_rng().gen_range(0..frac);
|
|
||||||
let y = chunk.height - 1;
|
|
||||||
chunk.set_cell(x, y, Element::Sand);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut chunk: Query<&mut Chunk>) {
|
pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut query: Query<&mut Chunk>) {
|
||||||
// We know for now there's only one chunk
|
for mut chunk in &mut query {
|
||||||
let chunk = chunk.get_single_mut();
|
// Determine which Margolus neighbourhood offset we're using this update
|
||||||
if chunk.is_err() {
|
let offset = if chunk.step == 0 {
|
||||||
return;
|
(0, 0)
|
||||||
}
|
} else if chunk.step == 1 {
|
||||||
|
(1, 1)
|
||||||
|
} else if chunk.step == 2 {
|
||||||
|
(0, 1)
|
||||||
|
} else {
|
||||||
|
(1, 0)
|
||||||
|
};
|
||||||
|
chunk.step = (chunk.step + 1) % 4;
|
||||||
|
|
||||||
let mut chunk = chunk.unwrap();
|
// We're operating on 2x2 blocks of cells
|
||||||
|
for block_y in 0..(chunk.height / 2) {
|
||||||
|
let y = block_y * 2 + offset.1;
|
||||||
|
for block_x in 0..(chunk.width / 2) {
|
||||||
|
let x = block_x * 2 + offset.0;
|
||||||
|
|
||||||
// Determine which Margolus neighbourhood offset we're using this update
|
// Get all the cells in our block and convert them to a rule state for lookup
|
||||||
let offset = if chunk.step == 0 {
|
// Because our offset can cause cell look-ups to go ourside of the grid we have
|
||||||
(0, 0)
|
// a default `Element::None`
|
||||||
} else if chunk.step == 1 {
|
// Cells are obtained in the order top-left, top-right, bottom-left, bottom-right
|
||||||
(1, 1)
|
let start_state = to_rule_state((
|
||||||
} else if chunk.step == 2 {
|
chunk.get_cell(x, y + 1).unwrap_or(Element::None),
|
||||||
(0, 1)
|
chunk.get_cell(x + 1, y + 1).unwrap_or(Element::None),
|
||||||
} else {
|
chunk.get_cell(x, y).unwrap_or(Element::None),
|
||||||
(1, 0)
|
chunk.get_cell(x + 1, y).unwrap_or(Element::None),
|
||||||
};
|
));
|
||||||
chunk.step = (chunk.step + 1) % 4;
|
let end_state = rules.get_result(start_state);
|
||||||
|
|
||||||
// We're operating on 2x2 blocks of cells
|
// We only need to actually update things if the state changed
|
||||||
for block_y in 0..(chunk.height / 2) {
|
// Same ordering as above.
|
||||||
let y = block_y * 2 + offset.1;
|
if start_state != end_state {
|
||||||
for block_x in 0..(chunk.width / 2) {
|
if (start_state & 0xFF000000) != (end_state & 0xFF000000) {
|
||||||
let x = block_x * 2 + offset.0;
|
chunk.set_cell(x, y + 1, Element::from((end_state >> 24) & 0xFF));
|
||||||
|
}
|
||||||
// Get all the cells in our block and convert them to a rule state for lookup
|
if (start_state & 0x00FF0000) != (end_state & 0x00FF0000) {
|
||||||
// Because our offset can cause cell look-ups to go ourside of the grid we have
|
chunk.set_cell(x + 1, y + 1, Element::from((end_state >> 16) & 0xFF));
|
||||||
// a default `Element::None`
|
}
|
||||||
// Cells are obtained in the order top-left, top-right, bottom-left, bottom-right
|
if (start_state & 0x0000FF00) != (end_state & 0x0000FF00) {
|
||||||
let start_state = to_rule_state((
|
chunk.set_cell(x, y, Element::from((end_state >> 8) & 0xFF));
|
||||||
chunk.get_cell(x, y + 1).unwrap_or(Element::None),
|
}
|
||||||
chunk.get_cell(x + 1, y + 1).unwrap_or(Element::None),
|
if (start_state & 0x000000FF) != (end_state & 0x000000FF) {
|
||||||
chunk.get_cell(x, y).unwrap_or(Element::None),
|
chunk.set_cell(x + 1, y, Element::from(end_state & 0xFF));
|
||||||
chunk.get_cell(x + 1, y).unwrap_or(Element::None),
|
}
|
||||||
));
|
|
||||||
let end_state = rules.get_result(start_state);
|
|
||||||
|
|
||||||
// We only need to actually update things if the state changed
|
|
||||||
// Same ordering as above.
|
|
||||||
if start_state != end_state {
|
|
||||||
if (start_state & 0xFF000000) != (end_state & 0xFF000000) {
|
|
||||||
chunk.set_cell(x, y + 1, Element::from((end_state >> 24) & 0xFF));
|
|
||||||
}
|
|
||||||
if (start_state & 0x00FF0000) != (end_state & 0x00FF0000) {
|
|
||||||
chunk.set_cell(x + 1, y + 1, Element::from((end_state >> 16) & 0xFF));
|
|
||||||
}
|
|
||||||
if (start_state & 0x0000FF00) != (end_state & 0x0000FF00) {
|
|
||||||
chunk.set_cell(x, y, Element::from((end_state >> 8) & 0xFF));
|
|
||||||
}
|
|
||||||
if (start_state & 0x000000FF) != (end_state & 0x000000FF) {
|
|
||||||
chunk.set_cell(x + 1, y, Element::from(end_state & 0xFF));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,42 +185,37 @@ pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut chunk: Query<&mut
|
||||||
|
|
||||||
pub fn update_chunk_texture_system(
|
pub fn update_chunk_texture_system(
|
||||||
mut images: ResMut<Assets<Image>>,
|
mut images: ResMut<Assets<Image>>,
|
||||||
mut chunk: Query<(&mut Chunk, &Handle<Image>)>,
|
mut query: Query<(&mut Chunk, &Handle<Image>)>,
|
||||||
) {
|
) {
|
||||||
// We know for now there's only one chunk
|
for (mut chunk, image_handle) in &mut query {
|
||||||
let chunk = chunk.get_single_mut();
|
if !chunk.dirty_rect.is_dirty() {
|
||||||
if chunk.is_err() {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let (mut chunk, image_handle) = chunk.unwrap();
|
if let Some(image) = images.get_mut(image_handle) {
|
||||||
if !chunk.dirty_rect.is_dirty() {
|
for y in chunk.dirty_rect.range_y() {
|
||||||
return;
|
for x in chunk.dirty_rect.range_x() {
|
||||||
}
|
let mut colour = (0, 0, 0);
|
||||||
|
if let Some(element) = chunk.cells.get(x + y * chunk.width) {
|
||||||
if let Some(image) = images.get_mut(image_handle) {
|
match element {
|
||||||
for y in chunk.dirty_rect.range_y() {
|
Element::Air => colour = (25, 24, 26),
|
||||||
for x in chunk.dirty_rect.range_x() {
|
Element::Sand => colour = (255, 216, 102),
|
||||||
let mut colour = (0, 0, 0);
|
Element::Water => colour = (120, 220, 232),
|
||||||
if let Some(element) = chunk.cells.get(x + y * chunk.width) {
|
_ => {}
|
||||||
match element {
|
}
|
||||||
Element::Air => colour = (25, 24, 26),
|
|
||||||
Element::Sand => colour = (255, 216, 102),
|
|
||||||
Element::Water => colour = (120, 220, 232),
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let index = (x + y * chunk.width) * 4;
|
let index = (x + y * chunk.width) * 4;
|
||||||
image.data[index] = colour.0;
|
image.data[index] = colour.0;
|
||||||
image.data[index + 1] = colour.1;
|
image.data[index + 1] = colour.1;
|
||||||
image.data[index + 2] = colour.2;
|
image.data[index + 2] = colour.2;
|
||||||
image.data[index + 3] = 255;
|
image.data[index + 3] = 255;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
chunk.dirty_rect.reset();
|
chunk.dirty_rect.reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
|
Loading…
Reference in New Issue