dropped item rotation
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_platform::collections::HashMap;
|
||||
|
||||
use crate::entities::item::decorations::ItemDecorations;
|
||||
use crate::entities::item::material::{GemType, Material, MetalType};
|
||||
@@ -7,6 +8,7 @@ use crate::entities::item::types::ItemType;
|
||||
use crate::entities::item::Item;
|
||||
use crate::entities::item::ItemBundle;
|
||||
use crate::entities::item::ItemName;
|
||||
use crate::world::tiles::TileMap;
|
||||
|
||||
pub fn stain_fade_system(time: Res<Time>, mut items: Query<&mut Item>) {
|
||||
if time.elapsed_secs_f64() as u32 % 10 == 0 {
|
||||
@@ -16,6 +18,20 @@ pub fn stain_fade_system(time: Res<Time>, mut items: Query<&mut Item>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensures all items in item_tiles have the ItemRotationState component.
|
||||
/// This handles items that were spawned before the rotation system was added,
|
||||
/// or items that somehow got missing the component.
|
||||
pub fn initialize_item_rotation_state(
|
||||
mut commands: Commands,
|
||||
items_without_state: Query<(Entity, &Item), Without<ItemRotationState>>,
|
||||
) {
|
||||
for (entity, _item) in items_without_state.iter() {
|
||||
commands.entity(entity).insert(ItemRotationState {
|
||||
should_be_visible: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to create a decorated item (DEMO ONLY)
|
||||
pub fn create_decorated_item(
|
||||
commands: &mut Commands,
|
||||
@@ -66,3 +82,86 @@ pub fn create_decorated_item(
|
||||
.insert(ItemName::new(item_type.name()))
|
||||
.id()
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct ItemRotationTimer {
|
||||
timer: Timer,
|
||||
current_indices: HashMap<IVec3, usize>,
|
||||
}
|
||||
|
||||
impl Default for ItemRotationTimer {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
timer: Timer::from_seconds(0.4f32, TimerMode::Repeating),
|
||||
current_indices: HashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct ItemRotationState {
|
||||
pub should_be_visible: bool,
|
||||
}
|
||||
|
||||
pub fn item_tile_management_system(
|
||||
time: Res<Time>,
|
||||
mut tilemap: ResMut<TileMap>,
|
||||
mut rotation_timer: ResMut<ItemRotationTimer>,
|
||||
mut item_query: Query<&mut ItemRotationState, With<Item>>,
|
||||
) {
|
||||
rotation_timer.timer.tick(time.delta());
|
||||
|
||||
let mut tiles_to_remove = Vec::new();
|
||||
let mut tiles_with_items = Vec::new();
|
||||
|
||||
for (position, items) in tilemap.item_tiles.iter() {
|
||||
if items.is_empty() {
|
||||
tiles_to_remove.push(*position);
|
||||
} else {
|
||||
tiles_with_items.push((*position, items.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
for position in tiles_to_remove {
|
||||
tilemap.item_tiles.remove(&position);
|
||||
rotation_timer.current_indices.remove(&position);
|
||||
}
|
||||
|
||||
if rotation_timer.timer.just_finished() {
|
||||
for (position, items) in tiles_with_items {
|
||||
if items.len() <= 1 {
|
||||
if let Some(&entity_id) = items.first() {
|
||||
let entity = Entity::from_raw(entity_id);
|
||||
if let Ok(mut rotation_state) = item_query.get_mut(entity) {
|
||||
rotation_state.should_be_visible = true;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
let current_index = rotation_timer
|
||||
.current_indices
|
||||
.get(&position)
|
||||
.copied()
|
||||
.unwrap_or(0);
|
||||
|
||||
for &entity_id in &items {
|
||||
let entity = Entity::from_raw(entity_id);
|
||||
if let Ok(mut rotation_state) = item_query.get_mut(entity) {
|
||||
rotation_state.should_be_visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
let next_index = (current_index + 1) % items.len();
|
||||
|
||||
if let Some(&next_entity_id) = items.get(next_index) {
|
||||
let next_entity = Entity::from_raw(next_entity_id);
|
||||
if let Ok(mut rotation_state) = item_query.get_mut(next_entity) {
|
||||
rotation_state.should_be_visible = true;
|
||||
}
|
||||
}
|
||||
|
||||
rotation_timer.current_indices.insert(position, next_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user