use bevy::prelude::*;
use game_library::{state::Game, Health, Xp};
use super::{
animation,
avatar::{self, PlayerAvatar},
menu_control, movement,
player_control::PlayerControlsPlugin,
player_creation,
};
use crate::{camera::MainCamera, despawn_with_tag};
pub struct PlayerPlugin;
impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app
.add_systems(Startup, player_creation::spawn_player_controller)
.add_plugins(PlayerControlsPlugin)
.add_systems(Update, (menu_control::menu_input,))
.add_systems(OnEnter(Game::Playing), avatar::spawn_player_avatar)
.add_systems(
Update,
(
movement::player_movement_controls,
movement::update_player_z_index,
animation::advance_animation_timer,
animation::set_casting_animation,
animation::update_avatar_animation,
)
.run_if(in_state(Game::Playing)),
)
.add_systems(OnExit(Game::Playing), despawn_with_tag::<PlayerAvatar>)
.add_state::<animation::PlayerAnimation>()
.add_state::<animation::PlayerAnimationSupplemental>()
.add_state::<animation::PlayerFacing>()
.init_resource::<animation::AnimationFrame>()
.insert_resource(animation::PlayerAnimationTimer(Timer::from_seconds(
0.1,
TimerMode::Repeating,
)));
app.add_systems(Update, subtract_health)
.add_systems(PostUpdate, camera_movement.run_if(in_state(Game::Playing)));
}
}
fn camera_movement(
mut camera: Query<&mut Transform, (With<MainCamera>, Without<PlayerAvatar>)>,
player: Query<&Transform, With<PlayerAvatar>>,
) {
for mut transform in &mut camera {
for player_transform in &player {
transform.translation.x = player_transform.translation.x;
transform.translation.y = player_transform.translation.y;
}
}
}
fn subtract_health(
mut player_health: Query<&mut Health, With<PlayerAvatar>>,
mut player_xp: Query<&mut Xp, With<PlayerAvatar>>,
keyboard_input: Res<Input<KeyCode>>,
) {
if keyboard_input.just_pressed(KeyCode::H) {
for mut health in &mut player_health {
health.value -= 1;
}
}
if keyboard_input.just_pressed(KeyCode::X) {
for mut xp in &mut player_xp {
xp.add(1);
}
}
}