Skip to content

Commit

Permalink
Add facing indicator (no custom sprite yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 21, 2024
1 parent 42edaae commit 9f0f4b6
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 17 deletions.
8 changes: 4 additions & 4 deletions assets/config/health_bar.ron
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(
color_ramp: [
Oklcha(Oklcha(lightness: 0.500, chroma: 0.300, hue: 030, alpha: 1.000)),
Oklcha(Oklcha(lightness: 0.500, chroma: 0.300, hue: 030, alpha: 1.000)),
Oklcha(Oklcha(lightness: 0.800, chroma: 0.300, hue: 090, alpha: 1.000)),
Oklcha(Oklcha(lightness: 0.700, chroma: 0.300, hue: 150, alpha: 1.000)),
Oklcha(Oklcha(lightness: 0.500, chroma: 0.300, hue: 030, alpha: 0.600)),
Oklcha(Oklcha(lightness: 0.500, chroma: 0.300, hue: 030, alpha: 0.600)),
Oklcha(Oklcha(lightness: 0.800, chroma: 0.300, hue: 090, alpha: 0.600)),
Oklcha(Oklcha(lightness: 0.700, chroma: 0.300, hue: 150, alpha: 0.600)),
],
)
2 changes: 2 additions & 0 deletions assets/config/theme.ron
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@

// Popup
Srgba(Srgba(red: 0.106, green: 0.118, blue: 0.122, alpha: 0.850)),
// FacingIndicator
Srgba(Srgba(red: 0.400, green: 0.800, blue: 1.000, alpha: 1.000)),
)),
)
3 changes: 2 additions & 1 deletion src/core/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ pub enum ThemeColor {

// Misc UI colors
Popup,
FacingIndicator,
}

impl ThemeColor {
pub const fn set<C: Component + ColorMut>(self) -> ThemeColorFor<C> {
pub const fn target<C: Component + ColorMut>(self) -> ThemeColorFor<C> {
ThemeColorFor(self, PhantomData)
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/game/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use bevy::math::vec2;
use bevy::math::vec3;
use bevy::prelude::*;
use bevy::utils::HashMap;
use facing::FacingIndicator;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -110,6 +111,12 @@ fn actor_helper(mut entity: EntityWorldMut, key: Option<String>) -> EntityWorldM
size: vec2(8.0, 1.0),
})
.insert(Transform::from_translation(vec3(0.0, -4.5, 1.0)));

children
.spawn_with(FacingIndicator {
radius: vec2(5.5, 4.5),
})
.insert(Transform::from_translation(vec3(0.0, -0.5, 2.0)));
});

entity
Expand Down
46 changes: 45 additions & 1 deletion src/game/actor/facing.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use bevy::ecs::system::EntityCommand;
use bevy::prelude::*;

use crate::animation::backup::Backup;
use crate::core::camera::CameraRoot;
use crate::core::theme::ThemeColor;
use crate::core::window::WindowRoot;
use crate::core::PostTransformSet;
use crate::core::UpdateSet;
use crate::game::actor::player::IsPlayer;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.configure::<(Facing, FacePlayer, FaceCursor)>();
app.configure::<(Facing, FacePlayer, FaceCursor, FacingIndicator)>();
}

#[derive(Component, Reflect)]
Expand Down Expand Up @@ -78,3 +82,43 @@ fn face_cursor(
facing.0 = c!(Dir2::new(target_pos - pos));
}
}

/// Reads from the `Facing` component on its parent entity.
#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct FacingIndicator {
pub radius: Vec2,
}

impl Configure for FacingIndicator {
fn configure(app: &mut App) {
app.register_type::<Self>();
app.add_systems(
PostUpdate,
update_facing_indicator.in_set(PostTransformSet::Blend),
);
}
}

fn update_facing_indicator(
facing_query: Query<&Facing>,
mut facing_indicator_query: Query<(&Parent, &FacingIndicator, &mut Transform)>,
) {
for (parent, facing_indicator, mut transform) in &mut facing_indicator_query {
let facing = c!(facing_query.get(parent.get()));
transform.translation += (facing_indicator.radius * facing.0.as_vec2()).extend(0.0);
transform.rotate(Quat::from_rotation_z(facing.0.to_angle()));
}
}

impl EntityCommand for FacingIndicator {
fn apply(self, id: Entity, world: &mut World) {
world.entity_mut(id).insert((
Name::new("FacingIndicator"),
SpriteBundle::default(),
ThemeColor::FacingIndicator.target::<Sprite>(),
Backup::<Transform>::default(),
self,
));
}
}
4 changes: 2 additions & 2 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const FADE_IN_SECS: f32 = 0.5;
fn fade_in(mut entity: EntityWorldMut) {
entity.add(widget::overlay).insert((
Name::new("ScreenFadeIn"),
ThemeColor::Body.set::<BackgroundColor>(),
ThemeColor::Body.target::<BackgroundColor>(),
FadeIn::new(FADE_IN_SECS),
));
}
Expand All @@ -60,7 +60,7 @@ fn fade_out(to_screen: Screen) -> impl EntityCommand<World> {
move |mut entity: EntityWorldMut| {
entity.add(widget::blocking_overlay).insert((
Name::new("ScreenFadeOut"),
ThemeColor::Body.set::<BackgroundColor>(),
ThemeColor::Body.target::<BackgroundColor>(),
FadeOut::new(FADE_OUT_SECS, to_screen),
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/screen/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn loading_bar(mut entity: EntityWorldMut) {
},
..default()
},
ThemeColor::BodyText.set::<BorderColor>(),
ThemeColor::BodyText.target::<BorderColor>(),
))
.with_children(|children| {
children.spawn_with(loading_bar_fill);
Expand All @@ -112,7 +112,7 @@ fn loading_bar_fill(mut entity: EntityWorldMut) {
},
..default()
},
ThemeColor::Primary.set::<BackgroundColor>(),
ThemeColor::Primary.target::<BackgroundColor>(),
IsLoadingBarFill,
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/screen/splash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn splash_image(mut entity: EntityWorldMut) {
)),
..default()
},
ThemeColor::BodyText.set::<UiImage>(),
ThemeColor::BodyText.target::<UiImage>(),
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl FromWorld for TooltipRoot {
z_index: ZIndex::Global(999),
..default()
},
ThemeColor::Popup.set::<BackgroundColor>(),
ThemeColor::Popup.target::<BackgroundColor>(),
))
.id();

Expand Down
10 changes: 5 additions & 5 deletions src/ui/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ pub fn menu_button(text: impl Into<String>) -> impl EntityCommand<World> {
border_radius: BorderRadius::MAX,
..default()
},
ThemeColor::default().set::<BackgroundColor>(),
ThemeColor::default().target::<BackgroundColor>(),
InteractionTable {
normal: ThemeColor::Primary.set::<BackgroundColor>(),
hovered: ThemeColor::PrimaryHovered.set::<BackgroundColor>(),
pressed: ThemeColor::PrimaryPressed.set::<BackgroundColor>(),
disabled: ThemeColor::PrimaryDisabled.set::<BackgroundColor>(),
normal: ThemeColor::Primary.target::<BackgroundColor>(),
hovered: ThemeColor::PrimaryHovered.target::<BackgroundColor>(),
pressed: ThemeColor::PrimaryPressed.target::<BackgroundColor>(),
disabled: ThemeColor::PrimaryDisabled.target::<BackgroundColor>(),
},
Offset::default(),
Backup::<Transform>::default(),
Expand Down

0 comments on commit 9f0f4b6

Please sign in to comment.