Singular tiles resource
This commit is contained in:
+2
-3
@@ -83,7 +83,7 @@ pub fn update_citizen_wandering_targets(
|
||||
tilemap: Res<TileMap>,
|
||||
chunk_map: Res<ChunkMap>,
|
||||
) {
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
|
||||
|
||||
for (mut ambulatory, _) in query.iter_mut() {
|
||||
if ambulatory.target.is_none()
|
||||
@@ -136,7 +136,6 @@ pub fn citizen_movement(
|
||||
z_index: Res<game::ZIndex>,
|
||||
) {
|
||||
for (mut ambulatory, mut transform, mut visibility) in query.iter_mut() {
|
||||
// Check if current position is valid
|
||||
let current_pos = transform.translation;
|
||||
let below_pos = Vec3::new(
|
||||
current_pos.x,
|
||||
@@ -287,7 +286,7 @@ fn calculate_path(tilemap: &TileMap, start: IVec3, goal: IVec3) -> Vec<Vec3> {
|
||||
move_dir.z.abs() / ITILE_SIZE,
|
||||
) {
|
||||
(1, 0) => 10, // Orthogonal movement
|
||||
(2, 0) => 10, // Diagonal movement
|
||||
(2, 0) => 11, // Diagonal movement
|
||||
(_, 1) => 14, // Moving up or down
|
||||
_ => continue, // Invalid movement
|
||||
};
|
||||
|
||||
+2
-1
@@ -15,6 +15,7 @@ use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
|
||||
fn main() {
|
||||
App::new()
|
||||
.insert_resource(game::ZIndex(0.0))
|
||||
.add_systems(Startup, tiles::initialize_textures)
|
||||
.add_plugins(
|
||||
DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
@@ -26,7 +27,7 @@ fn main() {
|
||||
})
|
||||
.set(ImagePlugin::default_nearest()),
|
||||
)
|
||||
.add_plugins(FrameTimeDiagnosticsPlugin::default()) // Add the FrameTimeDiagnosticsPlugin here
|
||||
.add_plugins(FrameTimeDiagnosticsPlugin::default())
|
||||
.insert_resource(ClearColor(Color::srgb(0.0, 0.0, 0.0)))
|
||||
.add_plugins(tilemap::TilemapPlugin)
|
||||
.add_plugins(PathfindingPlugin)
|
||||
|
||||
+13
-14
@@ -1,6 +1,6 @@
|
||||
use crate::constants::{ITILE_SIZE, TILE_SIZE};
|
||||
use crate::tile::{self, FixtureTile, FloorTile, NeedsOccluded, TileMap};
|
||||
use crate::tiles::{FixtureTilePrefab, FloorTilePrefab};
|
||||
use crate::tiles::{FixtureTilePrefab, FloorTilePrefab, Textures};
|
||||
use bevy::prelude::*;
|
||||
use noise::{NoiseFn, Perlin};
|
||||
use std::collections::HashMap;
|
||||
@@ -30,7 +30,6 @@ fn setup_chunk_system(mut commands: Commands) {
|
||||
commands.insert_resource(ChunkMap::default());
|
||||
}
|
||||
|
||||
// System to handle tile occlusion updates
|
||||
pub fn handle_tile_occlusion_updates(
|
||||
mut commands: Commands,
|
||||
tilemap: Res<TileMap>,
|
||||
@@ -163,7 +162,7 @@ pub fn generate_surface_noise(x: i32, y: i32) -> f32 {
|
||||
|
||||
fn handle_chunk_loading(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
textures: Res<Textures>,
|
||||
mut events: EventReader<LoadChunkEvent>,
|
||||
mut chunk_map: ResMut<ChunkMap>,
|
||||
mut tilemap: ResMut<TileMap>,
|
||||
@@ -212,19 +211,19 @@ fn handle_chunk_loading(
|
||||
z as f64 * 0.05,
|
||||
]);
|
||||
if noise_value < -0.5 {
|
||||
FloorTilePrefab::air(position, &asset_server).spawn(&mut commands);
|
||||
FloorTilePrefab::air(position, &textures).spawn(&mut commands);
|
||||
tilemap
|
||||
.floor_tiles
|
||||
.insert(pos_ivec, (0, false, true, 0, [0; 8])); // Air tile
|
||||
floor_positions.push((position, "air"));
|
||||
} else if noise_value < 0.8 {
|
||||
FloorTilePrefab::rock(position, &asset_server).spawn(&mut commands);
|
||||
FloorTilePrefab::rock(position, &textures).spawn(&mut commands);
|
||||
tilemap
|
||||
.floor_tiles
|
||||
.insert(pos_ivec, (2, true, false, 50, [0; 8])); // Rock tile
|
||||
floor_positions.push((position, "rock"));
|
||||
} else {
|
||||
FloorTilePrefab::dirt(position, &asset_server).spawn(&mut commands);
|
||||
FloorTilePrefab::dirt(position, &textures).spawn(&mut commands);
|
||||
tilemap
|
||||
.floor_tiles
|
||||
.insert(pos_ivec, (1, true, true, 85, [0; 8])); // Dirt tile
|
||||
@@ -234,20 +233,20 @@ fn handle_chunk_loading(
|
||||
if (generate_surface_noise(world_x, world_y) * TILE_SIZE).round()
|
||||
<= position.z + TILE_SIZE
|
||||
{
|
||||
FloorTilePrefab::grass(position, &asset_server).spawn(&mut commands);
|
||||
FloorTilePrefab::grass(position, &textures).spawn(&mut commands);
|
||||
tilemap
|
||||
.floor_tiles
|
||||
.insert(pos_ivec, (1, true, true, 100, [0; 8])); // Dirt tile (grass)
|
||||
floor_positions.push((position, "dirt"));
|
||||
} else {
|
||||
FloorTilePrefab::dirt(position, &asset_server).spawn(&mut commands);
|
||||
FloorTilePrefab::dirt(position, &textures).spawn(&mut commands);
|
||||
tilemap
|
||||
.floor_tiles
|
||||
.insert(pos_ivec, (1, true, true, 85, [0; 8])); // Dirt tile
|
||||
floor_positions.push((position, "dirt"));
|
||||
}
|
||||
} else {
|
||||
FloorTilePrefab::air(position, &asset_server).spawn(&mut commands);
|
||||
FloorTilePrefab::air(position, &textures).spawn(&mut commands);
|
||||
tilemap
|
||||
.floor_tiles
|
||||
.insert(pos_ivec, (0, false, true, 0, [0; 8])); // Air tile
|
||||
@@ -264,17 +263,17 @@ fn handle_chunk_loading(
|
||||
|
||||
match *floor_type {
|
||||
"dirt" => {
|
||||
FixtureTilePrefab::dirt_wall(above_pos, &asset_server).spawn(&mut commands);
|
||||
FixtureTilePrefab::dirt_wall(above_pos, &textures).spawn(&mut commands);
|
||||
tilemap.fixture_tiles.insert(above_ivec, (1, true, [0; 8]));
|
||||
// Dirt wall
|
||||
}
|
||||
"rock" => {
|
||||
FixtureTilePrefab::rock_wall(above_pos, &asset_server).spawn(&mut commands);
|
||||
FixtureTilePrefab::rock_wall(above_pos, &textures).spawn(&mut commands);
|
||||
tilemap.fixture_tiles.insert(above_ivec, (2, true, [0; 8]));
|
||||
// Rock wall
|
||||
}
|
||||
"bedrock" => {
|
||||
FixtureTilePrefab::bedrock_wall(above_pos, &asset_server).spawn(&mut commands);
|
||||
FixtureTilePrefab::bedrock_wall(above_pos, &textures).spawn(&mut commands);
|
||||
tilemap.fixture_tiles.insert(above_ivec, (3, true, [0; 8]));
|
||||
// Bedrock wall
|
||||
}
|
||||
@@ -286,8 +285,8 @@ fn handle_chunk_loading(
|
||||
|
||||
fn setup_initial_chunks(mut event_writer: EventWriter<LoadChunkEvent>) {
|
||||
println!("setup_initial_chunks");
|
||||
for x in -9..=9 {
|
||||
for y in -2..=2 {
|
||||
for x in -6..=6 {
|
||||
for y in -3..=3 {
|
||||
event_writer.send(LoadChunkEvent {
|
||||
chunk_position: IVec2::new(x, y),
|
||||
});
|
||||
|
||||
+52
-16
@@ -1,6 +1,42 @@
|
||||
use crate::constants::*;
|
||||
use crate::tile::{FixtureTile, FloorTile, NeedsOccluded, TileState};
|
||||
use bevy::prelude::*;
|
||||
use bevy::utils::HashMap;
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct Textures {
|
||||
pub handles: HashMap<String, Handle<Image>>,
|
||||
}
|
||||
|
||||
pub fn initialize_textures(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let mut textures = HashMap::new();
|
||||
textures.insert(
|
||||
"grass_floor".to_string(),
|
||||
asset_server.load("grass_floor.png"),
|
||||
);
|
||||
textures.insert(
|
||||
"dirt_floor".to_string(),
|
||||
asset_server.load("dirt_floor.png"),
|
||||
);
|
||||
textures.insert(
|
||||
"rock_floor".to_string(),
|
||||
asset_server.load("rock_floor.png"),
|
||||
);
|
||||
textures.insert(
|
||||
"bedrock_floor".to_string(),
|
||||
asset_server.load("bedrock_floor.png"),
|
||||
);
|
||||
textures.insert("sky".to_string(), asset_server.load("sky.png"));
|
||||
textures.insert("dirt_wall".to_string(), asset_server.load("dirt_wall.png"));
|
||||
textures.insert("rock_wall".to_string(), asset_server.load("rock_wall.png"));
|
||||
textures.insert(
|
||||
"bedrock_wall".to_string(),
|
||||
asset_server.load("bedrock_wall.png"),
|
||||
);
|
||||
|
||||
// Insert the textures resource into the world
|
||||
commands.insert_resource(Textures { handles: textures });
|
||||
}
|
||||
|
||||
#[derive(Bundle)]
|
||||
pub struct FloorTilePrefab {
|
||||
@@ -13,11 +49,11 @@ pub struct FloorTilePrefab {
|
||||
}
|
||||
|
||||
impl FloorTilePrefab {
|
||||
pub fn grass(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||
pub fn grass(position: Vec3, textures: &Res<Textures>) -> Self {
|
||||
FloorTilePrefab {
|
||||
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("grass_floor.png"),
|
||||
image: textures.handles.get("grass_floor").unwrap().clone(),
|
||||
..Default::default()
|
||||
},
|
||||
tile: FloorTile {
|
||||
@@ -35,11 +71,11 @@ impl FloorTilePrefab {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dirt(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||
pub fn dirt(position: Vec3, textures: &Res<Textures>) -> Self {
|
||||
FloorTilePrefab {
|
||||
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("dirt_floor.png"),
|
||||
image: textures.handles.get("dirt_floor").unwrap().clone(),
|
||||
..Default::default()
|
||||
},
|
||||
tile: FloorTile {
|
||||
@@ -57,11 +93,11 @@ impl FloorTilePrefab {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rock(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||
pub fn rock(position: Vec3, textures: &Res<Textures>) -> Self {
|
||||
FloorTilePrefab {
|
||||
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("rock_floor.png"),
|
||||
image: textures.handles.get("rock_floor").unwrap().clone(),
|
||||
..Default::default()
|
||||
},
|
||||
tile: FloorTile {
|
||||
@@ -79,11 +115,11 @@ impl FloorTilePrefab {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn air(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||
pub fn air(position: Vec3, textures: &Res<Textures>) -> Self {
|
||||
FloorTilePrefab {
|
||||
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("sky.png"),
|
||||
image: textures.handles.get("sky").unwrap().clone(),
|
||||
..Default::default()
|
||||
},
|
||||
tile: FloorTile {
|
||||
@@ -102,11 +138,11 @@ impl FloorTilePrefab {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bedrock(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||
pub fn bedrock(position: Vec3, textures: &Res<Textures>) -> Self {
|
||||
FloorTilePrefab {
|
||||
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("bedrock_floor.png"),
|
||||
image: textures.handles.get("bedrock_floor").unwrap().clone(),
|
||||
..Default::default()
|
||||
},
|
||||
tile: FloorTile {
|
||||
@@ -146,11 +182,11 @@ pub struct FixtureTilePrefab {
|
||||
}
|
||||
|
||||
impl FixtureTilePrefab {
|
||||
pub fn dirt_wall(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||
pub fn dirt_wall(position: Vec3, textures: &Res<Textures>) -> Self {
|
||||
FixtureTilePrefab {
|
||||
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("dirt_wall.png"),
|
||||
image: textures.handles.get("dirt_wall").unwrap().clone(),
|
||||
..Default::default()
|
||||
},
|
||||
tile: FixtureTile {
|
||||
@@ -164,11 +200,11 @@ impl FixtureTilePrefab {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rock_wall(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||
pub fn rock_wall(position: Vec3, textures: &Res<Textures>) -> Self {
|
||||
FixtureTilePrefab {
|
||||
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("rock_wall.png"),
|
||||
image: textures.handles.get("rock_wall").unwrap().clone(),
|
||||
..Default::default()
|
||||
},
|
||||
tile: FixtureTile {
|
||||
@@ -182,11 +218,11 @@ impl FixtureTilePrefab {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bedrock_wall(position: Vec3, asset_server: &Res<AssetServer>) -> Self {
|
||||
pub fn bedrock_wall(position: Vec3, textures: &Res<Textures>) -> Self {
|
||||
FixtureTilePrefab {
|
||||
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("bedrock_wall.png"),
|
||||
image: textures.handles.get("bedrock_wall").unwrap().clone(),
|
||||
..Default::default()
|
||||
},
|
||||
tile: FixtureTile {
|
||||
|
||||
Reference in New Issue
Block a user