gui.rs 16 KB

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