use ::rltk::{Point, Rltk, VirtualKeyCode, RGB}; use ::specs::prelude::*; use crate::components::{ CombatStats, HungerClock, HungerState, InBackpack, Name, Player, Position, Viewshed, }; use crate::game_log::GameLog; use crate::rex_assets::RexAssets; use crate::{camera, Equipped, Hidden, Map, RunState, State}; pub fn draw_ui(ecs: &World, ctx: &mut Rltk) { ctx.draw_box( 0, 43, 79, 6, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), ); let combat_stats = ecs.read_storage::(); let players = ecs.read_storage::(); let hunger = ecs.read_storage::(); // Display player health for (_player, stats, hc) in (&players, &combat_stats, &hunger).join() { let health = format!(" HP: {} / {} ", stats.hp, stats.max_hp); ctx.print_color( 12, 43, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), &health, ); ctx.draw_bar_horizontal( 29, 43, 51, stats.hp, stats.max_hp, RGB::named(rltk::RED), RGB::named(rltk::BLACK), ); match hc.state { HungerState::WellFed => ctx.print_color( 71, 42, RGB::named(rltk::GREEN), RGB::named(rltk::BLACK), "Well Fed", ), HungerState::Normal => {} HungerState::Hungry => ctx.print_color( 71, 42, RGB::named(rltk::ORANGE), RGB::named(rltk::BLACK), "Hungry", ), HungerState::Starving => ctx.print_color( 71, 42, RGB::named(rltk::RED), RGB::named(rltk::BLACK), "Starving", ), } } let map = ecs.fetch::(); let depth = format!("Depth: {}", map.depth); ctx.print_color( 2, 43, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), &depth, ); // Display logs let log = ecs.fetch::(); let mut y = 44; for s in log.entries.iter().rev() { if y < 49 { ctx.print(2, y, s); } y += 1; } // Mouse cursor let mouse_pos = ctx.mouse_pos(); ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::MAGENTA)); draw_tooltips(ecs, ctx); } fn draw_tooltips(ecs: &World, ctx: &mut Rltk) { let (min_x, _max_x, min_y, _max_y) = camera::get_screen_bounds(ecs, ctx); let map = ecs.fetch::(); let names = ecs.read_storage::(); let positions = ecs.read_storage::(); let hidden = ecs.read_storage::(); let mouse_pos = ctx.mouse_pos(); let mut mouse_map_pos = mouse_pos; mouse_map_pos.0 += min_x; mouse_map_pos.1 += min_y; if mouse_map_pos.0 >= map.width - 1 || mouse_map_pos.1 >= map.height - 1 || mouse_map_pos.0 < 1 || mouse_map_pos.1 < 1 { return; } if !map.visible_tiles[map.xy_idx(mouse_map_pos.0, mouse_map_pos.1)] { return; } let mut tooltip: Vec = Vec::new(); for (name, position, _hidden) in (&names, &positions, !&hidden).join() { if position.x == mouse_map_pos.0 && position.y == mouse_map_pos.1 { tooltip.push(name.name.to_string()); } } if !tooltip.is_empty() { let mut width: i32 = 0; for s in tooltip.iter() { if width < s.len() as i32 { width = s.len() as i32; } } width += 3; if mouse_pos.0 > 40 { let arrow_pos = Point::new(mouse_pos.0 - 2, mouse_pos.1); let left_x = mouse_pos.0 - width; let mut y = mouse_pos.1; for s in tooltip.iter() { ctx.print_color( left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), s, ); let padding = (width - s.len() as i32) - 1; for i in 0..padding { ctx.print_color( arrow_pos.x - i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string(), ); } y += 1; } ctx.print_color( arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"->".to_string(), ); } else { let arrow_pos = Point::new(mouse_pos.0 + 1, mouse_pos.1); let left_x = mouse_pos.0 + 3; let mut y = mouse_pos.1; for s in tooltip.iter() { ctx.print_color( left_x + 1, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), s, ); let padding = (width - s.len() as i32) - 1; for i in 0..padding { ctx.print_color( arrow_pos.x + 1 + i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string(), ); } y += 1; } ctx.print_color( arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"<-".to_string(), ); } } } #[derive(PartialEq, Copy, Clone)] pub enum ItemMenuResult { Cancel, NoResponse, Selected, } pub fn show_inventory(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option) { let player_entity = gs.ecs.fetch::(); let names = gs.ecs.read_storage::(); let backpack = gs.ecs.read_storage::(); let entities = gs.ecs.entities(); let inventory = (&backpack, &names) .join() .filter(|item| item.0.owner == *player_entity); let count = inventory.count(); let mut y = (25 - (count / 2)) as i32; ctx.draw_box( 15, y - 2, 31, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), ); ctx.print_color( 18, y - 2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Inventory", ); ctx.print_color( 18, y + count as i32 + 1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel", ); let mut equippable: Vec = Vec::new(); let mut j = 0; #[allow(clippy::explicit_counter_loop)] for (entity, _pack, name) in (&entities, &backpack, &names) .join() .filter(|item| item.1.owner == *player_entity) { ctx.set( 17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('), ); ctx.set( 18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97 + j as rltk::FontCharType, ); ctx.set( 19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'), ); ctx.print(21, y, &name.name.to_string()); equippable.push(entity); y += 1; j += 1; } match ctx.key { None => (ItemMenuResult::NoResponse, None), Some(key) => match key { VirtualKeyCode::Escape => (ItemMenuResult::Cancel, None), _ => { let selection = rltk::letter_to_option(key); if selection > -1 && selection < count as i32 { return ( ItemMenuResult::Selected, Some(equippable[selection as usize]), ); } (ItemMenuResult::NoResponse, None) } }, } } pub fn drop_item_menu(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option) { let player_entity = gs.ecs.fetch::(); let names = gs.ecs.read_storage::(); let backpack = gs.ecs.read_storage::(); let entities = gs.ecs.entities(); let inventory = (&backpack, &names) .join() .filter(|item| item.0.owner == *player_entity); let count = inventory.count(); let mut y = (25 - (count / 2)) as i32; ctx.draw_box( 15, y - 2, 31, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), ); ctx.print_color( 18, y - 2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Drop Which Item?", ); ctx.print_color( 18, y + count as i32 + 1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel", ); let mut equippable: Vec = Vec::new(); let mut j = 0; #[allow(clippy::explicit_counter_loop)] for (entity, _pack, name) in (&entities, &backpack, &names) .join() .filter(|item| item.1.owner == *player_entity) { ctx.set( 17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('), ); ctx.set( 18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97 + j as rltk::FontCharType, ); ctx.set( 19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'), ); ctx.print(21, y, &name.name.to_string()); equippable.push(entity); y += 1; j += 1; } match ctx.key { None => (ItemMenuResult::NoResponse, None), Some(key) => match key { VirtualKeyCode::Escape => (ItemMenuResult::Cancel, None), _ => { let selection = rltk::letter_to_option(key); if selection > -1 && selection < count as i32 { return ( ItemMenuResult::Selected, Some(equippable[selection as usize]), ); } (ItemMenuResult::NoResponse, None) } }, } } pub fn remove_item_menu(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option) { let player_entity = gs.ecs.fetch::(); let names = gs.ecs.read_storage::(); let backpack = gs.ecs.read_storage::(); let entities = gs.ecs.entities(); let inventory = (&backpack, &names) .join() .filter(|item| item.0.owner == *player_entity); let count = inventory.count(); let mut y = (25 - (count / 2)) as i32; ctx.draw_box( 15, y - 2, 31, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), ); ctx.print_color( 18, y - 2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Remove Which Item?", ); ctx.print_color( 18, y + count as i32 + 1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel", ); let mut equippable: Vec = Vec::new(); let mut j = 0; #[allow(clippy::explicit_counter_loop)] for (entity, _pack, name) in (&entities, &backpack, &names) .join() .filter(|item| item.1.owner == *player_entity) { ctx.set( 17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('), ); ctx.set( 18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97 + j as rltk::FontCharType, ); ctx.set( 19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'), ); ctx.print(21, y, &name.name.to_string()); equippable.push(entity); y += 1; j += 1; } match ctx.key { None => (ItemMenuResult::NoResponse, None), Some(key) => match key { VirtualKeyCode::Escape => (ItemMenuResult::Cancel, None), _ => { let selection = rltk::letter_to_option(key); if selection > -1 && selection < count as i32 { return ( ItemMenuResult::Selected, Some(equippable[selection as usize]), ); } (ItemMenuResult::NoResponse, None) } }, } } pub fn ranged_target( gs: &mut State, ctx: &mut Rltk, range: i32, ) -> (ItemMenuResult, Option) { let (min_x, max_x, min_y, max_y) = camera::get_screen_bounds(&gs.ecs, ctx); let player_entity = gs.ecs.fetch::(); let player_pos = gs.ecs.fetch::(); let viewsheds = gs.ecs.read_storage::(); ctx.print_color( 5, 0, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Select Target:", ); // Highlight available target cells let mut available_cells = Vec::new(); if let Some(visible) = viewsheds.get(*player_entity) { for idx in visible.visible_tiles.iter() { let distance = rltk::DistanceAlg::Pythagoras.distance2d(*player_pos, *idx); if distance <= range as f32 { let screen_x = idx.x - min_x; let screen_y = idx.y - min_y; if screen_x > 1 && screen_x < (max_x - min_x) - 1 && screen_y > 1 && screen_y < (max_y - min_y) - 1 { ctx.set_bg(screen_x, screen_y, RGB::named(rltk::BLUE)); available_cells.push(idx); } } } } else { return (ItemMenuResult::Cancel, None); } // Draw mouse cursor let mouse_pos = ctx.mouse_pos(); let mut mouse_map_pos = mouse_pos; mouse_map_pos.0 += min_x; mouse_map_pos.1 += min_y; let mut valid_target = false; for idx in available_cells.iter() { if idx.x == mouse_map_pos.0 && idx.y == mouse_map_pos.1 { valid_target = true; } } if valid_target { ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::CYAN)); if ctx.left_click { return ( ItemMenuResult::Selected, Some(Point::new(mouse_map_pos.0, mouse_map_pos.1)), ); } } else { ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::RED)); if ctx.left_click { return (ItemMenuResult::Cancel, None); } } (ItemMenuResult::NoResponse, None) } #[derive(PartialEq, Copy, Clone)] pub enum MainMenuSelection { NewGame, LoadGame, Quit, } #[derive(PartialEq, Copy, Clone)] pub enum MainMenuResult { NoSelection { selected: MainMenuSelection }, Selected { selected: MainMenuSelection }, } pub fn main_menu(gs: &mut State, ctx: &mut Rltk) -> MainMenuResult { let save_exists = crate::saveload_system::does_save_exist(); let runstate = gs.ecs.fetch::(); let assets = gs.ecs.fetch::(); ctx.render_xp_sprite(&assets.menu, 0, 0); ctx.draw_box_double( 24, 18, 31, 10, RGB::named(rltk::WHEAT), RGB::named(rltk::BLACK), ); ctx.print_color_centered( 20, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Rust Roguelike Tutorial", ); ctx.print_color_centered( 21, RGB::named(rltk::CYAN), RGB::named(rltk::BLACK), "by Herbert Wolverson", ); ctx.print_color_centered( 22, RGB::named(rltk::GRAY), RGB::named(rltk::BLACK), "Use Up/Down Arrows and Enter", ); let mut y = 24; if let RunState::MainMenu { menu_selection: selection, } = *runstate { if selection == MainMenuSelection::NewGame { ctx.print_color_centered( y, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Begin New Game", ); } else { ctx.print_color_centered( y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Begin New Game", ); } y += 1; if save_exists { if selection == MainMenuSelection::LoadGame { ctx.print_color_centered( y, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Load Game", ); } else { ctx.print_color_centered( y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Load Game", ); } y += 1; } if selection == MainMenuSelection::Quit { ctx.print_color_centered( y, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Quit", ); } else { ctx.print_color_centered(y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Quit"); } return match ctx.key { None => MainMenuResult::NoSelection { selected: selection, }, Some(key) => match key { VirtualKeyCode::Escape => MainMenuResult::NoSelection { selected: MainMenuSelection::Quit, }, VirtualKeyCode::Up => MainMenuResult::NoSelection { selected: match selection { MainMenuSelection::NewGame => MainMenuSelection::Quit, MainMenuSelection::LoadGame => MainMenuSelection::NewGame, MainMenuSelection::Quit => { if save_exists { MainMenuSelection::LoadGame } else { MainMenuSelection::NewGame } } }, }, VirtualKeyCode::Down => MainMenuResult::NoSelection { selected: match selection { MainMenuSelection::NewGame => { if save_exists { MainMenuSelection::LoadGame } else { MainMenuSelection::Quit } } MainMenuSelection::LoadGame => MainMenuSelection::Quit, MainMenuSelection::Quit => MainMenuSelection::NewGame, }, }, VirtualKeyCode::Return => MainMenuResult::Selected { selected: selection, }, _ => MainMenuResult::NoSelection { selected: selection, }, }, }; } MainMenuResult::NoSelection { selected: MainMenuSelection::NewGame, } } #[derive(PartialEq, Copy, Clone)] pub enum GameOverResult { NoSelection, QuitToMenu, } pub fn game_over(ctx: &mut Rltk) -> GameOverResult { ctx.print_color_centered( 15, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Your journey has ended!", ); ctx.print_color_centered( 17, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "One day, we'll tell you all about how you did.", ); ctx.print_color_centered( 18, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "That day, sadly, is not in this chapter...", ); ctx.print_color_centered( 20, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Press any key to return to the menu.", ); match ctx.key { None => GameOverResult::NoSelection, Some(_) => GameOverResult::QuitToMenu, } }