finished world def parser for conversations

This commit is contained in:
yuni 2024-03-20 05:52:02 +01:00
parent 96584db0a3
commit de47a87a27
3 changed files with 110 additions and 23 deletions

View file

@ -2,7 +2,7 @@ use bevy::prelude::*;
use crate::{nature, settings, actor, audio, hud}; use crate::{nature, settings, actor, audio, hud};
const MIN_INTERACT_DISTANCE: f32 = 30.0; const MIN_INTERACT_DISTANCE: f32 = 30.0;
const ASSET_CONVERSATIONS: &str = "scenes/conversations.scn.ron"; //const ASSET_CONVERSATIONS: &str = "scenes/conversations.scn.ron";
pub struct ActorPlugin; pub struct ActorPlugin;
impl Plugin for ActorPlugin { impl Plugin for ActorPlugin {
@ -59,6 +59,7 @@ impl Default for Actor {
#[derive(Component)] pub struct PlayerInConversation; #[derive(Component)] pub struct PlayerInConversation;
#[derive(Component)] pub struct InConversationWithPlayer; #[derive(Component)] pub struct InConversationWithPlayer;
#[derive(Debug)]
#[derive(Component, Reflect, Default)] #[derive(Component, Reflect, Default)]
#[reflect(Component)] #[reflect(Component)]
pub struct ChatBranch { pub struct ChatBranch {
@ -118,10 +119,10 @@ pub fn setup(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
) { ) {
commands.spawn(DynamicSceneBundle { // commands.spawn(DynamicSceneBundle {
scene: asset_server.load(ASSET_CONVERSATIONS), // scene: asset_server.load(ASSET_CONVERSATIONS),
..default() // ..default()
}); // });
} }
pub fn update_physics_actors( pub fn update_physics_actors(
@ -236,11 +237,13 @@ pub fn handle_send_messages(
let branch = branches[0]; let branch = branches[0];
// TODO despawn the choices // TODO despawn the choices
if !branch.reply.is_empty() {
match branch.level.as_str() { match branch.level.as_str() {
"chat" => log.chat(branch.reply.clone(), branch.name.clone()), "chat" => log.chat(branch.reply.clone(), branch.name.clone()),
"info" => log.info(branch.reply.clone()), "info" => log.info(branch.reply.clone()),
_ => (), _ => (),
} }
}
chat.label = branch.goto.clone(); chat.label = branch.goto.clone();
chat.timer = now + branch.delay; chat.timer = now + branch.delay;
@ -296,11 +299,13 @@ pub fn handle_conversations(
} }
let branch = branches[0]; let branch = branches[0];
if !branch.reply.is_empty() {
match branch.level.as_str() { match branch.level.as_str() {
"chat" => log.chat(branch.reply.clone(), branch.name.clone()), "chat" => log.chat(branch.reply.clone(), branch.name.clone()),
"info" => log.info(branch.reply.clone()), "info" => log.info(branch.reply.clone()),
_ => (), _ => (),
} }
}
if chat.label == "EXIT" { if chat.label == "EXIT" {
// TODO: isn't this dead code? // TODO: isn't this dead code?

View file

@ -2,26 +2,47 @@ chat "hialien"
name "Icarus" name "Icarus"
msg 2 "Requesting permission to communicate..." msg 2 "Requesting permission to communicate..."
label "INIT"
lvl "info" lvl "info"
goto "b"
msg 4 "Oh hey there!" msg 4 "Oh hey there!"
label "b"
goto "c"
choice 3 "Uhm... hi" choice 3 "Uhm... hi"
label "c"
goto "c1"
msg 8 "Didn't even notice you! Was playing some VR Game! What's up?"
label "c1"
goto "d"
msg 8 "Didn't even notice you! Was playing some VR Game! What's up?" msg 8 "Didn't even notice you! Was playing some VR Game! What's up?"
label "c"
goto "d"
choice "3" "I'm good, how are you?" choice "3" "I'm good, how are you?"
label "d"
goto "imgood" goto "imgood"
choice 3 "Uhm... where am I? I don't feel so good." choice 3 "Uhm... where am I? I don't feel so good."
label "d"
goto "imbad" goto "imbad"
msg 3.5 "Not so chatty, huh? That's ok. See you around." msg 3.5 "Not so chatty, huh? That's ok. See you around."
label "d"
goto "pizza"
msg 2.5 "Make sure to check out the Pizza place." msg 2.5 "Make sure to check out the Pizza place."
label "pizza" label "pizza"
choice 3 "Will do, bye!"
goto "disco" goto "disco"
choice 3 "Will do, bye!"
label "disco"
goto "disco1"
msg 0 "Disconnected."
label "disco1"
goto "EXIT"
msg 0 "Disconnected." msg 0 "Disconnected."
label "disco" label "disco"
goto "EXIT"
msg 5.5 "Are you sure? Your suit is sending a distress call. But whatever you say, have fun!" msg 5.5 "Are you sure? Your suit is sending a distress call. But whatever you say, have fun!"
label "imgood" label "imgood"

View file

@ -216,9 +216,9 @@ pub fn setup(
SceneBundle { SceneBundle {
transform: Transform { transform: Transform {
translation: Vec3::new( translation: Vec3::new(
-40.0, 20.0,
0.0,
0.0, 0.0,
40.0,
), ),
rotation: Quat::from_rotation_y(-PI / 3.), rotation: Quat::from_rotation_y(-PI / 3.),
scale: Vec3::splat(ASTRONAUT_SIZE), scale: Vec3::splat(ASTRONAUT_SIZE),
@ -314,6 +314,7 @@ pub fn setup(
}); });
} }
#[derive(Debug)]
struct ParserState { struct ParserState {
chat: String, chat: String,
name: String, name: String,
@ -323,6 +324,7 @@ struct ParserState {
label: String, label: String,
goto: String, goto: String,
is_choice: bool, is_choice: bool,
stores_item: bool,
} }
impl Default for ParserState { impl Default for ParserState {
fn default() -> Self { fn default() -> Self {
@ -331,15 +333,67 @@ impl Default for ParserState {
name: "".to_string(), name: "".to_string(),
delay: 0.0, delay: 0.0,
text: "".to_string(), text: "".to_string(),
level: "".to_string(), level: "chat".to_string(),
label: "".to_string(), label: "".to_string(),
goto: "".to_string(), goto: "".to_string(),
is_choice: false, is_choice: false,
stores_item: false,
}
}
}
impl std::fmt::Display for ParserState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ParserState")
.field("chat", &self.chat)
.field("name", &self.name)
.field("delay", &self.delay)
.field("text", &self.text)
.field("level", &self.level)
.field("label", &self.label)
.field("goto", &self.goto)
.field("is_choice", &self.is_choice)
.field("stores_item", &self.stores_item)
.finish()
}
}
impl ParserState {
fn reset_message(&mut self) {
let default = ParserState::default();
self.label = default.label;
self.delay = default.delay;
self.goto = default.goto;
self.level = default.level;
self.text = default.text;
self.is_choice = default.is_choice;
}
fn reset_chat(&mut self) {
let default = ParserState::default();
self.reset_message();
self.stores_item = default.stores_item;
}
fn as_chatbranch(&self) -> actor::ChatBranch {
return actor::ChatBranch {
id: self.chat.clone(),
name: self.name.clone(),
label: self.label.clone(),
delay: self.delay.clone(),
sound: "chat".to_string(),
level: self.level.clone(),
reply: if self.is_choice { "".to_string() } else { self.text.clone() },
choice: if self.is_choice { self.text.clone() } else { "".to_string() },
goto: self.goto.clone(),
}
}
fn spawn_chatbranch(&self, commands: &mut Commands) {
if self.stores_item {
info!("{:#?}", self.as_chatbranch());
commands.spawn(self.as_chatbranch());
} }
} }
} }
pub fn load_defs( pub fn load_defs(
mut commands: Commands,
) { ) {
let re1 = Regex::new(r"^\s*([a-z]+)\s+(.*)$").unwrap(); let re1 = Regex::new(r"^\s*([a-z]+)\s+(.*)$").unwrap();
let re2 = Regex::new("\"([^\"]*)\"|([0-9]+(?:\\.[0-9]+)?)").unwrap(); let re2 = Regex::new("\"([^\"]*)\"|([0-9]+(?:\\.[0-9]+)?)").unwrap();
@ -373,29 +427,35 @@ pub fn load_defs(
match parts.as_slice() { match parts.as_slice() {
["chat", chat_name] => { ["chat", chat_name] => {
debug!("Registering chat: {}", chat_name); debug!("Registering chat: {}", chat_name);
state.name = chat_name.to_string(); state.spawn_chatbranch(&mut commands);
state.reset_chat();
state.chat = chat_name.to_string();
} }
["name", name] => { ["name", name] => {
debug!("Registering name: {}", name); debug!("Registering name: {}", name);
state.name = name.to_string(); state.name = name.to_string();
} }
["msg", sleep, text] => { ["msg", sleep, text] => {
info!("Registering message (sleep={}): {}", sleep, text); debug!("Registering message (sleep={}): {}", sleep, text);
// TODO: write previous message/choice state.spawn_chatbranch(&mut commands);
if let Ok(sleep_float) = sleep.parse::<f64>() { if let Ok(sleep_float) = sleep.parse::<f64>() {
state.delay = sleep_float; state.delay = sleep_float;
state.text = text.to_string(); state.text = text.to_string();
state.stores_item = true;
state.is_choice = false;
} else { } else {
error!("The 'sleep' value for this message is not a float: {}", line); error!("The 'sleep' value for this message is not a float: {}", line);
continue; continue;
} }
} }
["choice", sleep, text] => { ["choice", sleep, text] => {
info!("Registering choice (sleep={}): {}", sleep, text); debug!("Registering choice (sleep={}): {}", sleep, text);
// TODO: write previous message/choice state.spawn_chatbranch(&mut commands);
if let Ok(sleep_float) = sleep.parse::<f64>() { if let Ok(sleep_float) = sleep.parse::<f64>() {
state.delay = sleep_float; state.delay = sleep_float;
state.text = text.to_string(); state.text = text.to_string();
state.stores_item = true;
state.is_choice = true;
} else { } else {
error!("The 'sleep' value for this message is not a float: {}", line); error!("The 'sleep' value for this message is not a float: {}", line);
continue; continue;
@ -410,7 +470,7 @@ pub fn load_defs(
state.label = label.to_string(); state.label = label.to_string();
} }
["lvl", level] => { ["lvl", level] => {
info!("Registering level: {}", level); debug!("Registering level: {}", level);
state.level = level.to_string(); state.level = level.to_string();
} }
_ => { _ => {
@ -418,6 +478,7 @@ pub fn load_defs(
} }
} }
} }
state.spawn_chatbranch(&mut commands);
} }
//pub fn swap_world_on_ar_toggle( //pub fn swap_world_on_ar_toggle(