Skip to content

Commit

Permalink
Merge #232
Browse files Browse the repository at this point in the history
232: new healing system | making it easier to organize r=zicklag a=DRuppFv

I though the fact that you use the healing item in the same way as a weapon a little weird, then, i made these mods to make it "normal".

If it was the intention, I'm sorry, but I don't see any motive to refuse this PR considering that it makes very easier to organize the future new item kinds when grabbed. As I said, if it was the intention, just tell me and I will make the health items be treated as the throwable items, but i don't think there's any reason to refuse this new way of organization. 

Obs: first, I tried to make it possible grab a health even with another item in the inventory, because I forgot that grab and throw are in the same keybind. I think it would be great, but first, these two actions should be divided, so, i have a question: If i divide them, would the PR be accepted?

Co-authored-by: DRuppFv <DRuppFv@gmail.com>
  • Loading branch information
bors[bot] and DRuppFv authored Aug 22, 2022
2 parents aaa1962 + b2780f6 commit 115752d
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions src/fighter_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,19 +939,9 @@ fn dying(
/// Throw the item in the player's inventory
fn throwing(
mut commands: Commands,
mut fighters: Query<
(
Entity,
&Transform,
&Facing,
&Stats,
&mut Inventory,
&mut Health,
),
With<Throwing>,
>,
mut fighters: Query<(Entity, &Transform, &Facing, &mut Inventory), With<Throwing>>,
) {
for (entity, fighter_transform, facing, stats, mut inventory, mut health) in &mut fighters {
for (entity, fighter_transform, facing, mut inventory) in &mut fighters {
// If the player has an item in their inventory
if let Some(item_meta) = inventory.take() {
// Check what kind of item this is.
Expand All @@ -968,11 +958,8 @@ fn throwing(
facing,
));
}
ItemKind::Health {
health: item_health,
} => {
// Refill player's health
**health = (**health + item_health).clamp(0, stats.max_health);
ItemKind::Health { health: _ } => {
panic!("Health items should be used immediately, and can't be thrown");
}
}
}
Expand All @@ -986,14 +973,15 @@ fn throwing(
// Trying to grab an item off the map
fn grabbing(
mut commands: Commands,
mut fighters: Query<(Entity, &Transform, &mut Inventory), With<Grabbing>>,
mut fighters: Query<(Entity, &Transform, &mut Inventory, &Stats, &mut Health), With<Grabbing>>,
items_query: Query<(Entity, &Transform, &Handle<ItemMeta>), With<Item>>,
items_assets: Res<Assets<ItemMeta>>,
) {
// We need to track the picked items, otherwise, in theory, two players could pick the same item.
let mut picked_item_ids = HashSet::new();

for (fighter_ent, fighter_transform, mut fighter_inventory) in &mut fighters {
for (fighter_ent, fighter_transform, mut fighter_inventory, stats, mut health) in &mut fighters
{
// If several items are at pick distance, an arbitrary one is picked.
for (item_ent, item_transform, item) in &items_query {
if !picked_item_ids.contains(&item_ent) {
Expand All @@ -1007,18 +995,27 @@ fn grabbing(
if fighter_item_distance <= consts::PICK_ITEM_RADIUS {
// And our fighter isn't carrying another item
if fighter_inventory.is_none() {
// Pick up the item
picked_item_ids.insert(item_ent);
**fighter_inventory =
Some(items_assets.get(item).expect("Item not loaded!").clone());
commands.entity(item_ent).despawn_recursive();
match items_assets.get(item).unwrap().kind {
ItemKind::Health {
health: item_health,
} => {
// If its health, refill player's health instantly
**health = (**health + item_health).clamp(0, stats.max_health);
commands.entity(item_ent).despawn_recursive();
}
ItemKind::Throwable { damage: _ } => {
// If its throwable, pick up the item
picked_item_ids.insert(item_ent);
**fighter_inventory =
Some(items_assets.get(item).expect("Item not loaded!").clone());
commands.entity(item_ent).despawn_recursive();
}
}
}

break;
}
}
}

// Grabbing is an "instant" state, that is removed at the end of every frame. Eventually it
// may not be and it might play a fighter animation.
commands.entity(fighter_ent).remove::<Grabbing>();
Expand Down

0 comments on commit 115752d

Please sign in to comment.