attempt noise gen

This commit is contained in:
StephenAdamson
2024-12-14 00:03:52 +00:00
parent 7363ec9d39
commit bf3ddd0e74
10 changed files with 102 additions and 70 deletions
Generated
+21
View File
@@ -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"
+1
View File
@@ -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:
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

+1 -1
View File
@@ -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;
+3 -5
View File
@@ -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<AssetServer>) {
commands.spawn((
@@ -14,7 +12,7 @@ pub fn setup_curor(mut commands: Commands, asset_server: Res<AssetServer>) {
image: asset_server.load("cursor.png"),
..Default::default()
},
Cursor { id: 1. },
Cursor {},
));
}
+29 -53
View File
@@ -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<AssetServer>) {
setup_tilemap(&mut commands, &asset_server);
for x in -50..50 {
for y in -25..25 {
if random::<f32>() < 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::<f32>() < 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<AssetServer>) {
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<ItemBundle> = vec![];
if random::<f32>() < 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::<f32>() < 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::<f32>() < 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::<f32>() < 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::<f32>() < 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<AssetServer>) {
parent.spawn(item);
}
});
} else if random::<f32>() < 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);
+1 -1
View File
@@ -42,6 +42,6 @@ pub struct Nameable {
}
#[derive(Component)]
pub struct Compostable {
pub struct Decaying {
decay: u8,
}
+2 -1
View File
@@ -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)
+40 -5
View File
@@ -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>>,
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<Time>,
mut query_tile: Query<(Entity, &Children, &mut TileState), With<Tile>>,
mut query_item: Query<(&mut Visibility, &Item)>,
+4 -4
View File
@@ -18,7 +18,7 @@ impl TilePrefab {
image: asset_server.load("dirt.png"),
..Default::default()
},
tile: Tile { position },
tile: Tile {id:1},
tile_state: TileState {
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
},
@@ -32,7 +32,7 @@ impl TilePrefab {
image: asset_server.load("grass.png"),
..Default::default()
},
tile: Tile { position },
tile: Tile {id:2},
tile_state: TileState {
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
},
@@ -46,7 +46,7 @@ impl TilePrefab {
image: asset_server.load("rock.png"),
..Default::default()
},
tile: Tile { position },
tile: Tile {id:3},
tile_state: TileState {
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
},
@@ -59,7 +59,7 @@ impl TilePrefab {
image: asset_server.load("sky.png"),
..Default::default()
},
tile: Tile { position },
tile: Tile {id:0},
tile_state: TileState {
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
},