debug map

This commit is contained in:
Niko Abeler 2023-01-06 22:11:17 +01:00
parent b94de7d4b0
commit d655da0738
1 changed files with 88 additions and 24 deletions

View File

@ -4,7 +4,7 @@ mod controls;
use crossterm::event::{read, Event, KeyCode};
use tui::{backend::Backend, Frame, layout::Layout};
use crate::{game::Game, entity::EntityId};
use crate::{game::Game, entity::EntityId, world::Terrain};
use self::{chat::{Chat, ChatLine}, controls::Controls};
@ -24,9 +24,11 @@ enum AppStatus {
Debug,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum DebugView {
Creatures,
Items,
Map,
}
struct DebugData<'a> {
@ -37,6 +39,7 @@ struct DebugData<'a> {
item_list_state: tui::widgets::ListState,
selected: DebugView,
map_scroll: (u16, u16),
}
pub struct App<'a> {
@ -160,30 +163,57 @@ impl<'a> App<'a> {
self.status = AppStatus::GuestSelection;
},
KeyCode::Up => {
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 {
state.select(Some(selected - 1));
if self.debug_data.as_ref().unwrap().selected == DebugView::Map {
let (x, y) = self.debug_data.as_ref().unwrap().map_scroll;
self.debug_data.as_mut().unwrap().map_scroll = (x.saturating_sub(1), y);
} else {
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,
_ => &mut self.debug_data.as_mut().unwrap().list_state,
};
let selected = state.selected().unwrap();
if selected > 0 {
state.select(Some(selected - 1));
}
}
},
KeyCode::Down => {
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));
if self.debug_data.as_ref().unwrap().selected == DebugView::Map {
let (x, y) = self.debug_data.as_ref().unwrap().map_scroll;
self.debug_data.as_mut().unwrap().map_scroll = (x + 1, y);
} else {
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
},
_ => {
max = self.debug_data.as_ref().unwrap().list.len() - 1;
&mut self.debug_data.as_mut().unwrap().list_state
},
};
let selected = state.selected().unwrap();
if selected < max {
state.select(Some(selected + 1));
}
}
},
KeyCode::Left => {
if self.debug_data.as_ref().unwrap().selected == DebugView::Map {
let (x, y) = self.debug_data.as_ref().unwrap().map_scroll;
self.debug_data.as_mut().unwrap().map_scroll = (x, y.saturating_sub(1));
}
},
KeyCode::Right => {
if self.debug_data.as_ref().unwrap().selected == DebugView::Map {
let (x, y) = self.debug_data.as_ref().unwrap().map_scroll;
self.debug_data.as_mut().unwrap().map_scroll = (x, y + 1);
}
},
KeyCode::Char('i') => {
@ -192,6 +222,9 @@ impl<'a> App<'a> {
KeyCode::Char('c') => {
self.debug_data.as_mut().unwrap().selected = DebugView::Creatures;
},
KeyCode::Char('m') => {
self.debug_data.as_mut().unwrap().selected = DebugView::Map;
},
_ => {}
}
},
@ -225,7 +258,8 @@ impl<'a> App<'a> {
list_state: tui::widgets::ListState::default(),
item_list,
item_list_state: tui::widgets::ListState::default(),
selected: DebugView::Creatures
selected: DebugView::Creatures,
map_scroll: (0, 0),
});
self.debug_data.as_mut().unwrap().list_state.select(Some(0));
self.debug_data.as_mut().unwrap().item_list_state.select(Some(0));
@ -353,13 +387,43 @@ impl<'a> App<'a> {
.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);
}
},
DebugView::Map => {
let mut rows = Vec::new();
for row in self.game.state.world.map.iter() {
rows.push(tui::text::Spans::from(
row.iter().map(|tile| {
match tile.terrain {
Terrain::Void => tui::text::Span::styled(" ", tui::style::Style::default().bg(tui::style::Color::Rgb(0, 0, 0)).fg(tui::style::Color::Rgb(0, 0, 0))),
Terrain::DeepOcean => tui::text::Span::styled("~", tui::style::Style::default().bg(tui::style::Color::Rgb(0, 0, 128)).fg(tui::style::Color::Rgb(32, 32, 128))),
Terrain::Ocean => tui::text::Span::styled("~", tui::style::Style::default().bg(tui::style::Color::Rgb(32, 32, 128)).fg(tui::style::Color::Rgb(128, 128, 128))),
Terrain::Beach => tui::text::Span::styled("-", tui::style::Style::default().bg(tui::style::Color::Rgb(200, 200, 10)).fg(tui::style::Color::Rgb(100, 100, 10))),
Terrain::Flats => tui::text::Span::styled("-", tui::style::Style::default().bg(tui::style::Color::Rgb(30, 150, 30)).fg(tui::style::Color::Rgb(30, 200, 30))),
Terrain::Hills => tui::text::Span::styled("~", tui::style::Style::default().bg(tui::style::Color::Rgb(120, 150, 30)).fg(tui::style::Color::Rgb(120, 150, 30))),
Terrain::Mountains => tui::text::Span::styled("A", tui::style::Style::default().bg(tui::style::Color::Rgb(120,120,120)).fg(tui::style::Color::Rgb(120,120,120))),
Terrain::HighMountains => tui::text::Span::styled("A", tui::style::Style::default().bg(tui::style::Color::Rgb(200,200,200)).fg(tui::style::Color::Rgb(200,200,200))),
}
}).collect::<Vec<tui::text::Span>>()
));
}
let main_window = tui::widgets::Paragraph::new(
tui::text::Text::from(rows)
)
.block(tui::widgets::Block::default().title("Map").borders(tui::widgets::Borders::ALL))
.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);
},
}
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("m".to_owned(), "Map".to_owned())
.add("Esc".to_owned(), "back".to_owned())
.render();
f.render_widget(controls, chunks[1]);