use crate::style::{GameStyle, normal_block};
pub mod icons;
pub mod view_item;
use textwrap::{fill, termwidth};
pub use view_item::ViewItem;
mod render_action;
mod render_env;
mod render_health;
mod render_item;
mod render_npc;
mod render_player;
mod render_system;
mod render_trig;
#[derive(Debug, Clone)]
pub struct View {
pub width: usize,
pub mode: ViewMode,
pub items: Vec<ViewEntry>,
pub sequence: usize,
}
impl Default for View {
fn default() -> Self {
Self::new()
}
}
impl View {
pub fn new() -> Self {
Self {
width: termwidth(),
mode: ViewMode::Verbose,
items: Vec::new(),
sequence: 0,
}
}
pub fn push(&mut self, item: ViewItem) {
self.push_with_custom_priority(item, None);
}
pub fn push_with_priority(&mut self, item: ViewItem, priority: isize) {
self.push_with_custom_priority(item, Some(priority));
}
pub fn push_with_custom_priority(&mut self, item: ViewItem, priority: Option<isize>) {
self.items.push(ViewEntry {
section: item.section(),
priority: item.default_priority(),
custom_priority: priority,
view_item: item,
sequence: self.sequence,
});
self.sequence += 1;
}
pub fn flush(&mut self) {
self.width = termwidth();
let mut transitions = Vec::new();
let mut environment = Vec::new();
let mut direct = Vec::new();
let mut world = Vec::new();
let mut ambient = Vec::new();
let mut system = Vec::new();
for item in &self.items {
match item.view_item.section() {
Section::Transition => transitions.push(item.clone()),
Section::Environment => environment.push(item.clone()),
Section::DirectResult => direct.push(item.clone()),
Section::WorldResponse => world.push(item.clone()),
Section::Ambient => ambient.push(item.clone()),
Section::System => system.push(item.clone()),
}
}
if let Some(msg) = transitions.iter().find_map(|i| match &i.view_item {
ViewItem::TransitionMessage(msg) => Some(msg),
_ => None,
}) {
println!("\n{}", fill(msg.as_str(), normal_block()).transition_style());
}
if !environment.is_empty() {
println!("{:.>width$}\n", "scene".section_style(), width = self.width);
self.environment();
}
if !ambient.is_empty() {
println!("{:.>width$}\n", "surroundings".section_style(), width = self.width);
self.ambience();
}
if !direct.is_empty() {
println!("{:.>width$}\n", "results".section_style(), width = self.width);
self.direct_results();
}
if !world.is_empty() {
println!("{:.>width$}\n", "reactions".section_style(), width = self.width);
self.world_reaction();
}
if !system.is_empty() {
println!("{:.>width$}\n", "game".section_style(), width = self.width);
self.system();
}
self.items.clear();
}
fn environment(&mut self) {
render_env::room_description(self);
render_env::room_overlays(self);
render_env::room_item_list(self);
render_env::room_exit_list(self);
render_env::room_npc_list(self);
}
fn direct_results(&mut self) {
render_item::item_detail(self);
render_item::item_text(self);
render_npc::npc_detail(self);
render_player::inventory(self);
render_player::goals(self);
render_action::action_success(self);
render_action::action_failure(self);
render_action::errors(self);
}
fn world_reaction(&mut self) {
let world_entries = self.world_entries_sorted();
if world_entries.is_empty() {
return;
}
let mut current_priority: Option<isize> = None;
let mut bucket: Vec<&ViewEntry> = Vec::new();
for entry in world_entries {
let priority = entry.effective_priority();
if current_priority.is_some_and(|p| p != priority) {
Self::render_world_bucket(&bucket);
bucket.clear();
}
bucket.push(entry);
current_priority = Some(priority);
}
if !bucket.is_empty() {
Self::render_world_bucket(&bucket);
}
}
fn render_world_bucket(entries: &[&ViewEntry]) {
if entries.is_empty() {
return;
}
render_trig::triggered_event(entries);
render_npc::npc_events_sorted(entries);
render_health::character_harmed(entries);
render_health::status_change(entries);
render_health::character_healed(entries);
render_trig::points_awarded(entries);
render_health::character_death(entries);
}
fn world_entries_sorted(&self) -> Vec<&ViewEntry> {
let mut world_entries: Vec<&ViewEntry> = self
.items
.iter()
.filter(|entry| entry.section == Section::WorldResponse)
.collect();
world_entries.sort_by(|a, b| {
a.effective_priority()
.cmp(&b.effective_priority())
.then_with(|| a.sequence.cmp(&b.sequence))
});
world_entries
}
fn ambience(&mut self) {
render_trig::ambient_event(self);
}
fn system(&mut self) {
render_system::show_help(self);
render_system::saved_games(self);
render_system::load_or_save(self);
render_system::engine_message(self);
render_system::quit_summary(self);
}
pub fn reset(&mut self) {
self.items.clear();
}
pub fn set_mode(&mut self, mode: ViewMode) -> ViewMode {
std::mem::replace(&mut self.mode, mode)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Section {
Transition,
Environment,
DirectResult,
WorldResponse,
Ambient,
System,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ViewMode {
ClearVerbose,
Verbose,
Brief,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ViewEntry {
pub section: Section,
pub priority: isize,
pub custom_priority: Option<isize>,
pub view_item: ViewItem,
pub sequence: usize,
}
impl ViewEntry {
fn effective_priority(&self) -> isize {
self.custom_priority.unwrap_or(self.priority)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum StatusAction {
Apply,
Remove,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ContentLine {
pub item_name: String,
pub restricted: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct ExitLine {
pub direction: String,
pub destination: String,
pub exit_locked: bool,
pub dest_visited: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NpcLine {
pub name: String,
pub description: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn world_entries_sorted_respects_custom_priority() {
let mut view = View::new();
view.push(ViewItem::ActionSuccess("ignored".into()));
view.push(ViewItem::NpcSpeech {
speaker: "NPC".into(),
quote: "Hello".into(),
});
view.push_with_priority(ViewItem::TriggeredEvent("Radio hums".into()), -25);
view.push_with_priority(
ViewItem::StatusChange {
action: StatusAction::Apply,
status: "Poisoned".into(),
},
25,
);
let ordered: Vec<&str> = view
.world_entries_sorted()
.iter()
.map(|entry| match &entry.view_item {
ViewItem::TriggeredEvent(_) => "triggered",
ViewItem::NpcSpeech { .. } => "speech",
ViewItem::StatusChange { .. } => "status",
other => panic!("Unexpected ViewItem in results: {other:?}"),
})
.collect();
assert_eq!(ordered, vec!["triggered", "speech", "status"]);
}
#[test]
fn world_entries_sorted_excludes_other_sections() {
let mut view = View::new();
view.push(ViewItem::ActionSuccess("direct result".into()));
view.push(ViewItem::AmbientEvent("ambient".into()));
view.push(ViewItem::NpcSpeech {
speaker: "NPC".into(),
quote: "Priority".into(),
});
let entries = view.world_entries_sorted();
assert_eq!(entries.len(), 1);
assert!(matches!(entries[0].view_item, ViewItem::NpcSpeech { .. }));
}
}