Skip to content

Commit

Permalink
Add RemoveOnXyz::bundle helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 25, 2024
1 parent 464dc6e commit 7d83109
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
8 changes: 3 additions & 5 deletions src/game/card/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ impl FromWorld for CardActionMap {
CardActionKey::Step,
world.register_system(|In(entity): In<Entity>, world: &mut World| {
r!(world.get_entity_mut(entity))
.insert((MoveTowardsFacing, RemoveOnBeat::<MoveTowardsFacing>::new(5)));
.insert(RemoveOnBeat::bundle(MoveTowardsFacing, 1));
}),
),
(
CardActionKey::DoubleBeat,
world.register_system(|In(entity): In<Entity>, world: &mut World| {
r!(world.get_entity_mut(entity)).insert((
DoubleBeat,
RemoveOnBeat::<DoubleBeat>::new(2),
AimTowardsFacing,
RemoveOnBeat::<AimTowardsFacing>::new(2),
RemoveOnBeat::bundle(DoubleBeat, 2),
RemoveOnBeat::bundle(AimTowardsFacing, 2),
));
}),
),
Expand Down
34 changes: 21 additions & 13 deletions src/game/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,24 @@ pub struct RemoveOnTimer<C: Component + TypePath> {
phantom: PhantomData<C>,
}

impl<C: Component + TypePath> Configure for RemoveOnTimer<C> {
fn configure(app: &mut App) {
app.register_type::<Self>();
app.add_systems(Update, remove_on_timer::<C>.in_set(UpdateSet::SyncLate));
}
}

#[allow(dead_code)]
impl<C: Component + TypePath> RemoveOnTimer<C> {
#[allow(dead_code)]
pub fn new(timer: Timer) -> Self {
RemoveOnTimer {
Self {
timer,
phantom: PhantomData::<C>,
phantom: PhantomData,
}
}
}

impl<C: Component + TypePath> Configure for RemoveOnTimer<C> {
fn configure(app: &mut App) {
app.register_type::<Self>();
app.add_systems(Update, remove_on_timer::<C>.in_set(UpdateSet::SyncLate));
pub fn bundle(component: C, timer: Timer) -> (C, Self) {
(component, Self::new(timer))
}
}

Expand All @@ -152,7 +156,7 @@ fn remove_on_timer<C: Component + TypePath>(
) {
for (entity, mut remove) in &mut remove_query {
if remove.timer.tick(time.delta()).finished() {
commands.entity(entity).remove::<(RemoveOnTimer<C>, C)>();
commands.entity(entity).remove::<(C, RemoveOnTimer<C>)>();
}
}
}
Expand All @@ -178,12 +182,16 @@ impl<C: Component + TypePath> Configure for RemoveOnBeat<C> {
}

impl<C: Component + TypePath> RemoveOnBeat<C> {
pub fn new(count: usize) -> Self {
RemoveOnBeat {
beat: count,
pub fn new(beat: usize) -> Self {
Self {
beat,
phantom: PhantomData,
}
}

pub fn bundle(component: C, beat: usize) -> (C, Self) {
(component, Self::new(beat))
}
}

fn remove_on_beat<C: Component + TypePath>(
Expand All @@ -192,7 +200,7 @@ fn remove_on_beat<C: Component + TypePath>(
) {
for (entity, mut remove) in &mut remove_query {
if remove.beat <= 1 {
commands.entity(entity).remove::<(RemoveOnBeat<C>, C)>();
commands.entity(entity).remove::<(C, RemoveOnBeat<C>)>();
}
remove.beat = remove.beat.saturating_sub(1);
}
Expand Down

0 comments on commit 7d83109

Please sign in to comment.