example item

This commit is contained in:
2025-09-12 18:04:03 +01:00
parent ee7ac39ad5
commit 8a5884c6d5
19 changed files with 1009 additions and 53 deletions
+37 -3
View File
@@ -1,17 +1,24 @@
use crate::constants::TILE_SIZE;
use crate::constants::*;
use crate::entities::item::{
create_decorated_item, spawn_prefab, FoodType, Item, ItemBundle, ItemDecorations, ItemType,
Material, MiscPrefab, Quality, WoodType,
};
use crate::entities::shared_components::Ambulatory;
use crate::world::VisibleGameEntity;
use bevy::ecs::spawn;
use bevy::prelude::*;
use bevy_rand::prelude::*;
use rand::Rng;
// Pig bundle
#[derive(Bundle)]
pub struct Pig {
ambulatory: Ambulatory,
sprite: Sprite,
transform: Transform,
visibility: Visibility,
drop_timer: PigDropTimer, // NEW
}
impl Pig {
@@ -31,19 +38,24 @@ impl Pig {
},
transform: Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
visibility: Visibility::Hidden,
drop_timer: PigDropTimer(Timer::from_seconds(5.0, TimerMode::Repeating)), // NEW
}
}
}
// Timer component for pigs dropping items
#[derive(Component, Deref, DerefMut)]
pub struct PigDropTimer(pub Timer);
// Spawn a handful of pigs
pub fn spawn_pigs(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut rng_q: Query<&mut Entropy<WyRand>, With<Global>>,
) {
if let Ok(mut rng) = rng_q.single_mut() {
// Spawn a handful of pigs
for _ in 0..5 {
let cit = commands
let pig = commands
.spawn(Pig::new(
&asset_server,
Vec3::new(
@@ -53,7 +65,29 @@ pub fn spawn_pigs(
) * TILE_SIZE,
))
.id();
commands.entity(cit).insert(VisibleGameEntity);
commands.entity(pig).insert(VisibleGameEntity);
}
}
}
// System: make pigs drop items every 5 seconds
pub fn pig_drop_system(
mut commands: Commands,
asset_server: Res<AssetServer>,
time: Res<Time>,
mut pigs: Query<(&mut PigDropTimer, &Transform)>,
) {
for (mut timer, transform) in &mut pigs {
timer.tick(time.delta());
if timer.just_finished() {
// Example: pig drops raw meat
spawn_prefab(
&mut commands,
&asset_server,
MiscPrefab::RawMeat,
transform.translation,
);
}
}
}
+2 -2
View File
@@ -43,7 +43,7 @@ pub fn spawn_rabbits(
if let Ok(mut rng) = rng_q.single_mut() {
// Spawn a handful of rabbits
for _ in 0..15 {
let cit = commands
let rab = commands
.spawn(Rabbit::new(
&asset_server,
Vec3::new(
@@ -53,7 +53,7 @@ pub fn spawn_rabbits(
) * TILE_SIZE,
))
.id();
commands.entity(cit).insert(VisibleGameEntity);
commands.entity(rab).insert(VisibleGameEntity);
}
}
}