ammo system
* heads have abilities * health and ammo shows in ui
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use super::{Projectile, TriggerState};
|
||||
use super::{Projectile, TriggerGun};
|
||||
use crate::{
|
||||
GameState,
|
||||
aim::AimState,
|
||||
@@ -50,77 +50,60 @@ fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Spr
|
||||
}
|
||||
|
||||
fn on_trigger_state(
|
||||
trigger: Trigger<TriggerState>,
|
||||
trigger: Trigger<TriggerGun>,
|
||||
mut commands: Commands,
|
||||
aim: Res<AimState>,
|
||||
player_rot: Query<&Transform, With<PlayerRig>>,
|
||||
player: Query<(&Transform, &Player)>,
|
||||
target_transform: Query<&Transform, (Without<Player>, Without<PlayerRig>)>,
|
||||
time: Res<Time>,
|
||||
mut polyline_materials: ResMut<Assets<PolylineMaterial>>,
|
||||
mut polylines: ResMut<Assets<Polyline>>,
|
||||
) {
|
||||
if matches!(trigger.event(), TriggerState::Active) {
|
||||
let Some((player_pos, player_head)) =
|
||||
player.iter().next().map(|(t, p)| (t.translation, p.0))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let state = trigger.0;
|
||||
|
||||
// TODO: proper weapon classes
|
||||
if ![2, 3, 9].contains(&player_head) {
|
||||
return;
|
||||
}
|
||||
commands.trigger(PlaySound::Gun);
|
||||
|
||||
commands.trigger(PlaySound::Gun);
|
||||
let rotation = if let Some(target) = aim.target {
|
||||
let t = target_transform
|
||||
.get(target)
|
||||
.expect("target must have transform");
|
||||
Transform::from_translation(state.pos)
|
||||
.looking_at(t.translation, Vec3::Y)
|
||||
.rotation
|
||||
} else {
|
||||
state.rot.mul_quat(Quat::from_rotation_y(PI))
|
||||
};
|
||||
|
||||
let Some(rotation) = player_rot.iter().next().map(|t| t.rotation) else {
|
||||
return;
|
||||
};
|
||||
let mut t = Transform::from_translation(state.pos).with_rotation(rotation);
|
||||
t.translation += t.forward().as_vec3() * 2.;
|
||||
|
||||
let rotation = if let Some(target) = aim.target {
|
||||
let t = target_transform
|
||||
.get(target)
|
||||
.expect("target must have transform");
|
||||
Transform::from_translation(player_pos)
|
||||
.looking_at(t.translation, Vec3::Y)
|
||||
.rotation
|
||||
} else {
|
||||
rotation.mul_quat(Quat::from_rotation_y(PI))
|
||||
};
|
||||
|
||||
let mut t = Transform::from_translation(player_pos).with_rotation(rotation);
|
||||
t.translation += t.forward().as_vec3() * 2.;
|
||||
|
||||
commands
|
||||
.spawn((
|
||||
Name::new("projectile-gun"),
|
||||
GunProjectile {
|
||||
time: time.elapsed_secs(),
|
||||
},
|
||||
Projectile { damage: 10 },
|
||||
Collider::capsule_endpoints(0.5, Vec3::new(0., 0., 0.), Vec3::new(0., 0., -3.)),
|
||||
CollisionLayers::new(
|
||||
LayerMask(GameLayer::Projectile.to_bits()),
|
||||
LayerMask(GameLayer::Npc.to_bits() | GameLayer::Level.to_bits()),
|
||||
),
|
||||
Sensor,
|
||||
Visibility::default(),
|
||||
t,
|
||||
))
|
||||
.with_child(PolylineBundle {
|
||||
polyline: PolylineHandle(polylines.add(Polyline {
|
||||
vertices: vec![Vec3::Z * 2., Vec3::Z * -2.],
|
||||
})),
|
||||
material: PolylineMaterialHandle(polyline_materials.add(PolylineMaterial {
|
||||
width: 10.0,
|
||||
color: LinearRgba::rgb(0.9, 0.9, 0.),
|
||||
perspective: false,
|
||||
..default()
|
||||
})),
|
||||
commands
|
||||
.spawn((
|
||||
Name::new("projectile-gun"),
|
||||
GunProjectile {
|
||||
time: time.elapsed_secs(),
|
||||
},
|
||||
Projectile { damage: 10 },
|
||||
Collider::capsule_endpoints(0.5, Vec3::new(0., 0., 0.), Vec3::new(0., 0., -3.)),
|
||||
CollisionLayers::new(
|
||||
LayerMask(GameLayer::Projectile.to_bits()),
|
||||
LayerMask(GameLayer::Npc.to_bits() | GameLayer::Level.to_bits()),
|
||||
),
|
||||
Sensor,
|
||||
Visibility::default(),
|
||||
t,
|
||||
))
|
||||
.with_child(PolylineBundle {
|
||||
polyline: PolylineHandle(polylines.add(Polyline {
|
||||
vertices: vec![Vec3::Z * 2., Vec3::Z * -2.],
|
||||
})),
|
||||
material: PolylineMaterialHandle(polyline_materials.add(PolylineMaterial {
|
||||
width: 10.0,
|
||||
color: LinearRgba::rgb(0.9, 0.9, 0.),
|
||||
perspective: false,
|
||||
..default()
|
||||
});
|
||||
}
|
||||
})),
|
||||
..default()
|
||||
});
|
||||
}
|
||||
|
||||
fn update(mut query: Query<&mut Transform, With<GunProjectile>>) {
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
mod gun;
|
||||
mod thrown;
|
||||
|
||||
use crate::{GameState, npc::Hit, tb_entities::EnemySpawn};
|
||||
use crate::{
|
||||
GameState,
|
||||
active_heads::ActiveHeads,
|
||||
npc::Hit,
|
||||
player::{Player, PlayerRig},
|
||||
tb_entities::EnemySpawn,
|
||||
};
|
||||
use avian3d::prelude::*;
|
||||
use bevy::prelude::*;
|
||||
|
||||
@@ -16,11 +22,33 @@ pub enum TriggerState {
|
||||
Inactive,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Reflect)]
|
||||
pub enum HeadAbility {
|
||||
None,
|
||||
Arrow,
|
||||
Thrown,
|
||||
Gun,
|
||||
}
|
||||
|
||||
#[derive(Debug, Reflect, Clone, Copy)]
|
||||
pub struct PlayerTriggerState {
|
||||
dir: Dir3,
|
||||
rot: Quat,
|
||||
pos: Vec3,
|
||||
}
|
||||
|
||||
#[derive(Event, Reflect)]
|
||||
pub struct TriggerGun(pub PlayerTriggerState);
|
||||
#[derive(Event, Reflect)]
|
||||
pub struct TriggerThrow(pub PlayerTriggerState);
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_plugins(gun::plugin);
|
||||
app.add_plugins(thrown::plugin);
|
||||
|
||||
app.add_systems(Update, enemy_hit.run_if(in_state(GameState::Playing)));
|
||||
|
||||
app.add_observer(on_trigger_state);
|
||||
}
|
||||
|
||||
fn enemy_hit(
|
||||
@@ -47,3 +75,44 @@ fn enemy_hit(
|
||||
commands.entity(enemy_entity).trigger(Hit { damage });
|
||||
}
|
||||
}
|
||||
|
||||
fn on_trigger_state(
|
||||
trigger: Trigger<TriggerState>,
|
||||
mut commands: Commands,
|
||||
player_rot: Query<&Transform, With<PlayerRig>>,
|
||||
player_transform: Query<&Transform, With<Player>>,
|
||||
mut active_heads: ResMut<ActiveHeads>,
|
||||
) {
|
||||
if matches!(trigger.event(), TriggerState::Active) {
|
||||
let Some(state) = active_heads.current() else {
|
||||
return;
|
||||
};
|
||||
|
||||
if !state.has_ammo() {
|
||||
//TOOD: play sound
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(transform) = player_transform.iter().next().copied() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some((rot, dir)) = player_rot.iter().next().map(|t| (t.rotation, t.forward())) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let trigger_state = PlayerTriggerState {
|
||||
dir,
|
||||
rot,
|
||||
pos: transform.translation,
|
||||
};
|
||||
|
||||
active_heads.use_ammo();
|
||||
|
||||
match state.ability {
|
||||
HeadAbility::Thrown => commands.trigger(TriggerThrow(trigger_state)),
|
||||
HeadAbility::Gun => commands.trigger(TriggerGun(trigger_state)),
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::TriggerState;
|
||||
use super::TriggerThrow;
|
||||
use crate::{
|
||||
GameState,
|
||||
aim::AimState,
|
||||
@@ -83,77 +83,58 @@ fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Spr
|
||||
}
|
||||
|
||||
fn on_trigger_state(
|
||||
trigger: Trigger<TriggerState>,
|
||||
trigger: Trigger<TriggerThrow>,
|
||||
mut commands: Commands,
|
||||
aim: Res<AimState>,
|
||||
player_rot: Query<&Transform, With<PlayerRig>>,
|
||||
player: Query<(&Transform, &Player)>,
|
||||
target_transform: Query<&Transform, (Without<Player>, Without<PlayerRig>)>,
|
||||
assets: Res<GameAssets>,
|
||||
) {
|
||||
if matches!(trigger.event(), TriggerState::Active) {
|
||||
let Some((player_pos, player_head)) =
|
||||
player.iter().next().map(|(t, p)| (t.translation, p.0))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let state = trigger.event().0;
|
||||
|
||||
// TODO: proper weapon classes
|
||||
if ![0, 8, 16, 17].contains(&player_head) {
|
||||
return;
|
||||
}
|
||||
commands.trigger(PlaySound::Throw);
|
||||
|
||||
commands.trigger(PlaySound::Throw);
|
||||
const SPEED: f32 = 35.;
|
||||
|
||||
let Some((rotation, player_forward)) =
|
||||
player_rot.iter().next().map(|t| (t.rotation, t.forward()))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let vel = if let Some(target) = aim.target {
|
||||
let t = target_transform
|
||||
.get(target)
|
||||
.expect("target must have transform");
|
||||
|
||||
const SPEED: f32 = 35.;
|
||||
launch_velocity(
|
||||
state.pos + state.dir.as_vec3() * 2.,
|
||||
t.translation,
|
||||
SPEED,
|
||||
9.81,
|
||||
)
|
||||
.map(|(low, _)| low)
|
||||
.unwrap()
|
||||
} else {
|
||||
state.rot.mul_quat(Quat::from_rotation_y(-PI / 2.))
|
||||
* (Vec3::new(2., 1., 0.).normalize() * SPEED)
|
||||
};
|
||||
|
||||
let vel = if let Some(target) = aim.target {
|
||||
let t = target_transform
|
||||
.get(target)
|
||||
.expect("target must have transform");
|
||||
let t = Transform::from_translation(state.pos);
|
||||
|
||||
launch_velocity(
|
||||
player_pos + player_forward.as_vec3() * 2.,
|
||||
t.translation,
|
||||
SPEED,
|
||||
9.81,
|
||||
)
|
||||
.map(|(low, _)| low)
|
||||
.unwrap()
|
||||
} else {
|
||||
rotation.mul_quat(Quat::from_rotation_y(-PI / 2.))
|
||||
* (Vec3::new(2., 1., 0.).normalize() * SPEED)
|
||||
};
|
||||
|
||||
let t = Transform::from_translation(player_pos);
|
||||
|
||||
commands
|
||||
.spawn((
|
||||
Name::new("projectile-thrown"),
|
||||
ThrownProjectile,
|
||||
Collider::sphere(0.5),
|
||||
CollisionLayers::new(
|
||||
LayerMask(GameLayer::Projectile.to_bits()),
|
||||
LayerMask(GameLayer::Npc.to_bits() | GameLayer::Level.to_bits()),
|
||||
),
|
||||
RigidBody::Dynamic,
|
||||
Mass(0.01),
|
||||
LinearVelocity(vel),
|
||||
Visibility::default(),
|
||||
t,
|
||||
))
|
||||
.with_child((
|
||||
AutoRotation(Quat::from_rotation_x(0.4) * Quat::from_rotation_z(0.3)),
|
||||
Transform::from_scale(Vec3::splat(10.)),
|
||||
SceneRoot(assets.molotov.clone()),
|
||||
));
|
||||
}
|
||||
commands
|
||||
.spawn((
|
||||
Name::new("projectile-thrown"),
|
||||
ThrownProjectile,
|
||||
Collider::sphere(0.5),
|
||||
CollisionLayers::new(
|
||||
LayerMask(GameLayer::Projectile.to_bits()),
|
||||
LayerMask(GameLayer::Npc.to_bits() | GameLayer::Level.to_bits()),
|
||||
),
|
||||
RigidBody::Dynamic,
|
||||
Mass(0.01),
|
||||
LinearVelocity(vel),
|
||||
Visibility::default(),
|
||||
t,
|
||||
))
|
||||
.with_child((
|
||||
AutoRotation(Quat::from_rotation_x(0.4) * Quat::from_rotation_z(0.3)),
|
||||
Transform::from_scale(Vec3::splat(10.)),
|
||||
SceneRoot(assets.molotov.clone()),
|
||||
));
|
||||
}
|
||||
|
||||
fn shot_collision(
|
||||
|
||||
138
src/active_heads.rs
Normal file
138
src/active_heads.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
use crate::{
|
||||
abilities::HeadAbility,
|
||||
backpack::{BackbackSwapEvent, Backpack},
|
||||
sounds::PlaySound,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub static HEAD_COUNT: usize = 18;
|
||||
pub static HEAD_SLOTS: usize = 5;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Reflect)]
|
||||
pub struct HeadState {
|
||||
pub head: usize,
|
||||
pub ability: HeadAbility,
|
||||
pub health: u32,
|
||||
pub health_max: u32,
|
||||
pub ammo: u32,
|
||||
pub ammo_max: u32,
|
||||
}
|
||||
|
||||
impl HeadState {
|
||||
pub fn new(head: usize, ammo: u32) -> Self {
|
||||
Self {
|
||||
head,
|
||||
health: 100,
|
||||
health_max: 100,
|
||||
ammo,
|
||||
ammo_max: ammo,
|
||||
ability: HeadAbility::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_ability(mut self, ability: HeadAbility) -> Self {
|
||||
self.ability = ability;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn has_ammo(&self) -> bool {
|
||||
self.ammo > 0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Default, Reflect)]
|
||||
#[reflect(Resource)]
|
||||
pub struct ActiveHeads {
|
||||
heads: [Option<HeadState>; 5],
|
||||
current_slot: usize,
|
||||
}
|
||||
|
||||
impl ActiveHeads {
|
||||
pub fn current(&self) -> Option<HeadState> {
|
||||
self.heads[self.current_slot]
|
||||
}
|
||||
|
||||
pub fn use_ammo(&mut self) {
|
||||
let Some(head) = &mut self.heads[self.current_slot] else {
|
||||
error!("cannot use ammo of empty head");
|
||||
return;
|
||||
};
|
||||
|
||||
head.ammo = head.ammo.saturating_sub(1);
|
||||
}
|
||||
|
||||
pub fn slot(&self) -> usize {
|
||||
self.current_slot
|
||||
}
|
||||
|
||||
pub fn head(&self, slot: usize) -> Option<HeadState> {
|
||||
self.heads[slot]
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Event, Reflect)]
|
||||
pub enum SelectActiveHead {
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Event)]
|
||||
pub struct HeadChanged(pub usize);
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.insert_resource(ActiveHeads {
|
||||
heads: [
|
||||
Some(HeadState::new(0, 10).with_ability(HeadAbility::Thrown)),
|
||||
Some(HeadState::new(3, 10).with_ability(HeadAbility::Gun)),
|
||||
Some(HeadState::new(6, 10).with_ability(HeadAbility::Arrow)),
|
||||
Some(HeadState::new(8, 10).with_ability(HeadAbility::Thrown)),
|
||||
Some(HeadState::new(9, 10).with_ability(HeadAbility::Gun)),
|
||||
],
|
||||
current_slot: 0,
|
||||
});
|
||||
|
||||
app.add_observer(on_select_active_head);
|
||||
app.add_observer(on_swap_backpack);
|
||||
}
|
||||
|
||||
fn on_select_active_head(
|
||||
trigger: Trigger<SelectActiveHead>,
|
||||
mut commands: Commands,
|
||||
mut res: ResMut<ActiveHeads>,
|
||||
) {
|
||||
match trigger.event() {
|
||||
SelectActiveHead::Right => {
|
||||
res.current_slot = (res.current_slot + 1) % HEAD_SLOTS;
|
||||
}
|
||||
SelectActiveHead::Left => {
|
||||
res.current_slot = (res.current_slot + (HEAD_SLOTS - 1)) % HEAD_SLOTS;
|
||||
}
|
||||
}
|
||||
|
||||
commands.trigger(PlaySound::Selection);
|
||||
commands.trigger(HeadChanged(res.heads[res.current_slot].unwrap().head));
|
||||
}
|
||||
|
||||
fn on_swap_backpack(
|
||||
trigger: Trigger<BackbackSwapEvent>,
|
||||
mut commands: Commands,
|
||||
mut res: ResMut<ActiveHeads>,
|
||||
mut backpack: ResMut<Backpack>,
|
||||
) {
|
||||
let backpack_slot = trigger.event().0;
|
||||
|
||||
let head = backpack.heads.get(backpack_slot).unwrap();
|
||||
|
||||
let current_active_slot = res.current_slot;
|
||||
|
||||
let current_active_head = res.heads[current_active_slot];
|
||||
res.heads[current_active_slot] = Some(*head);
|
||||
|
||||
if let Some(old_active) = current_active_head {
|
||||
backpack.heads[backpack_slot] = old_active;
|
||||
} else {
|
||||
backpack.heads.remove(backpack_slot);
|
||||
}
|
||||
|
||||
commands.trigger(HeadChanged(res.heads[res.current_slot].unwrap().head));
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::AimState;
|
||||
use crate::{
|
||||
GameState,
|
||||
backpack::BackpackHead,
|
||||
backpack::UiHeadState,
|
||||
heads_ui::HeadsImages,
|
||||
loading_assets::UIAssets,
|
||||
npc::{Hitpoints, NpcHead},
|
||||
@@ -18,7 +18,7 @@ struct HeadDamage;
|
||||
|
||||
#[derive(Resource, Default, PartialEq)]
|
||||
struct TargetUi {
|
||||
head: Option<BackpackHead>,
|
||||
head: Option<UiHeadState>,
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
@@ -150,7 +150,7 @@ fn sync(
|
||||
let mut new_state = None;
|
||||
if let Some(e) = aim.target {
|
||||
if let Ok((hp, head)) = target_data.get(e) {
|
||||
new_state = Some(BackpackHead {
|
||||
new_state = Some(UiHeadState {
|
||||
head: head.0,
|
||||
health: hp.health(),
|
||||
ammo: 1.,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{BackbackSwapEvent, Backpack, BackpackHead};
|
||||
use super::{BackbackSwapEvent, Backpack, UiHeadState};
|
||||
use crate::{GameState, heads_ui::HeadsImages, loading_assets::UIAssets, sounds::PlaySound};
|
||||
use bevy::prelude::*;
|
||||
|
||||
@@ -29,7 +29,7 @@ struct HeadDamage(pub usize);
|
||||
|
||||
#[derive(Resource, Default, Debug)]
|
||||
struct BackpackUiState {
|
||||
heads: [Option<BackpackHead>; 5],
|
||||
heads: [Option<UiHeadState>; 5],
|
||||
scroll: usize,
|
||||
count: usize,
|
||||
current_slot: usize,
|
||||
@@ -316,7 +316,7 @@ fn sync(backpack: &Res<Backpack>, state: &mut ResMut<BackpackUiState>) {
|
||||
|
||||
for i in 0..HEAD_SLOTS {
|
||||
if let Some(head) = backpack.heads.get(i + state.scroll) {
|
||||
state.heads[i] = Some(*head);
|
||||
state.heads[i] = Some(UiHeadState::from(*head));
|
||||
} else {
|
||||
state.heads[i] = None;
|
||||
}
|
||||
|
||||
@@ -1,38 +1,18 @@
|
||||
mod backpack_ui;
|
||||
mod ui_head_state;
|
||||
|
||||
use crate::{GameState, heads_ui::HEAD_COUNT};
|
||||
use crate::{
|
||||
GameState,
|
||||
active_heads::{HEAD_COUNT, HeadState},
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub use backpack_ui::BackpackAction;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Reflect)]
|
||||
pub struct BackpackHead {
|
||||
pub head: usize,
|
||||
pub health: f32,
|
||||
pub ammo: f32,
|
||||
}
|
||||
|
||||
impl BackpackHead {
|
||||
pub fn new(head: usize) -> Self {
|
||||
Self {
|
||||
head,
|
||||
health: 1.0,
|
||||
ammo: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn damage(&self) -> f32 {
|
||||
1. - self.health
|
||||
}
|
||||
|
||||
pub fn ammo_used(&self) -> f32 {
|
||||
1. - self.ammo
|
||||
}
|
||||
}
|
||||
pub use ui_head_state::UiHeadState;
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct Backpack {
|
||||
pub heads: Vec<BackpackHead>,
|
||||
pub heads: Vec<HeadState>,
|
||||
}
|
||||
|
||||
#[derive(Event)]
|
||||
@@ -47,11 +27,7 @@ pub fn plugin(app: &mut App) {
|
||||
fn setup(mut commands: Commands) {
|
||||
commands.insert_resource(Backpack {
|
||||
heads: (0usize..HEAD_COUNT)
|
||||
.map(|i| BackpackHead {
|
||||
head: i,
|
||||
health: 1.,
|
||||
ammo: 1.,
|
||||
})
|
||||
.map(|i| HeadState::new(i, 10))
|
||||
.collect(),
|
||||
});
|
||||
}
|
||||
|
||||
30
src/backpack/ui_head_state.rs
Normal file
30
src/backpack/ui_head_state.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::active_heads::HeadState;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Reflect, Default)]
|
||||
pub struct UiHeadState {
|
||||
pub head: usize,
|
||||
pub health: f32,
|
||||
pub ammo: f32,
|
||||
}
|
||||
|
||||
impl UiHeadState {
|
||||
pub fn damage(&self) -> f32 {
|
||||
1. - self.health
|
||||
}
|
||||
|
||||
pub fn ammo_used(&self) -> f32 {
|
||||
1. - self.ammo
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HeadState> for UiHeadState {
|
||||
fn from(value: HeadState) -> Self {
|
||||
Self {
|
||||
head: value.head,
|
||||
ammo: value.ammo as f32 / value.ammo_max as f32,
|
||||
health: value.health as f32 / value.health_max as f32,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
GameState, abilities::TriggerState, backpack::BackpackAction, heads_ui::SelectActiveHead,
|
||||
GameState, abilities::TriggerState, active_heads::SelectActiveHead, backpack::BackpackAction,
|
||||
};
|
||||
use bevy::{
|
||||
input::{
|
||||
|
||||
@@ -1,23 +1,14 @@
|
||||
use crate::{
|
||||
GameState,
|
||||
backpack::{BackbackSwapEvent, Backpack, BackpackHead},
|
||||
active_heads::{ActiveHeads, HEAD_COUNT, HEAD_SLOTS},
|
||||
backpack::UiHeadState,
|
||||
loading_assets::UIAssets,
|
||||
player::head_id_to_str,
|
||||
sounds::PlaySound,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
use bevy_ui_gradients::{AngularColorStop, BackgroundGradient, ConicGradient, Gradient, Position};
|
||||
use std::f32::consts::PI;
|
||||
|
||||
pub static HEAD_COUNT: usize = 18;
|
||||
static HEAD_SLOTS: usize = 5;
|
||||
|
||||
#[derive(Event, Reflect)]
|
||||
pub enum SelectActiveHead {
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Component, Reflect, Default)]
|
||||
#[reflect(Component)]
|
||||
struct HeadSelector(pub usize);
|
||||
@@ -37,25 +28,20 @@ pub struct HeadsImages {
|
||||
|
||||
#[derive(Resource, Default, Reflect)]
|
||||
#[reflect(Resource)]
|
||||
pub struct ActiveHeads {
|
||||
heads: [Option<BackpackHead>; 5],
|
||||
struct UiActiveHeads {
|
||||
heads: [Option<UiHeadState>; 5],
|
||||
current_slot: usize,
|
||||
}
|
||||
|
||||
#[derive(Event)]
|
||||
pub struct HeadChanged(pub usize);
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.register_type::<HeadDamage>();
|
||||
app.register_type::<ActiveHeads>();
|
||||
app.register_type::<UiActiveHeads>();
|
||||
|
||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
||||
app.add_systems(
|
||||
Update,
|
||||
(update, update_ammo, update_health).run_if(in_state(GameState::Playing)),
|
||||
(sync, update, update_ammo, update_health).run_if(in_state(GameState::Playing)),
|
||||
);
|
||||
app.add_observer(on_select_active_head);
|
||||
app.add_observer(on_swap_backpack);
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, assets: Res<UIAssets>) {
|
||||
@@ -87,16 +73,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, assets: Res<UIA
|
||||
|
||||
commands.insert_resource(HeadsImages { heads });
|
||||
|
||||
commands.insert_resource(ActiveHeads {
|
||||
heads: [
|
||||
Some(BackpackHead::new(0)),
|
||||
Some(BackpackHead::new(3)),
|
||||
Some(BackpackHead::new(6)),
|
||||
Some(BackpackHead::new(8)),
|
||||
Some(BackpackHead::new(9)),
|
||||
],
|
||||
current_slot: 0,
|
||||
});
|
||||
commands.init_resource::<UiActiveHeads>();
|
||||
}
|
||||
|
||||
fn spawn_head_ui(
|
||||
@@ -190,7 +167,7 @@ fn spawn_head_ui(
|
||||
}
|
||||
|
||||
fn update(
|
||||
res: Res<ActiveHeads>,
|
||||
res: Res<UiActiveHeads>,
|
||||
heads_images: Res<HeadsImages>,
|
||||
mut head_image: Query<(&HeadImage, &mut Visibility, &mut ImageNode), Without<HeadSelector>>,
|
||||
mut head_selector: Query<(&HeadSelector, &mut Visibility), Without<HeadImage>>,
|
||||
@@ -214,7 +191,10 @@ fn update(
|
||||
}
|
||||
}
|
||||
|
||||
fn update_ammo(res: Res<ActiveHeads>, mut gradients: Query<(&mut BackgroundGradient, &HeadImage)>) {
|
||||
fn update_ammo(
|
||||
res: Res<UiActiveHeads>,
|
||||
mut gradients: Query<(&mut BackgroundGradient, &HeadImage)>,
|
||||
) {
|
||||
if res.is_changed() {
|
||||
for (mut gradient, HeadImage(head)) in gradients.iter_mut() {
|
||||
if let Some(head) = res.heads[*head] {
|
||||
@@ -230,7 +210,7 @@ fn update_ammo(res: Res<ActiveHeads>, mut gradients: Query<(&mut BackgroundGradi
|
||||
}
|
||||
}
|
||||
|
||||
fn update_health(res: Res<ActiveHeads>, mut query: Query<(&mut Node, &HeadDamage)>) {
|
||||
fn update_health(res: Res<UiActiveHeads>, mut query: Query<(&mut Node, &HeadDamage)>) {
|
||||
if res.is_changed() {
|
||||
for (mut node, HeadDamage(head)) in query.iter_mut() {
|
||||
node.height =
|
||||
@@ -239,44 +219,11 @@ fn update_health(res: Res<ActiveHeads>, mut query: Query<(&mut Node, &HeadDamage
|
||||
}
|
||||
}
|
||||
|
||||
fn on_select_active_head(
|
||||
trigger: Trigger<SelectActiveHead>,
|
||||
mut commands: Commands,
|
||||
mut res: ResMut<ActiveHeads>,
|
||||
) {
|
||||
match trigger.event() {
|
||||
SelectActiveHead::Right => {
|
||||
res.current_slot = (res.current_slot + 1) % HEAD_SLOTS;
|
||||
}
|
||||
SelectActiveHead::Left => {
|
||||
res.current_slot = (res.current_slot + (HEAD_SLOTS - 1)) % HEAD_SLOTS;
|
||||
fn sync(active: Res<ActiveHeads>, mut state: ResMut<UiActiveHeads>) {
|
||||
if active.is_changed() {
|
||||
state.current_slot = active.slot();
|
||||
for i in 0..HEAD_SLOTS {
|
||||
state.heads[i] = active.head(i).map(UiHeadState::from);
|
||||
}
|
||||
}
|
||||
|
||||
commands.trigger(PlaySound::Selection);
|
||||
commands.trigger(HeadChanged(res.heads[res.current_slot].unwrap().head));
|
||||
}
|
||||
|
||||
fn on_swap_backpack(
|
||||
trigger: Trigger<BackbackSwapEvent>,
|
||||
mut commands: Commands,
|
||||
mut res: ResMut<ActiveHeads>,
|
||||
mut backpack: ResMut<Backpack>,
|
||||
) {
|
||||
let backpack_slot = trigger.event().0;
|
||||
|
||||
let head = backpack.heads.get(backpack_slot).unwrap();
|
||||
|
||||
let current_active_slot = res.current_slot;
|
||||
|
||||
let current_active_head = res.heads[current_active_slot];
|
||||
res.heads[current_active_slot] = Some(*head);
|
||||
|
||||
if let Some(old_active) = current_active_head {
|
||||
backpack.heads[backpack_slot] = old_active;
|
||||
} else {
|
||||
backpack.heads.remove(backpack_slot);
|
||||
}
|
||||
|
||||
commands.trigger(HeadChanged(res.heads[res.current_slot].unwrap().head));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod abilities;
|
||||
mod active_heads;
|
||||
mod aim;
|
||||
mod alien;
|
||||
mod backpack;
|
||||
@@ -116,6 +117,7 @@ fn main() {
|
||||
app.add_plugins(loading_map::plugin);
|
||||
app.add_plugins(sprite_3d_animation::plugin);
|
||||
app.add_plugins(abilities::plugin);
|
||||
app.add_plugins(active_heads::plugin);
|
||||
|
||||
app.init_state::<GameState>();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
GameState, heads_ui::HEAD_COUNT, keys::KeySpawn, player::head_id_to_str, sounds::PlaySound,
|
||||
GameState, active_heads::HEAD_COUNT, keys::KeySpawn, player::head_id_to_str, sounds::PlaySound,
|
||||
tb_entities::EnemySpawn,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::{
|
||||
GameState,
|
||||
active_heads::HeadChanged,
|
||||
alien::Animations,
|
||||
camera::{CameraArmRotation, CameraTarget},
|
||||
cash::{Cash, CashCollectEvent},
|
||||
@@ -7,7 +8,6 @@ use crate::{
|
||||
Controls,
|
||||
controller::{CharacterControllerBundle, MovementBundle, PlayerMovement},
|
||||
},
|
||||
heads_ui::HeadChanged,
|
||||
loading_assets::GameAssets,
|
||||
physics_layers::GameLayer,
|
||||
sounds::PlaySound,
|
||||
|
||||
Reference in New Issue
Block a user