dropped item rotation

This commit is contained in:
2026-02-15 18:56:38 +00:00
parent 975ac20876
commit 886d162963
6 changed files with 146 additions and 97 deletions
+2 -1
View File
@@ -49,7 +49,8 @@ impl Plugin for WorldPlugin {
.add_systems(
Update,
(
compute_visibility_of_game_entities,
compute_visibility_of_game_entities
.after(crate::entities::item::item_tile_management_system),
(
handle_tile_occlusion_updates,
build_quilted_terrain_sprites,
+1 -86
View File
@@ -1,94 +1,9 @@
use bevy::prelude::*;
use bevy_platform::collections::hash_map::HashMap;
use crate::{entities::item::Item, game};
#[derive(Resource, Default, Clone)]
pub struct TileMap {
pub floor_tiles: HashMap<IVec3, (i32, bool, bool, bool, i32, [u32; 8])>, //id, canStandIn, canStandOn, visiblyTransparent, astar_weight, visible_range
pub fixture_tiles: HashMap<IVec3, (i32, bool, bool, [u32; 8])>, // id, canStandIn, canStandOn, visible_range //TODO - consider moving alot of this into the fixtures components
pub fixture_tiles: HashMap<IVec3, (i32, bool, bool, [u32; 8])>, // id, canStandIn, canStandOn, visible_range
pub item_tiles: HashMap<IVec3, Vec<u32>>, // Entity.id's of items on this tile
}
#[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(),
}
}
}
// TODO - broken, doesnt work with the visibleGameEntity system
pub fn item_tile_management_system(
time: Res<Time>,
mut tilemap: ResMut<TileMap>,
mut rotation_timer: ResMut<ItemRotationTimer>,
z_index: Res<game::ZIndex>,
mut visibility_query: Query<&mut Visibility, 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 position.z > z_index.0 as i32 {
continue;
}
if items.len() <= 1 {
if let Some(&entity_id) = items.first() {
let entity = Entity::from_raw(entity_id);
if let Ok(mut visibility) = visibility_query.get_mut(entity) {
*visibility = Visibility::Visible;
}
}
continue;
}
let current_index = rotation_timer
.current_indices
.get(&position)
.copied()
.unwrap_or(0);
if let Some(&current_entity_id) = items.get(current_index) {
let current_entity = Entity::from_raw(current_entity_id);
if let Ok(mut visibility) = visibility_query.get_mut(current_entity) {
*visibility = Visibility::Hidden;
}
}
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 visibility) = visibility_query.get_mut(next_entity) {
*visibility = Visibility::Visible;
}
}
rotation_timer.current_indices.insert(position, next_index);
}
}
}
+19 -3
View File
@@ -1,5 +1,6 @@
use crate::{
constants::{ITILE_SIZE, TILE_SIZE},
entities::item::ItemRotationState,
game,
world::{
chunks::{Z_ABOVE, Z_BELOW, Z_TOTAL},
@@ -14,12 +15,28 @@ use bevy_platform::time::Instant;
pub struct VisibleGameEntity;
pub fn compute_visibility_of_game_entities(
mut query: Query<(&Transform, &mut Visibility, &mut Sprite), With<VisibleGameEntity>>,
mut query: Query<
(
&Transform,
&mut Visibility,
&mut Sprite,
Option<&ItemRotationState>,
),
With<VisibleGameEntity>,
>,
z_index: Res<game::ZIndex>,
) {
query
.par_iter_mut()
.for_each(|(transform, mut visibility, mut sprite)| {
.for_each(|(transform, mut visibility, mut sprite, rotation_state)| {
// Check if this item should be hidden due to rotation
if let Some(rotation_state) = rotation_state {
if !rotation_state.should_be_visible {
*visibility = Visibility::Hidden;
return;
}
}
let entity_z = (transform.translation.z / TILE_SIZE) - 1.0;
// if same z-level, always visible
if z_index.0 == entity_z {
@@ -46,7 +63,6 @@ pub fn compute_visibility_of_game_entities(
((z_index.0 - (transform.translation.z / TILE_SIZE) + 1.) / 8.0).clamp(0.0, 1.0);
sprite.color = Color::hsv(194.7, saturation, 1.0 - (saturation / 2.0));
sprite.color.set_alpha(1.0 - saturation);
});
}