debug items

This commit is contained in:
Niko Abeler 2023-01-06 21:19:02 +01:00
parent a479774b3e
commit b94de7d4b0
2 changed files with 85 additions and 22 deletions

View File

@ -1,3 +1,5 @@
use std::fmt;
use crate::entity::{Entity, Location};
#[derive(Clone)]
@ -24,9 +26,6 @@ pub struct Weapon {
pub struct Armor {
pub armor_class: u32,
}
pub struct ItemGenerator;
impl ItemGenerator {
@ -42,3 +41,12 @@ impl ItemGenerator {
}
}
}
impl fmt::Display for ItemType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ItemType::Weapon(weapon) => write!(f, "Weapon: {}d{}+{}", weapon.damage_dice, weapon.damage_sides, weapon.damage_base),
ItemType::Armor(armor) => write!(f, "Armor: {}", armor.armor_class),
}
}
}

View File

@ -24,9 +24,19 @@ enum AppStatus {
Debug,
}
enum DebugView {
Creatures,
Items,
}
struct DebugData<'a> {
list: Vec<tui::widgets::ListItem<'a>>,
list_state: tui::widgets::ListState,
item_list: Vec<tui::widgets::ListItem<'a>>,
item_list_state: tui::widgets::ListState,
selected: DebugView,
}
pub struct App<'a> {
@ -42,7 +52,7 @@ pub struct App<'a> {
conversation_scroll: u16,
// Debug
dabug_data: Option<DebugData<'a>>,
debug_data: Option<DebugData<'a>>,
}
impl<'a> App<'a> {
@ -54,7 +64,7 @@ impl<'a> App<'a> {
status: AppStatus::Initial,
conversation: Chat::new(),
conversation_scroll: 0,
dabug_data: None,
debug_data: None,
}
}
@ -150,17 +160,38 @@ impl<'a> App<'a> {
self.status = AppStatus::GuestSelection;
},
KeyCode::Up => {
let selected = self.dabug_data.as_mut().unwrap().list_state.selected().unwrap();
let ref mut state = match self.debug_data.as_ref().unwrap().selected {
DebugView::Creatures => &mut self.debug_data.as_mut().unwrap().list_state,
DebugView::Items => &mut self.debug_data.as_mut().unwrap().item_list_state,
};
let selected = state.selected().unwrap();
if selected > 0 {
self.dabug_data.as_mut().unwrap().list_state.select(Some(selected - 1));
state.select(Some(selected - 1));
}
},
KeyCode::Down => {
let selected = self.dabug_data.as_mut().unwrap().list_state.selected().unwrap();
if selected < self.dabug_data.as_ref().unwrap().list.len() - 1 {
self.dabug_data.as_mut().unwrap().list_state.select(Some(selected + 1));
let mut max = 0;
let ref mut state = match self.debug_data.as_ref().unwrap().selected {
DebugView::Creatures => {
max = self.debug_data.as_ref().unwrap().list.len() - 1;
&mut self.debug_data.as_mut().unwrap().list_state
},
DebugView::Items => {
max = self.debug_data.as_ref().unwrap().item_list.len() - 1;
&mut self.debug_data.as_mut().unwrap().item_list_state
},
};
let selected = state.selected().unwrap();
if selected < max {
state.select(Some(selected + 1));
}
},
KeyCode::Char('i') => {
self.debug_data.as_mut().unwrap().selected = DebugView::Items;
},
KeyCode::Char('c') => {
self.debug_data.as_mut().unwrap().selected = DebugView::Creatures;
},
_ => {}
}
},
@ -173,6 +204,7 @@ impl<'a> App<'a> {
fn open_debug(&mut self) {
let mut list = vec![];
let mut item_list = vec![];
for (id, creature) in self.game.state.creatures.iter() {
list.push(tui::widgets::ListItem::new(format!("{}: {} ({}) at {}",
id, creature.name,
@ -180,11 +212,23 @@ impl<'a> App<'a> {
creature.entity.loc,
)));
}
self.dabug_data = Some(DebugData {
for (id, item) in self.game.state.items.iter() {
item_list.push(tui::widgets::ListItem::new(format!("{}: {} ({}) at {}",
id, item.name,
item.item_type,
item.entity.loc,
)));
}
self.debug_data = Some(DebugData {
list,
list_state: tui::widgets::ListState::default(),
item_list,
item_list_state: tui::widgets::ListState::default(),
selected: DebugView::Creatures
});
self.dabug_data.as_mut().unwrap().list_state.select(Some(0));
self.debug_data.as_mut().unwrap().list_state.select(Some(0));
self.debug_data.as_mut().unwrap().item_list_state.select(Some(0));
self.status = AppStatus::Debug;
}
@ -291,22 +335,33 @@ impl<'a> App<'a> {
fn draw_debug<B: Backend>(&mut self, f: &mut Frame<B>) {
let chunks = self.default_layout().split(f.size());
let data = self.dabug_data.as_mut().unwrap();
let main_window = tui::widgets::List::new(data.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 data = self.debug_data.as_mut().unwrap();
match data.selected {
DebugView::Creatures => {
let main_window = tui::widgets::List::new(data.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(">>");
f.render_stateful_widget(main_window, chunks[0], &mut data.list_state);
},
DebugView::Items => {
let main_window = tui::widgets::List::new(data.item_list.clone())
.block(tui::widgets::Block::default().title("Items").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(">>");
f.render_stateful_widget(main_window, chunks[0], &mut data.item_list_state);
}
}
let mut binding = Controls::new();
let controls = binding
.add("↑↓".to_owned(), "select guest".to_owned())
.add("c".to_owned(), "Creatures".to_owned())
.add("i".to_owned(), "Items".to_owned())
.add("Esc".to_owned(), "back".to_owned())
.render();
f.render_stateful_widget(main_window, chunks[0], &mut data.list_state);
f.render_widget(controls, chunks[1]);
}