tavern_keeper/src/ui/talk_to_guest.rs

138 lines
4.8 KiB
Rust

use crossterm::event::{read, Event, KeyCode};
use tui::{backend::Backend, Frame};
use crate::{game::Game, entity::{EntityId, EntityType}};
use super::{AppStatus, DefaultLayout, controls::Controls, status_line::StatusLine, chat::{Chat, ChatLine}, guest_selection::GuestSelectionView};
pub struct TalkToGuestView {
guest_id: EntityId,
conversation: Chat,
conversation_scroll: u16,
}
impl TalkToGuestView {
pub fn new(game: &Game, guest_id: EntityId) -> TalkToGuestView {
let mut view = TalkToGuestView {
guest_id,
conversation: Chat::new(),
conversation_scroll: 0,
};
view.greet_guest(game);
view
}
pub fn control(&mut self, game: &mut Game) -> (bool, Option<AppStatus>) {
match read() {
Ok(Event::Key(event)) => {
match event.code {
KeyCode::Esc => {
return (true, Some(AppStatus::GuestSelection(GuestSelectionView::new(game))));
},
KeyCode::Char('a') => {
self.ask_business(game);
},
KeyCode::Char('b') => {
// self.status = AppStatus::BuyFromGuest(guest.unwrap());
},
KeyCode::Up => {
self.conversation_scroll += 1;
},
KeyCode::Down => {
if self.conversation_scroll > 0 {
self.conversation_scroll -= 1;
}
},
_ => {}
}
},
_ => {}
}
return (true, None);
}
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>, game: &Game) {
let chunks = DefaultLayout::default(f.size());
let guest = game.state.get_creature(self.guest_id).unwrap();
let key_style = tui::style::Style::default()
.add_modifier(tui::style::Modifier::BOLD)
.fg(tui::style::Color::Green)
.bg(tui::style::Color::Black);
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?"),
]));
full_text.push(tui::text::Spans::from(vec![
tui::text::Span::styled("(b) ", key_style),
tui::text::Span::raw("What do you have to sell?"),
]));
full_text.push(tui::text::Spans::from(vec![
tui::text::Span::styled("(c) ", key_style),
tui::text::Span::raw("Heard of anything interesting?"),
]));
let text = tui::text::Text::from(full_text);
let scroll: u16 = ((text.lines.len() as u16) )
.saturating_sub(
(chunks.main.height as u16).saturating_sub(2) // 2 lines for the border
)
.saturating_sub(self.conversation_scroll);
let main_window = tui::widgets::Paragraph::new(text)
.block(tui::widgets::Block::default().title(guest.name.clone()).borders(tui::widgets::Borders::ALL))
.style(tui::style::Style::default().fg(tui::style::Color::White))
.scroll((scroll, 0));
let mut binding = Controls::new();
let controls = binding
.add("a-z".to_owned(), "Talk".to_owned())
.add("Esc".to_owned(), "Back".to_owned())
.render();
StatusLine::draw(f, chunks.status, game);
f.render_widget(main_window, chunks.main);
f.render_widget(controls, chunks.controls);
}
/**
* Conversation
*/
fn greet_guest(&mut self, game: &Game) {
let guest = game.state.get_creature(self.guest_id).unwrap();
self.conversation.add_line(ChatLine::new(
(EntityType::Creature, 0),
"You".to_owned(),
"Greetings traveller!".to_owned(),
false
));
self.conversation.add_line(ChatLine::new(
self.guest_id,
guest.name.clone(),
"Hello, I'm ".to_owned() + &guest.name + ", nice to meet you! "
+ "I'm a " + &guest.profession.to_string() + ".",
false
));
self.conversation_scroll = 0;
}
fn ask_business(&mut self, game: &mut Game) {
let guest = game.state.get_creature(self.guest_id).unwrap();
self.conversation.add_line(ChatLine::new(
(EntityType::Creature, 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(& game.state),
false
));
self.conversation_scroll = 0;
}
}