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};
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;
impl Plugin for ActorPlugin {
@ -59,6 +59,7 @@ impl Default for Actor {
#[derive(Component)] pub struct PlayerInConversation;
#[derive(Component)] pub struct InConversationWithPlayer;
#[derive(Debug)]
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
pub struct ChatBranch {
@ -118,10 +119,10 @@ pub fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
commands.spawn(DynamicSceneBundle {
scene: asset_server.load(ASSET_CONVERSATIONS),
..default()
});
// commands.spawn(DynamicSceneBundle {
// scene: asset_server.load(ASSET_CONVERSATIONS),
// ..default()
// });
}
pub fn update_physics_actors(
@ -236,10 +237,12 @@ pub fn handle_send_messages(
let branch = branches[0];
// TODO despawn the choices
match branch.level.as_str() {
"chat" => log.chat(branch.reply.clone(), branch.name.clone()),
"info" => log.info(branch.reply.clone()),
_ => (),
if !branch.reply.is_empty() {
match branch.level.as_str() {
"chat" => log.chat(branch.reply.clone(), branch.name.clone()),
"info" => log.info(branch.reply.clone()),
_ => (),
}
}
chat.label = branch.goto.clone();
@ -296,10 +299,12 @@ pub fn handle_conversations(
}
let branch = branches[0];
match branch.level.as_str() {
"chat" => log.chat(branch.reply.clone(), branch.name.clone()),
"info" => log.info(branch.reply.clone()),
_ => (),
if !branch.reply.is_empty() {
match branch.level.as_str() {
"chat" => log.chat(branch.reply.clone(), branch.name.clone()),
"info" => log.info(branch.reply.clone()),
_ => (),
}
}
if chat.label == "EXIT" {

View file

@ -2,26 +2,47 @@ chat "hialien"
name "Icarus"
msg 2 "Requesting permission to communicate..."
label "INIT"
lvl "info"
goto "b"
msg 4 "Oh hey there!"
label "b"
goto "c"
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?"
label "c"
goto "d"
choice "3" "I'm good, how are you?"
label "d"
goto "imgood"
choice 3 "Uhm... where am I? I don't feel so good."
label "d"
goto "imbad"
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."
label "pizza"
goto "disco"
choice 3 "Will do, bye!"
goto "disco"
label "disco"
goto "disco1"
msg 0 "Disconnected."
label "disco1"
goto "EXIT"
msg 0 "Disconnected."
label "disco"
goto "EXIT"
msg 5.5 "Are you sure? Your suit is sending a distress call. But whatever you say, have fun!"
label "imgood"

View file

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