chat refactoring WIP

This commit is contained in:
Niko Abeler 2023-01-04 19:41:31 +01:00
parent 62565bd2d1
commit 0af3b17810
3 changed files with 91 additions and 29 deletions

View File

@ -5,11 +5,11 @@ mod time;
mod generators;
mod creature;
mod site;
mod app;
mod ui;
mod entity;
mod game;
use app::App;
use ui::App;
use noise::{Perlin, ScalePoint, Add, NoiseFn, ScaleBias};
use noise::utils::{NoiseMapBuilder, PlaneMapBuilder};
use image::{RgbImage, Rgb};

57
src/ui/chat.rs Normal file
View File

@ -0,0 +1,57 @@
use tui::{text::{Span}, style::{Style, Color}};
use crate::entity::EntityId;
pub struct ChatLine {
pub speaker_id: EntityId,
pub speaker: String,
pub text: String,
pub action: bool,
}
pub struct Chat {
pub lines: Vec<ChatLine>,
}
impl ChatLine {
pub fn new(speaker_id: EntityId, speaker: String, text: String, action: bool) -> ChatLine {
ChatLine {
speaker_id: speaker_id,
speaker: speaker,
text: text,
action: action,
}
}
pub fn to_spans(&self) -> tui::text::Spans {
let mut spans = Vec::new();
spans.push(Span::styled(format!("{}: ", self.speaker), Style::default().fg(Color::Red)));
spans.push(Span::styled(format!("{}", self.text), Style::default().fg(Color::White)));
tui::text::Spans::from(spans)
}
}
impl Chat {
pub fn new() -> Chat {
Chat {
lines: Vec::new(),
}
}
pub fn add_line(&mut self, line: ChatLine) {
self.lines.push(line);
}
pub fn to_spans(&self) -> Vec<tui::text::Spans> {
let mut spans = Vec::new();
for line in &self.lines {
spans.push(line.to_spans());
spans.push(tui::text::Spans::from(vec![
Span::styled("\n", Style::default().fg(Color::White))
]));
}
spans
}
}

View File

@ -1,8 +1,12 @@
mod chat;
use crossterm::event::{read, Event, KeyCode};
use tui::{backend::Backend, Frame, layout::Layout};
use crate::{game::Game, entity::EntityId};
use self::chat::{Chat, ChatLine};
/**
* |........................|
* | Conversations/Selection|
@ -23,7 +27,7 @@ pub struct App<'a> {
guest_list: Vec<tui::widgets::ListItem<'a>>,
guest_list_state: tui::widgets::ListState,
status: AppStatus,
conversation: Vec<tui::text::Spans<'a>>,
conversation: Chat,
}
impl<'a> App<'a> {
@ -33,7 +37,7 @@ impl<'a> App<'a> {
guest_list: vec![],
guest_list_state: tui::widgets::ListState::default(),
status: AppStatus::Initial,
conversation: Vec::new(),
conversation: Chat::new(),
}
}
@ -69,7 +73,7 @@ impl<'a> App<'a> {
KeyCode::Enter => {
let selected = self.guest_list_state.selected().unwrap();
let guest_id = &self.game.state.guests()[selected];
self.conversation = Vec::new();
self.conversation = Chat::new();
self.greet_guest(Some(*guest_id));
self.status = AppStatus::TalkToGuest(Some(*guest_id));
},
@ -174,7 +178,7 @@ impl<'a> App<'a> {
.fg(tui::style::Color::Green)
.bg(tui::style::Color::Black);
let mut full_text = self.conversation.clone();
let mut full_text = self.conversation.to_spans();
full_text.push(tui::text::Spans::from(vec![
tui::text::Span::styled("(a) ", key_style),
tui::text::Span::raw("What's your business?"),
@ -192,32 +196,33 @@ impl<'a> App<'a> {
*/
fn greet_guest(&mut self, guest_id: Option<EntityId>) {
let guest = self.game.state.get_creature(guest_id.unwrap()).unwrap();
self.conversation.push(
tui::text::Spans::from(vec![
tui::text::Span::styled("Greetings traveller?\n", tui::style::Style::default().fg(tui::style::Color::Yellow)),
])
);
self.conversation.push(
tui::text::Spans::from(vec![
tui::text::Span::raw("Hello, I'm "),
tui::text::Span::styled(guest.name.clone(), tui::style::Style::default().add_modifier(tui::style::Modifier::BOLD)),
tui::text::Span::raw(".\n"),
])
);
self.conversation.add_line(ChatLine::new(
0,
"You".to_owned(),
"Greetings traveller!".to_owned(),
false
));
self.conversation.add_line(ChatLine::new(
guest_id.unwrap(),
guest.name.clone(),
"Hello, I'm ".to_owned() + &guest.name + ".",
false
));
}
fn ask_business(&mut self, guest: Option<EntityId>) {
let guest = self.game.state.get_creature(guest.unwrap()).unwrap();
self.conversation.push(
tui::text::Spans::from(vec![
tui::text::Span::styled("What's your business?\n", tui::style::Style::default().fg(tui::style::Color::Yellow))
])
);
self.conversation.push(
tui::text::Spans::from(vec![
tui::text::Span::raw(guest.say_agenda(& self.game.state)),
tui::text::Span::raw("\n"),
])
);
self.conversation.add_line(ChatLine::new(
0,
"You".to_owned(),
"What's your business?".to_owned(),
false
));
self.conversation.add_line(ChatLine::new(
guest.entity.id,
guest.name.clone(),
guest.say_agenda(& self.game.state),
false
));
}
}