Heavily reorganize code

This commit is contained in:
Alex Page 2022-02-02 03:58:48 -05:00
parent 6ae930cb43
commit 9d911d8c72
18 changed files with 454 additions and 386 deletions

19
src/graphics/entities.rs Normal file
View file

@ -0,0 +1,19 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use crate::{components::*, constants::*};
pub fn draw(world: &World, bterm: &mut BTerm) {
let positions = world.read_storage::<Position>();
let renderables = world.read_storage::<Renderable>();
for (pos, render) in (&positions, &renderables).join() {
bterm.set(
pos.x + MAP_X as i32,
pos.y + MAP_Y as i32,
render.fg,
render.bg,
render.glyph,
);
}
}

9
src/graphics/map.rs Normal file
View file

@ -0,0 +1,9 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use crate::resources::*;
pub fn draw(world: &World, bterm: &mut BTerm) {
let map = world.fetch::<Map>();
map.draw(bterm);
}

16
src/graphics/mod.rs Normal file
View file

@ -0,0 +1,16 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
mod entities;
mod map;
mod sidebar;
pub mod vga_color;
mod whip;
pub fn draw(world: &World, bterm: &mut BTerm) {
bterm.cls();
map::draw(world, bterm);
entities::draw(world, bterm);
whip::draw(world, bterm);
sidebar::draw(world, bterm);
}

113
src/graphics/sidebar.rs Normal file
View file

@ -0,0 +1,113 @@
use crate::constants::{SIDEBAR_POS_X, SIDEBAR_POS_Y};
use crate::graphics::vga_color as vga;
use crate::resources::{Clock, LevelNumber, ShowDebugInfo, Stats};
use bracket_lib::prelude::*;
use specs::prelude::*;
pub fn draw(world: &World, bterm: &mut BTerm) {
// Blue background
bterm.fill_region(
Rect {
x1: SIDEBAR_POS_X,
x2: SIDEBAR_POS_X + 14,
y1: SIDEBAR_POS_Y,
y2: SIDEBAR_POS_Y + 19,
},
to_cp437(' '),
RGB::named(vga::YELLOW_BRIGHT),
RGB::named(vga::BLUE),
);
// Gray number boxes
(1..17).step_by(3).for_each(|y| {
bterm.fill_region(
Rect {
x1: SIDEBAR_POS_X + 3,
x2: SIDEBAR_POS_X + 10,
y1: SIDEBAR_POS_Y + y,
y2: SIDEBAR_POS_Y + y + 1,
},
to_cp437(' '),
RGB::named(vga::RED),
RGB::named(vga::WHITE),
);
});
// Stats
bterm.print(SIDEBAR_POS_X + 4, SIDEBAR_POS_Y, "Score");
bterm.print(SIDEBAR_POS_X + 4, SIDEBAR_POS_Y + 3, "Level");
bterm.print_centered_at(
SIDEBAR_POS_X + 6,
SIDEBAR_POS_Y + 4,
world.read_resource::<LevelNumber>().0 + 1,
);
bterm.print(SIDEBAR_POS_X + 4, SIDEBAR_POS_Y + 6, "Gems");
bterm.print(SIDEBAR_POS_X + 4, SIDEBAR_POS_Y + 9, "Whips");
bterm.print(SIDEBAR_POS_X + 2, SIDEBAR_POS_Y + 12, "Teleports");
bterm.print(SIDEBAR_POS_X + 4, SIDEBAR_POS_Y + 15, "Keys");
let stats = world.read_resource::<Stats>();
bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 1, stats.score);
bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 7, stats.gems);
bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 10, stats.whips);
bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 13, stats.teleports);
bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 16, stats.keys);
// Hotkey list
bterm.print_color(
SIDEBAR_POS_X + 3,
SIDEBAR_POS_Y + 18,
RGB::named(vga::CYAN_BRIGHT),
RGB::named(vga::RED),
"OPTIONS",
);
bterm.fill_region(
Rect {
x1: SIDEBAR_POS_X,
x2: SIDEBAR_POS_X + 14,
y1: SIDEBAR_POS_Y + 19,
y2: SIDEBAR_POS_Y + 25,
},
to_cp437(' '),
RGB::named(vga::WHITE),
RGB::named(vga::BLUE),
);
bterm.fill_region(
Rect {
x1: SIDEBAR_POS_X + 3,
x2: SIDEBAR_POS_X + 4,
y1: SIDEBAR_POS_Y + 19,
y2: SIDEBAR_POS_Y + 25,
},
to_cp437(' '),
RGB::named(vga::WHITE_BRIGHT),
RGB::named(vga::BLUE),
);
bterm.print(SIDEBAR_POS_X + 3, SIDEBAR_POS_Y + 19, "Whip");
bterm.print(SIDEBAR_POS_X + 3, SIDEBAR_POS_Y + 20, "Teleport");
bterm.print(SIDEBAR_POS_X + 3, SIDEBAR_POS_Y + 21, "Pause");
bterm.print(SIDEBAR_POS_X + 3, SIDEBAR_POS_Y + 22, "Quit");
bterm.print(SIDEBAR_POS_X + 3, SIDEBAR_POS_Y + 23, "Save");
bterm.print(SIDEBAR_POS_X + 3, SIDEBAR_POS_Y + 24, "Restore");
if world.read_resource::<ShowDebugInfo>().0 {
bterm.print_color_right(
SIDEBAR_POS_X + 14,
SIDEBAR_POS_Y,
RGB::named(vga::GREEN_BRIGHT),
RGB::named(vga::BLACK),
&format!("{}", bterm.fps),
);
bterm.print_color(
0,
0,
RGB::named(vga::YELLOW_BRIGHT),
RGB::named(vga::BLACK),
&format!("{}", world.read_resource::<Clock>().ticks),
);
}
}

79
src/graphics/vga_color.rs Normal file
View file

@ -0,0 +1,79 @@
//! Definitions for VGA 4-bit colors, like the original Kroz would have used
//!
//! In the Kroz source, colors are referenced by their index in the 4-bit VGA palette.
//! Here they are for reference:
//!
//! - `0` = `BLACK`
//! - `1` = `BLUE`
//! - `2` = `GREEN`
//! - `3` = `CYAN`
//! - `4` = `RED`
//! - `5` = `MAGENTA`
//! - `6` = `YELLOW`
//! - `7` = `WHITE`
//! - `8` = `BLACK_BRIGHT`
//! - `9` = `BLUE_BRIGHT`
//! - `10` = `GREEN_BRIGHT`
//! - `11` = `CYAN_BRIGHT`
//! - `12` = `RED_BRIGHT`
//! - `13` = `MAGENTA_BRIGHT`
//! - `14` = `YELLOW_BRIGHT`
//! - `15` = `WHITE_BRIGHT`
// VGA 4-bit Colors
/// Index `0`
pub const BLACK: (u8, u8, u8) = (0, 0, 0);
/// Index `1`
pub const BLUE: (u8, u8, u8) = (0, 0, 170);
/// Index `2`
pub const GREEN: (u8, u8, u8) = (0, 170, 0);
/// Index `3`
pub const CYAN: (u8, u8, u8) = (0, 170, 170);
/// Index `4`
pub const RED: (u8, u8, u8) = (170, 0, 0);
/// Index `5`
pub const MAGENTA: (u8, u8, u8) = (170, 0, 170);
/// Index `6`
pub const YELLOW: (u8, u8, u8) = (170, 85, 0);
/// Index `7`
pub const WHITE: (u8, u8, u8) = (170, 170, 170);
// "Bold" VGA 4-bit Colors
/// Index `8`
pub const BLACK_BRIGHT: (u8, u8, u8) = (85, 85, 85);
/// Index `9`
pub const BLUE_BRIGHT: (u8, u8, u8) = (85, 85, 255);
/// Index `10`
pub const GREEN_BRIGHT: (u8, u8, u8) = (85, 255, 85);
/// Index `11`
pub const CYAN_BRIGHT: (u8, u8, u8) = (85, 255, 255);
/// Index `12`
pub const RED_BRIGHT: (u8, u8, u8) = (255, 85, 85);
/// Index `13`
pub const MAGENTA_BRIGHT: (u8, u8, u8) = (255, 85, 255);
/// Index `14`
pub const YELLOW_BRIGHT: (u8, u8, u8) = (255, 255, 85);
/// Index `15`
pub const WHITE_BRIGHT: (u8, u8, u8) = (255, 255, 255);
pub fn get_by_index(index: usize) -> (u8, u8, u8) {
match index {
0 => BLACK,
1 => BLUE,
2 => GREEN,
3 => CYAN,
4 => RED,
5 => MAGENTA,
6 => YELLOW,
7 => WHITE,
8 => BLACK_BRIGHT,
9 => BLUE_BRIGHT,
10 => GREEN_BRIGHT,
11 => CYAN_BRIGHT,
12 => RED_BRIGHT,
13 => MAGENTA_BRIGHT,
14 => YELLOW_BRIGHT,
15 => WHITE_BRIGHT,
_ => BLACK,
}
}

53
src/graphics/whip.rs Normal file
View file

@ -0,0 +1,53 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use crate::{components::*, constants::*, resources::*};
use super::{vga_color as vga, vga_color::get_by_index};
pub fn draw(world: &World, bterm: &mut BTerm) {
let positions = world.read_storage::<Position>();
let wants_to_whips = world.read_storage::<WantsToWhip>();
let map = world.read_resource::<Map>();
let mut rng = RandomNumberGenerator::new();
for (position, wants_to_whip) in (&positions, &wants_to_whips).join() {
let color = RGB::named(get_by_index(rng.range(1, 16)));
let mut rendered_frame = wants_to_whip.frame;
let frame_data = loop {
let frame_data = match rendered_frame {
0 => Some((-1, -1, '\\')),
1 => Some((-1, 0, '─')),
2 => Some((-1, 1, '/')),
3 => Some((0, 1, '│')),
4 => Some((1, 1, '\\')),
5 => Some((1, 0, '─')),
6 => Some((1, -1, '/')),
7 => Some((0, -1, '│')),
_ => None,
};
if let Some(data) = frame_data {
let dest = Point {
x: position.x + data.0,
y: position.y + data.1,
};
if map.in_bounds(dest) {
break frame_data;
}
rendered_frame += 1;
if rendered_frame > 7 {
break None;
}
}
};
if let Some(data) = frame_data {
bterm.set(
(position.x + MAP_X as i32) + data.0,
(position.y + MAP_Y as i32) + data.1,
color,
RGB::named(vga::BLACK),
to_cp437(data.2),
);
};
}
}