diff --git a/Cargo.lock b/Cargo.lock index 9b4a21f..03b9d73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1793,6 +1793,7 @@ name = "dorf" version = "0.1.0" dependencies = [ "bevy", + "noise", "rand", ] @@ -2718,6 +2719,17 @@ dependencies = [ "libc", ] +[[package]] +name = "noise" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6da45c8333f2e152fc665d78a380be060eb84fad8ca4c9f7ac8ca29216cff0cc" +dependencies = [ + "num-traits", + "rand", + "rand_xorshift", +] + [[package]] name = "nom" version = "7.1.3" @@ -3307,6 +3319,15 @@ dependencies = [ "rand", ] +[[package]] +name = "rand_xorshift" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" +dependencies = [ + "rand_core", +] + [[package]] name = "range-alloc" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index 5368afc..9ae15c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] bevy = "0.15.0" +noise = "0.9.0" rand = "0.8.5" # Enable max optimizations for dependencies, but not for our code: diff --git a/assets/sky.png b/assets/sky.png index 84f5256..e618637 100644 Binary files a/assets/sky.png and b/assets/sky.png differ diff --git a/src/constants.rs b/src/constants.rs index 7a1f585..8246d63 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,2 +1,2 @@ -pub const PIXEL_RATIO: f32 = 4.0; +pub const PIXEL_RATIO: f32 = 2.0; pub const TILE_SIZE: f32 = 16.0 * PIXEL_RATIO; diff --git a/src/cursor.rs b/src/cursor.rs index 98d96fe..40466ee 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -1,11 +1,9 @@ use crate::constants::*; -use bevy::{prelude::*, winit::cursor}; +use bevy::prelude::*; #[derive(Component)] #[require(Transform)] -pub struct Cursor { - id: f32, -} +pub struct Cursor {} pub fn setup_curor(mut commands: Commands, asset_server: Res) { commands.spawn(( @@ -14,7 +12,7 @@ pub fn setup_curor(mut commands: Commands, asset_server: Res) { image: asset_server.load("cursor.png"), ..Default::default() }, - Cursor { id: 1. }, + Cursor {}, )); } diff --git a/src/game.rs b/src/game.rs index 4c086a0..c5ed295 100644 --- a/src/game.rs +++ b/src/game.rs @@ -4,77 +4,53 @@ use crate::tiles::TilePrefab; use crate::constants::*; use bevy::prelude::*; use rand::prelude::*; +use noise::{NoiseFn, Perlin}; + pub fn setup_level(mut commands: Commands, asset_server: Res) { setup_tilemap(&mut commands, &asset_server); - for x in -50..50 { - for y in -25..25 { - if random::() < 0.1 { - commands.spawn(Citizen::new( - &asset_server, - Vec3::new(x as f32 * 64., y as f32 * 64., 1.), - )); - } - } - } + // for x in -50..50 { + // for y in -25..25 { + // if random::() < 0.1 { + // commands.spawn(Citizen::new( + // &asset_server, + // Vec3::new(x as f32 * 64., y as f32 * 64., 1.), + // )); + // } + // } + // } } fn setup_tilemap(commands: &mut Commands, asset_server: &Res) { - for x in -50..50 { - for y in -25..25 { - for z in 0..4 { + let perlin = Perlin::new(0); + + for x in -150..150 { + for y in -150..150 { + for z in -10..10 { let position = Vec3::new( x as f32 * TILE_SIZE, y as f32 * TILE_SIZE, -z as f32, ); + // Generate Perlin noise value + let noise_value = perlin.get([ + x as f64 * 0.1, + y as f64 * 0.1, + z as f64 * 0.1, + ]); + let mut items: Vec = vec![]; - - if random::() < 0.1 { - items.push(ItemBundle { - transform: Transform::from_translation(Vec3::new(0., 0., 0.1)), - sprite: Sprite { - image: asset_server.load("beer.png"), - ..Default::default() - }, - item: Item::new(), - visibility: Visibility::Visible, - }); - } - if random::() < 0.1 { - items.push(ItemBundle { - transform: Transform::from_translation(Vec3::new(0., 0., 0.1)), - sprite: Sprite { - image: asset_server.load("chalice.png"), - ..Default::default() - }, - item: Item::new(), - visibility: Visibility::Visible, - }); - } - if random::() < 0.1 { - items.push(ItemBundle { - transform: Transform::from_translation(Vec3::new(0., 0., 0.1)), - sprite: Sprite { - image: asset_server.load("fish.png"), - ..Default::default() - }, - item: Item::new(), - visibility: Visibility::Visible, - }); - } - - if random::() < 0.7 { + if noise_value < -0.3 { commands - .spawn(TilePrefab::grass(position, asset_server)) + .spawn(TilePrefab::rock(position, asset_server)) .with_children(|parent| { for item in items { parent.spawn(item); } }); - } else if random::() < 0.2 { + } else if noise_value < 0. { commands .spawn(TilePrefab::dirt(position, asset_server)) .with_children(|parent| { @@ -82,9 +58,9 @@ fn setup_tilemap(commands: &mut Commands, asset_server: &Res) { parent.spawn(item); } }); - } else if random::() < 0.1 { + } else if noise_value < 0.3 { commands - .spawn(TilePrefab::rock(position, asset_server)) + .spawn(TilePrefab::grass(position, asset_server)) .with_children(|parent| { for item in items { parent.spawn(item); diff --git a/src/item.rs b/src/item.rs index 16e7c15..c66b8b5 100644 --- a/src/item.rs +++ b/src/item.rs @@ -42,6 +42,6 @@ pub struct Nameable { } #[derive(Component)] -pub struct Compostable { +pub struct Decaying { decay: u8, } diff --git a/src/main.rs b/src/main.rs index 1bba431..1c473f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,13 +24,14 @@ fn main() { ) .insert_resource(ClearColor(Color::srgb(0.1, 0.1, 0.15))) .add_systems(Startup, (game::setup_level, camera::spawn_panning_camera)) + .add_systems(Startup, tile::tile_sprite_visibility_update) .add_systems(Startup, cursor::setup_curor) .add_systems( FixedUpdate, ( camera::camera_movement, citizen::citizen_movement, - tile::tile_sprite_update, + tile::tile_item_sprite_update, ), ) .add_systems(FixedUpdate, cursor::move_cursor) diff --git a/src/tile.rs b/src/tile.rs index ccf6684..081117b 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,17 +1,16 @@ use crate::item::Item; +use crate::constants::*; use bevy::prelude::*; #[derive(Component)] #[require(Sprite)] pub struct Tile { - pub position: Vec3, + pub id: u32 } impl Default for Tile { fn default() -> Self { - Self { - position: Vec3::ZERO, - } + Self {id:1} } } @@ -52,7 +51,43 @@ pub struct DoorTile { pub locked: bool, } -pub fn tile_sprite_update( + + +pub fn tile_sprite_visibility_update( + mut query: Query<(&mut Visibility, &Transform, Entity), With>, + tile_query: Query<(&Transform, &Tile)>, +) { + for (mut visibility, transform, entity) in query.iter_mut() { + *visibility = Visibility::Hidden; + + for x_offset in -1..=1 { + for y_offset in -1..=1 { + if x_offset == 0 && y_offset == 0 { + continue; // Skip the current tile + } + + let neighbor_position = transform.translation + Vec3::new( + x_offset as f32 * TILE_SIZE, + y_offset as f32 * TILE_SIZE, + 0.0, + ); + + if let Some((neighbor_transform, neighbor_tile)) = tile_query.iter().find(|(neighbor_transform, _)| { + neighbor_transform.translation == neighbor_position + }) { + if neighbor_tile.id == 0 { + *visibility = Visibility::Visible; + break; + } + } + } + } + } +} + + + +pub fn tile_item_sprite_update( time: Res