use std::time::Duration; use bevy::math::ivec2; use bevy::prelude::*; use crate::control::ai::{Automated, ShouldAct}; use crate::entities::animations::{PositionBundle, PositionTween}; use crate::entities::lifecycle::Player; use crate::entities::timing::ActionCooldown; use crate::world::level_map::{GridPosition, LevelMap, WORLD_TILE_SIZE}; use crate::world::pathing::PathingReqs; pub fn handle_wait( mut commands: Commands, input: Res<Input<KeyCode>>, player_query: Query< (Entity, &GridPosition), (With<Player>, With<ShouldAct>, Without<ActionCooldown>), >, level_query: Query<&LevelMap>, npc_query: Query<Entity, (With<Automated>, Without<ShouldAct>)>, ) { if input.pressed(KeyCode::Space) && let Ok((entity, pos)) = player_query.get_single() { if let Ok(level) = level_query.get_single() { let found = level.flood_fill(pos.0, 5, PathingReqs { can_walk: Some(true), ..Default::default() }); log::info!("Found walkable tiles: {:?}", found); } commands .entity(entity) .insert(ActionCooldown::from(Duration::from_millis(250))) .remove::<ShouldAct>(); for entity in &npc_query { commands.entity(entity).insert(ShouldAct); } } } pub fn handle_player_input( mut commands: Commands, input: Res<Input<KeyCode>>, mut player_query: Query< (Entity, &mut GridPosition), (With<Player>, With<ShouldAct>, Without<ActionCooldown>), >, npc_query: Query<Entity, (With<Automated>, Without<ShouldAct>)>, ) { let mut dx = 0; let mut dy = 0; if input.pressed(KeyCode::D) || input.pressed(KeyCode::Right) { dx += 1; } if input.pressed(KeyCode::W) || input.pressed(KeyCode::Up) { dy += 1; } if input.pressed(KeyCode::A) || input.pressed(KeyCode::Left) { dx -= 1; } if input.pressed(KeyCode::S) || input.pressed(KeyCode::Down) { dy -= 1; } if dx != 0 || dy != 0 { for (entity, mut position) in &mut player_query { if dx != 0 || dy != 0 { let current_position = **position; let next_position = ((current_position.as_ivec2()) + ivec2(dx, dy)).as_uvec2(); **position = next_position; commands .entity(entity) // .insert_bundle(PositionBundle::from(PositionTween::new( // next_position.as_vec2() * WORLD_TILE_SIZE + WORLD_TILE_SIZE / 2.0, // current_position.as_vec2() * WORLD_TILE_SIZE + WORLD_TILE_SIZE / 2.0, // Duration::from_millis(100), // ))) .insert(ActionCooldown::from(Duration::from_millis(250))) .remove::<ShouldAct>(); } } for entity in &npc_query { commands.entity(entity).insert(ShouldAct); } } } pub fn reset_player_action( mut commands: Commands, player_query: Query<Entity, (With<Player>, Without<ShouldAct>)>, npc_query: Query<(), (With<Automated>, With<ShouldAct>)>, ) { if npc_query.iter().len() == 0 { for entity in &player_query { commands.entity(entity).insert(ShouldAct); } } }