Initial tile push with item cycle
This commit is contained in:
+131
@@ -0,0 +1,131 @@
|
||||
use crate::item::Item;
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Sprite)]
|
||||
pub struct Tile {
|
||||
pub position: Vec3,
|
||||
}
|
||||
|
||||
impl Default for Tile {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
position: Vec3::ZERO,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tile {
|
||||
pub fn new(position: Vec3) -> Self {
|
||||
Self {
|
||||
position,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Tile)]
|
||||
pub struct FloorTile {
|
||||
pub walkable: bool,
|
||||
pub astar_weight: u8,
|
||||
}
|
||||
|
||||
impl Default for FloorTile {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
walkable: true,
|
||||
astar_weight: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct WallTile {
|
||||
pub tile: Tile,
|
||||
pub solid: bool,
|
||||
pub embrasure: bool, // Arrowslit, crenelle, grate, cage etc
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct WaterTile {
|
||||
pub tile: Tile,
|
||||
pub swimmable: bool,
|
||||
pub astarmultiplier: f32,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct DoorTile {
|
||||
pub tile: Tile,
|
||||
pub open: bool,
|
||||
pub locked: bool,
|
||||
}
|
||||
|
||||
pub fn spawn_tile(
|
||||
commands: &mut Commands,
|
||||
position: Vec3,
|
||||
floor_tile: FloorTile,
|
||||
// fixture: Option<Fixture>,
|
||||
items: Vec<Item>,
|
||||
) {
|
||||
let tile_entity = commands.spawn((Tile { position }, floor_tile)).id();
|
||||
|
||||
// if let Some(fixture) = fixture {
|
||||
// commands.entity(tile_entity).add_children(&[
|
||||
// commands
|
||||
// .spawn((fixture,))
|
||||
// .id(),
|
||||
// ]);
|
||||
// }
|
||||
|
||||
let mut child_entities = Vec::new();
|
||||
for item in items {
|
||||
child_entities.push(commands.spawn(item).id());
|
||||
}
|
||||
commands.entity(tile_entity).add_children(&child_entities);
|
||||
}
|
||||
|
||||
pub fn tile_update(
|
||||
time: Res<Time>,
|
||||
mut query_tile: Query<(Entity, &Children, &mut TileState), With<Tile>>,
|
||||
mut query_item: Query<(&mut Visibility, &Item)>,
|
||||
) {
|
||||
for (tile_entity, children, mut state) in query_tile.iter_mut() {
|
||||
state.timer.tick(time.delta());
|
||||
if !state.timer.finished() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut visible_index: Option<usize> = None;
|
||||
let mut visible_child: Option<Entity> = None;
|
||||
|
||||
for (i, &child) in children.iter().enumerate() {
|
||||
if let Ok((mut visibility, _)) = query_item.get_mut(child) {
|
||||
if matches!(*visibility, Visibility::Visible) {
|
||||
visible_index = Some(i);
|
||||
visible_child = Some(child);
|
||||
*visibility = Visibility::Hidden;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let next_index = if let Some(current_index) = visible_index {
|
||||
(current_index + 1) % children.len()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
if let Some(&next_child) = children.get(next_index) {
|
||||
if let Ok((mut visibility, _)) = query_item.get_mut(next_child) {
|
||||
*visibility = Visibility::Visible;
|
||||
// println!("{}",next_index.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct TileState {
|
||||
pub timer: Timer,
|
||||
}
|
||||
Reference in New Issue
Block a user