example item
This commit is contained in:
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user