gui.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. extern crate rltk;
  2. use rltk::{ RGB, Rltk, Console, Point, VirtualKeyCode };
  3. extern crate specs;
  4. use specs::prelude::*;
  5. use super::{CombatStats, Player, gamelog::GameLog, Map, Name, Position, State, InBackpack,
  6. Viewshed, RunState, Equipped, HungerClock, HungerState, rex_assets::RexAssets,
  7. Hidden };
  8. pub fn draw_ui(ecs: &World, ctx : &mut Rltk) {
  9. ctx.draw_box(0, 43, 79, 6, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  10. let combat_stats = ecs.read_storage::<CombatStats>();
  11. let players = ecs.read_storage::<Player>();
  12. let hunger = ecs.read_storage::<HungerClock>();
  13. for (_player, stats, hc) in (&players, &combat_stats, &hunger).join() {
  14. let health = format!(" HP: {} / {} ", stats.hp, stats.max_hp);
  15. ctx.print_color(12, 43, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), &health);
  16. ctx.draw_bar_horizontal(28, 43, 51, stats.hp, stats.max_hp, RGB::named(rltk::RED), RGB::named(rltk::BLACK));
  17. match hc.state {
  18. HungerState::WellFed => ctx.print_color(71, 42, RGB::named(rltk::GREEN), RGB::named(rltk::BLACK), "Well Fed"),
  19. HungerState::Normal => {}
  20. HungerState::Hungry => ctx.print_color(71, 42, RGB::named(rltk::ORANGE), RGB::named(rltk::BLACK), "Hungry"),
  21. HungerState::Starving => ctx.print_color(71, 42, RGB::named(rltk::RED), RGB::named(rltk::BLACK), "Starving"),
  22. }
  23. }
  24. let map = ecs.fetch::<Map>();
  25. let depth = format!("Depth: {}", map.depth);
  26. ctx.print_color(2, 43, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), &depth);
  27. let log = ecs.fetch::<GameLog>();
  28. let mut y = 44;
  29. for s in log.entries.iter() {
  30. if y < 49 { ctx.print(2, y, &s.to_string()); }
  31. y += 1;
  32. }
  33. // Draw mouse cursor
  34. let mouse_pos = ctx.mouse_pos();
  35. ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::MAGENTA));
  36. draw_tooltips(ecs, ctx);
  37. }
  38. fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
  39. let map = ecs.fetch::<Map>();
  40. let names = ecs.read_storage::<Name>();
  41. let positions = ecs.read_storage::<Position>();
  42. let hidden = ecs.read_storage::<Hidden>();
  43. let mouse_pos = ctx.mouse_pos();
  44. if mouse_pos.0 >= map.width || mouse_pos.1 >= map.height { return; }
  45. let mut tooltip : Vec<String> = Vec::new();
  46. for (name, position, _hidden) in (&names, &positions, !&hidden).join() {
  47. if position.x == mouse_pos.0 && position.y == mouse_pos.1 {
  48. tooltip.push(name.name.to_string());
  49. }
  50. }
  51. if !tooltip.is_empty() {
  52. let mut width :i32 = 0;
  53. for s in tooltip.iter() {
  54. if width < s.len() as i32 { width = s.len() as i32; }
  55. }
  56. width += 3;
  57. if mouse_pos.0 > 40 {
  58. let arrow_pos = Point::new(mouse_pos.0 - 2, mouse_pos.1);
  59. let left_x = mouse_pos.0 - width;
  60. let mut y = mouse_pos.1;
  61. for s in tooltip.iter() {
  62. ctx.print_color(left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &s.to_string());
  63. let padding = (width - s.len() as i32)-1;
  64. for i in 0..padding {
  65. ctx.print_color(arrow_pos.x - i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  66. }
  67. y += 1;
  68. }
  69. ctx.print_color(arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"->".to_string());
  70. } else {
  71. let arrow_pos = Point::new(mouse_pos.0 + 1, mouse_pos.1);
  72. let left_x = mouse_pos.0 +3;
  73. let mut y = mouse_pos.1;
  74. for s in tooltip.iter() {
  75. ctx.print_color(left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &s.to_string());
  76. let padding = (width - s.len() as i32)-1;
  77. for i in 0..padding {
  78. ctx.print_color(left_x + s.len() as i32 + i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  79. }
  80. y += 1;
  81. }
  82. ctx.print_color(arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"<-".to_string());
  83. }
  84. }
  85. }
  86. #[derive(PartialEq, Copy, Clone)]
  87. pub enum ItemMenuResult { Cancel, NoResponse, Selected }
  88. pub fn show_inventory(gs : &mut State, ctx : &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
  89. let player_entity = gs.ecs.fetch::<Entity>();
  90. let names = gs.ecs.read_storage::<Name>();
  91. let backpack = gs.ecs.read_storage::<InBackpack>();
  92. let entities = gs.ecs.entities();
  93. let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity );
  94. let count = inventory.count();
  95. let mut y = (25 - (count / 2)) as i32;
  96. ctx.draw_box(15, y-2, 31, (count+3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  97. ctx.print_color(18, y-2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Inventory");
  98. ctx.print_color(18, y+count as i32+1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel");
  99. let mut equippable : Vec<Entity> = Vec::new();
  100. let mut j = 0;
  101. for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity ) {
  102. ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
  103. ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97+j as u8);
  104. ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
  105. ctx.print(21, y, &name.name.to_string());
  106. equippable.push(entity);
  107. y += 1;
  108. j += 1;
  109. }
  110. match ctx.key {
  111. None => (ItemMenuResult::NoResponse, None),
  112. Some(key) => {
  113. match key {
  114. VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) }
  115. _ => {
  116. let selection = rltk::letter_to_option(key);
  117. if selection > -1 && selection < count as i32 {
  118. return (ItemMenuResult::Selected, Some(equippable[selection as usize]));
  119. }
  120. (ItemMenuResult::NoResponse, None)
  121. }
  122. }
  123. }
  124. }
  125. }
  126. pub fn drop_item_menu(gs : &mut State, ctx : &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
  127. let player_entity = gs.ecs.fetch::<Entity>();
  128. let names = gs.ecs.read_storage::<Name>();
  129. let backpack = gs.ecs.read_storage::<InBackpack>();
  130. let entities = gs.ecs.entities();
  131. let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity );
  132. let count = inventory.count();
  133. let mut y = (25 - (count / 2)) as i32;
  134. ctx.draw_box(15, y-2, 31, (count+3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  135. ctx.print_color(18, y-2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Drop Which Item?");
  136. ctx.print_color(18, y+count as i32+1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel");
  137. let mut equippable : Vec<Entity> = Vec::new();
  138. let mut j = 0;
  139. for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity ) {
  140. ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
  141. ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97+j as u8);
  142. ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
  143. ctx.print(21, y, &name.name.to_string());
  144. equippable.push(entity);
  145. y += 1;
  146. j += 1;
  147. }
  148. match ctx.key {
  149. None => (ItemMenuResult::NoResponse, None),
  150. Some(key) => {
  151. match key {
  152. VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) }
  153. _ => {
  154. let selection = rltk::letter_to_option(key);
  155. if selection > -1 && selection < count as i32 {
  156. return (ItemMenuResult::Selected, Some(equippable[selection as usize]));
  157. }
  158. (ItemMenuResult::NoResponse, None)
  159. }
  160. }
  161. }
  162. }
  163. }
  164. pub fn remove_item_menu(gs : &mut State, ctx : &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
  165. let player_entity = gs.ecs.fetch::<Entity>();
  166. let names = gs.ecs.read_storage::<Name>();
  167. let backpack = gs.ecs.read_storage::<Equipped>();
  168. let entities = gs.ecs.entities();
  169. let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity );
  170. let count = inventory.count();
  171. let mut y = (25 - (count / 2)) as i32;
  172. ctx.draw_box(15, y-2, 31, (count+3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  173. ctx.print_color(18, y-2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Remove Which Item?");
  174. ctx.print_color(18, y+count as i32+1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel");
  175. let mut equippable : Vec<Entity> = Vec::new();
  176. let mut j = 0;
  177. for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity ) {
  178. ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
  179. ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97+j as u8);
  180. ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
  181. ctx.print(21, y, &name.name.to_string());
  182. equippable.push(entity);
  183. y += 1;
  184. j += 1;
  185. }
  186. match ctx.key {
  187. None => (ItemMenuResult::NoResponse, None),
  188. Some(key) => {
  189. match key {
  190. VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) }
  191. _ => {
  192. let selection = rltk::letter_to_option(key);
  193. if selection > -1 && selection < count as i32 {
  194. return (ItemMenuResult::Selected, Some(equippable[selection as usize]));
  195. }
  196. (ItemMenuResult::NoResponse, None)
  197. }
  198. }
  199. }
  200. }
  201. }
  202. pub fn ranged_target(gs : &mut State, ctx : &mut Rltk, range : i32) -> (ItemMenuResult, Option<Point>) {
  203. let player_entity = gs.ecs.fetch::<Entity>();
  204. let player_pos = gs.ecs.fetch::<Point>();
  205. let viewsheds = gs.ecs.read_storage::<Viewshed>();
  206. ctx.print_color(5, 0, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Select Target:");
  207. // Highlight available target cells
  208. let mut available_cells = Vec::new();
  209. let visible = viewsheds.get(*player_entity);
  210. if let Some(visible) = visible {
  211. // We have a viewshed
  212. for idx in visible.visible_tiles.iter() {
  213. let distance = rltk::DistanceAlg::Pythagoras.distance2d(*player_pos, *idx);
  214. if distance <= range as f32 {
  215. ctx.set_bg(idx.x, idx.y, RGB::named(rltk::BLUE));
  216. available_cells.push(idx);
  217. }
  218. }
  219. } else {
  220. return (ItemMenuResult::Cancel, None);
  221. }
  222. // Draw mouse cursor
  223. let mouse_pos = ctx.mouse_pos();
  224. let mut valid_target = false;
  225. for idx in available_cells.iter() { if idx.x == mouse_pos.0 && idx.y == mouse_pos.1 { valid_target = true; } }
  226. if valid_target {
  227. ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::CYAN));
  228. if ctx.left_click {
  229. return (ItemMenuResult::Selected, Some(Point::new(mouse_pos.0, mouse_pos.1)));
  230. }
  231. } else {
  232. ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::RED));
  233. if ctx.left_click {
  234. return (ItemMenuResult::Cancel, None);
  235. }
  236. }
  237. (ItemMenuResult::NoResponse, None)
  238. }
  239. #[derive(PartialEq, Copy, Clone)]
  240. pub enum MainMenuSelection { NewGame, LoadGame, Quit }
  241. #[derive(PartialEq, Copy, Clone)]
  242. pub enum MainMenuResult { NoSelection{ selected : MainMenuSelection }, Selected{ selected: MainMenuSelection } }
  243. pub fn main_menu(gs : &mut State, ctx : &mut Rltk) -> MainMenuResult {
  244. let save_exists = super::saveload_system::does_save_exist();
  245. let runstate = gs.ecs.fetch::<RunState>();
  246. let assets = gs.ecs.fetch::<RexAssets>();
  247. ctx.render_xp_sprite(&assets.menu, 0, 0);
  248. ctx.draw_box_double(24, 18, 31, 10, RGB::named(rltk::WHEAT), RGB::named(rltk::BLACK));
  249. ctx.print_color_centered(20, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Rust Roguelike Tutorial");
  250. ctx.print_color_centered(21, RGB::named(rltk::CYAN), RGB::named(rltk::BLACK), "by Herbert Wolverson");
  251. ctx.print_color_centered(22, RGB::named(rltk::GRAY), RGB::named(rltk::BLACK), "Use Up/Down Arrows and Enter");
  252. let mut y = 24;
  253. if let RunState::MainMenu{ menu_selection : selection } = *runstate {
  254. if selection == MainMenuSelection::NewGame {
  255. ctx.print_color_centered(y, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Begin New Game");
  256. } else {
  257. ctx.print_color_centered(y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Begin New Game");
  258. }
  259. y += 1;
  260. if save_exists {
  261. if selection == MainMenuSelection::LoadGame {
  262. ctx.print_color_centered(y, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Load Game");
  263. } else {
  264. ctx.print_color_centered(y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Load Game");
  265. }
  266. y += 1;
  267. }
  268. if selection == MainMenuSelection::Quit {
  269. ctx.print_color_centered(y, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Quit");
  270. } else {
  271. ctx.print_color_centered(y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Quit");
  272. }
  273. match ctx.key {
  274. None => return MainMenuResult::NoSelection{ selected: selection },
  275. Some(key) => {
  276. match key {
  277. VirtualKeyCode::Escape => { return MainMenuResult::NoSelection{ selected: MainMenuSelection::Quit } }
  278. VirtualKeyCode::Up => {
  279. let mut newselection;
  280. match selection {
  281. MainMenuSelection::NewGame => newselection = MainMenuSelection::Quit,
  282. MainMenuSelection::LoadGame => newselection = MainMenuSelection::NewGame,
  283. MainMenuSelection::Quit => newselection = MainMenuSelection::LoadGame
  284. }
  285. if newselection == MainMenuSelection::LoadGame && !save_exists {
  286. newselection = MainMenuSelection::NewGame;
  287. }
  288. return MainMenuResult::NoSelection{ selected: newselection }
  289. }
  290. VirtualKeyCode::Down => {
  291. let mut newselection;
  292. match selection {
  293. MainMenuSelection::NewGame => newselection = MainMenuSelection::LoadGame,
  294. MainMenuSelection::LoadGame => newselection = MainMenuSelection::Quit,
  295. MainMenuSelection::Quit => newselection = MainMenuSelection::NewGame
  296. }
  297. if newselection == MainMenuSelection::LoadGame && !save_exists {
  298. newselection = MainMenuSelection::Quit;
  299. }
  300. return MainMenuResult::NoSelection{ selected: newselection }
  301. }
  302. VirtualKeyCode::Return => return MainMenuResult::Selected{ selected : selection },
  303. _ => return MainMenuResult::NoSelection{ selected: selection }
  304. }
  305. }
  306. }
  307. }
  308. MainMenuResult::NoSelection { selected: MainMenuSelection::NewGame }
  309. }
  310. #[derive(PartialEq, Copy, Clone)]
  311. pub enum GameOverResult { NoSelection, QuitToMenu }
  312. pub fn game_over(ctx : &mut Rltk) -> GameOverResult {
  313. ctx.print_color_centered(15, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Your journey has ended!");
  314. ctx.print_color_centered(17, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "One day, we'll tell you all about how you did.");
  315. ctx.print_color_centered(18, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "That day, sadly, is not in this chapter..");
  316. ctx.print_color_centered(20, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Press any key to return to the menu.");
  317. match ctx.key {
  318. None => GameOverResult::NoSelection,
  319. Some(_) => GameOverResult::QuitToMenu
  320. }
  321. }