propagate forest

This commit is contained in:
Arkitu 2024-08-26 13:04:16 +02:00
parent 26c2b2970f
commit b0dcc83519

View File

@ -44,7 +44,7 @@ impl CellData {
CellKind::Sea => [0., 0., 1., 1.], CellKind::Sea => [0., 0., 1., 1.],
CellKind::Plain => [0., 1., 0., 1.], CellKind::Plain => [0., 1., 0., 1.],
CellKind::Beach => [0.82, 0.84, 0.51, 1.], CellKind::Beach => [0.82, 0.84, 0.51, 1.],
CellKind::Forest => [0., 0.5, 0., 1.], CellKind::Forest => [0., 0.5 - (self.moisture*0.4), 0., 1.],
CellKind::Dirt => [0.53 - (self.moisture*0.4), 0.38-(self.moisture*0.4), 0.29-(self.moisture*0.4), 1.], CellKind::Dirt => [0.53 - (self.moisture*0.4), 0.38-(self.moisture*0.4), 0.29-(self.moisture*0.4), 1.],
CellKind::Stone => [0.5, 0.5, 0.5, 1.] CellKind::Stone => [0.5, 0.5, 0.5, 1.]
} }
@ -152,7 +152,6 @@ impl State {
} }
}, },
Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, ..} => { Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, ..} => {
dbg!(&position);
let w_size = window.inner_size(); let w_size = window.inner_size();
let pos = Point { let pos = Point {
x: ((2.*position.x/(w_size.width as f64))-1.)*(self.uniforms.camera[2] as f64), x: ((2.*position.x/(w_size.width as f64))-1.)*(self.uniforms.camera[2] as f64),
@ -162,9 +161,9 @@ impl State {
if self.mouse_pressed { if self.mouse_pressed {
for i in c.iter_path(pos) { for i in c.iter_path(pos) {
self.selected_tile = i; self.selected_tile = i;
let c = &mut self.map.cells_data[self.selected_tile]; let cd = &mut self.map.cells_data[self.selected_tile];
if let CellKind::Dirt = c.kind { if let CellKind::Dirt = cd.kind {
c.kind = CellKind::Forest; cd.kind = CellKind::Forest;
} }
} }
} else { } else {
@ -184,8 +183,8 @@ impl State {
self.vertices = Vec::new(); self.vertices = Vec::new();
self.indices = Vec::new(); self.indices = Vec::new();
for (c, data) in self.map.voronoi.iter_cells().zip(self.map.cells_data.iter()) { for (c, cd) in self.map.voronoi.iter_cells().zip(self.map.cells_data.iter()) {
let mut color = data.color(); let mut color = cd.color();
if c.site() == self.selected_tile { if c.site() == self.selected_tile {
color[0] = (color[0]+0.4).clamp(0., 1.); color[0] = (color[0]+0.4).clamp(0., 1.);
color[1] = (color[1]+0.4).clamp(0., 1.); color[1] = (color[1]+0.4).clamp(0., 1.);
@ -194,7 +193,7 @@ impl State {
let vs = c.iter_vertices().collect::<Vec<_>>(); let vs = c.iter_vertices().collect::<Vec<_>>();
let i = self.vertices.len() as u32; let i = self.vertices.len() as u32;
for v in vs.iter() { for v in vs.iter() {
self.vertices.push(Vertex { pos: [v.x as f32, v.y as f32, data.z], color }); self.vertices.push(Vertex { pos: [v.x as f32, v.y as f32, cd.z], color });
} }
for v in 1..(vs.len()-1) as u32 { for v in 1..(vs.len()-1) as u32 {
self.indices.push(i); self.indices.push(i);
@ -209,7 +208,24 @@ impl State {
} }
} }
pub fn update(&mut self) { pub fn update(&mut self) {
let mut rng = thread_rng();
let mut new_forest = Vec::new();
for cd in self.map.cells_data.iter() {
if let CellKind::Forest = cd.kind {
if rng.gen::<f32>() < (0.03*cd.moisture) {
let c = self.map.voronoi.cell(cd.cell);
let n = c.iter_neighbors().choose(&mut rng).unwrap();
new_forest.push(n);
}
}
}
for i in new_forest {
let cd = &mut self.map.cells_data[i];
if let CellKind::Dirt = cd.kind {
cd.kind = CellKind::Forest;
}
}
self.frame += 1; self.frame += 1;
} }
} }