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

feat: Add a find helper method to PlayerInventories #897

Merged
merged 1 commit into from
Jan 11, 2024
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
8 changes: 2 additions & 6 deletions src/core/elements/crate_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,8 @@ fn update_idle_crates(

let break_timeout = *break_timeout;

if let Some(Inv { player, .. }) = player_inventories
.iter()
.find_map(|x| x.filter(|x| x.inventory == entity))
{
if items_used.get(entity).is_some() {
items_used.remove(entity);
if let Some(Inv { player, .. }) = player_inventories.find_item(entity) {
if items_used.remove(entity).is_some() {
commands.add(PlayerCommand::set_inventory(player, None));
commands.add(
move |mut idle: CompMut<IdleCrate>, mut thrown: CompMut<ThrownCrate>| {
Expand Down
5 changes: 1 addition & 4 deletions src/core/elements/grenade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,7 @@ fn update_lit_grenades(
let emote_region = emote_regions.get_mut(entity).unwrap();

// If the item is being held
if let Some(inventory) = player_inventories
.iter()
.find_map(|x| x.filter(|x| x.inventory == entity))
{
if let Some(inventory) = player_inventories.find_item(entity) {
let player = inventory.player;
let layers = player_layers.get_mut(player).unwrap();
layers.fin_anim = *fin_anim;
Expand Down
10 changes: 3 additions & 7 deletions src/core/elements/jellyfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,16 @@ fn update_unused_jellyfish(
jellyfish.ammo -= 1;

// Get the owner of the jellyfish, if any
let Some(owner) = player_inventories
.iter()
.find_map(|inv| inv.filter(|i| i.inventory == jellyfish_ent))
.map(|inv| inv.player)
else {
let Some(Inv { player, .. }) = player_inventories.find_item(jellyfish_ent) else {
continue;
};

// Prevent the jellyfish from being used if the owner isn't idle
if player_states.get(owner).map(|s| s.current) != Some(*idle::ID) {
if player_states.get(player).map(|s| s.current) != Some(*idle::ID) {
continue;
}

commands.add(flappy_jellyfish::spawn(owner, jellyfish_ent));
commands.add(flappy_jellyfish::spawn(player, jellyfish_ent));
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/core/elements/kick_bomb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ fn update_idle_kick_bombs(
let arm_delay = *arm_delay;
let fuse_time = *fuse_time;

if items_used.get(entity).is_some() {
if items_used.remove(entity).is_some() {
audio_events.play(*fuse_sound, *fuse_sound_volume);
items_used.remove(entity);
let animated_sprite = animated_sprites.get_mut(entity).unwrap();
animated_sprite.frames = [3, 4, 5].into_iter().collect();
animated_sprite.repeat = true;
Expand Down Expand Up @@ -237,11 +236,7 @@ fn update_lit_kick_bombs(

let mut should_explode = false;
// If the item is being held
if let Some(inventory) = player_inventories
.iter()
.find_map(|x| x.filter(|x| x.inventory == entity))
{
let player = inventory.player;
if let Some(Inv { player, .. }) = player_inventories.find_item(entity) {
let body = bodies.get_mut(entity).unwrap();
player_layers.get_mut(player).unwrap().fin_anim = *fin_anim;

Expand Down
8 changes: 2 additions & 6 deletions src/core/elements/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,8 @@ fn update_idle_mines(
};
let arm_delay = *arm_delay;

if let Some(Inv { player, .. }) = player_inventories
.iter()
.find_map(|x| x.filter(|x| x.inventory == entity))
{
if items_used.get(entity).is_some() {
items_used.remove(entity);
if let Some(Inv { player, .. }) = player_inventories.find_item(entity) {
if items_used.remove(entity).is_some() {
commands.add(PlayerCommand::set_inventory(player, None));
commands.add(
move |mut idle: CompMut<IdleMine>, mut thrown: CompMut<ThrownMine>| {
Expand Down
12 changes: 2 additions & 10 deletions src/core/elements/musket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,9 @@ fn update(
musket.cooldown.tick(time.delta());

// If the item is being held
if let Some(inventory) = player_inventories
.iter()
.find_map(|x| x.filter(|x| x.inventory == entity))
{
let player = inventory.player;

if let Some(Inv { player, .. }) = player_inventories.find_item(entity) {
// If the item is being used
let item_used = items_used.get(entity).is_some();
if item_used {
items_used.remove(entity);
}
let item_used = items_used.remove(entity).is_some();
if item_used && musket.cooldown.finished() {
// Empty
if musket.ammo.eq(&0) {
Expand Down
5 changes: 1 addition & 4 deletions src/core/elements/stomp_boots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ fn update(
};

// If the item is being held
if let Some(Inv { player, .. }) = player_inventories
.iter()
.find_map(|x| x.filter(|x| x.inventory == entity))
{
if let Some(Inv { player, .. }) = player_inventories.find_item(entity) {
// If the item is being used
let is_item_used = items_used.get(entity).is_some();
let player_decoration = *player_decoration;
Expand Down
19 changes: 6 additions & 13 deletions src/core/elements/sword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ fn update(
};

// If the item is being held
if let Some(inventory) = player_inventories
.iter()
.find_map(|x| x.filter(|x| x.inventory == entity))
{
let player = inventory.player;
if let Some(Inv { player, .. }) = player_inventories.find_item(entity) {
let sprite = sprites.get_mut(entity).unwrap();
let player_translation = transforms.get(player).unwrap().translation;
let flip = sprite.flip_x;
Expand Down Expand Up @@ -289,14 +285,11 @@ fn update(
}

// If the item is being used
let item_used = items_used.get(entity).is_some();
if item_used {
items_used.remove(entity);
if matches!(sword.state, SwordState::Idle) {
sprite.index = 8;
sword.state = SwordState::Swinging { frame: 0 };
audio_events.play(*sound, *sound_volume);
}
let item_used = items_used.remove(entity).is_some();
if item_used && matches!(sword.state, SwordState::Idle) {
sprite.index = 8;
sword.state = SwordState::Swinging { frame: 0 };
audio_events.play(*sound, *sound_volume);
}
} else {
let body = bodies.get(entity).unwrap();
Expand Down
13 changes: 9 additions & 4 deletions src/core/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ pub struct Inv {
#[derive(Deref, DerefMut, Debug)]
pub struct PlayerInventories<'a>(&'a [Option<Inv>; MAX_PLAYERS]);

impl PlayerInventories<'_> {
pub fn find_item(&self, item: Entity) -> Option<Inv> {
self.0
.iter()
.find_map(|i| i.filter(|inv| inv.inventory == item))
}
}

impl<'a> SystemParam for PlayerInventories<'a> {
type State = [Option<Inv>; MAX_PLAYERS];
type Param<'s> = PlayerInventories<'s>;
Expand Down Expand Up @@ -117,8 +125,7 @@ pub fn drop_items(
player_inventories: PlayerInventories,
) {
for Inv { player, inventory } in player_inventories.iter().flatten() {
if drop_items.contains(*inventory) {
drop_items.remove(*inventory);
if drop_items.remove(*inventory).is_some() {
commands.add(PlayerCommand::set_inventory(*player, None));
}
}
Expand All @@ -141,8 +148,6 @@ pub fn grab_items(
} = *item_grab;

if let Some(ItemGrabbed { player }) = items_grabbed.remove(entity) {
items_grabbed.remove(entity);

player_layers.get_mut(player).unwrap().fin_anim = fin_anim;

if let Some(body) = bodies.get_mut(entity) {
Expand Down
14 changes: 5 additions & 9 deletions src/core/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,27 +955,23 @@ fn equip_hats(
) {
for (hat_ent, hat) in entities.iter_with(&hats) {
// If the hat is being held
if let Some(inventory) = player_inventories
.iter()
.find_map(|x| x.filter(|x| x.inventory == hat_ent))
{
if items_used.contains(hat_ent) {
items_used.remove(hat_ent).unwrap();
inventories.get_mut(inventory.player).unwrap().0 = None;
if let Some(Inv { player, .. }) = player_inventories.find_item(hat_ent) {
if items_used.remove(hat_ent).is_some() {
inventories.get_mut(player).unwrap().0 = None;

let hat_meta = assets.get(hat.0);
kinematic_bodies.get_mut(hat_ent).unwrap().is_deactivated = true;
player_body_attachments.insert(
hat_ent,
PlayerBodyAttachment {
player: inventory.player,
player,
offset: hat_meta.offset.extend(PlayerLayers::HAT_Z_OFFSET),
head: true,
sync_animation: false,
sync_color: true,
},
);
player_layers.get_mut(inventory.player).unwrap().hat_ent = Some(hat_ent);
player_layers.get_mut(player).unwrap().hat_ent = Some(hat_ent);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/player/state/states/drive_jellyfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ pub fn player_state_transition(
}
} else {
let Some(player_state) = player_inventories
.iter()
.find_map(|inv| inv.filter(|i| i.inventory == jellyfish_ent))
.find_item(jellyfish_ent)
.and_then(|inventory| player_states.get_mut(inventory.player))
else {
continue;
Expand Down
Loading