diff --git a/src/camera.rs b/src/camera.rs index 8bd5bfe..f9521d5 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,9 +1,10 @@ -use bevy::prelude::*; +use bevy::{prelude::*, window::PrimaryWindow}; pub struct Plugin; impl bevy::prelude::Plugin for Plugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, setup); + app.add_systems(Startup, setup) + .add_systems(Update, move_cam); } } @@ -13,4 +14,22 @@ fn setup(mut cmds: Commands, window: Query<&Window>) { Camera2d, Transform::from_scale(Vec3::new(zoom, zoom, 1.)) )); +} + +fn move_cam( + mut cam: Query<&mut Transform, With>, + mouse_buttons: Res>, + mut mouse_events: EventReader, + window: Query<&Window, With> +) { + let window = window.single(); + if mouse_buttons.pressed(MouseButton::Left) && !mouse_buttons.just_pressed(MouseButton::Left) { + for ev in mouse_events.read() { + if let Some(delta) = ev.delta { + let mut cam = cam.single_mut(); + cam.translation.x -= delta.x/window.width(); + cam.translation.y += delta.y/window.height(); + } + } + } } \ No newline at end of file diff --git a/src/map.rs b/src/map.rs index 1bb83e4..96515eb 100644 --- a/src/map.rs +++ b/src/map.rs @@ -3,6 +3,9 @@ use noise::{Fbm, MultiFractal, NoiseFn, Perlin}; use rand::{Rng, SeedableRng}; use voronoice::{BoundingBox, Point, VoronoiBuilder}; +mod cells; +use cells::*; + pub struct Plugin; impl bevy::prelude::Plugin for Plugin { fn build(&self, app: &mut App) { @@ -126,6 +129,9 @@ fn setup( )); } +#[derive(Component)] +pub struct LastUpdate(usize); + fn update_cells( mut cells: Query<(&mut CellData, &mut LastUpdate)>, mut map: Query<(&Mesh2d, &mut MapColors), With>, @@ -156,143 +162,4 @@ fn update_cells( mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, cols.0.clone()); } } -} - - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum CellKind { - Void, - Sea, - Beach, - Forest, - Dirt, - Stone, - Grass -} - -#[derive(Component)] -pub struct LastUpdate(usize); - -#[derive(Debug, Component)] -pub struct CellData { - pub kind: CellKind, - pub cid: usize, - z: u8, - pub moisture: u8, - pub resource: u8, // How much resource there is (between 0 and 4) - pub vertices: Vec -} -impl CellData { - pub fn new(kind: CellKind, cell: usize, z: u8, moisture: u8, resource: u8, vertices: Vec) -> Self { - Self { - kind, - cid: cell, - z, - moisture, - resource, - vertices - } - } - // pub fn pos<'a>(&self, map: &'a Map) -> &'a Point { - // &map.voronoi.sites()[self.cid] - // } - pub fn color(&self) -> [f32; 4] { - // let mut rng = thread_rng(); - // [rng.gen(), rng.gen(), rng.gen(), 1.] - match self.kind { - 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 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 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 { -// pub voronoi: Voronoi, -// pub cells_data: Vec, -// pub seed: u32 -// } -// impl Map { -// pub const HEIGHT: f32 = 2.; -// pub const WIDTH: f32 = 2.; -// pub const REAL_HEIGHT: f32 = 500.; -// pub const REAL_WIDTH: f32 = 500.; -// pub const CELL_AREA: f32 = Self::REAL_HEIGHT * Self::REAL_WIDTH / Self::SIZE as f32; -// pub const SIZE: usize = 10000; -// pub fn new(seed: u32, t: usize) -> Self { -// let mut rng = rand::rngs::SmallRng::seed_from_u64(seed as u64); -// let mut sites = Vec::with_capacity(Self::SIZE); -// for _ in 0..Self::SIZE { -// sites.push(Point { x:rng.gen_range(-Self::WIDTH/2.0..Self::WIDTH/2.0) as f64, y:rng.gen_range(-Self::HEIGHT/2.0..Self::HEIGHT/2.0) as f64 }) -// } -// let voronoi = VoronoiBuilder::default() -// .set_sites(sites) -// .set_bounding_box(BoundingBox::new_centered(Self::WIDTH as f64, Self::HEIGHT as f64)) -// .set_lloyd_relaxation_iterations(3) -// .build() -// .unwrap(); -// let mut cells_data = Vec::with_capacity(Self::SIZE); -// let z_noise = Fbm::::new(seed); -// let moisture_noise = Fbm::::new(seed+1) -// .set_frequency(2.); -// for i in 0..Self::SIZE { -// let c = voronoi.cell(i); -// let site = c.site_position(); -// let z = ( -// 0.3 // Arbitrary value -// + ((z_noise.get([site.x, site.y])+1.)/2.) // Noise + [0; 1] -// - ((site.x.powi(2)+site.y.powi(2)).sqrt()*0.5) // Distance - [0; sqrt(2)] * 0.5 -// ).clamp(0., 1.); -// let m = ( -// (moisture_noise.get([site.x, site.y])+1.)/2. // Noise + [0; 1] -// ).clamp(0., 1.) as f32; -// let k = if z <= 0.5 { -// CellKind::Sea -// } else if z <= 0.52 { -// CellKind::Beach -// } else if z < 0.8 { -// CellKind::Dirt -// } else { -// CellKind::Stone -// }; -// cells_data.push(CellData::new(k, i, z as f32, m, 1., t)); -// } -// Self { -// voronoi, -// cells_data, -// seed -// } -// } -// } \ No newline at end of file +} \ No newline at end of file diff --git a/src/map/cells.rs b/src/map/cells.rs new file mode 100644 index 0000000..7217d2a --- /dev/null +++ b/src/map/cells.rs @@ -0,0 +1,45 @@ +use bevy::prelude::*; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CellKind { + Void, + Sea, + Beach, + Forest, + Dirt, + Stone, + Grass +} + +#[derive(Debug, Component)] +pub struct CellData { + pub kind: CellKind, + pub cid: usize, + z: u8, + pub moisture: u8, + pub resource: u8, // How much resource there is (between 0 and 4) + pub vertices: Vec +} +impl CellData { + pub fn new(kind: CellKind, cell: usize, z: u8, moisture: u8, resource: u8, vertices: Vec) -> Self { + Self { + kind, + cid: cell, + z, + moisture, + resource, + vertices + } + } + pub fn color(&self) -> [f32; 4] { + match self.kind { + 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 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 as f32/4.*0.4), (204./255.) - (self.resource as f32/4.*0.4), (59./255.) - (self.resource as f32/4.*0.4), 1.] + } + } +} \ No newline at end of file