attempt noise gen
This commit is contained in:
+1
-1
@@ -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
@@ -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
@@ -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
@@ -42,6 +42,6 @@ pub struct Nameable {
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Compostable {
|
||||
pub struct Decaying {
|
||||
decay: u8,
|
||||
}
|
||||
|
||||
+2
-1
@@ -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
@@ -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
@@ -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),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user