status bar

This commit is contained in:
Niko Abeler 2023-01-07 14:42:42 +01:00
parent 427595edb1
commit b9050db85c
3 changed files with 79 additions and 24 deletions

View File

@ -27,6 +27,7 @@ pub struct Creature {
pub agenda: Agenda,
pub weapon: Option<EntityId>,
pub armor: Option<EntityId>,
pub coins: u32,
}
pub struct CreatureGenerator;
@ -53,6 +54,7 @@ impl Creature {
agenda: Agenda::Idle(0),
weapon: None,
armor: None,
coins: 0,
}
}

View File

@ -37,6 +37,23 @@ pub struct Armor {
}
pub struct ItemGenerator;
impl Item {
pub fn value(&self) -> u32 {
match &self.item_type {
ItemType::Weapon(weapon) => self.weapon_value(weapon),
ItemType::Armor(armor) => self.armor_value(armor),
}
}
fn weapon_value(&self, weapon: &Weapon) -> u32 {
weapon.damage_base + weapon.damage_dice * weapon.damage_sides
}
fn armor_value(&self, armor: &Armor) -> u32 {
armor.armor_class * 10
}
}
impl ItemGenerator {
pub fn generate_item() -> Item {
Item {

View File

@ -58,6 +58,33 @@ pub struct App<'a> {
debug_data: Option<DebugData<'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 {
@ -266,17 +293,9 @@ impl<'a> App<'a> {
self.status = AppStatus::Debug;
}
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(),
)
}
// fn default_layout(&self) -> Layout {
// DefaultLayout::default()
// }
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>) {
match &self.status {
@ -296,10 +315,24 @@ impl<'a> App<'a> {
}
fn draw_status<B: Backend>(&self, f: &mut Frame<B>, rect: tui::layout::Rect) {
let spans = tui::text::Spans::from(vec![
tui::text::Span::raw("Date: "),
tui::text::Span::raw(format!("{}", self.game.state.time)),
]);
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>) {}
pub fn draw_guest_selection<B: Backend>(&mut self, f: &mut Frame<B>) {
let chunks = self.default_layout().split(f.size());
let chunks = DefaultLayout::default(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))
@ -315,12 +348,13 @@ impl<'a> App<'a> {
.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]);
self.draw_status(f, chunks.status);
f.render_stateful_widget(main_window, chunks.main, &mut self.guest_list_state);
f.render_widget(controls, chunks.controls);
}
fn draw_talk_to_guest<B: Backend>(&self, f: &mut Frame<B>, guest: Option<EntityId>) {
let chunks = self.default_layout().split(f.size());
let chunks = DefaultLayout::default(f.size());
let guest = self.game.state.get_creature(guest.unwrap()).unwrap();
@ -345,7 +379,7 @@ impl<'a> App<'a> {
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
(chunks.main.height as u16).checked_sub(2).unwrap_or(0) // 2 lines for the border
)
.unwrap_or(0)
.checked_sub(self.conversation_scroll)
@ -362,12 +396,13 @@ impl<'a> App<'a> {
.add("Esc".to_owned(), "Back".to_owned())
.render();
f.render_widget(main_window, chunks[0]);
f.render_widget(controls, chunks[1]);
self.draw_status(f, chunks.status);
f.render_widget(main_window, chunks.main);
f.render_widget(controls, chunks.controls);
}
fn draw_debug<B: Backend>(&mut self, f: &mut Frame<B>) {
let chunks = self.default_layout().split(f.size());
let chunks = DefaultLayout::default(f.size());
let data = self.debug_data.as_mut().unwrap();
@ -378,7 +413,7 @@ impl<'a> App<'a> {
.style(tui::style::Style::default().fg(tui::style::Color::White))
.highlight_style(tui::style::Style::default().add_modifier(tui::style::Modifier::ITALIC))
.highlight_symbol(">>");
f.render_stateful_widget(main_window, chunks[0], &mut data.list_state);
f.render_stateful_widget(main_window, chunks.main, &mut data.list_state);
},
DebugView::Items => {
let main_window = tui::widgets::List::new(data.item_list.clone())
@ -386,7 +421,7 @@ impl<'a> App<'a> {
.style(tui::style::Style::default().fg(tui::style::Color::White))
.highlight_style(tui::style::Style::default().add_modifier(tui::style::Modifier::ITALIC))
.highlight_symbol(">>");
f.render_stateful_widget(main_window, chunks[0], &mut data.item_list_state);
f.render_stateful_widget(main_window, chunks.main, &mut data.item_list_state);
},
DebugView::Map => {
let mut rows = Vec::new();
@ -414,10 +449,11 @@ impl<'a> App<'a> {
.style(tui::style::Style::default().fg(tui::style::Color::White))
.scroll(data.map_scroll);
f.render_widget(main_window, chunks[0]);
// f.render_stateful_widget(main_window, chunks[0], &mut data.map_list_state);
f.render_widget(main_window, chunks.main);
},
}
self.draw_status(f, chunks.status);
let mut binding = Controls::new();
let controls = binding
.add("↑↓".to_owned(), "select guest".to_owned())
@ -426,7 +462,7 @@ impl<'a> App<'a> {
.add("m".to_owned(), "Map".to_owned())
.add("Esc".to_owned(), "back".to_owned())
.render();
f.render_widget(controls, chunks[1]);
f.render_widget(controls, chunks.controls);
}
/**