Skip to content

Commit

Permalink
Decouple immunity from player
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Aug 21, 2024
1 parent 8da9750 commit e3b7721
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 46 deletions.
17 changes: 17 additions & 0 deletions src/game/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod health;
pub mod level;
pub mod movement;
pub mod player;
mod shield;

use avian2d::prelude::*;
use bevy::ecs::system::EntityCommand;
Expand All @@ -17,6 +18,7 @@ use bevy::utils::HashMap;
use iyes_progress::prelude::*;
use serde::Deserialize;
use serde::Serialize;
use shield::IsShield;

use crate::game::actor::attack::Attack;
use crate::game::actor::attack::AttackController;
Expand All @@ -33,6 +35,7 @@ use crate::game::audio::music::Beat;
use crate::game::card::deck::Deck;
use crate::game::combat::hit::Hurtbox;
use crate::game::sprite::SpriteAnimation;
use crate::screen::playing::PlayingAssets;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
Expand All @@ -47,6 +50,7 @@ pub(super) fn plugin(app: &mut App) {
level::plugin,
movement::plugin,
player::plugin,
shield::plugin,
));
}

Expand Down Expand Up @@ -130,6 +134,7 @@ fn one() -> f64 {
impl EntityCommand for Actor {
fn apply(mut self, id: Entity, world: &mut World) {
self.deck.active += world.resource::<Beat>().total as isize - 1;
let bubble_texture = world.resource::<PlayingAssets>().bubble.clone();

world
.entity_mut(id)
Expand Down Expand Up @@ -174,6 +179,18 @@ impl EntityCommand for Actor {
size: vec2(8.0, 1.0),
})
.insert(Transform::from_translation(vec3(0.0, -4.5, 1.0)));
children
.spawn((
Name::new("Shield"),
SpriteBundle {
transform: Transform::default(),
texture: bubble_texture,
visibility: Visibility::Hidden,
..default()
},
IsShield,
))
.insert(Transform::from_translation(vec3(0.0, -0.5, 2.0)));
});
}
}
42 changes: 2 additions & 40 deletions src/game/actor/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bevy::prelude::*;

use crate::core::camera::CameraRoot;
use crate::core::camera::SmoothFollow;
use crate::core::UpdateSet;
use crate::game::actor::attack::input::attack_action;
use crate::game::actor::facing::FaceCursor;
use crate::game::actor::facing::FacingIndicator;
Expand All @@ -17,15 +16,14 @@ use crate::game::actor::ActorConfig;
use crate::game::combat::death::DeathSfx;
use crate::game::combat::hit::Hitbox;
use crate::game::combat::hit::HurtSfx;
use crate::game::combat::hit::Immune;
use crate::game::combat::knockback::HitboxKnockback;
use crate::game::GameLayer;
use crate::game::GameRoot;
use crate::screen::playing::PlayingAssets;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.configure::<(IsPlayer, IsImmuneBubble)>();
app.configure::<IsPlayer>();
}

#[derive(Component, Reflect, Default)]
Expand All @@ -42,7 +40,7 @@ pub fn player(key: impl Into<String>) -> impl EntityCommand {
let key = key.into();

move |entity: Entity, world: &mut World| {
let (actor, parent, camera, sfx_hurt, sfx_death, bubble_texture) = {
let (actor, parent, camera, sfx_hurt, sfx_death) = {
let (config, game_root, camera_root, assets) = SystemState::<(
ConfigRef<ActorConfig>,
Res<GameRoot>,
Expand All @@ -59,7 +57,6 @@ pub fn player(key: impl Into<String>) -> impl EntityCommand {
camera_root.primary,
assets.sfx_player_hurt.clone(),
assets.sfx_restart.clone(),
assets.bubble.clone(),
)
};

Expand All @@ -85,17 +82,6 @@ pub fn player(key: impl Into<String>) -> impl EntityCommand {
offset: vec2(6.0, 5.0),
})
.insert(Transform::from_translation(vec3(0.0, -0.5, 2.0)));
children
.spawn((
SpriteBundle {
transform: Transform::default(),
texture: bubble_texture,
visibility: Visibility::Hidden,
..default()
},
IsImmuneBubble,
))
.insert(Transform::from_translation(vec3(0.0, -0.5, 2.0)));
});

// Allow manual movement / attack input in dev builds.
Expand All @@ -108,27 +94,3 @@ pub fn player(key: impl Into<String>) -> impl EntityCommand {
r!(world.entity_mut(camera).get_mut::<SmoothFollow>()).target = entity;
}
}

#[derive(Component, Reflect)]
#[reflect(Component)]
struct IsImmuneBubble;

impl Configure for IsImmuneBubble {
fn configure(app: &mut App) {
app.register_type::<Self>();
app.add_systems(Update, update_immune_bubble.in_set(UpdateSet::SyncLate));
}
}

fn update_immune_bubble(
mut bubble_query: Query<(&mut Visibility, &Parent), With<IsImmuneBubble>>,
immune_query: Query<(), With<Immune>>,
) {
for (mut visibility, parent) in &mut bubble_query {
*visibility = if immune_query.contains(parent.get()) {
Visibility::Inherited
} else {
Visibility::Hidden
};
}
}
33 changes: 33 additions & 0 deletions src/game/actor/shield.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use bevy::prelude::*;

use crate::core::UpdateSet;
use crate::game::combat::hit::Immune;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.configure::<IsShield>();
}

#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct IsShield;

impl Configure for IsShield {
fn configure(app: &mut App) {
app.register_type::<Self>();
app.add_systems(Update, update_shield.in_set(UpdateSet::SyncLate));
}
}

fn update_shield(
mut shield_query: Query<(&mut Visibility, &Parent), With<IsShield>>,
immune_query: Query<(), With<Immune>>,
) {
for (mut visibility, parent) in &mut shield_query {
*visibility = if immune_query.contains(parent.get()) {
Visibility::Inherited
} else {
Visibility::Hidden
};
}
}
12 changes: 6 additions & 6 deletions src/game/combat/hit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::game::cleanup::RemoveOnTimer;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.configure::<(Hitbox, Hurtbox, OnHit, HurtSfx, Immune)>();
app.configure::<(Hitbox, Hurtbox, Immune, OnHit, HurtSfx)>();
}

#[derive(Component, Reflect)]
Expand All @@ -23,21 +23,21 @@ impl Configure for Hitbox {

#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct Immune;
pub struct Hurtbox;

impl Configure for Immune {
impl Configure for Hurtbox {
fn configure(app: &mut App) {
app.configure::<RemoveOnTimer<Self>>();
app.register_type::<Self>();
}
}

#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct Hurtbox;
pub struct Immune;

impl Configure for Hurtbox {
impl Configure for Immune {
fn configure(app: &mut App) {
app.configure::<RemoveOnTimer<Self>>();
app.register_type::<Self>();
}
}
Expand Down

0 comments on commit e3b7721

Please sign in to comment.