Skip to content

Commit

Permalink
Refactor prompt stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim committed Sep 15, 2024
1 parent ed16ad4 commit 889f186
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 95 deletions.
Binary file modified art/foxtrot.blend
Binary file not shown.
Binary file modified assets/levels/World.glb
Binary file not shown.
146 changes: 91 additions & 55 deletions assets/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@
"type": "array",
"typeInfo": "Tuple"
},
"(foxtrot::player::interactions::components::PlayerInteractionParameters, foxtrot::player::interactions::components::AvailablePlayerInteraction, foxtrot::player::interactions::components::PlayerInteraction)": {
"isComponent": false,
"isResource": false,
"items": false,
"long_name": "(foxtrot::player::interactions::components::PlayerInteractionParameters, foxtrot::player::interactions::components::AvailablePlayerInteraction, foxtrot::player::interactions::components::PlayerInteraction)",
"prefixItems": [
{
"type": {
"$ref": "#/$defs/foxtrot::player::interactions::components::PlayerInteractionParameters"
}
},
{
"type": {
"$ref": "#/$defs/foxtrot::player::interactions::components::AvailablePlayerInteraction"
}
},
{
"type": {
"$ref": "#/$defs/foxtrot::player::interactions::components::PlayerInteraction"
}
}
],
"short_name": "(PlayerInteractionParameters, AvailablePlayerInteraction, PlayerInteraction)",
"type": "array",
"typeInfo": "Tuple"
},
"(u8, u8)": {
"isComponent": false,
"isResource": false,
Expand Down Expand Up @@ -18304,61 +18330,6 @@
"type": "string",
"typeInfo": "Enum"
},
"foxtrot::opportunities::available_opportunities::PlayerInteractable": {
"additionalProperties": false,
"isComponent": true,
"isResource": false,
"long_name": "foxtrot::opportunities::available_opportunities::PlayerInteractable",
"properties": {
"interaction": {
"type": {
"$ref": "#/$defs/foxtrot::opportunities::available_opportunities::PlayerInteraction"
}
},
"max_distance": {
"type": {
"$ref": "#/$defs/f32"
}
},
"prompt": {
"type": {
"$ref": "#/$defs/alloc::string::String"
}
}
},
"required": [
"prompt",
"interaction",
"max_distance"
],
"short_name": "PlayerInteractable",
"type": "object",
"typeInfo": "Struct"
},
"foxtrot::opportunities::available_opportunities::PlayerInteraction": {
"isComponent": true,
"isResource": false,
"long_name": "foxtrot::opportunities::available_opportunities::PlayerInteraction",
"oneOf": [
{
"items": false,
"long_name": "Dialog",
"prefixItems": [
{
"type": {
"$ref": "#/$defs/alloc::string::String"
}
}
],
"short_name": "Dialog",
"type": "array",
"typeInfo": "Tuple"
}
],
"short_name": "PlayerInteraction",
"type": "object",
"typeInfo": "Enum"
},
"foxtrot::player::Player": {
"additionalProperties": false,
"isComponent": true,
Expand Down Expand Up @@ -18430,6 +18401,71 @@
"type": "object",
"typeInfo": "Struct"
},
"foxtrot::player::interactions::components::AvailablePlayerInteraction": {
"isComponent": true,
"isResource": false,
"items": false,
"long_name": "foxtrot::player::interactions::components::AvailablePlayerInteraction",
"prefixItems": [
{
"type": {
"$ref": "#/$defs/core::option::Option<bevy_ecs::entity::Entity>"
}
}
],
"short_name": "AvailablePlayerInteraction",
"type": "array",
"typeInfo": "TupleStruct"
},
"foxtrot::player::interactions::components::PlayerInteraction": {
"isComponent": true,
"isResource": false,
"long_name": "foxtrot::player::interactions::components::PlayerInteraction",
"oneOf": [
{
"items": false,
"long_name": "Dialog",
"prefixItems": [
{
"type": {
"$ref": "#/$defs/alloc::string::String"
}
}
],
"short_name": "Dialog",
"type": "array",
"typeInfo": "Tuple"
}
],
"short_name": "PlayerInteraction",
"type": "object",
"typeInfo": "Enum"
},
"foxtrot::player::interactions::components::PlayerInteractionParameters": {
"additionalProperties": false,
"isComponent": true,
"isResource": false,
"long_name": "foxtrot::player::interactions::components::PlayerInteractionParameters",
"properties": {
"max_distance": {
"type": {
"$ref": "#/$defs/f32"
}
},
"prompt": {
"type": {
"$ref": "#/$defs/alloc::string::String"
}
}
},
"required": [
"prompt",
"max_distance"
],
"short_name": "PlayerInteractionParameters",
"type": "object",
"typeInfo": "Struct"
},
"foxtrot::screens::splash::SplashTimer": {
"isComponent": false,
"isResource": true,
Expand Down
3 changes: 2 additions & 1 deletion src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn spawn_crosshair(mut commands: Commands, asset_server: Res<AssetServer>) {
let crosshair_texture = asset_server.load("textures/crosshair.png");
commands
.spawn((
Name::new("Crosshair UI"),
StateScoped(GameplayState::Playing),
NodeBundle {
style: Style {
Expand All @@ -57,7 +58,7 @@ fn spawn_crosshair(mut commands: Commands, asset_server: Res<AssetServer>) {
))
.with_children(|parent| {
parent.spawn(ImageBundle {
image: crosshair_texture.into(),
image: UiImage::from(crosshair_texture).with_color(Color::WHITE.with_alpha(0.4)),
..default()
});
});
Expand Down
13 changes: 12 additions & 1 deletion src/dialog/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_yarnspinner::prelude::{DialogueRunner, YarnSpinnerPlugin};
use bevy_yarnspinner::prelude::*;
use bevy_yarnspinner_example_dialogue_view::ExampleYarnSpinnerDialogueViewPlugin;

pub mod conditions;
Expand All @@ -10,6 +10,10 @@ pub(super) fn plugin(app: &mut App) {
ExampleYarnSpinnerDialogueViewPlugin::default(),
));
app.observe(start_dialogue);
app.add_systems(
PreUpdate,
spawn_dialogue_runner.run_if(resource_added::<YarnProject>),
);
}

/// Event triggered to start a dialogue with the targeted entity.
Expand All @@ -24,3 +28,10 @@ fn start_dialogue(
dialogue_runner.start_node(&trigger.event().0);
}
}

fn spawn_dialogue_runner(mut commands: Commands, project: Res<YarnProject>) {
commands.spawn((
Name::new("Dialogue Runner"),
project.create_dialogue_runner(),
));
}
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod dev_tools;
mod dialog;
mod hacks;
mod level;
mod opportunities;
mod player;
mod screens;
mod system_set;
Expand Down Expand Up @@ -106,7 +105,6 @@ impl Plugin for AppPlugin {
hacks::plugin,
cursor::plugin,
dialog::plugin,
opportunities::plugin,
));

// Enable dev tools for dev builds.
Expand Down
4 changes: 2 additions & 2 deletions src/player/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use leafwing_input_manager::InputManagerBundle;

use crate::{
asset_tracking::LoadResource as _,
character::{action::CharacterAction, controller::OverrideForwardDirection}, opportunities::available_opportunities::AvailablePlayerInteraction,
character::{action::CharacterAction, controller::OverrideForwardDirection},
};

use super::{camera::PlayerCamera, Player};
use super::{camera::PlayerCamera, interactions::components::AvailablePlayerInteraction, Player};

pub(super) fn plugin(app: &mut App) {
app.load_resource::<PlayerAssets>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
use crate::{
collision_layer::CollisionLayer,
dialog::conditions::dialog_running,
player::{camera::PlayerCamera, Player},
};
use avian3d::prelude::*;
use bevy::{
ecs::component::{ComponentHooks, StorageType},
prelude::*,
};

use super::OpportunitySystem;

pub(super) fn plugin(app: &mut App) {
app.register_type::<(
PlayerInteractionParameters,
Expand Down Expand Up @@ -40,9 +32,9 @@ pub struct PlayerInteractionParameters {
impl PlayerInteractionParameters {
pub fn default(player_interaction: &PlayerInteraction) -> Self {
match player_interaction {
PlayerInteraction::Dialog(node) => Self {
PlayerInteraction::Dialog(..) => Self {
prompt: "Talk".to_string(),
max_distance: 10.0,
max_distance: 2.0,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use avian3d::prelude::ColliderParent;
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;

use crate::{character::action::CharacterAction, dialog::StartDialog, player::Player};

use super::{
available_opportunities::{
AvailablePlayerInteraction, PlayerInteraction, PlayerInteractionParameters,
components::{
AvailablePlayerInteraction, PlayerInteraction,
},
OpportunitySystem,
};
Expand All @@ -25,21 +24,21 @@ fn usher_interact(
),
With<Player>,
>,
q_interactable: Query<(Entity, &PlayerInteractionParameters)>,
q_interaction: Query<(Entity, &PlayerInteraction)>,
mut commands: Commands,
) {
for (action_state, mut active_interactable) in &mut q_player {
if active_interactable.is_none() || !action_state.just_pressed(&CharacterAction::Interact) {
continue;
}
let Some((entity, interactable)) = active_interactable
let Some((entity, interaction)) = active_interactable
// Clear the current interactable so that it won't show up if we end up in a dialog
.take()
.and_then(|e| q_interactable.get(e).ok())
.and_then(|e| q_interaction.get(e).ok())
else {
continue;
};
match &interactable.interaction {
match interaction {
PlayerInteraction::Dialog(node) => {
commands.trigger_targets(StartDialog(node.clone()), entity);
}
Expand Down
4 changes: 2 additions & 2 deletions src/opportunities/mod.rs → src/player/interactions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::system_set::VariableGameSystem;
use bevy::prelude::*;

pub mod available_opportunities;
pub mod components;
mod interact;
mod prompt;
mod update_available;

pub(super) fn plugin(app: &mut App) {
app.add_plugins((
available_opportunities::plugin,
components::plugin,
prompt::plugin,
interact::plugin,
update_available::plugin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::ui::Val::*;
use sickle_ui::{prelude::*, ui_commands::SetTextExt as _};

use super::{
available_opportunities::{AvailablePlayerInteraction, PlayerInteractionParameters},
components::{AvailablePlayerInteraction, PlayerInteractionParameters},
OpportunitySystem,
};

Expand All @@ -14,7 +14,7 @@ pub(super) fn plugin(app: &mut App) {

fn show_prompt(
q_active_interactable: Query<&AvailablePlayerInteraction, Changed<AvailablePlayerInteraction>>,
q_interactable: Query<&PlayerInteractionParameters>,
q_interaction_params: Query<&PlayerInteractionParameters>,
mut q_prompt_text_node: Query<&mut Text, With<PromptTextNode>>,
mut q_prompt_visibility: Query<&mut Visibility, With<PromptUiRootNode>>,
) {
Expand All @@ -25,14 +25,13 @@ fn show_prompt(
let Ok(mut prompt_visibility) = q_prompt_visibility.get_single_mut() else {
return;
};
let Some(interactable) = active_interactable
let Some(interaction_params) = active_interactable
.0
.and_then(|entity| q_interactable.get(entity).ok())
.and_then(|entity| q_interaction_params.get(entity).ok())
else {
// The previous interactable is no longer available.
// Note that we don't check against previous values for change detection
// because this system is only run when the active interactable changes in the first place.
info!("hidder");
*prompt_visibility = Visibility::Hidden;
return;
};
Expand All @@ -41,7 +40,7 @@ fn show_prompt(
};

*prompt_visibility = Visibility::Inherited;
prompt_text_node.sections[0].value = interactable.prompt.clone();
prompt_text_node.sections[0].value = interaction_params.prompt.clone();
}

fn spawn_prompt(mut commands: Commands) {
Expand Down
Loading

0 comments on commit 889f186

Please sign in to comment.