319 lines
11 KiB
Rust
319 lines
11 KiB
Rust
mod chat;
|
|
mod controls;
|
|
mod debug_view;
|
|
mod guest_selection;
|
|
mod status_line;
|
|
|
|
use crossterm::event::{read, Event, KeyCode};
|
|
use tui::{backend::Backend, Frame, layout::Layout};
|
|
|
|
use crate::{game::Game, entity::{EntityId, EntityType}, world::Terrain};
|
|
|
|
use self::{chat::{Chat, ChatLine}, controls::Controls, debug_view::DebugView, guest_selection::GuestSelectionView};
|
|
|
|
/**
|
|
* |........................|
|
|
* | Conversations/Selection|
|
|
* | |
|
|
* |........................|
|
|
* | Available Options |
|
|
* |........................|
|
|
*/
|
|
|
|
pub enum AppStatus {
|
|
Initial,
|
|
GuestSelection,
|
|
TalkToGuest(Option<EntityId>),
|
|
BuyFromGuest(EntityId),
|
|
Debug,
|
|
}
|
|
|
|
pub struct App<'a> {
|
|
game: Game,
|
|
status: AppStatus,
|
|
|
|
// Guest Selection
|
|
guest_selection: Option<GuestSelectionView<'a>>,
|
|
|
|
// Conversation
|
|
conversation: Chat,
|
|
conversation_scroll: u16,
|
|
|
|
// Debug
|
|
debug_view: Option<DebugView<'a>>,
|
|
}
|
|
|
|
pub struct DefaultLayout {
|
|
pub status: tui::layout::Rect,
|
|
pub main: tui::layout::Rect,
|
|
pub controls: tui::layout::Rect,
|
|
}
|
|
|
|
impl DefaultLayout {
|
|
pub fn default(rect: tui::layout::Rect) -> DefaultLayout {
|
|
let chunks = Layout::default()
|
|
.direction(tui::layout::Direction::Vertical)
|
|
.constraints(
|
|
[
|
|
tui::layout::Constraint::Length(1),
|
|
tui::layout::Constraint::Min(3),
|
|
tui::layout::Constraint::Length(2)
|
|
]
|
|
.as_ref(),
|
|
).split(rect);
|
|
|
|
DefaultLayout {
|
|
status: chunks[0],
|
|
main: chunks[1],
|
|
controls: chunks[2],
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> App<'a> {
|
|
pub fn new(state: Game) -> App<'a> {
|
|
App {
|
|
game: state,
|
|
guest_selection: None,
|
|
status: AppStatus::Initial,
|
|
conversation: Chat::new(),
|
|
conversation_scroll: 0,
|
|
debug_view: None,
|
|
}
|
|
}
|
|
|
|
pub fn step(&mut self) -> bool {
|
|
match &self.status {
|
|
AppStatus::Initial => {
|
|
self.guest_selection = Some(GuestSelectionView::new(&self.game));
|
|
self.status = AppStatus::GuestSelection;
|
|
true
|
|
},
|
|
AppStatus::GuestSelection => {
|
|
let ret = self.guest_selection.as_mut().unwrap().control(&mut self.game);
|
|
if let Some(next) = ret.1 {
|
|
self.debug_view = None;
|
|
self.status = next;
|
|
}
|
|
ret.0
|
|
},
|
|
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::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;
|
|
}
|
|
},
|
|
_ => {}
|
|
}
|
|
},
|
|
_ => {}
|
|
}
|
|
true
|
|
},
|
|
AppStatus::BuyFromGuest(guest_id) => {
|
|
match read() {
|
|
Ok(Event::Key(event)) => {
|
|
match event.code {
|
|
KeyCode::Esc => {
|
|
self.status = AppStatus::TalkToGuest(Some(*guest_id));
|
|
},
|
|
_ => {}
|
|
}
|
|
},
|
|
_ => {}
|
|
}
|
|
true
|
|
},
|
|
AppStatus::Debug => {
|
|
let ret = self.debug_view.as_mut().unwrap().control(&mut self.game);
|
|
if let Some(next) = ret.1 {
|
|
self.debug_view = None;
|
|
self.status = next;
|
|
}
|
|
ret.0
|
|
}
|
|
}
|
|
}
|
|
|
|
fn open_debug(&mut self) {
|
|
self.debug_view = Some(DebugView::new(&self.game));
|
|
self.status = AppStatus::Debug;
|
|
}
|
|
|
|
// fn default_layout(&self) -> Layout {
|
|
// DefaultLayout::default()
|
|
// }
|
|
|
|
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>) {
|
|
match &self.status {
|
|
AppStatus::Initial => {
|
|
self.draw_initial(f);
|
|
},
|
|
AppStatus::GuestSelection => {
|
|
self.guest_selection.as_mut().unwrap().draw(f, &self.game);
|
|
},
|
|
AppStatus::TalkToGuest(guest_id) => {
|
|
self.draw_talk_to_guest(f, *guest_id);
|
|
},
|
|
AppStatus::BuyFromGuest(guest_id) => {
|
|
self.draw_buy_from_guest(f, *guest_id);
|
|
},
|
|
AppStatus::Debug => {
|
|
self.debug_view.as_mut().unwrap().draw(f, &self.game);
|
|
},
|
|
}
|
|
|
|
}
|
|
|
|
fn draw_status<B: Backend>(&self, f: &mut Frame<B>, rect: tui::layout::Rect) {
|
|
|
|
let tavern = self.game.state.world.get_site(
|
|
self.game.state.tavern.unwrap()
|
|
).unwrap();
|
|
|
|
let spans = tui::text::Spans::from(vec![
|
|
tui::text::Span::raw("Date: "),
|
|
tui::text::Span::raw(format!("{}", self.game.state.time)),
|
|
tui::text::Span::raw(" "),
|
|
tui::text::Span::raw("Funds: "),
|
|
tui::text::Span::raw(format!("{} gold coins", tavern.coins)),
|
|
]);
|
|
|
|
let status_text = tui::widgets::Paragraph::new(spans)
|
|
.block(tui::widgets::Block::default().borders(tui::widgets::Borders::LEFT | tui::widgets::Borders::RIGHT))
|
|
.style(tui::style::Style::default().fg(tui::style::Color::White));
|
|
|
|
f.render_widget(status_text, rect);
|
|
}
|
|
|
|
pub fn draw_initial<B: Backend>(&mut self, _f: &mut Frame<B>) {}
|
|
|
|
fn draw_talk_to_guest<B: Backend>(&self, f: &mut Frame<B>, guest: Option<EntityId>) {
|
|
let chunks = DefaultLayout::default(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?"),
|
|
]));
|
|
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();
|
|
|
|
self.draw_status(f, chunks.status);
|
|
f.render_widget(main_window, chunks.main);
|
|
f.render_widget(controls, chunks.controls);
|
|
}
|
|
|
|
fn draw_buy_from_guest<B: Backend>(&self, f: &mut Frame<B>, guest_id: EntityId) {
|
|
let chunks = DefaultLayout::default(f.size());
|
|
|
|
let guest = self.game.state.get_creature(guest_id).unwrap();
|
|
|
|
let inventory = self.game.state.get_inventory(guest_id);
|
|
let mut list_items = vec![];
|
|
for item in inventory {
|
|
let item = self.game.state.get_item(item).unwrap();
|
|
list_items.push(tui::widgets::ListItem::new(format!("{} ({} gold)", item.name, item.value())));
|
|
}
|
|
|
|
let main_window = tui::widgets::List::new(list_items)
|
|
.block(tui::widgets::Block::default().title(guest.name.clone()).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("a-z".to_owned(), "Select".to_owned())
|
|
.add("Esc".to_owned(), "Back".to_owned())
|
|
.render();
|
|
|
|
self.draw_status(f, chunks.status);
|
|
f.render_widget(main_window, chunks.main);
|
|
f.render_widget(controls, chunks.controls);
|
|
}
|
|
|
|
/**
|
|
* 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(
|
|
(EntityType::Creature, 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(
|
|
(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(& self.game.state),
|
|
false
|
|
));
|
|
self.conversation_scroll = 0;
|
|
}
|
|
|
|
} |