rework pathfinding, game entity fog rendering, and tilemap usage
This commit is contained in:
+1
-1
@@ -75,7 +75,7 @@ pub fn spawn_panning_camera(mut commands: Commands) {
|
||||
scale: 1.0,
|
||||
..OrthographicProjection::default_2d()
|
||||
}),
|
||||
Transform::from_xyz(0., 0., 10. * TILE_SIZE),
|
||||
Transform::from_xyz(0., 0., 10.),
|
||||
PanningCamera { pan_speed: 15.0 },
|
||||
));
|
||||
}
|
||||
|
||||
+84
-36
@@ -145,14 +145,8 @@ pub fn citizen_movement(
|
||||
.par_iter_mut()
|
||||
.for_each(|(mut ambulatory, mut transform, _, _)| {
|
||||
let current_pos = transform.translation;
|
||||
let below_pos = Vec3::new(
|
||||
current_pos.x,
|
||||
current_pos.y,
|
||||
current_pos.z - TILE_SIZE - 0.1,
|
||||
);
|
||||
|
||||
if !is_standable_tile(&tilemap, current_pos.as_ivec3()) {
|
||||
transform.translation = below_pos + Vec3::new(0.0, 0.0, 0.1);
|
||||
transform.translation.z -= TILE_SIZE;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -204,26 +198,32 @@ pub fn citizen_movement(
|
||||
}
|
||||
|
||||
fn is_standable_tile(tilemap: &TileMap, pos: IVec3) -> bool {
|
||||
let mut can_i_stand_in_tile: bool = false;
|
||||
let mut can_i_stand_on_tile_bellow: bool = false;
|
||||
let mut can_i_stand_in_fixture: bool = false;
|
||||
let mut can_i_stand_on_fixture_bellow: bool = false;
|
||||
|
||||
// Check if current position has a blocking floor tile
|
||||
if let Some(current_tile) = tilemap.floor_tiles.get(&pos) {
|
||||
return current_tile.0 == 0;
|
||||
if let Some(current_floor_tile) = tilemap.floor_tiles.get(&pos) {
|
||||
can_i_stand_in_tile = current_floor_tile.1;
|
||||
}
|
||||
// Check if current position has a solid fixture tile (e.g., log, leaf)
|
||||
if let Some(current_tile) = tilemap.fixture_tiles.get(&pos) {
|
||||
return current_tile.0 == 0;
|
||||
// Check if current position has a solid fixture tile (e.g., log)
|
||||
if let Some(current_fixture_tile) = tilemap.fixture_tiles.get(&pos) {
|
||||
can_i_stand_in_fixture = current_fixture_tile.1;
|
||||
}
|
||||
|
||||
// Check if there's solid ground below (fixture or floor)
|
||||
let pos_below = pos - IVec3::new(0, 0, ITILE_SIZE);
|
||||
|
||||
if let Some(below_tile) = tilemap.floor_tiles.get(&pos_below) {
|
||||
return below_tile.0 != 0;
|
||||
if let Some(below_floor_tile) = tilemap.floor_tiles.get(&pos_below) {
|
||||
can_i_stand_on_tile_bellow = below_floor_tile.2;
|
||||
}
|
||||
|
||||
if let Some(below_tile) = tilemap.fixture_tiles.get(&pos_below) {
|
||||
return below_tile.0 != 0;
|
||||
if let Some(below_fixture_tile) = tilemap.fixture_tiles.get(&pos_below) {
|
||||
can_i_stand_on_fixture_bellow = below_fixture_tile.2;
|
||||
}
|
||||
false
|
||||
return (can_i_stand_in_tile || can_i_stand_in_fixture)
|
||||
&& (can_i_stand_on_tile_bellow || can_i_stand_on_fixture_bellow);
|
||||
}
|
||||
|
||||
fn calculate_path(tilemap: &TileMap, start: IVec3, goal: IVec3) -> Vec<Vec3> {
|
||||
@@ -304,15 +304,43 @@ fn calculate_path(tilemap: &TileMap, start: IVec3, goal: IVec3) -> Vec<Vec3> {
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: Add terrain-based cost modifiers
|
||||
// movement_cost = apply_terrain_modifier(movement_cost, neighbor_pos, tilemap);
|
||||
// Examples:
|
||||
// - Mud/sand: +50% cost
|
||||
// - Ice: +100% cost
|
||||
// - Designated high-traffic areas: -25% cost
|
||||
// - Designated restricted areas: +500% cost
|
||||
// - Etc
|
||||
|
||||
let movement_cost = match (
|
||||
move_dir.x.abs() / ITILE_SIZE,
|
||||
move_dir.y.abs() / ITILE_SIZE,
|
||||
move_dir.z.abs() / ITILE_SIZE,
|
||||
) {
|
||||
(1, 0, 0) | (0, 1, 0) | (0, 0, 1) => 10, // Orthogonal movement (1 axis)
|
||||
(1, 1, 0) | (1, 0, 1) | (0, 1, 1) => 14, // Diagonal movement (2 axes)
|
||||
(1, 1, 1) => 20, // Full 3D diagonal movement (3 axes)
|
||||
_ => continue, // Invalid movement
|
||||
// 2D Movement (Dwarf Fortress style)
|
||||
(1, 0, 0) | (0, 1, 0) => 10, // Orthogonal movement
|
||||
(1, 1, 0) => 14, // Diagonal movement (~√2 × 10)
|
||||
|
||||
// Vertical Movement (Raw climbing - very expensive)
|
||||
// (0, 0, 1) => 50, // Pure vertical climb/fall
|
||||
|
||||
// 3D Movement (Climbing diagonally - even more expensive)
|
||||
(1, 0, 1) | (0, 1, 1) => 52, // Orthogonal + vertical climb
|
||||
(1, 1, 1) => 56, // Diagonal + vertical climb
|
||||
|
||||
// TODO: Implement stairs and ramps for efficient vertical movement
|
||||
// Stairs would reduce vertical costs significantly:
|
||||
// (0, 0, 1) => 20 if has_stairs(current, neighbor_pos), // Stairs: 2× horizontal cost
|
||||
// (1, 0, 1) | (0, 1, 1) => 24 if has_stairs(current, neighbor_pos), // Stairs + horizontal
|
||||
// (1, 1, 1) => 28 if has_stairs(current, neighbor_pos), // Stairs + diagonal
|
||||
|
||||
// TODO: Implement ramps for even smoother vertical movement
|
||||
// Ramps would be cheaper than stairs:
|
||||
// (0, 0, 1) => 15 if has_ramp(current, neighbor_pos), // Ramps: 1.5× horizontal cost
|
||||
// (1, 0, 1) | (0, 1, 1) => 18 if has_ramp(current, neighbor_pos), // Ramps + horizontal
|
||||
// (1, 1, 1) => 21 if has_ramp(current, neighbor_pos), // Ramps + diagonal
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
let new_g = g_scores.get(¤t).unwrap_or(&i32::MAX) + movement_cost;
|
||||
@@ -352,19 +380,35 @@ fn octile_distance_3d(a: IVec3, b: IVec3) -> i32 {
|
||||
let dy = (a.y - b.y).abs();
|
||||
let dz = (a.z - b.z).abs();
|
||||
|
||||
// Costs for movement along 1, 2, or 3 axes
|
||||
let cost1 = 10; // Orthogonal
|
||||
let cost2 = 14; // 2D Diagonal
|
||||
let cost3 = 17; // 3D Diagonal
|
||||
// Dwarf Fortress style costs
|
||||
let cost_orthogonal = 10; // Horizontal orthogonal
|
||||
let cost_diagonal = 14; // Horizontal diagonal (~√2 × 10)
|
||||
let cost_climb = 50; // Raw vertical movement (climbing)
|
||||
|
||||
let mut diffs = [dx, dy, dz];
|
||||
diffs.sort_unstable(); // Sorts ascending: [dmin, dmid, dmax]
|
||||
|
||||
diffs.sort_unstable();
|
||||
let dmin = diffs[0];
|
||||
let dmid = diffs[1];
|
||||
let dmax = diffs[2];
|
||||
|
||||
cost3 * dmin + cost2 * (dmid - dmin) + cost1 * (dmax - dmid) // Yikes..
|
||||
if dz == 0 {
|
||||
// Pure 2D movement
|
||||
let diagonal_moves = dmin / ITILE_SIZE;
|
||||
let orthogonal_moves = (dmax - dmin) / ITILE_SIZE;
|
||||
cost_diagonal * diagonal_moves + cost_orthogonal * orthogonal_moves
|
||||
} else {
|
||||
// Movement involves Z - assume raw climbing for now
|
||||
// TODO: Modify this when stairs/ramps are implemented
|
||||
let z_moves = dz / ITILE_SIZE;
|
||||
let xy_distance = ((dx * dx + dy * dy) as f32).sqrt() as i32;
|
||||
let remaining_2d_diagonal = (xy_distance.min(dz)) / ITILE_SIZE;
|
||||
let remaining_2d_orthogonal =
|
||||
(xy_distance - remaining_2d_diagonal * ITILE_SIZE) / ITILE_SIZE;
|
||||
|
||||
// Raw climbing cost + remaining 2D movement
|
||||
cost_climb * z_moves
|
||||
+ cost_diagonal * remaining_2d_diagonal
|
||||
+ cost_orthogonal * remaining_2d_orthogonal
|
||||
}
|
||||
}
|
||||
|
||||
fn reconstruct_path(came_from: HashMap<IVec3, IVec3>, mut current: IVec3) -> Vec<Vec3> {
|
||||
@@ -391,13 +435,17 @@ pub fn spawn_citizens(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let mut rng = rand::rng();
|
||||
|
||||
// Spawn a handful of citizens
|
||||
for _ in 0..1000 {
|
||||
let x: f32 = rng.random_range(-35.0..35.0);
|
||||
let y: f32 = rng.random_range(-35.0..35.0);
|
||||
let mut position = Vec3::new(x.round(), y.round(), 35.0) * TILE_SIZE;
|
||||
position.z += 0.1;
|
||||
|
||||
let cit = commands.spawn(Citizen::new(&asset_server, position)).id();
|
||||
for _ in 0..100 {
|
||||
let cit = commands
|
||||
.spawn(Citizen::new(
|
||||
&asset_server,
|
||||
Vec3::new(
|
||||
rng.random_range(-25.0f32..25.0f32).round(),
|
||||
rng.random_range(-25.0f32..25.0f32).round(),
|
||||
35.0,
|
||||
) * TILE_SIZE,
|
||||
))
|
||||
.id();
|
||||
commands.entity(cit).insert(VisibleGameEntity);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ fn main() {
|
||||
.insert_resource(tiles::CurrentWorldSpriteState {
|
||||
state: tiles::TerrainSpriteState::Inactive,
|
||||
})
|
||||
.insert_resource(camera::CameraMoved(true))
|
||||
.insert_resource(camera::CameraMoved(false))
|
||||
.insert_resource(tiles::QuiltCache::default())
|
||||
.add_systems(PreStartup, tiles::initialize_textures)
|
||||
.add_plugins(
|
||||
|
||||
+7
-10
@@ -47,8 +47,8 @@ impl Default for FixtureTile {
|
||||
|
||||
#[derive(Resource, Default, Clone)]
|
||||
pub struct TileMap {
|
||||
pub floor_tiles: HashMap<IVec3, (u32, bool, bool, u8, [u32; 8])>, //id, opaque, walkable, astar_weight, visible_range
|
||||
pub fixture_tiles: HashMap<IVec3, (u32, bool, [u32; 8])>, // id, solid, visible_range
|
||||
pub floor_tiles: HashMap<IVec3, (i32, bool, bool, bool, i32, [u32; 8])>, //id, canStandIn, canStandOn, visiblyTransparent, astar_weight, visible_range
|
||||
pub fixture_tiles: HashMap<IVec3, (i32, bool, bool, [u32; 8])>, // id, canStandIn, canStandOn, visible_range
|
||||
}
|
||||
|
||||
pub fn update_tile_visibility(
|
||||
@@ -58,15 +58,12 @@ pub fn update_tile_visibility(
|
||||
) {
|
||||
let now = Instant::now();
|
||||
|
||||
let current_z = z_index.0 as isize;
|
||||
|
||||
for (terrain_sprite, mut visibility) in query.iter_mut() {
|
||||
*visibility =
|
||||
if terrain_sprite.z_index == ((current_z + tilemap::Z_BELOW as isize) as usize) {
|
||||
Visibility::Visible
|
||||
} else {
|
||||
Visibility::Hidden
|
||||
};
|
||||
*visibility = if terrain_sprite.z_index == ((z_index.0 + tilemap::Z_BELOW) as usize) {
|
||||
Visibility::Visible
|
||||
} else {
|
||||
Visibility::Hidden
|
||||
};
|
||||
}
|
||||
cwss.state = TerrainSpriteState::Inactive;
|
||||
println!("Visibility update: {:.2?}", now.elapsed());
|
||||
|
||||
+64
-67
@@ -15,9 +15,11 @@ use noise::{NoiseFn, Perlin};
|
||||
|
||||
pub const CHUNK_SIZE: i32 = 8;
|
||||
|
||||
pub const Z_BELOW: f32 = 10.0;
|
||||
pub const Z_BELOW: f32 = 5.0;
|
||||
pub const Z_ABOVE: f32 = 15.0;
|
||||
pub const Z_TOTAL: f32 = Z_ABOVE + Z_BELOW; // MAX 255 DO NOT EXCEED
|
||||
pub const Z_TOTAL: f32 = Z_ABOVE + Z_BELOW;
|
||||
|
||||
const _: () = assert!(Z_TOTAL <= 255.0);
|
||||
|
||||
pub const SEED: u32 = 420;
|
||||
|
||||
@@ -100,14 +102,14 @@ pub fn handle_tile_occlusion_updates(
|
||||
if let Some(visibility) = update_map.get(&pos.translation.as_ivec3()) {
|
||||
tile.visible_range = *visibility;
|
||||
if let Some(tile_data) = tilemap.floor_tiles.get_mut(&pos.translation.as_ivec3()) {
|
||||
tile_data.4 = *visibility;
|
||||
tile_data.5 = *visibility;
|
||||
}
|
||||
}
|
||||
}
|
||||
if count > 0 {
|
||||
cwss.state = TerrainSpriteState::WaitingForRender;
|
||||
println!(
|
||||
"Tile occlusion updated {} tiles in {:.2?}",
|
||||
"Tile occlusion calculated for {} tiles in {:.2?}",
|
||||
count,
|
||||
start.elapsed()
|
||||
);
|
||||
@@ -131,8 +133,10 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] {
|
||||
'vertical_check: for z_offset in 1..v_check_height {
|
||||
let above_pos = IVec3::new(pos.x, pos.y, pos.z + (z_offset * ITILE_SIZE));
|
||||
if above_pos.z <= camera_z {
|
||||
if let Some(&(_, opaque, _, _, _)) = tilemap.floor_tiles.get(&above_pos) {
|
||||
if opaque {
|
||||
if let Some(&(_, _, _, visibly_transparent, _, _)) =
|
||||
tilemap.floor_tiles.get(&above_pos)
|
||||
{
|
||||
if !visibly_transparent {
|
||||
is_occluded = true;
|
||||
break 'vertical_check;
|
||||
}
|
||||
@@ -154,8 +158,9 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] {
|
||||
pos.z + z_offset * ITILE_SIZE,
|
||||
);
|
||||
|
||||
if let Some(&(id, _, _, _, _)) = tilemap.floor_tiles.get(&neighbor_pos) {
|
||||
if let Some(&(id, _, _, _, _, _)) = tilemap.floor_tiles.get(&neighbor_pos) {
|
||||
if id == 0 {
|
||||
// id 0 = air tile
|
||||
is_visible = true;
|
||||
break 'neighbor_check;
|
||||
}
|
||||
@@ -188,7 +193,7 @@ pub fn generate_surface_terrain(x: i32, y: i32) -> f32 {
|
||||
(noise_value * 2.5) as f32
|
||||
}
|
||||
|
||||
fn generate_chunks_from_algo(
|
||||
fn handle_chunk_events(
|
||||
mut chunk_events: EventReader<GenerateChunkEvent>,
|
||||
mut terrain_event_writer: EventWriter<ChunkTerrainEvent>,
|
||||
mut weathering_event_writer: EventWriter<ChunkWeatheringAndPrecipitationEvent>,
|
||||
@@ -255,7 +260,8 @@ fn generate_chunk_terrain(
|
||||
let start_y = chunk_pos.y * CHUNK_SIZE;
|
||||
|
||||
let mut surface_positions: Vec<(Vec3, String)> = Vec::new();
|
||||
let mut local_tilemap_updates = HashMap::new();
|
||||
let mut local_tilemap_updates: HashMap<IVec3, (i32, bool, bool, bool, i32, [u32; 8])> =
|
||||
HashMap::new();
|
||||
|
||||
// Generate tiles for this chunk
|
||||
for local_y in 0..CHUNK_SIZE {
|
||||
@@ -288,19 +294,22 @@ fn generate_chunk_terrain(
|
||||
commands.command_scope(|mut cmd| {
|
||||
FloorTilePrefab::air(position).spawn(&mut cmd);
|
||||
});
|
||||
local_tilemap_updates.insert(pos_ivec, (0, false, true, 0, [0; 8]));
|
||||
local_tilemap_updates
|
||||
.insert(pos_ivec, (0, true, false, true, 0, [0; 8]));
|
||||
// Air tile
|
||||
} else if cave_value < 0.8 {
|
||||
commands.command_scope(|mut cmd| {
|
||||
FloorTilePrefab::rock(position).spawn(&mut cmd);
|
||||
});
|
||||
local_tilemap_updates.insert(pos_ivec, (2, true, false, 50, [0; 8]));
|
||||
local_tilemap_updates
|
||||
.insert(pos_ivec, (2, false, true, false, 50, [0; 8]));
|
||||
// Rock tile
|
||||
} else {
|
||||
commands.command_scope(|mut cmd| {
|
||||
FloorTilePrefab::dirt(position).spawn(&mut cmd);
|
||||
});
|
||||
local_tilemap_updates.insert(pos_ivec, (1, true, true, 85, [0; 8]));
|
||||
local_tilemap_updates
|
||||
.insert(pos_ivec, (1, false, true, false, 85, [0; 8]));
|
||||
// Dirt tile
|
||||
}
|
||||
} else if noise_position.z > position.z {
|
||||
@@ -310,20 +319,22 @@ fn generate_chunk_terrain(
|
||||
commands.command_scope(|mut cmd| {
|
||||
FloorTilePrefab::grass(position).spawn(&mut cmd);
|
||||
});
|
||||
local_tilemap_updates.insert(pos_ivec, (1, true, true, 100, [0; 8])); // Dirt tile (grass)
|
||||
local_tilemap_updates
|
||||
.insert(pos_ivec, (1, false, true, false, 100, [0; 8])); // Dirt tile (grass)
|
||||
surface_positions.push((position, ("grass").to_string()));
|
||||
} else {
|
||||
commands.command_scope(|mut cmd| {
|
||||
FloorTilePrefab::dirt(position).spawn(&mut cmd);
|
||||
});
|
||||
local_tilemap_updates.insert(pos_ivec, (1, true, true, 85, [0; 8]));
|
||||
local_tilemap_updates
|
||||
.insert(pos_ivec, (1, false, true, false, 85, [0; 8]));
|
||||
// Dirt tile
|
||||
}
|
||||
} else {
|
||||
commands.command_scope(|mut cmd| {
|
||||
FloorTilePrefab::air(position).spawn(&mut cmd);
|
||||
});
|
||||
local_tilemap_updates.insert(pos_ivec, (0, false, true, 0, [0; 8]));
|
||||
local_tilemap_updates.insert(pos_ivec, (0, true, false, true, 0, [0; 8]));
|
||||
// Air tile
|
||||
}
|
||||
}
|
||||
@@ -387,16 +398,16 @@ fn generate_chunk_forrestry(
|
||||
let start = Instant::now();
|
||||
let count = events.len();
|
||||
|
||||
let collected_tilemap_updates = Mutex::new(Vec::<(IVec3, (u32, bool, [u32; 8]))>::new());
|
||||
let collected_tilemap_updates: Mutex<Vec<(IVec3, (i32, bool, bool, [u32; 8]))>> =
|
||||
Mutex::new(Vec::<(IVec3, (i32, bool, bool, [u32; 8]))>::new());
|
||||
|
||||
events.par_read().for_each(|event| {
|
||||
let floor_positions = &event.floor_tiles;
|
||||
let tree_positions: Vec<Vec3> = Vec::new();
|
||||
let mut tree_positions: Vec<Vec3> = Vec::new();
|
||||
let min_distance = 7.0 * TILE_SIZE;
|
||||
|
||||
for (position, floor_type) in floor_positions.iter() {
|
||||
let above_pos = *position + Vec3::new(0.0, 0.0, TILE_SIZE);
|
||||
let above_ivec = above_pos.as_ivec3();
|
||||
|
||||
match floor_type.as_str() {
|
||||
"grass" => {
|
||||
@@ -409,10 +420,10 @@ fn generate_chunk_forrestry(
|
||||
.iter()
|
||||
.all(|&tree_pos| above_pos.distance(tree_pos) > min_distance);
|
||||
|
||||
// 1 in 65 chance if far enough from other trees
|
||||
if is_far_enough && rand::random::<u32>() % 65 == 0 {
|
||||
// 1 in 100 chance if far enough from other trees
|
||||
if is_far_enough && rand::random::<u32>() % 100 == 0 {
|
||||
// Generate trunk
|
||||
let trunk_height = 3 + rand::random::<u32>() % 4;
|
||||
let trunk_height = 4 + rand::random::<u32>() % 5;
|
||||
for i in 0..trunk_height {
|
||||
let trunk_pos =
|
||||
above_pos + Vec3::new(0.0, 0.0, i as f32 * TILE_SIZE);
|
||||
@@ -423,36 +434,34 @@ fn generate_chunk_forrestry(
|
||||
continue;
|
||||
}
|
||||
|
||||
FixtureTilePrefab::log(trunk_pos).spawn(&mut commands);
|
||||
let trunk_entity =
|
||||
FixtureTilePrefab::log(trunk_pos).spawn(&mut commands);
|
||||
tree_positions.push(trunk_pos);
|
||||
// Add sprite component to the same entity if texture exists
|
||||
if let Some(texture_id) = texture_ids.refs.get(&500004) {
|
||||
if let Some(texture) = textures.handles.get(texture_id) {
|
||||
let sprite = Sprite {
|
||||
image: texture.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
let log = commands
|
||||
.spawn((
|
||||
sprite,
|
||||
Transform::from_xyz(
|
||||
trunk_pos.x,
|
||||
trunk_pos.y,
|
||||
trunk_pos.z,
|
||||
),
|
||||
))
|
||||
.id();
|
||||
commands.entity(log).insert(VisibleGameEntity);
|
||||
collected_tilemap_updates
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((trunk_ivec, (1, true, [0; 8])));
|
||||
|
||||
commands
|
||||
.entity(trunk_entity)
|
||||
.insert((sprite, VisibleGameEntity));
|
||||
// .insert(Visibility::Visible); // Override the hidden visibility
|
||||
}
|
||||
}
|
||||
|
||||
collected_tilemap_updates
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((trunk_ivec, (1, false, true, [0; 8])));
|
||||
|
||||
log_positions.insert(trunk_ivec);
|
||||
}
|
||||
|
||||
// Generate leaves - 3D spherical canopy with random shape
|
||||
let base_leaf_radius = 3.0;
|
||||
let base_leaf_radius = 2.25;
|
||||
let leaf_center =
|
||||
above_pos + Vec3::new(0.0, 0.0, trunk_height as f32 * TILE_SIZE);
|
||||
|
||||
@@ -478,7 +487,7 @@ fn generate_chunk_forrestry(
|
||||
let y_f = y as f32;
|
||||
let z_f = z as f32;
|
||||
let radius = base_leaf_radius
|
||||
* (1.0 + (rand::random::<f32>() * 0.2 - 0.1));
|
||||
* (1.0 + (rand::random::<f32>() * 0.35 - 0.1));
|
||||
|
||||
if x_f * x_f + y_f * y_f + z_f * z_f <= radius * radius {
|
||||
FixtureTilePrefab::leaves(pos).spawn(&mut commands);
|
||||
@@ -503,7 +512,7 @@ fn generate_chunk_forrestry(
|
||||
collected_tilemap_updates
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((ivec, (5, true, [1; 8])));
|
||||
.push((ivec, (5, false, true, [0; 8])));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -512,11 +521,6 @@ fn generate_chunk_forrestry(
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
collected_tilemap_updates
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((above_ivec, (1, true, [0; 8])));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -554,7 +558,7 @@ fn generate_chunk_fauna(
|
||||
|
||||
fn setup_initial_chunks(mut event_writer: EventWriter<GenerateChunkEvent>) {
|
||||
for x in -10..=10 {
|
||||
for y in -10..=10 {
|
||||
for y in -5..=5 {
|
||||
event_writer.write(GenerateChunkEvent {
|
||||
chunk_position: IVec2::new(x, y),
|
||||
});
|
||||
@@ -562,22 +566,6 @@ fn setup_initial_chunks(mut event_writer: EventWriter<GenerateChunkEvent>) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn index_z_to_absolute_z(z: f32) -> f32 {
|
||||
z + Z_BELOW
|
||||
}
|
||||
|
||||
pub fn world_z_to_absolute_z(z: f32) -> f32 {
|
||||
z + Z_BELOW * ITILE_SIZE as f32
|
||||
}
|
||||
|
||||
pub fn absolute_z_to_index_z(z: f32) -> f32 {
|
||||
z - Z_BELOW
|
||||
}
|
||||
|
||||
pub fn absolute_z_to_world_z(z: f32) -> f32 {
|
||||
z - Z_BELOW * ITILE_SIZE as f32
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct VisibleGameEntity;
|
||||
|
||||
@@ -588,13 +576,22 @@ pub fn compute_visibility_of_game_entities(
|
||||
query
|
||||
.par_iter_mut()
|
||||
.for_each(|(transform, mut visibility, mut sprite)| {
|
||||
// if too far away, saturation does not matter
|
||||
if (z_index.0 - transform.translation.z / TILE_SIZE) / 8.0 > 1.0 {
|
||||
let entity_z = (transform.translation.z / TILE_SIZE) - 1.0;
|
||||
// if same z-level, always visible
|
||||
if z_index.0 == entity_z {
|
||||
sprite.color = Color::WHITE;
|
||||
sprite.color.set_alpha(1.0);
|
||||
*visibility = Visibility::Visible;
|
||||
return;
|
||||
};
|
||||
|
||||
// if entity too low, saturation does not matter
|
||||
if z_index.0 - entity_z > 8.0 {
|
||||
*visibility = Visibility::Hidden;
|
||||
return;
|
||||
}
|
||||
// if too high, saturation does not matter
|
||||
if (transform.translation.z) / TILE_SIZE > z_index.0 + 1.0 {
|
||||
// if entity too high, saturation does not matter
|
||||
if entity_z > z_index.0 {
|
||||
*visibility = Visibility::Hidden;
|
||||
return;
|
||||
}
|
||||
@@ -626,7 +623,7 @@ impl Plugin for TilemapPlugin {
|
||||
.add_systems(
|
||||
FixedUpdate,
|
||||
(
|
||||
generate_chunks_from_algo,
|
||||
handle_chunk_events,
|
||||
generate_chunk_terrain,
|
||||
generate_chunk_weathering_and_precipitation,
|
||||
generate_chunk_forrestry,
|
||||
|
||||
+9
-8
@@ -1,4 +1,5 @@
|
||||
use crate::tile::{FixtureTile, FloorTile, TileState};
|
||||
use crate::tilemap::Z_BELOW;
|
||||
use crate::{constants::*, tilemap};
|
||||
use bevy::asset::RenderAssetUsages;
|
||||
use bevy::prelude::*;
|
||||
@@ -33,6 +34,9 @@ const LOG_PATH: &str = "log.png";
|
||||
const LEAVES_PATH: &str = "leaves.png";
|
||||
|
||||
pub fn initialize_textures(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
println!("");
|
||||
println!("####################");
|
||||
let start = Instant::now();
|
||||
let mut textures: HashMap<String, Handle<Image>> = HashMap::new();
|
||||
let mut texture_ids: HashMap<u32, String> = HashMap::new();
|
||||
texture_ids.insert(u32::MAX, DEFAULT_TEXTURE.to_string());
|
||||
@@ -82,6 +86,7 @@ pub fn initialize_textures(mut commands: Commands, asset_server: Res<AssetServer
|
||||
|
||||
commands.insert_resource(Textures { handles: textures });
|
||||
commands.insert_resource(TextureIDs { refs: texture_ids });
|
||||
println!("Textures initialized in {:.2?}", start.elapsed());
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -97,6 +102,7 @@ pub struct CurrentWorldSpriteState {
|
||||
pub state: TerrainSpriteState,
|
||||
}
|
||||
|
||||
use std::process::exit;
|
||||
use std::sync::Mutex;
|
||||
use std::time::Instant;
|
||||
|
||||
@@ -153,9 +159,7 @@ pub fn build_quilted_terrain_sprites(
|
||||
let position = Vec2::new(transform.translation.x, transform.translation.y);
|
||||
|
||||
for z_index in 0..=tilemap::Z_TOTAL as usize {
|
||||
let is_visible =
|
||||
(floortile.visible_range[z_index / 32] & (1 << (z_index % 32) as u32)) != 0;
|
||||
if is_visible {
|
||||
if (floortile.visible_range[z_index / 32] & (1 << (z_index % 32) as u32)) != 0 {
|
||||
tiles_by_z
|
||||
.entry(z_index)
|
||||
.or_default()
|
||||
@@ -306,7 +310,7 @@ pub fn build_quilted_terrain_sprites(
|
||||
Transform::from_xyz(
|
||||
center_x - TILE_SIZE / 2.0,
|
||||
center_y - TILE_SIZE / 2.0,
|
||||
-10.0 * TILE_SIZE,
|
||||
-Z_BELOW * TILE_SIZE,
|
||||
)
|
||||
.with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
Visibility::Hidden,
|
||||
@@ -317,10 +321,7 @@ pub fn build_quilted_terrain_sprites(
|
||||
|
||||
quilt_cache.dirty_indices.clear();
|
||||
cwss.state = TerrainSpriteState::RenderReady;
|
||||
println!(
|
||||
"Quilted world sprites built. Elapsed: {:.2?}",
|
||||
now.elapsed()
|
||||
);
|
||||
println!("Terrain sprites baked in: {:.2?}", now.elapsed());
|
||||
}
|
||||
|
||||
// Helper function to blit a texture onto another texture
|
||||
|
||||
Reference in New Issue
Block a user