cells differencied update

This commit is contained in:
Arkitu 2024-12-23 19:05:35 +01:00
parent e5b2e51a29
commit 18ca080175

View File

@ -7,7 +7,8 @@ pub struct Plugin;
impl bevy::prelude::Plugin for Plugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup)
.add_systems(Update, update)
.insert_resource(Time::<Fixed>::from_seconds(0.25)) // Time for a day
.add_systems(FixedUpdate, update_cells)
.insert_resource(ClearColor(Color::srgb(0., 0., 1.)));
}
}
@ -69,7 +70,7 @@ fn setup(
} else {
CellKind::Stone
};
cells_data.push(CellData::new(k, i, z as f32, m, 1., vec![]));
cells_data.push(CellData::new(k, i, (z*255.) as u8, (m*255.) as u8, 0, vec![]));
}
@ -95,7 +96,11 @@ fn setup(
indices.extend_from_slice(&[i as u32, (i+v) as u32, (i+v+1) as u32]);
cd.vertices.extend_from_slice(&[i, i+v, i+v+1]);
}
cmds.spawn(cd);
match cd.kind {
CellKind::Forest | CellKind::Grass => cmds.spawn((cd, LastUpdate(0))),
_ => cmds.spawn(cd)
};
}
let mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default())
@ -121,25 +126,36 @@ fn setup(
));
}
fn update(
mut cells: Query<&mut CellData>,
fn update_cells(
mut cells: Query<(&mut CellData, &mut LastUpdate)>,
mut map: Query<(&Mesh2d, &mut MapColors), With<MapMarker>>,
mut meshes: ResMut<Assets<Mesh>>
) {
let (mesh, mut cols) = map.single_mut();
if let Some(mesh) = meshes.get_mut(mesh) {
// let cols = mesh.attribute_mut(Mesh::ATTRIBUTE_COLOR).unwrap();
for mut cd in cells.iter_mut() {
// info!(cd.resource);
cd.update();
// dbg!(cd.resource);
let mut modified = false;
for (mut cd, mut lu) in cells.iter_mut() {
lu.0 += 1;
if lu.0 > match cd.kind {
CellKind::Void | CellKind::Sea | CellKind::Beach | CellKind::Dirt | CellKind::Stone => usize::MAX,
CellKind::Forest => 100*365/4, // Let's say that a forest takes 100 years to mature
CellKind::Grass => 7*7/4 // Let's say that grass takes 7 weaks to reach its max
} {
lu.0 = 0;
cd.resource = (cd.resource + 1).clamp(0, 4);
modified = true;
}
// cd.update();
let col = cd.color();
for id in cd.vertices.iter() {
cols.0[*id] = col.clone();
}
}
if modified {
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, cols.0.clone());
}
}
}
@ -154,17 +170,20 @@ pub enum CellKind {
Grass
}
#[derive(Component)]
pub struct LastUpdate(usize);
#[derive(Debug, Component)]
pub struct CellData {
pub kind: CellKind,
pub cid: usize,
z: f32,
pub moisture: f32,
pub resource: f32, // How much resource there is (between 0 and 1)
z: u8,
pub moisture: u8,
pub resource: u8, // How much resource there is (between 0 and 4)
pub vertices: Vec<usize>
}
impl CellData {
pub fn new(kind: CellKind, cell: usize, z: f32, moisture: f32, resource: f32, vertices: Vec<usize>) -> Self {
pub fn new(kind: CellKind, cell: usize, z: u8, moisture: u8, resource: u8, vertices: Vec<usize>) -> Self {
Self {
kind,
cid: cell,
@ -184,40 +203,40 @@ impl CellData {
CellKind::Void => [0.; 4],
CellKind::Sea => [0., 0., 1., 1.],
CellKind::Beach => [0.82, 0.84, 0.51, 1.],
CellKind::Forest => [0., 0.5 - (self.resource*0.4), 0., 1.],
CellKind::Dirt => [0.53 - (self.resource*0.4), 0.38-(self.resource*0.4), 0.29-(self.resource*0.4), 1.],
CellKind::Forest => [0., 0.5 - (self.resource as f32/4.*0.4), 0., 1.],
CellKind::Dirt => [0.53 - (self.resource as f32/4.*0.4), 0.38-(self.resource as f32/4.*0.4), 0.29-(self.resource as f32/4.*0.4), 1.],
CellKind::Stone => [0.5, 0.5, 0.5, 1.],
CellKind::Grass => [(136./255.) - (self.resource*0.4), (204./255.) - (self.resource*0.4), (59./255.) - (self.resource*0.4), 1.]
}
}
pub fn update(&mut self) {
// How much it get by day
let recuperation_rate = match self.kind {
CellKind::Void | CellKind::Sea | CellKind::Beach | CellKind::Dirt | CellKind::Stone => 0.,
CellKind::Forest => 1. / (100. * 365.25), // Let's say that a forest takes 100 years to mature
CellKind::Grass => 1. / (7. * 7.) // Let's say that grass takes 7 weaks to reach its max
};
self.resource = (self.resource + recuperation_rate).clamp(0., 1.);
}
pub fn set_resource(&mut self, val: f32, t: usize) {
self.resource = val.clamp(0., 1.);
if self.resource == 0. {
match self.kind {
CellKind::Forest => {
self.kind = CellKind::Grass;
self.resource = 1.;
},
CellKind::Grass => {
self.kind = CellKind::Dirt;
self.resource = 0.5;
},
CellKind::Beach => {
self.kind = CellKind::Sea;
},
_ => {}
}
CellKind::Grass => [(136./255.) - (self.resource as f32/4.*0.4), (204./255.) - (self.resource as f32/4.*0.4), (59./255.) - (self.resource as f32/4.*0.4), 1.]
}
}
// pub fn update(&mut self) {
// // How much it get by day
// let recuperation_rate = match self.kind {
// CellKind::Void | CellKind::Sea | CellKind::Beach | CellKind::Dirt | CellKind::Stone => 0.,
// CellKind::Forest => 1. / (100. * 365.25), // Let's say that a forest takes 100 years to mature
// CellKind::Grass => 1. / (7. * 7.) // Let's say that grass takes 7 weaks to reach its max
// };
// self.resource = (self.resource + recuperation_rate).clamp(0., 1.);
// }
// pub fn set_resource(&mut self, val: f32, t: usize) {
// self.resource = val.clamp(0., 1.);
// if self.resource == 0. {
// match self.kind {
// CellKind::Forest => {
// self.kind = CellKind::Grass;
// self.resource = 1.;
// },
// CellKind::Grass => {
// self.kind = CellKind::Dirt;
// self.resource = 0.5;
// },
// CellKind::Beach => {
// self.kind = CellKind::Sea;
// },
// _ => {}
// }
// }
// }
}
// pub struct Map {