Skip to content

Commit

Permalink
Re-implement fishfolk#202
Browse files Browse the repository at this point in the history
Changes from fishfolk#202 were broken in the rebase and this re-adds the life
item functionality from it.

Co-authored-by: Saverio Miroddi <saverio.pub2@gmail.com>
  • Loading branch information
zicklag and 64kramsystem committed Aug 6, 2022
1 parent d821b1a commit 5fce6de
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
4 changes: 3 additions & 1 deletion assets/items/bottle/bottle.item.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ name: Bottle
image:
image: bottle.png
image_size: [11, 31]
damage: 20

kind: !Throwable
damage: 10
7 changes: 5 additions & 2 deletions assets/items/health/health.item.yaml
Original file line number Diff line number Diff line change
@@ -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
43 changes: 34 additions & 9 deletions src/fighter_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Throwing>>,
mut fighters: Query<
(
Entity,
&Transform,
&Facing,
&Stats,
&mut Inventory,
&mut Health,
),
With<Throwing>,
>,
item_assets: Res<Assets<ItemMeta>>,
) {
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 {
Expand Down
5 changes: 4 additions & 1 deletion src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 8 additions & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 5fce6de

Please sign in to comment.