Skip to content

Commit

Permalink
Redesign carried items as Player child entities
Browse files Browse the repository at this point in the history
  • Loading branch information
64kramsystem committed Jul 25, 2022
1 parent 39d8a0d commit 43c2f3f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
24 changes: 12 additions & 12 deletions src/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use bevy::{
core::{Time, Timer},
hierarchy::{BuildChildren, DespawnRecursiveExt},
hierarchy::{BuildChildren, Children, DespawnRecursiveExt},
math::Vec2,
prelude::{
default, App, AssetServer, Assets, Bundle, Commands, Component, Entity, EventReader,
Expand All @@ -24,7 +24,7 @@ use crate::{
ITEM_WIDTH, THROW_ITEM_ROTATION_SPEED,
},
input::PlayerAction,
item::{item_carried_by_player, CarriedBy},
item::item_carried_by_player,
metadata::{FighterMeta, ItemMeta},
movement::{MoveInArc, MoveInDirection, Rotate, Target},
state::State,
Expand Down Expand Up @@ -181,28 +181,28 @@ impl ThrownItem {
fn player_projectile_attack(
player_query: Query<
(
Entity,
&Children,
&Transform,
&Facing,
&State,
&ActionState<PlayerAction>,
),
With<Player>,
>,
carried_items_query: Query<((Entity, &Handle<ItemMeta>), &CarriedBy)>,
items_meta_query: Query<&Handle<ItemMeta>>,
items_meta: Res<Assets<ItemMeta>>,
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
for (player_id, transform, facing, state, input) in player_query.iter() {
for (player_children, transform, facing, state, input) in player_query.iter() {
if *state != State::Idle && *state != State::Running {
continue;
}

let carried_item = item_carried_by_player(
player_id,
player_children,
ITEM_BOTTLE_NAME,
&carried_items_query,
&items_meta_query,
&items_meta,
);

Expand All @@ -228,22 +228,22 @@ fn player_throw(
mut commands: Commands,
player_query: Query<
(
Entity,
&Children,
&Transform,
Option<&Facing>,
&ActionState<PlayerAction>,
),
With<Player>,
>,
carried_items_query: Query<((Entity, &Handle<ItemMeta>), &CarriedBy)>,
items_meta_query: Query<&Handle<ItemMeta>>,
items_meta: Res<Assets<ItemMeta>>,
asset_server: Res<AssetServer>,
) {
for (player_id, transform, facing_option, input) in player_query.iter() {
for (player_children, transform, facing_option, input) in player_query.iter() {
let carried_item = item_carried_by_player(
player_id,
player_children,
ITEM_BOTTLE_NAME,
&carried_items_query,
&items_meta_query,
&items_meta,
);

Expand Down
36 changes: 15 additions & 21 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashSet;

use bevy::{
ecs::system::EntityCommands,
hierarchy::{BuildChildren, Children},
math::Vec3,
prelude::{Assets, Bundle, Commands, Component, Entity, Handle, Query, Transform, With},
transform::TransformBundle,
Expand All @@ -19,12 +20,9 @@ use crate::{
#[derive(Component)]
pub struct Item;

#[derive(Component)]
pub struct CarriedBy(pub Entity);

/// Represents an item, that is either on the map (waiting to be picked up), or carried.
/// If an item is on the map, it has a TransformBundle; if it's carried, it instead has a
/// CarriedBy.
/// If an item is on the map, it has a TransformBundle; if it's carried, it doesn't, and it's the
/// child entity of a Player.
#[derive(Bundle)]
pub struct ItemBundle {
item: Item,
Expand Down Expand Up @@ -72,10 +70,8 @@ pub fn pick_items(
.distance(item_transform.translation.truncate());

if player_item_distance <= PICK_ITEM_RADIUS {
commands
.entity(item_id)
.remove_bundle::<TransformBundle>()
.insert(CarriedBy(player_id));
commands.entity(item_id).remove_bundle::<TransformBundle>();
commands.entity(player_id).add_child(item_id);
picked_item_ids.insert(item_id);
break;
}
Expand All @@ -87,22 +83,20 @@ pub fn pick_items(

/// Utility method, not system!
pub fn item_carried_by_player(
player_id: Entity,
children: &Children,
item_name: &str,
carried_items_query: &Query<((Entity, &Handle<ItemMeta>), &CarriedBy)>,
items_meta_query: &Query<&Handle<ItemMeta>>,
items_meta: &Assets<ItemMeta>,
) -> Option<Entity> {
carried_items_query
.iter()
.find_map(|((item_entity, item_meta_handle), carried_by)| {
if carried_by.0 == player_id {
if let Some(item_meta) = items_meta.get(item_meta_handle) {
if item_meta.name == item_name {
return Some(item_entity);
}
for child_id in children.iter() {
if let Ok(item_meta_handle) = items_meta_query.get(*child_id) {
if let Some(item_meta) = items_meta.get(item_meta_handle) {
if item_meta.name == item_name {
return Some(*child_id);
}
}
}
}

None
})
None
}

0 comments on commit 43c2f3f

Please sign in to comment.