Skip to content

Commit

Permalink
Add Health item use
Browse files Browse the repository at this point in the history
  • Loading branch information
64kramsystem committed Aug 3, 2022
1 parent 769a2b0 commit f18698b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
30 changes: 27 additions & 3 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ use bevy::{
ecs::system::EntityCommands,
hierarchy::{BuildChildren, Children},
math::Vec3,
prelude::{Assets, Bundle, Commands, Component, Entity, Handle, Query, Transform, With},
prelude::{Assets, Bundle, Commands, Component, Entity, Handle, Query, Res, Transform, With},
transform::TransformBundle,
};
use leafwing_input_manager::prelude::ActionState;

use crate::{
consts::{self, ITEM_LAYER, PICK_ITEM_RADIUS},
consts::{self, ITEM_HEALTH_NAME, ITEM_LAYER, PICK_ITEM_RADIUS},
input::PlayerAction,
metadata::{ItemMeta, ItemSpawnMeta},
metadata::{FighterMeta, ItemMeta, ItemSpawnMeta},
player::Player,
state::State,
Stats,
};

#[derive(Component)]
Expand Down Expand Up @@ -100,3 +101,26 @@ pub fn item_carried_by_player(

None
}

pub fn use_health_item(
mut player_query: Query<(&Children, &mut Stats, &Handle<FighterMeta>), With<Player>>,
items_meta_query: Query<&Handle<ItemMeta>>,
items_meta: Res<Assets<ItemMeta>>,
mut commands: Commands,
fighter_assets: Res<Assets<FighterMeta>>,
) {
for (player_children, mut stats, player_meta) in player_query.iter_mut() {
let health_item = item_carried_by_player(
player_children,
ITEM_HEALTH_NAME,
&items_meta_query,
&items_meta,
);

if let Some(health_id) = health_item {
let player_meta = fighter_assets.get(player_meta).unwrap();
stats.health = player_meta.stats.health;
commands.entity(health_id).despawn();
}
}
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ use ui::UIPlugin;
use utils::ResetController;
use y_sort::*;

use crate::{input::PlayerAction, item::pick_items};
use crate::{
input::PlayerAction,
item::{pick_items, use_health_item},
};

#[cfg_attr(feature = "debug", derive(bevy_inspector_egui::Inspectable))]
#[derive(Component, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -212,6 +215,7 @@ fn main() {
.run_in_state(GameState::InGame)
.with_system(player_controller)
.with_system(pick_items)
.with_system(use_health_item)
.with_system(y_sort)
.with_system(attack_fighter_collision)
.with_system(kill_entities)
Expand Down

0 comments on commit f18698b

Please sign in to comment.