From 5fce6de60701b7b9f68b637ca4b041fa0d6fd3ce Mon Sep 17 00:00:00 2001 From: Zicklag Date: Sat, 6 Aug 2022 14:22:30 -0500 Subject: [PATCH] Re-implement #202 Changes from #202 were broken in the rebase and this re-adds the life item functionality from it. Co-authored-by: Saverio Miroddi --- assets/items/bottle/bottle.item.yaml | 4 ++- assets/items/health/health.item.yaml | 7 +++-- src/fighter_state.rs | 43 ++++++++++++++++++++++------ src/item.rs | 5 +++- src/metadata.rs | 9 +++++- 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/assets/items/bottle/bottle.item.yaml b/assets/items/bottle/bottle.item.yaml index b874089e..25b35c5d 100644 --- a/assets/items/bottle/bottle.item.yaml +++ b/assets/items/bottle/bottle.item.yaml @@ -3,4 +3,6 @@ name: Bottle image: image: bottle.png image_size: [11, 31] -damage: 20 + +kind: !Throwable + damage: 10 diff --git a/assets/items/health/health.item.yaml b/assets/items/health/health.item.yaml index aeee288b..e528ebcf 100644 --- a/assets/items/health/health.item.yaml +++ b/assets/items/health/health.item.yaml @@ -1,5 +1,8 @@ name: Health image: - image: health.png - image_size: [22, 20] + image: health.png + image_size: [22, 20] + +kind: !Health + health: 1000000 # Refill all of it diff --git a/src/fighter_state.rs b/src/fighter_state.rs index 28c88b77..c367ea91 100644 --- a/src/fighter_state.rs +++ b/src/fighter_state.rs @@ -20,7 +20,7 @@ use crate::{ fighter::Inventory, input::PlayerAction, item::{Item, Projectile}, - metadata::{FighterMeta, ItemMeta}, + metadata::{FighterMeta, ItemKind, ItemMeta}, movement::LinearVelocity, player::Player, GameState, Stats, @@ -672,20 +672,45 @@ fn dying( /// Throw the item in the player's inventory fn throwing( mut commands: Commands, - mut fighters: Query<(Entity, &Transform, &Facing, &mut Inventory), With>, + mut fighters: Query< + ( + Entity, + &Transform, + &Facing, + &Stats, + &mut Inventory, + &mut Health, + ), + With, + >, item_assets: Res>, ) { - for (entity, fighter_transform, facing, mut inventory) in &mut fighters { + for (entity, fighter_transform, facing, stats, mut inventory, mut health) in &mut fighters { // If the player has an item in their inventory if let Some(meta_handle) = inventory.take() { // If the item asset has loaded if let Some(item) = item_assets.get(&meta_handle) { - // Throw the item! - commands.spawn_bundle(Projectile::from_thrown_item( - fighter_transform.translation + consts::THROW_ITEM_OFFSET.extend(0.0), - item, - facing, - )); + // Check what kind of item this is. + // + // TODO: We should probably create a flexible item system abstraction similar to the + // fighter state abstraction so that items can flexibly defined without a + // centralized enum. + match item.kind { + ItemKind::Throwable { .. } => { + // Throw the item! + commands.spawn_bundle(Projectile::from_thrown_item( + fighter_transform.translation + consts::THROW_ITEM_OFFSET.extend(0.0), + item, + facing, + )); + } + ItemKind::Health { + health: item_health, + } => { + // Refill player's health + **health = (**health + item_health).clamp(0, stats.max_health); + } + } // If the item asset isn't loaded yet } else { diff --git a/src/item.rs b/src/item.rs index 2c7e17a7..b07f289a 100644 --- a/src/item.rs +++ b/src/item.rs @@ -73,7 +73,10 @@ impl Projectile { ..default() }, attack: Attack { - damage: item.damage, + damage: match item.kind { + crate::metadata::ItemKind::Throwable { damage } => damage, + crate::metadata::ItemKind::Health { .. } => panic!("Cannot throw health item"), + }, velocity: Vec2::new(consts::ATTACK_VELOCITY, 0.0) * direction_mul, }, velocity: LinearVelocity(consts::THROW_ITEM_SPEED * direction_mul), diff --git a/src/metadata.rs b/src/metadata.rs index 50c55c08..3e4abd0f 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -99,7 +99,14 @@ pub struct FighterMeta { pub struct ItemMeta { pub name: String, pub image: ImageMeta, - pub damage: i32, + pub kind: ItemKind, +} + +#[derive(Deserialize, Clone, Debug)] +#[serde(deny_unknown_fields)] +pub enum ItemKind { + Throwable { damage: i32 }, + Health { health: i32 }, } #[derive(Deserialize, Clone, Debug)]