98 lines
3.4 KiB
Rust
98 lines
3.4 KiB
Rust
use bevy::prelude::*;
|
|
|
|
use crate::entities::item::{
|
|
Item, ItemBundle, ItemDecorations, ItemRotationState, ItemType, Material, Quality,
|
|
};
|
|
use crate::world::tiles::tilemap;
|
|
use crate::world::VisibleGameEntity;
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub enum MiscPrefab {
|
|
RawMeat,
|
|
Coin,
|
|
}
|
|
|
|
// Spawns a misc prefab at a given position.
|
|
// These are all likely temporary to speed up development but will be replaced by semi-custom items everywhere.
|
|
// As well as acting as demonstration of how to spawn items for the future.
|
|
pub fn spawn_prefab(
|
|
commands: &mut Commands,
|
|
asset_server: &Res<AssetServer>,
|
|
prefab: MiscPrefab,
|
|
position: Vec3,
|
|
tilemap: &mut ResMut<tilemap::TileMap>,
|
|
) -> Entity {
|
|
match prefab {
|
|
MiscPrefab::RawMeat => {
|
|
let meat = commands
|
|
.spawn(ItemBundle {
|
|
item: Item {
|
|
quality: Quality::Ordinary,
|
|
..Item::default()
|
|
},
|
|
item_type: ItemType::Food(crate::entities::item::FoodType::Meat),
|
|
base_material: Material::NA,
|
|
decorations: ItemDecorations::new(),
|
|
transform: Transform::from_translation(position - Vec3::new(0.0, 0.0, 0.1)),
|
|
visibility: Visibility::Visible,
|
|
sprite: Sprite {
|
|
image: asset_server.load("raw_meat.png"),
|
|
..Default::default()
|
|
},
|
|
})
|
|
.id();
|
|
let mut items = tilemap
|
|
.item_tiles
|
|
.get(&position.as_ivec3())
|
|
.unwrap_or(&Vec::new())
|
|
.clone();
|
|
items.push(meat.index_u32());
|
|
tilemap
|
|
.item_tiles
|
|
.insert(position.as_ivec3(), items.clone());
|
|
return commands
|
|
.entity(meat)
|
|
.insert(VisibleGameEntity)
|
|
.insert(ItemRotationState {
|
|
should_be_visible: true,
|
|
})
|
|
.id();
|
|
}
|
|
MiscPrefab::Coin => {
|
|
let coin = commands
|
|
.spawn(ItemBundle {
|
|
item: Item {
|
|
quality: Quality::Ordinary,
|
|
..Item::default()
|
|
},
|
|
item_type: ItemType::Coin,
|
|
base_material: Material::Metal(crate::entities::item::MetalType::Copper),
|
|
decorations: ItemDecorations::new(),
|
|
transform: Transform::from_translation(position - Vec3::new(0.0, 0.0, 0.1)),
|
|
visibility: Visibility::Visible,
|
|
sprite: Sprite {
|
|
image: asset_server.load("coin.png"),
|
|
..Default::default()
|
|
},
|
|
})
|
|
.id();
|
|
let mut items = tilemap
|
|
.item_tiles
|
|
.get(&position.as_ivec3())
|
|
.unwrap_or(&Vec::new())
|
|
.clone();
|
|
items.push(coin.index_u32());
|
|
tilemap
|
|
.item_tiles
|
|
.insert(position.as_ivec3(), items.clone());
|
|
commands
|
|
.entity(coin)
|
|
.insert(VisibleGameEntity)
|
|
.insert(ItemRotationState {
|
|
should_be_visible: true,
|
|
})
|
|
.id()
|
|
}
|
|
}
|
|
}
|