264 lines
9.7 KiB
Rust
264 lines
9.7 KiB
Rust
mod chat;
|
|
mod controls;
|
|
|
|
use crossterm::event::{read, Event, KeyCode};
|
|
use tui::{backend::Backend, Frame, layout::Layout};
|
|
|
|
use crate::{game::Game, entity::EntityId};
|
|
|
|
use self::{chat::{Chat, ChatLine}, controls::Controls};
|
|
|
|
/**
|
|
* |........................|
|
|
* | Conversations/Selection|
|
|
* | |
|
|
* |........................|
|
|
* | Available Options |
|
|
* |........................|
|
|
*/
|
|
|
|
enum AppStatus {
|
|
Initial,
|
|
GuestSelection,
|
|
TalkToGuest(Option<u32>),
|
|
}
|
|
|
|
pub struct App<'a> {
|
|
game: Game,
|
|
guest_list: Vec<tui::widgets::ListItem<'a>>,
|
|
guest_list_state: tui::widgets::ListState,
|
|
status: AppStatus,
|
|
conversation: Chat,
|
|
conversation_scroll: u16,
|
|
}
|
|
|
|
impl<'a> App<'a> {
|
|
pub fn new(state: Game) -> App<'a> {
|
|
App {
|
|
game: state,
|
|
guest_list: vec![],
|
|
guest_list_state: tui::widgets::ListState::default(),
|
|
status: AppStatus::Initial,
|
|
conversation: Chat::new(),
|
|
conversation_scroll: 0,
|
|
}
|
|
}
|
|
|
|
pub fn step(&mut self) -> bool {
|
|
match &self.status {
|
|
AppStatus::Initial => {
|
|
// determine guests
|
|
self.guest_list = self.game.state.guests().iter().map(|creature_id| {
|
|
let creature = self.game.state.get_creature(*creature_id).unwrap();
|
|
tui::widgets::ListItem::new(creature.name.clone())
|
|
}).collect();
|
|
self.guest_list_state = tui::widgets::ListState::default();
|
|
self.guest_list_state.select(Some(0));
|
|
self.status = AppStatus::GuestSelection;
|
|
true
|
|
},
|
|
AppStatus::GuestSelection => {
|
|
match read() {
|
|
Ok(Event::Key(event)) => {
|
|
match event.code {
|
|
KeyCode::Up => {
|
|
let selected = self.guest_list_state.selected().unwrap();
|
|
if selected > 0 {
|
|
self.guest_list_state.select(Some(selected - 1));
|
|
}
|
|
},
|
|
KeyCode::Down => {
|
|
let selected = self.guest_list_state.selected().unwrap();
|
|
if selected < self.guest_list.len() - 1 {
|
|
self.guest_list_state.select(Some(selected + 1));
|
|
}
|
|
},
|
|
KeyCode::Enter => {
|
|
if self.game.state.guests().len() > 0 {
|
|
match self.guest_list_state.selected() {
|
|
Some(selected) => {
|
|
let guest_id = &self.game.state.guests()[selected];
|
|
self.conversation = Chat::new();
|
|
self.greet_guest(Some(*guest_id));
|
|
self.status = AppStatus::TalkToGuest(Some(*guest_id));
|
|
},
|
|
None => {}
|
|
}
|
|
}
|
|
},
|
|
KeyCode::Char('.') => {
|
|
self.game.step();
|
|
self.status = AppStatus::Initial;
|
|
},
|
|
KeyCode::Esc => {
|
|
return false;
|
|
},
|
|
_ => {}
|
|
}
|
|
},
|
|
_ => {}
|
|
}
|
|
true
|
|
},
|
|
AppStatus::TalkToGuest(guest) => {
|
|
match read() {
|
|
Ok(Event::Key(event)) => {
|
|
match event.code {
|
|
KeyCode::Esc => {
|
|
self.status = AppStatus::GuestSelection;
|
|
},
|
|
KeyCode::Char('a') => {
|
|
self.ask_business(*guest);
|
|
},
|
|
KeyCode::Up => {
|
|
self.conversation_scroll += 1;
|
|
},
|
|
KeyCode::Down => {
|
|
if self.conversation_scroll > 0 {
|
|
self.conversation_scroll -= 1;
|
|
}
|
|
},
|
|
_ => {}
|
|
}
|
|
},
|
|
_ => {}
|
|
}
|
|
true
|
|
}
|
|
}
|
|
}
|
|
|
|
fn default_layout(&self) -> Layout {
|
|
Layout::default()
|
|
.direction(tui::layout::Direction::Vertical)
|
|
.constraints(
|
|
[
|
|
tui::layout::Constraint::Min(3),
|
|
tui::layout::Constraint::Length(2)
|
|
]
|
|
.as_ref(),
|
|
)
|
|
}
|
|
|
|
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>) {
|
|
match &self.status {
|
|
AppStatus::Initial => {
|
|
self.draw_initial(f);
|
|
},
|
|
AppStatus::GuestSelection => {
|
|
self.draw_guest_selection(f);
|
|
},
|
|
AppStatus::TalkToGuest(guest_id) => {
|
|
self.draw_talk_to_guest(f, *guest_id);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
pub fn draw_initial<B: Backend>(&mut self, _f: &mut Frame<B>) {}
|
|
|
|
pub fn draw_guest_selection<B: Backend>(&mut self, f: &mut Frame<B>) {
|
|
let chunks = self.default_layout().split(f.size());
|
|
|
|
let main_window = tui::widgets::List::new(self.guest_list.clone())
|
|
.block(tui::widgets::Block::default().title("Guests").borders(tui::widgets::Borders::ALL))
|
|
.style(tui::style::Style::default().fg(tui::style::Color::White))
|
|
.highlight_style(tui::style::Style::default().add_modifier(tui::style::Modifier::ITALIC))
|
|
.highlight_symbol(">>");
|
|
|
|
let mut binding = Controls::new();
|
|
let controls = binding
|
|
.add("↑↓".to_owned(), "select guest".to_owned())
|
|
.add("⏎".to_owned(), "talk to guest".to_owned())
|
|
.add(".".to_owned(), "pass one day".to_owned())
|
|
.add("Esc".to_owned(), "quit".to_owned())
|
|
.render();
|
|
|
|
f.render_stateful_widget(main_window, chunks[0], &mut self.guest_list_state);
|
|
f.render_widget(controls, chunks[1]);
|
|
}
|
|
fn draw_talk_to_guest<B: Backend>(&self, f: &mut Frame<B>, guest: Option<u32>) {
|
|
let chunks = self.default_layout().split(f.size());
|
|
|
|
let guest = self.game.state.get_creature(guest.unwrap()).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?\n\n"),
|
|
]));
|
|
full_text.push(tui::text::Spans::from(vec![
|
|
tui::text::Span::styled("(b) ", key_style),
|
|
tui::text::Span::raw("Let's trade?\n\n"),
|
|
]));
|
|
full_text.push(tui::text::Spans::from(vec![
|
|
tui::text::Span::styled("(c) ", key_style),
|
|
tui::text::Span::raw("Heard of anything interesting?\n\n"),
|
|
]));
|
|
let text = tui::text::Text::from(full_text);
|
|
let scroll: u16 = ((text.lines.len() as u16) )
|
|
.checked_sub(
|
|
(chunks[0].height as u16).checked_sub(2).unwrap_or(0) // 2 lines for the border
|
|
)
|
|
.unwrap_or(0)
|
|
.checked_sub(self.conversation_scroll)
|
|
.unwrap_or(0);
|
|
|
|
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();
|
|
|
|
f.render_widget(main_window, chunks[0]);
|
|
f.render_widget(controls, chunks[1]);
|
|
}
|
|
|
|
/**
|
|
* Conversation
|
|
*/
|
|
fn greet_guest(&mut self, guest_id: Option<EntityId>) {
|
|
let guest = self.game.state.get_creature(guest_id.unwrap()).unwrap();
|
|
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 + ", nice to meet you! "
|
|
+ "I'm a " + &guest.profession.to_string() + ".",
|
|
false
|
|
));
|
|
self.conversation_scroll = 0;
|
|
}
|
|
|
|
fn ask_business(&mut self, guest: Option<EntityId>) {
|
|
let guest = self.game.state.get_creature(guest.unwrap()).unwrap();
|
|
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
|
|
));
|
|
self.conversation_scroll = 0;
|
|
}
|
|
} |