This commit is contained in:
2026-03-22 12:56:52 +00:00
parent 360ffc7526
commit 2eaf0196a5
7 changed files with 420 additions and 23 deletions
+64
View File
@@ -0,0 +1,64 @@
//! Log cargo spawning — called when a tree is felled.
//!
//! Each trunk tile that was removed becomes a Cargo log entity placed on
//! that tile. Logs are Haulable — dorfs pick them up into their HaulSlot.
//!
//! The log sprite reuses the trunk sprite (texture ID 500004) as a
//! placeholder until a dedicated ground-log sprite exists.
use bevy::prelude::*;
use crate::constants::PIXEL_RATIO;
use crate::entities::cargo::{Cargo, Haulable};
use crate::entities::item::constants::ITEM_Z_FIGHTING_OFFSET;
use crate::entities::item::inventory::constants::SIZE_LARGE;
use crate::world::tiles::TileMap;
/// Weight of a single log in kg. Enough to encumber a dorf carrying one.
pub const LOG_WEIGHT_KG: u32 = 15;
/// Spawn a Cargo log entity at `tile_pos` and register it in cargo_tiles.
///
/// If the tile is already occupied (another log landed here), finds the
/// nearest free tile via TileMap::find_nearest_free_cargo_tile.
///
/// Returns the spawned Entity, or None if no free tile found within radius.
pub fn spawn_log_cargo(
commands: &mut Commands,
tilemap: &mut TileMap,
tile_pos: IVec3,
log_sprite: Handle<Image>,
) -> Option<Entity> {
// Find a free tile — the exact trunk position may be occupied
let drop_pos = tilemap.find_nearest_free_cargo_tile(tile_pos, 4)?;
let entity = commands
.spawn((
Cargo {
tile_pos: drop_pos,
name: "log",
weight: LOG_WEIGHT_KG,
size: SIZE_LARGE,
ground_sprite: log_sprite.clone(),
},
Haulable,
Sprite {
image: log_sprite.clone(),
..Default::default()
},
Transform::from_translation(Vec3::new(
drop_pos.x as f32,
drop_pos.y as f32,
drop_pos.z as f32 + ITEM_Z_FIGHTING_OFFSET,
))
.with_scale(Vec3::splat(PIXEL_RATIO)),
Visibility::Visible,
))
.id();
tilemap
.place_cargo(drop_pos, entity)
.expect("place_cargo failed after find_nearest_free_cargo_tile succeeded");
Some(entity)
}
+2
View File
@@ -1,8 +1,10 @@
pub mod components;
pub mod log;
pub mod systems;
pub use crate::world::tiles::tilemap::CargoPlaceError;
pub use components::{Cargo, CarryVisualState, HaulSlot, Haulable};
pub use log::spawn_log_cargo;
pub use systems::{any_hauling, carry_visual_system, haul_encumbrance_system};
pub use crate::plugins::cargo::CargoPlugin;