Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Health item #202

Merged
merged 4 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion assets/beach/beach.level.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ enemies:
stop_points: [500, 1000]

items:
- item: &health /items/health/health.item.yaml
location: [50, -70, 0]
- item: &bottle /items/bottle/bottle.item.yaml
location: [50, -20, 0]
- item: &bottle /items/bottle/bottle.item.yaml
- item: *bottle
location: [50, 20, 0]
5 changes: 5 additions & 0 deletions assets/items/health/health.item.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Health

image:
image: health.png
image_size: [22, 20]
Binary file added assets/items/health/health.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ pub const THROW_ITEM_ROTATION_SPEED: f32 = 10.;
pub const PICK_ITEM_RADIUS: f32 = 24.;

pub const ITEM_BOTTLE_NAME: &str = "Bottle";

pub const ITEM_HEALTH_NAME: &str = "Health";
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