initial commit
using rltk
This commit is contained in:
parent
40a2ac07be
commit
d3a09df7a8
29 changed files with 3323 additions and 0 deletions
2287
Cargo.lock
generated
Normal file
2287
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "rust-llyrl"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rltk = { version = "0.8.7" }
|
||||||
|
specs = "0.16.1"
|
||||||
|
specs-derive = "0.4.1"
|
||||||
12
resources/backing.fs
Normal file
12
resources/backing.fs
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec2 TexCoords;
|
||||||
|
|
||||||
|
uniform sampler2D screenTexture;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec3 col = texture(screenTexture, TexCoords).rgb;
|
||||||
|
FragColor = vec4(col, 1.0);
|
||||||
|
}
|
||||||
11
resources/backing.vs
Normal file
11
resources/backing.vs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec2 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoords;
|
||||||
|
|
||||||
|
out vec2 TexCoords;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
TexCoords = aTexCoords;
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
|
||||||
|
}
|
||||||
17
resources/console_no_bg.fs
Normal file
17
resources/console_no_bg.fs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
in vec3 ourBackground;
|
||||||
|
|
||||||
|
// texture sampler
|
||||||
|
uniform sampler2D texture1;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 original = texture(texture1, TexCoord);
|
||||||
|
if (original.r < 0.1f || original.g < 0.1f || original.b < 0.1f) discard;
|
||||||
|
vec4 fg = original * vec4(ourColor, 1.f);
|
||||||
|
FragColor = fg;
|
||||||
|
}
|
||||||
17
resources/console_no_bg.vs
Normal file
17
resources/console_no_bg.vs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec3 bColor;
|
||||||
|
layout (location = 3) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec3 ourBackground;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
ourBackground = bColor;
|
||||||
|
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
|
||||||
|
}
|
||||||
16
resources/console_with_bg.fs
Normal file
16
resources/console_with_bg.fs
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
in vec3 ourBackground;
|
||||||
|
|
||||||
|
// texture sampler
|
||||||
|
uniform sampler2D texture1;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 original = texture(texture1, TexCoord);
|
||||||
|
vec4 fg = original.r > 0.1f || original.g > 0.1f || original.b > 0.1f ? original * vec4(ourColor, 1.f) : vec4(ourBackground, 1.f);
|
||||||
|
FragColor = fg;
|
||||||
|
}
|
||||||
17
resources/console_with_bg.vs
Normal file
17
resources/console_with_bg.vs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec3 bColor;
|
||||||
|
layout (location = 3) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec3 ourBackground;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
ourBackground = bColor;
|
||||||
|
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
|
||||||
|
}
|
||||||
BIN
resources/example_tiles.jpg
Normal file
BIN
resources/example_tiles.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
BIN
resources/example_tiles.xcf
Normal file
BIN
resources/example_tiles.xcf
Normal file
Binary file not shown.
BIN
resources/mltest.xp
Normal file
BIN
resources/mltest.xp
Normal file
Binary file not shown.
BIN
resources/nyan.xp
Normal file
BIN
resources/nyan.xp
Normal file
Binary file not shown.
26
resources/scanlines.fs
Normal file
26
resources/scanlines.fs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec2 TexCoords;
|
||||||
|
|
||||||
|
uniform sampler2D screenTexture;
|
||||||
|
uniform vec3 screenSize;
|
||||||
|
uniform bool screenBurn;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec3 col = texture(screenTexture, TexCoords).rgb;
|
||||||
|
float scanLine = mod(gl_FragCoord.y, 2.0) * 0.25;
|
||||||
|
vec3 scanColor = col.rgb - scanLine;
|
||||||
|
|
||||||
|
if (col.r < 0.1f && col.g < 0.1f && col.b < 0.1f) {
|
||||||
|
if (screenBurn) {
|
||||||
|
float dist = (1.0 - distance(vec2(gl_FragCoord.x / screenSize.x, gl_FragCoord.y / screenSize.y), vec2(0.5,0.5))) * 0.2;
|
||||||
|
FragColor = vec4(0.0, dist, dist, 1.0);
|
||||||
|
} else {
|
||||||
|
FragColor = vec4(0.0, 0.0, 0.0, 1.0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
FragColor = vec4(scanColor, 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
resources/scanlines.vs
Normal file
11
resources/scanlines.vs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec2 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoords;
|
||||||
|
|
||||||
|
out vec2 TexCoords;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
TexCoords = aTexCoords;
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
|
||||||
|
}
|
||||||
BIN
resources/terminal10x10_gs_tc.png
Normal file
BIN
resources/terminal10x10_gs_tc.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.6 KiB |
BIN
resources/terminal8x8.jpg
Normal file
BIN
resources/terminal8x8.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
BIN
resources/vga8x16.jpg
Normal file
BIN
resources/vga8x16.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
66
src/components.rs
Normal file
66
src/components.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
use rltk::RGB;
|
||||||
|
use specs::prelude::*;
|
||||||
|
use specs_derive::*;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Position {
|
||||||
|
pub x: i32,
|
||||||
|
pub y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Renderable {
|
||||||
|
pub glyph: rltk::FontCharType,
|
||||||
|
pub fg: RGB,
|
||||||
|
pub bg: RGB,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Player {}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Monster {}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Viewshed {
|
||||||
|
pub visible_tiles: Vec<rltk::Point>,
|
||||||
|
pub range: i32,
|
||||||
|
pub dirty: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Name {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct BlocksTile {}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct CombatStats {
|
||||||
|
pub max_hp: i32,
|
||||||
|
pub hp: i32,
|
||||||
|
pub defence: i32,
|
||||||
|
pub power: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct WantsToMelee {
|
||||||
|
pub target: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct SufferDamage {
|
||||||
|
pub amount: Vec<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SufferDamage {
|
||||||
|
pub fn new_damage(store: &mut WriteStorage<SufferDamage>, victim: Entity, amount: i32) {
|
||||||
|
if let Some(suffering) = store.get_mut(victim) {
|
||||||
|
suffering.amount.push(amount);
|
||||||
|
} else {
|
||||||
|
let dmg = SufferDamage { amount: vec![amount] };
|
||||||
|
store.insert(victim, dmg).expect("Unable to insert damage.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
50
src/damage_system.rs
Normal file
50
src/damage_system.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
use super::{CombatStats, GameLog, Name, Player, SufferDamage};
|
||||||
|
use rltk::console;
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct DamageSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for DamageSystem {
|
||||||
|
type SystemData = (WriteStorage<'a, CombatStats>, WriteStorage<'a, SufferDamage>);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (mut stats, mut damage) = data;
|
||||||
|
|
||||||
|
for (mut stats, damage) in (&mut stats, &damage).join() {
|
||||||
|
stats.hp -= damage.amount.iter().sum::<i32>();
|
||||||
|
}
|
||||||
|
|
||||||
|
damage.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_the_dead(ecs: &mut World) {
|
||||||
|
let mut dead: Vec<Entity> = Vec::new();
|
||||||
|
// Using scope to make borrow checker happy
|
||||||
|
{
|
||||||
|
let combat_stats = ecs.read_storage::<CombatStats>();
|
||||||
|
let players = ecs.read_storage::<Player>();
|
||||||
|
let names = ecs.read_storage::<Name>();
|
||||||
|
let entities = ecs.entities();
|
||||||
|
let mut log = ecs.write_resource::<GameLog>();
|
||||||
|
for (entity, stats) in (&entities, &combat_stats).join() {
|
||||||
|
if stats.hp < 1 {
|
||||||
|
let player = players.get(entity);
|
||||||
|
match player {
|
||||||
|
None => {
|
||||||
|
let victim_name = names.get(entity);
|
||||||
|
if let Some(victim_name) = victim_name {
|
||||||
|
log.entries.push(format!("{} died!", &victim_name.name));
|
||||||
|
}
|
||||||
|
dead.push(entity)
|
||||||
|
}
|
||||||
|
Some(_) => console::log("You died."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for victim in dead {
|
||||||
|
ecs.delete_entity(victim).expect("Unable to delete.");
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/gamelog.rs
Normal file
3
src/gamelog.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
pub struct GameLog {
|
||||||
|
pub entries: Vec<String>,
|
||||||
|
}
|
||||||
112
src/gui.rs
Normal file
112
src/gui.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
||||||
|
use super::{CombatStats, GameLog, Map, Name, Player, Point, Position};
|
||||||
|
use rltk::{Rltk, RGB};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
||||||
|
ctx.draw_box(0, 43, 79, 6, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
|
||||||
|
|
||||||
|
// Render stats
|
||||||
|
let combat_stats = ecs.read_storage::<CombatStats>();
|
||||||
|
let players = ecs.read_storage::<Player>();
|
||||||
|
for (_player, stats) in (&players, &combat_stats).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(28, 43, 51, stats.hp, stats.max_hp, RGB::named(rltk::RED), RGB::named(rltk::BLACK));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render message log
|
||||||
|
let log = ecs.fetch::<GameLog>();
|
||||||
|
let mut y = 44;
|
||||||
|
for s in log.entries.iter().rev() {
|
||||||
|
if y < 49 {
|
||||||
|
ctx.print(2, y, s);
|
||||||
|
}
|
||||||
|
y += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render 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 map = ecs.fetch::<Map>();
|
||||||
|
let names = ecs.read_storage::<Name>();
|
||||||
|
let positions = ecs.read_storage::<Position>();
|
||||||
|
|
||||||
|
let mouse_pos = ctx.mouse_pos();
|
||||||
|
if mouse_pos.0 >= map.width || mouse_pos.1 >= map.height {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let mut tooltip: Vec<String> = Vec::new();
|
||||||
|
for (name, position) in (&names, &positions).join() {
|
||||||
|
let idx = map.xy_idx(position.x, position.y);
|
||||||
|
if position.x == mouse_pos.0 && position.y == mouse_pos.1 && map.visible_tiles[idx] {
|
||||||
|
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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
176
src/main.rs
Normal file
176
src/main.rs
Normal file
|
|
@ -0,0 +1,176 @@
|
||||||
|
use rltk::{GameState, Point, Rltk, RGB};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
mod components;
|
||||||
|
pub use components::*;
|
||||||
|
mod map;
|
||||||
|
pub use map::*;
|
||||||
|
mod player;
|
||||||
|
use player::*;
|
||||||
|
mod rect;
|
||||||
|
pub use rect::Rect;
|
||||||
|
mod gamelog;
|
||||||
|
use gamelog::GameLog;
|
||||||
|
mod gui;
|
||||||
|
mod visibility_system;
|
||||||
|
use visibility_system::VisibilitySystem;
|
||||||
|
mod monster_ai_system;
|
||||||
|
use monster_ai_system::MonsterAI;
|
||||||
|
mod map_indexing_system;
|
||||||
|
use map_indexing_system::MapIndexingSystem;
|
||||||
|
mod damage_system;
|
||||||
|
use damage_system::*;
|
||||||
|
mod melee_combat_system;
|
||||||
|
use melee_combat_system::MeleeCombatSystem;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum RunState {
|
||||||
|
AwaitingInput,
|
||||||
|
PreRun,
|
||||||
|
PlayerTurn,
|
||||||
|
MonsterTurn,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct State {
|
||||||
|
pub ecs: World,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
fn run_systems(&mut self) {
|
||||||
|
let mut vis = VisibilitySystem {};
|
||||||
|
vis.run_now(&self.ecs);
|
||||||
|
let mut mob = MonsterAI {};
|
||||||
|
mob.run_now(&self.ecs);
|
||||||
|
let mut mapindex = MapIndexingSystem {};
|
||||||
|
mapindex.run_now(&self.ecs);
|
||||||
|
let mut melee_system = MeleeCombatSystem {};
|
||||||
|
melee_system.run_now(&self.ecs);
|
||||||
|
let mut damage_system = DamageSystem {};
|
||||||
|
damage_system.run_now(&self.ecs);
|
||||||
|
self.ecs.maintain();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameState for State {
|
||||||
|
fn tick(&mut self, ctx: &mut Rltk) {
|
||||||
|
ctx.cls();
|
||||||
|
|
||||||
|
let mut new_runstate;
|
||||||
|
{
|
||||||
|
let runstate = self.ecs.fetch::<RunState>();
|
||||||
|
new_runstate = *runstate;
|
||||||
|
}
|
||||||
|
|
||||||
|
match new_runstate {
|
||||||
|
RunState::PreRun => {
|
||||||
|
self.run_systems();
|
||||||
|
new_runstate = RunState::AwaitingInput;
|
||||||
|
}
|
||||||
|
RunState::AwaitingInput => {
|
||||||
|
new_runstate = player_input(self, ctx);
|
||||||
|
}
|
||||||
|
RunState::PlayerTurn => {
|
||||||
|
self.run_systems();
|
||||||
|
new_runstate = RunState::MonsterTurn;
|
||||||
|
}
|
||||||
|
RunState::MonsterTurn => {
|
||||||
|
self.run_systems();
|
||||||
|
new_runstate = RunState::AwaitingInput;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut runwriter = self.ecs.write_resource::<RunState>();
|
||||||
|
*runwriter = new_runstate;
|
||||||
|
}
|
||||||
|
|
||||||
|
damage_system::delete_the_dead(&mut self.ecs);
|
||||||
|
draw_map(&self.ecs, ctx);
|
||||||
|
|
||||||
|
let positions = self.ecs.read_storage::<Position>();
|
||||||
|
let renderables = self.ecs.read_storage::<Renderable>();
|
||||||
|
let map = self.ecs.fetch::<Map>();
|
||||||
|
|
||||||
|
for (pos, render) in (&positions, &renderables).join() {
|
||||||
|
let idx = map.xy_idx(pos.x, pos.y);
|
||||||
|
if map.visible_tiles[idx] {
|
||||||
|
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gui::draw_ui(&self.ecs, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> rltk::BError {
|
||||||
|
use rltk::RltkBuilder;
|
||||||
|
let mut context = RltkBuilder::simple80x50()
|
||||||
|
.with_gutter(0)
|
||||||
|
.with_tile_dimensions(16, 16)
|
||||||
|
//.with_fitscreen(true)
|
||||||
|
.with_title("Roguelike Tutorial")
|
||||||
|
.build()?;
|
||||||
|
context.with_post_scanlines(true);
|
||||||
|
let mut gs = State { ecs: World::new() };
|
||||||
|
|
||||||
|
gs.ecs.register::<Position>();
|
||||||
|
gs.ecs.register::<Renderable>();
|
||||||
|
gs.ecs.register::<Player>();
|
||||||
|
gs.ecs.register::<Monster>();
|
||||||
|
gs.ecs.register::<Viewshed>();
|
||||||
|
gs.ecs.register::<Name>();
|
||||||
|
gs.ecs.register::<BlocksTile>();
|
||||||
|
gs.ecs.register::<CombatStats>();
|
||||||
|
gs.ecs.register::<WantsToMelee>();
|
||||||
|
gs.ecs.register::<SufferDamage>();
|
||||||
|
|
||||||
|
let map = Map::new_map_rooms_and_corridors();
|
||||||
|
let (player_x, player_y) = map.rooms[0].centre();
|
||||||
|
|
||||||
|
let player_entity = gs
|
||||||
|
.ecs
|
||||||
|
.create_entity()
|
||||||
|
.with(Position { x: player_x, y: player_y })
|
||||||
|
.with(Renderable { glyph: rltk::to_cp437('@'), fg: RGB::named(rltk::YELLOW), bg: RGB::named(rltk::BLACK) })
|
||||||
|
.with(Player {})
|
||||||
|
.with(Viewshed { visible_tiles: Vec::new(), range: 8, dirty: true })
|
||||||
|
.with(Name { name: "Player".to_string() })
|
||||||
|
.with(CombatStats { max_hp: 30, hp: 30, defence: 2, power: 5 })
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let mut rng = rltk::RandomNumberGenerator::new();
|
||||||
|
for (i, room) in map.rooms.iter().skip(1).enumerate() {
|
||||||
|
let (x, y) = room.centre();
|
||||||
|
let glyph: rltk::FontCharType;
|
||||||
|
let name: String;
|
||||||
|
let roll = rng.roll_dice(1, 2);
|
||||||
|
match roll {
|
||||||
|
1 => {
|
||||||
|
glyph = rltk::to_cp437('g');
|
||||||
|
name = "Goblin".to_string();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
glyph = rltk::to_cp437('o');
|
||||||
|
name = "Orc".to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gs.ecs
|
||||||
|
.create_entity()
|
||||||
|
.with(Position { x, y })
|
||||||
|
.with(Renderable { glyph: glyph, fg: RGB::named(rltk::RED), bg: RGB::named(rltk::BLACK) })
|
||||||
|
.with(Viewshed { visible_tiles: Vec::new(), range: 8, dirty: true })
|
||||||
|
.with(Monster {})
|
||||||
|
.with(Name { name: format!("{} #{}", &name, i) })
|
||||||
|
.with(BlocksTile {})
|
||||||
|
.with(CombatStats { max_hp: 16, hp: 16, defence: 1, power: 4 })
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
gs.ecs.insert(map);
|
||||||
|
gs.ecs.insert(Point::new(player_x, player_y));
|
||||||
|
gs.ecs.insert(player_entity);
|
||||||
|
gs.ecs.insert(gamelog::GameLog { entries: vec!["Here's your welcome message.".to_string()] });
|
||||||
|
gs.ecs.insert(RunState::PreRun);
|
||||||
|
|
||||||
|
rltk::main_loop(context, gs)
|
||||||
|
}
|
||||||
224
src/map.rs
Normal file
224
src/map.rs
Normal file
|
|
@ -0,0 +1,224 @@
|
||||||
|
use super::Rect;
|
||||||
|
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, RGB};
|
||||||
|
use specs::prelude::*;
|
||||||
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum TileType {
|
||||||
|
Wall,
|
||||||
|
Floor,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const MAPWIDTH: usize = 80;
|
||||||
|
pub const MAPHEIGHT: usize = 43;
|
||||||
|
const MAPCOUNT: usize = MAPHEIGHT * MAPWIDTH;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Map {
|
||||||
|
pub tiles: Vec<TileType>,
|
||||||
|
pub rooms: Vec<Rect>,
|
||||||
|
pub width: i32,
|
||||||
|
pub height: i32,
|
||||||
|
pub revealed_tiles: Vec<bool>,
|
||||||
|
pub visible_tiles: Vec<bool>,
|
||||||
|
pub blocked: Vec<bool>,
|
||||||
|
pub tile_content: Vec<Vec<Entity>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Map {
|
||||||
|
pub fn xy_idx(&self, x: i32, y: i32) -> usize {
|
||||||
|
(y as usize) * (self.width as usize) + (x as usize)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_room_to_map(&mut self, room: &Rect) {
|
||||||
|
for y in room.y1 + 1..=room.y2 {
|
||||||
|
for x in room.x1 + 1..=room.x2 {
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
|
self.tiles[idx] = TileType::Floor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
|
||||||
|
for x in min(x1, x2)..=max(x1, x2) {
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
|
if idx > 0 && idx < (self.width as usize) * (self.height as usize) {
|
||||||
|
self.tiles[idx as usize] = TileType::Floor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_vertical_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
|
||||||
|
for y in min(y1, y2)..=max(y1, y2) {
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
|
if idx > 0 && idx < (self.width as usize) * (self.height as usize) {
|
||||||
|
self.tiles[idx as usize] = TileType::Floor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Takes an index, and calculates if it can be entered.
|
||||||
|
fn is_exit_valid(&self, x: i32, y: i32) -> bool {
|
||||||
|
if x < 1 || x > self.width - 1 || y < 1 || y > self.height - 1 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
|
!self.blocked[idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn populate_blocked(&mut self) {
|
||||||
|
for (i, tile) in self.tiles.iter_mut().enumerate() {
|
||||||
|
self.blocked[i] = *tile == TileType::Wall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear_content_index(&mut self) {
|
||||||
|
for content in self.tile_content.iter_mut() {
|
||||||
|
content.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Makes a procgen map out of rooms and corridors, and returns the rooms and the map.
|
||||||
|
pub fn new_map_rooms_and_corridors() -> Map {
|
||||||
|
let mut map = Map {
|
||||||
|
tiles: vec![TileType::Wall; MAPCOUNT],
|
||||||
|
rooms: Vec::new(),
|
||||||
|
width: MAPWIDTH as i32,
|
||||||
|
height: MAPHEIGHT as i32,
|
||||||
|
revealed_tiles: vec![false; MAPCOUNT],
|
||||||
|
visible_tiles: vec![false; MAPCOUNT],
|
||||||
|
blocked: vec![false; MAPCOUNT],
|
||||||
|
tile_content: vec![Vec::new(); MAPCOUNT],
|
||||||
|
};
|
||||||
|
|
||||||
|
const MAX_ROOMS: i32 = 30;
|
||||||
|
const MIN_SIZE: i32 = 6;
|
||||||
|
const MAX_SIZE: i32 = 10;
|
||||||
|
|
||||||
|
let mut rng = RandomNumberGenerator::new();
|
||||||
|
|
||||||
|
for _i in 0..MAX_ROOMS {
|
||||||
|
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
||||||
|
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
||||||
|
let x = rng.roll_dice(1, map.width - w - 1) - 1;
|
||||||
|
let y = rng.roll_dice(1, map.height - h - 1) - 1;
|
||||||
|
let new_room = Rect::new(x, y, w, h);
|
||||||
|
let mut ok = true;
|
||||||
|
for other_room in map.rooms.iter() {
|
||||||
|
if new_room.intersect(other_room) {
|
||||||
|
ok = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
map.apply_room_to_map(&new_room);
|
||||||
|
|
||||||
|
if !map.rooms.is_empty() {
|
||||||
|
let (new_x, new_y) = new_room.centre();
|
||||||
|
let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].centre();
|
||||||
|
if rng.range(0, 2) == 1 {
|
||||||
|
map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
|
||||||
|
map.apply_vertical_tunnel(prev_y, new_y, prev_x);
|
||||||
|
} else {
|
||||||
|
map.apply_vertical_tunnel(prev_y, new_y, prev_x);
|
||||||
|
map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
map.rooms.push(new_room);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
map
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Algorithm2D for Map {
|
||||||
|
fn dimensions(&self) -> Point {
|
||||||
|
Point::new(self.width, self.height)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BaseMap for Map {
|
||||||
|
fn is_opaque(&self, idx: usize) -> bool {
|
||||||
|
self.tiles[idx as usize] == TileType::Wall
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_pathing_distance(&self, idx1: usize, idx2: usize) -> f32 {
|
||||||
|
let w = self.width as usize;
|
||||||
|
let p1 = Point::new(idx1 % w, idx1 / w);
|
||||||
|
let p2 = Point::new(idx2 % w, idx2 / w);
|
||||||
|
rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Evaluate every possible exit from a given tile in a cardinal direction, and return it as a vector.
|
||||||
|
fn get_available_exits(&self, idx: usize) -> rltk::SmallVec<[(usize, f32); 10]> {
|
||||||
|
let mut exits = rltk::SmallVec::new();
|
||||||
|
let x = (idx as i32) % self.width;
|
||||||
|
let y = (idx as i32) / self.width;
|
||||||
|
let w = self.width as usize;
|
||||||
|
|
||||||
|
// Cardinal directions
|
||||||
|
if self.is_exit_valid(x - 1, y) {
|
||||||
|
exits.push((idx - 1, 1.0));
|
||||||
|
}
|
||||||
|
if self.is_exit_valid(x + 1, y) {
|
||||||
|
exits.push((idx + 1, 1.0));
|
||||||
|
}
|
||||||
|
if self.is_exit_valid(x, y - 1) {
|
||||||
|
exits.push((idx - w, 1.0));
|
||||||
|
}
|
||||||
|
if self.is_exit_valid(x, y + 1) {
|
||||||
|
exits.push((idx + w, 1.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Diagonals
|
||||||
|
if self.is_exit_valid(x - 1, y - 1) {
|
||||||
|
exits.push((idx - w - 1, 1.45));
|
||||||
|
}
|
||||||
|
if self.is_exit_valid(x + 1, y - 1) {
|
||||||
|
exits.push((idx - w + 1, 1.45));
|
||||||
|
}
|
||||||
|
if self.is_exit_valid(x - 1, y + 1) {
|
||||||
|
exits.push((idx - w - 1, 1.45));
|
||||||
|
}
|
||||||
|
if self.is_exit_valid(x + 1, y + 1) {
|
||||||
|
exits.push((idx + w + 1, 1.45));
|
||||||
|
}
|
||||||
|
|
||||||
|
exits
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||||
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
|
let mut y = 0;
|
||||||
|
let mut x = 0;
|
||||||
|
for (idx, tile) in map.tiles.iter().enumerate() {
|
||||||
|
if map.revealed_tiles[idx] {
|
||||||
|
let glyph;
|
||||||
|
let mut fg;
|
||||||
|
match tile {
|
||||||
|
TileType::Floor => {
|
||||||
|
glyph = rltk::to_cp437('.');
|
||||||
|
fg = RGB::from_f32(0.0, 1.0, 0.5);
|
||||||
|
}
|
||||||
|
TileType::Wall => {
|
||||||
|
glyph = rltk::to_cp437('#');
|
||||||
|
fg = RGB::from_f32(0.0, 1.0, 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !map.visible_tiles[idx] {
|
||||||
|
fg = fg.to_greyscale();
|
||||||
|
}
|
||||||
|
ctx.set(x, y, fg, RGB::from_f32(0.0, 0.0, 0.0), glyph);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move the coordinates
|
||||||
|
x += 1;
|
||||||
|
if x > MAPWIDTH - 1 {
|
||||||
|
x = 0;
|
||||||
|
y += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/map_indexing_system.rs
Normal file
26
src/map_indexing_system.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
use super::{BlocksTile, Map, Position};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct MapIndexingSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for MapIndexingSystem {
|
||||||
|
type SystemData = (WriteExpect<'a, Map>, ReadStorage<'a, Position>, ReadStorage<'a, BlocksTile>, Entities<'a>);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (mut map, position, blockers, entities) = data;
|
||||||
|
|
||||||
|
map.populate_blocked();
|
||||||
|
map.clear_content_index();
|
||||||
|
for (entity, position) in (&entities, &position).join() {
|
||||||
|
let idx = map.xy_idx(position.x, position.y);
|
||||||
|
|
||||||
|
let _p: Option<&BlocksTile> = blockers.get(entity);
|
||||||
|
if let Some(_p) = _p {
|
||||||
|
map.blocked[idx] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push the entity to the appropriate index slot.
|
||||||
|
map.tile_content[idx].push(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/melee_combat_system.rs
Normal file
41
src/melee_combat_system.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
use super::{gamelog::GameLog, CombatStats, Name, SufferDamage, WantsToMelee};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct MeleeCombatSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for MeleeCombatSystem {
|
||||||
|
type SystemData = (
|
||||||
|
Entities<'a>,
|
||||||
|
WriteExpect<'a, GameLog>,
|
||||||
|
WriteStorage<'a, WantsToMelee>,
|
||||||
|
ReadStorage<'a, Name>,
|
||||||
|
ReadStorage<'a, CombatStats>,
|
||||||
|
WriteStorage<'a, SufferDamage>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (entities, mut log, mut wants_melee, names, combat_stats, mut inflict_damage) = data;
|
||||||
|
|
||||||
|
for (_entity, wants_melee, name, stats) in (&entities, &wants_melee, &names, &combat_stats).join() {
|
||||||
|
if stats.hp <= 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let target_stats = combat_stats.get(wants_melee.target).unwrap();
|
||||||
|
if target_stats.hp <= 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let target_name = names.get(wants_melee.target).unwrap();
|
||||||
|
let damage = i32::max(0, stats.power - target_stats.defence);
|
||||||
|
|
||||||
|
if damage == 0 {
|
||||||
|
log.entries.push(format!("{} is unable to hurt {}.", &name.name, &target_name.name));
|
||||||
|
} else {
|
||||||
|
log.entries.push(format!("{} hits {} for {} damage.", &name.name, &target_name.name, damage));
|
||||||
|
SufferDamage::new_damage(&mut inflict_damage, wants_melee.target, damage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wants_melee.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/monster_ai_system.rs
Normal file
62
src/monster_ai_system.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
use super::{Map, Monster, Position, RunState, Viewshed, WantsToMelee};
|
||||||
|
use rltk::Point;
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct MonsterAI {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for MonsterAI {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
type SystemData = (
|
||||||
|
WriteExpect<'a, Map>,
|
||||||
|
ReadExpect<'a, Point>,
|
||||||
|
ReadExpect<'a, Entity>,
|
||||||
|
ReadExpect<'a, RunState>,
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, Viewshed>,
|
||||||
|
ReadStorage<'a, Monster>,
|
||||||
|
WriteStorage<'a, Position>,
|
||||||
|
WriteStorage<'a, WantsToMelee>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (
|
||||||
|
mut map,
|
||||||
|
player_pos,
|
||||||
|
player_entity,
|
||||||
|
runstate,
|
||||||
|
entities,
|
||||||
|
mut viewshed,
|
||||||
|
monster,
|
||||||
|
mut position,
|
||||||
|
mut wants_to_melee,
|
||||||
|
) = data;
|
||||||
|
|
||||||
|
if *runstate != RunState::MonsterTurn {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (entity, mut viewshed, _monster, mut pos) in (&entities, &mut viewshed, &monster, &mut position).join() {
|
||||||
|
let distance = rltk::DistanceAlg::Pythagoras.distance2d(Point::new(pos.x, pos.y), *player_pos);
|
||||||
|
if distance < 1.5 {
|
||||||
|
wants_to_melee
|
||||||
|
.insert(entity, WantsToMelee { target: *player_entity })
|
||||||
|
.expect("Unable to insert attack.");
|
||||||
|
} else if viewshed.visible_tiles.contains(&*player_pos) {
|
||||||
|
let path = rltk::a_star_search(
|
||||||
|
map.xy_idx(pos.x, pos.y) as i32,
|
||||||
|
map.xy_idx(player_pos.x, player_pos.y) as i32,
|
||||||
|
&mut *map,
|
||||||
|
);
|
||||||
|
if path.success && path.steps.len() > 1 {
|
||||||
|
let mut idx = map.xy_idx(pos.x, pos.y);
|
||||||
|
map.blocked[idx] = false;
|
||||||
|
pos.x = (path.steps[1] as i32) % map.width;
|
||||||
|
pos.y = (path.steps[1] as i32) / map.width;
|
||||||
|
idx = map.xy_idx(pos.x, pos.y);
|
||||||
|
map.blocked[idx] = true;
|
||||||
|
viewshed.dirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
76
src/player.rs
Normal file
76
src/player.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
use super::{CombatStats, Map, Player, Position, RunState, State, Viewshed, WantsToMelee, MAPHEIGHT, MAPWIDTH};
|
||||||
|
use rltk::{Point, Rltk, VirtualKeyCode};
|
||||||
|
use specs::prelude::*;
|
||||||
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
|
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||||
|
let mut positions = ecs.write_storage::<Position>();
|
||||||
|
let mut players = ecs.write_storage::<Player>();
|
||||||
|
let mut viewsheds = ecs.write_storage::<Viewshed>();
|
||||||
|
let combat_stats = ecs.read_storage::<CombatStats>();
|
||||||
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
|
let entities = ecs.entities();
|
||||||
|
let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();
|
||||||
|
|
||||||
|
for (entity, _player, pos, viewshed) in (&entities, &mut players, &mut positions, &mut viewsheds).join() {
|
||||||
|
if pos.x + delta_x < 1
|
||||||
|
|| pos.x + delta_x > map.width - 1
|
||||||
|
|| pos.y + delta_y < 1
|
||||||
|
|| pos.y + delta_y > map.height - 1
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||||
|
|
||||||
|
for potential_target in map.tile_content[destination_idx].iter() {
|
||||||
|
let target = combat_stats.get(*potential_target);
|
||||||
|
if let Some(_target) = target {
|
||||||
|
wants_to_melee.insert(entity, WantsToMelee { target: *potential_target }).expect("Add target failed.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !map.blocked[destination_idx] {
|
||||||
|
pos.x = min((MAPWIDTH as i32) - 1, max(0, pos.x + delta_x));
|
||||||
|
pos.y = min((MAPHEIGHT as i32) - 1, max(0, pos.y + delta_y));
|
||||||
|
viewshed.dirty = true;
|
||||||
|
let mut ppos = ecs.write_resource::<Point>();
|
||||||
|
ppos.x = pos.x;
|
||||||
|
ppos.y = pos.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
||||||
|
// Player movement
|
||||||
|
match ctx.key {
|
||||||
|
None => {
|
||||||
|
return RunState::AwaitingInput;
|
||||||
|
}
|
||||||
|
Some(key) => match key {
|
||||||
|
// Cardinals
|
||||||
|
VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H => {
|
||||||
|
try_move_player(-1, 0, &mut gs.ecs);
|
||||||
|
}
|
||||||
|
VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::L => {
|
||||||
|
try_move_player(1, 0, &mut gs.ecs);
|
||||||
|
}
|
||||||
|
VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::K => {
|
||||||
|
try_move_player(0, -1, &mut gs.ecs);
|
||||||
|
}
|
||||||
|
VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::J => {
|
||||||
|
try_move_player(0, 1, &mut gs.ecs);
|
||||||
|
}
|
||||||
|
// Diagonals
|
||||||
|
VirtualKeyCode::Numpad9 | VirtualKeyCode::Y => try_move_player(1, -1, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::Numpad7 | VirtualKeyCode::U => try_move_player(-1, -1, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::Numpad3 | VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::Numpad1 | VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
|
||||||
|
_ => {
|
||||||
|
return RunState::PlayerTurn;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
RunState::PlayerTurn
|
||||||
|
}
|
||||||
21
src/rect.rs
Normal file
21
src/rect.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
pub struct Rect {
|
||||||
|
pub x1: i32,
|
||||||
|
pub x2: i32,
|
||||||
|
pub y1: i32,
|
||||||
|
pub y2: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Rect {
|
||||||
|
pub fn new(x: i32, y: i32, w: i32, h: i32) -> Rect {
|
||||||
|
Rect { x1: x, y1: y, x2: x + w, y2: y + h }
|
||||||
|
}
|
||||||
|
|
||||||
|
//Returns true if this overlaps with other
|
||||||
|
pub fn intersect(&self, other: &Rect) -> bool {
|
||||||
|
self.x1 <= other.x2 && self.x2 >= other.x1 && self.y1 <= other.y2 && self.y2 >= other.y1
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn centre(&self) -> (i32, i32) {
|
||||||
|
((self.x1 + self.x2) / 2, (self.y1 + self.y2) / 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/visibility_system.rs
Normal file
41
src/visibility_system.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
use super::{Map, Player, Position, Viewshed};
|
||||||
|
use rltk::{field_of_view, Point};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct VisibilitySystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for VisibilitySystem {
|
||||||
|
type SystemData = (
|
||||||
|
WriteExpect<'a, Map>,
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, Viewshed>,
|
||||||
|
WriteStorage<'a, Position>,
|
||||||
|
ReadStorage<'a, Player>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (mut map, entities, mut viewshed, pos, player) = data;
|
||||||
|
|
||||||
|
for (ent, viewshed, pos) in (&entities, &mut viewshed, &pos).join() {
|
||||||
|
if viewshed.dirty {
|
||||||
|
viewshed.dirty = false;
|
||||||
|
viewshed.visible_tiles.clear();
|
||||||
|
viewshed.visible_tiles = field_of_view(Point::new(pos.x, pos.y), viewshed.range, &*map);
|
||||||
|
viewshed.visible_tiles.retain(|p| p.x >= 0 && p.x < map.width && p.y >= 0 && p.y < map.height);
|
||||||
|
|
||||||
|
// If this is the player, reveal what they can see
|
||||||
|
let _p: Option<&Player> = player.get(ent);
|
||||||
|
if let Some(_p) = _p {
|
||||||
|
for t in map.visible_tiles.iter_mut() {
|
||||||
|
*t = false;
|
||||||
|
}
|
||||||
|
for vis in viewshed.visible_tiles.iter() {
|
||||||
|
let idx = map.xy_idx(vis.x, vis.y);
|
||||||
|
map.revealed_tiles[idx] = true;
|
||||||
|
map.visible_tiles[idx] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue