roguelike-game/src/gui.rs

1198 lines
33 KiB
Rust
Raw Normal View History

2022-01-04 13:54:57 -05:00
use std::cmp::Ordering;
2021-12-24 10:38:44 -05:00
use ::rltk::{Point, Rltk, VirtualKeyCode, RGB};
use ::specs::prelude::*;
2021-12-10 20:16:48 -05:00
2021-11-18 11:47:57 -05:00
use crate::components::{
2022-01-13 11:29:20 -05:00
Attribute, Attributes, Consumable, HungerClock, HungerState, InBackpack, Item, Name, Pools,
Position, Vendor, Viewshed,
2021-11-18 11:47:57 -05:00
};
2021-12-10 20:16:48 -05:00
use crate::game_log::GameLog;
use crate::rex_assets::RexAssets;
2022-01-13 11:29:20 -05:00
use crate::{camera, Equipped, Hidden, Map, RunState, State, VendorMode};
2022-01-04 12:12:08 -05:00
pub fn draw_hollow_box(
console: &mut Rltk,
sx: i32,
sy: i32,
width: i32,
height: i32,
fg: RGB,
bg: RGB,
) {
use rltk::to_cp437;
console.set(sx, sy, fg, bg, to_cp437('┌'));
console.set(sx + width, sy, fg, bg, to_cp437('┐'));
console.set(sx, sy + height, fg, bg, to_cp437('└'));
console.set(sx + width, sy + height, fg, bg, to_cp437('┘'));
for x in sx + 1..sx + width {
console.set(x, sy, fg, bg, to_cp437('─'));
console.set(x, sy + height, fg, bg, to_cp437('─'));
}
for y in sy + 1..sy + height {
console.set(sx, y, fg, bg, to_cp437('│'));
console.set(sx + width, y, fg, bg, to_cp437('│'));
}
}
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
2022-01-04 12:12:08 -05:00
use rltk::to_cp437;
let box_gray: RGB = RGB::from_hex("#999999").expect("Ooops");
let black = RGB::named(rltk::BLACK);
let white = RGB::named(rltk::WHITE);
draw_hollow_box(ctx, 0, 0, 79, 59, box_gray, black); // Overall box
draw_hollow_box(ctx, 0, 0, 49, 45, box_gray, black); // Map box
draw_hollow_box(ctx, 0, 45, 79, 14, box_gray, black); // Log box
draw_hollow_box(ctx, 49, 0, 30, 8, box_gray, black); // Top-right panel
ctx.set(0, 45, box_gray, black, to_cp437('├'));
ctx.set(49, 8, box_gray, black, to_cp437('├'));
ctx.set(49, 0, box_gray, black, to_cp437('┬'));
ctx.set(49, 45, box_gray, black, to_cp437('┴'));
ctx.set(79, 8, box_gray, black, to_cp437('┤'));
ctx.set(79, 45, box_gray, black, to_cp437('┤'));
// Draw the town name
let map = ecs.fetch::<Map>();
let name_length = map.name.len() + 2;
let x_pos = (22 - (name_length / 2)) as i32;
ctx.set(x_pos, 0, box_gray, black, to_cp437('┤'));
ctx.set(
x_pos + name_length as i32,
0,
2022-01-04 12:12:08 -05:00
box_gray,
black,
to_cp437('├'),
);
ctx.print_color(x_pos + 1, 0, white, black, &map.name);
std::mem::drop(map);
// Draw stats
let player_entity = ecs.fetch::<Entity>();
let pools = ecs.read_storage::<Pools>();
let player_pools = pools.get(*player_entity).unwrap();
let health = format!(
"Health: {}/{}",
player_pools.hit_points.current, player_pools.hit_points.max
);
let mana = format!(
"Mana: {}/{}",
player_pools.mana.current, player_pools.mana.max
);
let xp = format!("Level: {}", player_pools.level);
2022-01-04 12:12:08 -05:00
ctx.print_color(50, 1, white, black, &health);
ctx.print_color(50, 2, white, black, &mana);
ctx.print_color(50, 3, white, black, &xp);
2022-01-04 12:12:08 -05:00
ctx.draw_bar_horizontal(
64,
1,
14,
player_pools.hit_points.current,
player_pools.hit_points.max,
RGB::named(rltk::RED),
RGB::named(rltk::BLACK),
);
2022-01-04 12:12:08 -05:00
ctx.draw_bar_horizontal(
64,
2021-11-09 15:50:42 -05:00
2,
2022-01-04 12:12:08 -05:00
14,
player_pools.mana.current,
player_pools.mana.max,
RGB::named(rltk::BLUE),
2021-11-09 15:50:42 -05:00
RGB::named(rltk::BLACK),
);
let xp_level_start = (player_pools.level - 1) * 1000;
ctx.draw_bar_horizontal(
64,
3,
14,
player_pools.xp - xp_level_start,
1000,
RGB::named(rltk::GOLD),
black,
);
2022-01-04 12:24:04 -05:00
// Attributes
let attributes = ecs.read_storage::<Attributes>();
let attr = attributes.get(*player_entity).unwrap();
draw_attribute("Might:", &attr.might, 4, ctx);
draw_attribute("Quickness:", &attr.quickness, 5, ctx);
draw_attribute("Fitness:", &attr.fitness, 6, ctx);
draw_attribute("Intelligence:", &attr.intelligence, 7, ctx);
// Initiative and weight
ctx.print_color(
50,
9,
white,
black,
&format!(
"{:.0} lbs ({} lbs max)",
player_pools.total_weight,
(attr.might.base + attr.might.modifiers) * 15
),
);
ctx.print_color(
50,
10,
white,
black,
&format!(
"Initiative Penalty: {:.0}",
player_pools.total_initiative_penalty
),
);
2022-01-13 10:42:02 -05:00
ctx.print_color(
50,
11,
RGB::named(rltk::GOLD),
black,
&format!("Gold: {:.1}", player_pools.gold),
);
2022-01-04 12:24:04 -05:00
// Equipped
let mut y = 13;
2022-01-04 12:24:04 -05:00
let equipped = ecs.read_storage::<Equipped>();
let name = ecs.read_storage::<Name>();
for (equipped_by, item_name) in (&equipped, &name).join() {
if equipped_by.owner == *player_entity {
ctx.print_color(50, y, white, black, &item_name.name);
y += 1;
}
}
// Consumables
y += 1;
let green = RGB::from_f32(0.0, 1.0, 0.0);
let yellow = RGB::named(rltk::YELLOW);
let consumables = ecs.read_storage::<Consumable>();
let backpack = ecs.read_storage::<InBackpack>();
let mut index = 1;
for (carried_by, _consumable, item_name) in (&backpack, &consumables, &name).join() {
if carried_by.owner == *player_entity && index < 10 {
ctx.print_color(50, y, yellow, black, &format!("{}", index));
ctx.print_color(53, y, green, black, &item_name.name);
y += 1;
index += 1;
}
}
// Status
let hunger = ecs.read_storage::<HungerClock>();
let hc = hunger.get(*player_entity).unwrap();
match hc.state {
HungerState::WellFed => ctx.print_color(50, 44, RGB::named(rltk::GREEN), black, "Well Fed"),
HungerState::Normal => {}
HungerState::Hungry => ctx.print_color(50, 44, RGB::named(rltk::ORANGE), black, "Hungry"),
HungerState::Starving => ctx.print_color(50, 44, RGB::named(rltk::RED), black, "Starving"),
}
// Draw the log
let log = ecs.fetch::<GameLog>();
let mut y = 46;
for s in log.entries.iter().rev() {
if y < 59 {
ctx.print(2, y, s);
}
y += 1;
}
draw_tooltips(ecs, ctx);
2022-01-04 12:12:08 -05:00
}
2021-11-09 15:50:42 -05:00
2022-01-04 12:12:08 -05:00
fn draw_attribute(name: &str, attribute: &Attribute, y: i32, ctx: &mut Rltk) {
let black = RGB::named(rltk::BLACK);
let attr_gray = RGB::from_hex("#CCCCCC").expect("Oops");
ctx.print_color(50, y, attr_gray, black, name);
2022-01-04 13:54:57 -05:00
let color = match attribute.modifiers.cmp(&0) {
Ordering::Less => RGB::from_f32(1.0, 0.0, 0.0),
Ordering::Equal => RGB::named(rltk::WHITE),
Ordering::Greater => RGB::from_f32(0.0, 1.0, 0.0),
2022-01-04 12:12:08 -05:00
};
2022-01-04 13:54:57 -05:00
2022-01-04 12:12:08 -05:00
ctx.print_color(
67,
y,
color,
black,
&format!("{}", attribute.base + attribute.modifiers),
);
ctx.print_color(73, y, color, black, &format!("{}", attribute.bonus));
2022-01-04 12:12:08 -05:00
if attribute.bonus > 0 {
ctx.set(72, y, color, black, rltk::to_cp437('+'));
}
}
2022-01-04 13:11:56 -05:00
struct Tooltip {
lines: Vec<String>,
}
impl Tooltip {
fn new() -> Tooltip {
Tooltip { lines: Vec::new() }
}
fn add<S: ToString>(&mut self, line: S) {
self.lines.push(line.to_string());
}
fn width(&self) -> i32 {
let mut max = 0;
for s in self.lines.iter() {
if s.len() > max {
max = s.len();
}
}
max as i32 + 2
}
fn height(&self) -> i32 {
self.lines.len() as i32 + 2
}
fn render(&self, ctx: &mut Rltk, x: i32, y: i32) {
let box_gray = RGB::from_hex("#999999").expect("Oops");
let light_gray = RGB::from_hex("#DDDDDD").expect("Oops");
let white = RGB::named(rltk::WHITE);
let black = RGB::named(rltk::BLACK);
ctx.draw_box(x, y, self.width() - 1, self.height() - 1, white, box_gray);
for (i, s) in self.lines.iter().enumerate() {
let col = if i == 0 { white } else { light_gray };
ctx.print_color(x + 1, y + i as i32 + 1, col, black, &s);
}
}
}
fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
2022-01-04 13:11:56 -05:00
use rltk::to_cp437;
let (min_x, _max_x, min_y, _max_y) = camera::get_screen_bounds(ecs, ctx);
let map = ecs.fetch::<Map>();
let names = ecs.read_storage::<Name>();
let positions = ecs.read_storage::<Position>();
2021-11-29 16:00:07 -05:00
let hidden = ecs.read_storage::<Hidden>();
2022-01-04 13:11:56 -05:00
let attributes = ecs.read_storage::<Attributes>();
let pools = ecs.read_storage::<Pools>();
let entities = ecs.entities();
let mouse_pos = ctx.mouse_pos();
2022-01-04 13:34:38 -05:00
let mut mouse_map_pos = mouse_pos;
mouse_map_pos.0 += min_x;
mouse_map_pos.1 += min_y;
2022-01-04 13:11:56 -05:00
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;
}
2022-01-04 13:11:56 -05:00
let mut tip_boxes: Vec<Tooltip> = Vec::new();
for (entity, name, position, _hidden) in (&entities, &names, &positions, !&hidden).join() {
if position.x == mouse_map_pos.0 && position.y == mouse_map_pos.1 {
2022-01-04 13:11:56 -05:00
let mut tip = Tooltip::new();
tip.add(name.name.to_string());
// Comment on attributes
if let Some(attr) = attributes.get(entity) {
let mut s = String::new();
if attr.might.bonus < 0 {
s += "Weak. "
2022-01-04 13:54:57 -05:00
}
if attr.might.bonus > 0 {
2022-01-04 13:11:56 -05:00
s += "String. "
}
if attr.quickness.bonus < 0 {
s += "Clumsy. "
2022-01-04 13:54:57 -05:00
}
if attr.quickness.bonus > 0 {
2022-01-04 13:11:56 -05:00
s += "Agile. "
}
2022-01-04 13:11:56 -05:00
if attr.fitness.bonus < 0 {
s += "Unhealthy. "
2022-01-04 13:54:57 -05:00
}
if attr.fitness.bonus > 0 {
2022-01-04 13:11:56 -05:00
s += "Healthy. "
}
if attr.intelligence.bonus < 0 {
s += "Unintelligent. "
2022-01-04 13:54:57 -05:00
}
if attr.intelligence.bonus > 0 {
2022-01-04 13:11:56 -05:00
s += "Smart. "
}
2022-01-04 13:11:56 -05:00
if s.is_empty() {
s = "Quite Average".to_string();
}
tip.add(s);
}
2022-01-04 13:11:56 -05:00
// Comment on pools
if let Some(stat) = pools.get(entity) {
tip.add(format!("Level: {}", stat.level));
}
tip_boxes.push(tip);
}
}
2022-01-04 13:11:56 -05:00
if tip_boxes.is_empty() {
return;
}
let box_gray = RGB::from_hex("#999999").expect("Oops");
let white = RGB::named(rltk::WHITE);
let arrow;
let arrow_x;
let arrow_y = mouse_pos.1;
if mouse_pos.0 < 40 {
// Render to the left
arrow = to_cp437('→');
arrow_x = mouse_pos.0 - 1;
} else {
// Render to the right
arrow = to_cp437('←');
arrow_x = mouse_pos.0 + 1;
}
ctx.set(arrow_x, arrow_y, white, box_gray, arrow);
let mut total_height = 0;
for tt in tip_boxes.iter() {
total_height += tt.height();
}
let mut y = mouse_pos.1 - (total_height / 2);
while y + (total_height / 2) > 50 {
y -= 1;
}
for tt in tip_boxes.iter() {
let x = if mouse_pos.0 < 40 {
mouse_pos.0 - (1 + tt.width())
} else {
mouse_pos.0 + (1 + tt.width())
};
tt.render(ctx, x, y);
y += tt.height();
}
}
2021-11-03 15:11:19 -04:00
#[derive(PartialEq, Copy, Clone)]
pub enum ItemMenuResult {
Cancel,
NoResponse,
Selected,
}
2021-11-03 15:59:23 -04:00
pub fn show_inventory(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
2021-11-03 15:11:19 -04:00
let player_entity = gs.ecs.fetch::<Entity>();
let names = gs.ecs.read_storage::<Name>();
let backpack = gs.ecs.read_storage::<InBackpack>();
2021-11-03 15:59:23 -04:00
let entities = gs.ecs.entities();
2021-11-03 15:11:19 -04:00
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,
2021-11-04 09:55:39 -04:00
31,
2021-11-03 15:11:19 -04:00
(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",
);
2021-11-04 09:40:58 -04:00
let mut equippable: Vec<Entity> = Vec::new();
let mut j = 0;
#[allow(clippy::explicit_counter_loop)]
2021-11-04 09:40:58 -04:00
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<Entity>) {
let player_entity = gs.ecs.fetch::<Entity>();
let names = gs.ecs.read_storage::<Name>();
let backpack = gs.ecs.read_storage::<InBackpack>();
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",
);
2021-11-03 15:59:23 -04:00
let mut equippable: Vec<Entity> = Vec::new();
2021-11-03 15:11:19 -04:00
let mut j = 0;
#[allow(clippy::explicit_counter_loop)]
2021-11-03 15:59:23 -04:00
for (entity, _pack, name) in (&entities, &backpack, &names)
2021-11-03 15:11:19 -04:00
.join()
2021-11-03 15:59:23 -04:00
.filter(|item| item.1.owner == *player_entity)
2021-11-03 15:11:19 -04:00
{
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());
2021-11-03 15:59:23 -04:00
equippable.push(entity);
2021-11-03 15:11:19 -04:00
y += 1;
j += 1;
}
match ctx.key {
2021-11-03 15:59:23 -04:00
None => (ItemMenuResult::NoResponse, None),
2021-11-03 15:11:19 -04:00
Some(key) => match key {
2021-11-03 15:59:23 -04:00
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)
}
2021-11-03 15:11:19 -04:00
},
}
}
2021-11-05 10:42:44 -04:00
pub fn remove_item_menu(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
let player_entity = gs.ecs.fetch::<Entity>();
let names = gs.ecs.read_storage::<Name>();
let backpack = gs.ecs.read_storage::<Equipped>();
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<Entity> = 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)
}
},
}
}
2021-11-05 10:42:44 -04:00
pub fn ranged_target(
gs: &mut State,
ctx: &mut Rltk,
range: i32,
) -> (ItemMenuResult, Option<Point>) {
let (min_x, max_x, min_y, max_y) = camera::get_screen_bounds(&gs.ecs, ctx);
2021-11-05 10:42:44 -04:00
let player_entity = gs.ecs.fetch::<Entity>();
let player_pos = gs.ecs.fetch::<Point>();
let viewsheds = gs.ecs.read_storage::<Viewshed>();
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) {
2021-11-05 10:42:44 -04:00
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
{
2021-12-23 12:04:50 -05:00
ctx.set_bg(screen_x, screen_y, RGB::named(rltk::BLUE));
available_cells.push(idx);
}
2021-11-05 10:42:44 -04:00
}
}
} 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;
2021-11-05 10:42:44 -04:00
let mut valid_target = false;
for idx in available_cells.iter() {
if idx.x == mouse_map_pos.0 && idx.y == mouse_map_pos.1 {
2021-11-05 10:42:44 -04:00
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)),
2021-11-05 10:42:44 -04:00
);
}
} 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)
}
2021-11-08 10:22:11 -05:00
#[derive(PartialEq, Copy, Clone)]
pub enum MainMenuSelection {
NewGame,
LoadGame,
Quit,
}
#[derive(PartialEq, Copy, Clone)]
pub enum MainMenuResult {
NoSelection { selected: MainMenuSelection },
Selected { selected: MainMenuSelection },
}
2021-11-08 10:22:11 -05:00
pub fn main_menu(gs: &mut State, ctx: &mut Rltk) -> MainMenuResult {
2021-11-09 10:29:23 -05:00
let save_exists = crate::saveload_system::does_save_exist();
2021-11-08 10:22:11 -05:00
let runstate = gs.ecs.fetch::<RunState>();
2021-11-29 14:59:46 -05:00
let assets = gs.ecs.fetch::<RexAssets>();
ctx.render_xp_sprite(&assets.menu, 0, 0);
ctx.draw_box_double(
24,
18,
31,
10,
RGB::named(rltk::WHEAT),
RGB::named(rltk::BLACK),
);
2021-11-08 10:22:11 -05:00
ctx.print_color_centered(
2021-11-29 14:59:46 -05:00
20,
2021-11-08 10:22:11 -05:00
RGB::named(rltk::YELLOW),
RGB::named(rltk::BLACK),
"Rust Roguelike Tutorial",
);
2021-11-29 14:59:46 -05:00
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",
);
2021-11-08 10:22:11 -05:00
2021-11-29 14:59:46 -05:00
let mut y = 24;
2021-11-08 10:22:11 -05:00
if let RunState::MainMenu {
menu_selection: selection,
} = *runstate
{
if selection == MainMenuSelection::NewGame {
ctx.print_color_centered(
2021-11-29 14:59:46 -05:00
y,
2021-11-08 10:22:11 -05:00
RGB::named(rltk::MAGENTA),
RGB::named(rltk::BLACK),
"Begin New Game",
);
} else {
ctx.print_color_centered(
2021-11-29 14:59:46 -05:00
y,
2021-11-08 10:22:11 -05:00
RGB::named(rltk::WHITE),
RGB::named(rltk::BLACK),
"Begin New Game",
);
}
2021-11-29 14:59:46 -05:00
y += 1;
2021-11-08 10:22:11 -05:00
2021-11-09 10:29:23 -05:00
if save_exists {
if selection == MainMenuSelection::LoadGame {
ctx.print_color_centered(
2021-11-29 14:59:46 -05:00
y,
2021-11-09 10:29:23 -05:00
RGB::named(rltk::MAGENTA),
RGB::named(rltk::BLACK),
"Load Game",
);
} else {
ctx.print_color_centered(
2021-11-29 14:59:46 -05:00
y,
2021-11-09 10:29:23 -05:00
RGB::named(rltk::WHITE),
RGB::named(rltk::BLACK),
"Load Game",
);
}
2021-11-29 14:59:46 -05:00
y += 1;
2021-11-08 10:22:11 -05:00
}
if selection == MainMenuSelection::Quit {
ctx.print_color_centered(
2021-11-29 14:59:46 -05:00
y,
2021-11-08 10:22:11 -05:00
RGB::named(rltk::MAGENTA),
RGB::named(rltk::BLACK),
"Quit",
);
} else {
2021-11-29 14:59:46 -05:00
ctx.print_color_centered(y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Quit");
2021-11-08 10:22:11 -05:00
}
return match ctx.key {
None => MainMenuResult::NoSelection {
selected: selection,
},
2021-11-08 10:22:11 -05:00
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,
},
2021-11-08 10:22:11 -05:00
},
};
2021-11-08 10:22:11 -05:00
}
MainMenuResult::NoSelection {
selected: MainMenuSelection::NewGame,
}
}
2021-11-15 11:32:09 -05:00
2021-11-15 13:27:40 -05:00
#[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,
}
}
2022-01-07 09:39:47 -05:00
#[derive(PartialEq, Copy, Clone)]
pub enum CheatMenuResult {
NoResponse,
Cancel,
TeleportToExit,
}
pub fn show_cheat_mode(_gs: &mut State, ctx: &mut Rltk) -> CheatMenuResult {
let count = 2;
let 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),
"Cheating!",
);
ctx.print_color(
18,
y + count as i32 + 1,
RGB::named(rltk::YELLOW),
RGB::named(rltk::BLACK),
"ESCAPE to cancel",
);
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),
rltk::to_cp437('T'),
);
ctx.set(
19,
y,
RGB::named(rltk::WHITE),
RGB::named(rltk::BLACK),
rltk::to_cp437(')'),
);
ctx.print(21, y, "Teleport to exit");
match ctx.key {
None => CheatMenuResult::NoResponse,
Some(key) => match key {
VirtualKeyCode::T => CheatMenuResult::TeleportToExit,
VirtualKeyCode::Escape => CheatMenuResult::Cancel,
_ => CheatMenuResult::NoResponse,
},
}
}
2022-01-13 11:29:20 -05:00
#[derive(PartialEq, Copy, Clone)]
pub enum VendorResult {
NoResponse,
Cancel,
Sell,
BuyMode,
SellMode,
Buy,
}
fn vendor_sell_menu(
gs: &mut State,
ctx: &mut Rltk,
_vendor: Entity,
_mode: VendorMode,
) -> (VendorResult, Option<Entity>, Option<String>, Option<f32>) {
let player_entity = gs.ecs.fetch::<Entity>();
let names = gs.ecs.read_storage::<Name>();
let backpack = gs.ecs.read_storage::<InBackpack>();
let items = gs.ecs.read_storage::<Item>();
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,
51,
(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),
"Sell Which Item? (space to switch to buy mode)",
);
ctx.print_color(
18,
y + count as i32 + 1,
RGB::named(rltk::YELLOW),
RGB::named(rltk::BLACK),
"ESCAPE to cancel",
);
let mut equippable: Vec<Entity> = Vec::new();
for (j, (entity, _pack, name, item)) in (&entities, &backpack, &names, &items)
.join()
.filter(|item| item.1.owner == *player_entity)
.enumerate()
{
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());
ctx.print(50, y, &format!("{:.1} gp", item.base_value * 0.8));
equippable.push(entity);
y += 1;
}
match ctx.key {
None => (VendorResult::NoResponse, None, None, None),
Some(key) => match key {
VirtualKeyCode::Space => (VendorResult::BuyMode, None, None, None),
VirtualKeyCode::Escape => (VendorResult::Cancel, None, None, None),
_ => {
let selection = rltk::letter_to_option(key);
if selection > -1 && selection < count as i32 {
return (
VendorResult::Sell,
Some(equippable[selection as usize]),
None,
None,
);
}
(VendorResult::NoResponse, None, None, None)
}
},
}
}
fn vendor_buy_menu(
gs: &mut State,
ctx: &mut Rltk,
vendor: Entity,
_mode: VendorMode,
) -> (VendorResult, Option<Entity>, Option<String>, Option<f32>) {
use crate::raws::*;
let vendors = gs.ecs.read_storage::<Vendor>();
let inventory = get_vendor_items(
&vendors.get(vendor).unwrap().categories,
&RAWS.lock().unwrap(),
);
let count = inventory.len();
let mut y = (25 - (count / 2)) as i32;
ctx.draw_box(
15,
y - 2,
51,
(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),
"Buy Which Item? (space to switch to sell mode)",
);
ctx.print_color(
18,
y + count as i32 + 1,
RGB::named(rltk::YELLOW),
RGB::named(rltk::BLACK),
"ESCAPE to cancel",
);
for (j, sale) in inventory.iter().enumerate() {
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, &sale.0);
ctx.print(50, y, &format!("{:.1} gp", sale.1 * 1.2));
y += 1;
}
match ctx.key {
None => (VendorResult::NoResponse, None, None, None),
Some(key) => match key {
VirtualKeyCode::Space => (VendorResult::SellMode, None, None, None),
VirtualKeyCode::Escape => (VendorResult::Cancel, None, None, None),
_ => {
let selection = ::rltk::letter_to_option(key);
if selection > -1 && selection < count as i32 {
return (
VendorResult::Buy,
None,
Some(inventory[selection as usize].0.clone()),
Some(inventory[selection as usize].1),
);
}
(VendorResult::NoResponse, None, None, None)
}
},
}
}
pub fn show_vendor_menu(
gs: &mut State,
ctx: &mut Rltk,
vendor: Entity,
mode: VendorMode,
) -> (VendorResult, Option<Entity>, Option<String>, Option<f32>) {
match mode {
VendorMode::Buy => vendor_buy_menu(gs, ctx, vendor, mode),
VendorMode::Sell => vendor_sell_menu(gs, ctx, vendor, mode),
}
}