Compare commits
34 Commits
cefb0cc5f2
...
new_level_
| Author | SHA1 | Date | |
|---|---|---|---|
| 3249529f94 | |||
| 72f7be2ed8 | |||
| 5311f56ca0 | |||
| df80dfdd8a | |||
| 8b23c565b8 | |||
| 789a41cb3f | |||
| d42d8a12b4 | |||
| 8ee142586b | |||
| 1878959e69 | |||
| 8bae1c7668 | |||
| 3096386ad1 | |||
| f5d257e826 | |||
| a82a847ecd | |||
| 50d98bfb4d | |||
| c429784775 | |||
| e96660cea0 | |||
| c6492c28c2 | |||
| 7f288dbcd1 | |||
| 74831857cf | |||
| 1ae7ff45ab | |||
| b833b43c7c | |||
| e490011b4e | |||
| 8267ad083f | |||
| 38dc7285c2 | |||
| 6f4c37728f | |||
| 5111fec1fa | |||
| 9fb3c83b67 | |||
| 9ae713b6c8 | |||
| 5949b2a2d6 | |||
| 2b8f7eebba | |||
| a0635de65a | |||
| d6f4fdaa5b | |||
| 8dfe56a3c2 | |||
| b3d85ac7b3 |
16
.gitea/workflows/non_main.yaml
Normal file
16
.gitea/workflows/non_main.yaml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
name: publish package
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches-ignore:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Explore-Gitea-Actions:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out repository code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: run tests
|
||||||
|
run: |
|
||||||
|
cd ${{ gitea.workspace }}
|
||||||
|
cargo test
|
||||||
24
.gitea/workflows/publish.yaml
Normal file
24
.gitea/workflows/publish.yaml
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
name: publish package
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Explore-Gitea-Actions:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out repository code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: run tests
|
||||||
|
run: |
|
||||||
|
cd ${{ gitea.workspace }}
|
||||||
|
cargo test --release
|
||||||
|
- name: build & release package
|
||||||
|
run: |
|
||||||
|
cd ${{ gitea.workspace }}
|
||||||
|
cargo build --release
|
||||||
|
cargo deb
|
||||||
|
ls -al target/debian
|
||||||
|
UPLOAD_FILE=$(ls target/debian/*.deb)
|
||||||
|
curl --user jlusiardi:${{ secrets.UPLOAD_TOKEN }} --upload-file ${UPLOAD_FILE} https://gitea.intern.haus-in-hirschaid.de/api/packages/jlusiardi/debian/pool/bionic/main/upload
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
*.profraw
|
||||||
|
|||||||
76
Cargo.lock
generated
76
Cargo.lock
generated
@@ -104,6 +104,41 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling"
|
||||||
|
version = "0.20.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"darling_macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_core"
|
||||||
|
version = "0.20.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
|
||||||
|
dependencies = [
|
||||||
|
"fnv",
|
||||||
|
"ident_case",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"strsim",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_macro"
|
||||||
|
version = "0.20.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "either"
|
name = "either"
|
||||||
version = "1.9.0"
|
version = "1.9.0"
|
||||||
@@ -115,6 +150,7 @@ name = "el_diabolo"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crossterm",
|
"crossterm",
|
||||||
|
"macros",
|
||||||
"petgraph",
|
"petgraph",
|
||||||
"rand",
|
"rand",
|
||||||
"ratatui",
|
"ratatui",
|
||||||
@@ -143,6 +179,12 @@ version = "0.4.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
|
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fnv"
|
||||||
|
version = "1.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.11"
|
version = "0.2.11"
|
||||||
@@ -176,6 +218,12 @@ version = "0.3.9"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ident_case"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "2.1.0"
|
version = "2.1.0"
|
||||||
@@ -257,6 +305,16 @@ dependencies = [
|
|||||||
"hashbrown",
|
"hashbrown",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"darling",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
@@ -323,18 +381,18 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.69"
|
version = "1.0.89"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
|
checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.33"
|
version = "1.0.37"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
@@ -481,6 +539,12 @@ version = "1.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strum"
|
name = "strum"
|
||||||
version = "0.26.3"
|
version = "0.26.3"
|
||||||
@@ -505,9 +569,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.38"
|
version = "2.0.85"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b"
|
checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
|||||||
15
Cargo.toml
15
Cargo.toml
@@ -1,3 +1,6 @@
|
|||||||
|
[workspace]
|
||||||
|
members = ["macros"]
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "el_diabolo"
|
name = "el_diabolo"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@@ -11,3 +14,15 @@ crossterm = "0.28.1"
|
|||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
petgraph = "0.6.5"
|
petgraph = "0.6.5"
|
||||||
whoami = "1.5.2"
|
whoami = "1.5.2"
|
||||||
|
macros = { path = "./macros" }
|
||||||
|
|
||||||
|
[package.metadata.deb]
|
||||||
|
maintainer = "Joachim Lusiardi <joachim@lusiardi.de>"
|
||||||
|
copyright = "2024, Joachim Lusiardi <joachim@lusiardi.de>"
|
||||||
|
extended-description = """Little roguelike game"""
|
||||||
|
depends = "$auto"
|
||||||
|
section = "game"
|
||||||
|
priority = "optional"
|
||||||
|
assets = [
|
||||||
|
["target/release/el_diabolo", "/usr/bin/", "755"],
|
||||||
|
]
|
||||||
10
coverage.md
Normal file
10
coverage.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
```
|
||||||
|
cargo install grcov
|
||||||
|
rustup component add llvm-tools-preview
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
RUSTFLAGS="-Cinstrument-coverage" cargo clean
|
||||||
|
RUSTFLAGS="-Cinstrument-coverage" cargo test
|
||||||
|
grcov . --binary-path ./target/debug/deps/ -s . -t html --branch --ignore-not-existing --ignore '../*' --ignore "/*" -o target/coverage/html
|
||||||
|
```
|
||||||
17
macros/Cargo.toml
Normal file
17
macros/Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
[package]
|
||||||
|
name = "macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
[lib]
|
||||||
|
name = "macros"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
syn = "2.0.85"
|
||||||
|
quote = "1.0.37"
|
||||||
|
proc-macro2 = "1.0.89"
|
||||||
|
darling= "0.20.10"
|
||||||
57
macros/src/lib.rs
Normal file
57
macros/src/lib.rs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
extern crate proc_macro;
|
||||||
|
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
#[proc_macro_derive(CreateMonsters)]
|
||||||
|
pub fn create_monsters(input: TokenStream) -> TokenStream {
|
||||||
|
let input = syn::parse_macro_input!(input as syn::File);
|
||||||
|
let (enum_item, _) = match &input.items[0] {
|
||||||
|
syn::Item::Enum(enum_item) => (enum_item, &enum_item.attrs),
|
||||||
|
_ => panic!("must be an enum"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let structs = enum_item.variants.iter().map(|variant| {
|
||||||
|
let variant_name = &variant.ident;
|
||||||
|
|
||||||
|
let t = quote! {
|
||||||
|
pub struct #variant_name {
|
||||||
|
name: String,
|
||||||
|
life: usize,
|
||||||
|
position: Position,
|
||||||
|
symbol: String,
|
||||||
|
color: Color,
|
||||||
|
experience_gain: usize,
|
||||||
|
ticks_between_steps: u128,
|
||||||
|
damage_range: RangeInclusive<usize>,
|
||||||
|
}
|
||||||
|
impl Monster for #variant_name {
|
||||||
|
fn get_name(&self) -> &str { &self.name }
|
||||||
|
fn is_dead(&self) -> bool { self.life <= 0 }
|
||||||
|
fn get_experience_gain(&self) -> usize { self.experience_gain }
|
||||||
|
fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) }
|
||||||
|
fn decrease_life(&mut self, by: usize) {
|
||||||
|
self.life = self.life.saturating_sub(by);
|
||||||
|
}
|
||||||
|
fn get_ticks_between_steps(&self) -> u128 { self.ticks_between_steps }
|
||||||
|
fn damage(&self) -> usize { rand::thread_rng().gen_range(self.damage_range.clone()) }
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
fn get_life(&self) -> usize { self.life }
|
||||||
|
}
|
||||||
|
impl HasPosition for #variant_name {
|
||||||
|
fn get_position(&mut self) -> &mut Position {
|
||||||
|
&mut self.position
|
||||||
|
}
|
||||||
|
fn get_immutable_position(&self) -> &Position {
|
||||||
|
&self.position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
t
|
||||||
|
});
|
||||||
|
let output = quote::quote! {
|
||||||
|
#(#structs)*
|
||||||
|
};
|
||||||
|
output.into()
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use rand::Rng;
|
||||||
use ratatui::style::Color;
|
use ratatui::style::Color;
|
||||||
|
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
@@ -5,6 +6,7 @@ use crate::position::Position;
|
|||||||
|
|
||||||
pub trait Artifact {
|
pub trait Artifact {
|
||||||
//! An artifact that can be collected by the player
|
//! An artifact that can be collected by the player
|
||||||
|
/// get the character and color used to draw the artifact into the level
|
||||||
fn get_representation(&self) -> (&str, Color);
|
fn get_representation(&self) -> (&str, Color);
|
||||||
/// get the position of the artifact in the level
|
/// get the position of the artifact in the level
|
||||||
fn get_immutable_position(&self) -> &Position;
|
fn get_immutable_position(&self) -> &Position;
|
||||||
@@ -14,26 +16,30 @@ pub trait Artifact {
|
|||||||
fn was_collected(&self) -> bool;
|
fn was_collected(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An artifact that contains a random amount of gold pieces.
|
||||||
pub struct Chest {
|
pub struct Chest {
|
||||||
/// a chest that contains some gold
|
/// the chests position
|
||||||
position: Position,
|
position: Position,
|
||||||
|
/// the chests value
|
||||||
gold: usize,
|
gold: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Chest {
|
impl Chest {
|
||||||
|
/// create a chest at the given position with a random amount of gold.
|
||||||
|
/// The gold amount depends on the level, the deeper you go, the more you get.
|
||||||
pub fn new(position: Position) -> Self {
|
pub fn new(position: Position) -> Self {
|
||||||
let level = position.get_level();
|
let min_gold = 10 * (position.get_level() + 1);
|
||||||
|
let max_gold = min_gold + 10 * position.get_level();
|
||||||
Self {
|
Self {
|
||||||
position,
|
position,
|
||||||
// TODO maybe randomize this?
|
gold: rand::thread_rng().gen_range(min_gold..=max_gold),
|
||||||
gold: (level + 1) * 10,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Artifact for Chest {
|
impl Artifact for Chest {
|
||||||
fn get_representation(&self) -> (&str, Color) {
|
fn get_representation(&self) -> (&str, Color) {
|
||||||
("C", Color::Blue)
|
("C", Color::Cyan)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_immutable_position(&self) -> &Position {
|
fn get_immutable_position(&self) -> &Position {
|
||||||
@@ -53,48 +59,64 @@ impl Artifact for Chest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
/// An artifact that gives the player some health on consumption.
|
||||||
pub struct Potion {
|
pub struct Potion {
|
||||||
/// a potion that restores some health
|
/// a potion that restores some health
|
||||||
position: Position,
|
position: Position,
|
||||||
health: usize,
|
health: usize,
|
||||||
|
was_collected: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Potion {
|
impl Potion {
|
||||||
pub fn new(position: Position) -> Self {
|
pub fn new(position: Position) -> Self {
|
||||||
|
let min_health_gain = 5 + position.get_level();
|
||||||
|
let max_health_gain = min_health_gain + 3 * position.get_level();
|
||||||
Self {
|
Self {
|
||||||
position,
|
position,
|
||||||
health: 5,
|
health: rand::thread_rng().gen_range(min_health_gain..=max_health_gain),
|
||||||
|
was_collected: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_health(&self) -> usize {
|
||||||
|
self.health
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Artifact for Potion {
|
impl Artifact for Potion {
|
||||||
fn get_representation(&self) -> (&str, Color) {
|
fn get_representation(&self) -> (&str, Color) {
|
||||||
("P", Color::Green)
|
("P", Color::Green)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_immutable_position(&self) -> &Position {
|
fn get_immutable_position(&self) -> &Position {
|
||||||
&self.position
|
&self.position
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect(&mut self, player: &mut Player, messages: &mut Vec<String>) {
|
fn collect(&mut self, player: &mut Player, messages: &mut Vec<String>) {
|
||||||
// only consume potion of the player can gain at least one health point
|
//! called when the player walked on to a potion.
|
||||||
|
//!
|
||||||
|
//! Depending on health status and inventory usage the potion will
|
||||||
|
//! be consumed directly or moved to inventory.
|
||||||
if !player.is_healthy() {
|
if !player.is_healthy() {
|
||||||
let old = player.get_life();
|
let old = player.get_life();
|
||||||
player.change_life(self.health.try_into().unwrap());
|
player.change_life(self.health.try_into().unwrap());
|
||||||
let new = player.get_life();
|
let new = player.get_life();
|
||||||
messages.insert(
|
messages.insert(
|
||||||
0,
|
0,
|
||||||
format!("picked up potion and gained {} hp.", new - old).to_string(),
|
format!("picked up potion and gained {} health.", new - old).to_string(),
|
||||||
);
|
);
|
||||||
self.health = 0;
|
self.health = 0;
|
||||||
|
self.was_collected = true;
|
||||||
|
} else if player.add_to_inventory(self) {
|
||||||
|
messages.insert(0, "move potion to inventory.".to_string());
|
||||||
|
self.was_collected = true;
|
||||||
} else {
|
} else {
|
||||||
messages.insert(
|
messages.insert(0, "inventory is full.".to_string());
|
||||||
0,
|
|
||||||
"not using the potion because you're healthy.".to_string(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn was_collected(&self) -> bool {
|
fn was_collected(&self) -> bool {
|
||||||
self.health == 0
|
self.was_collected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
137
src/constants.rs
Normal file
137
src/constants.rs
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
use std::{collections::HashMap, ops::RangeInclusive};
|
||||||
|
|
||||||
|
use crate::{monster::MonsterTypes, room::RoomType};
|
||||||
|
|
||||||
|
/// the number of rooms in vertical direction
|
||||||
|
pub const ROOMS_HORIZONTAL: usize = 8;
|
||||||
|
/// the number of rooms in horizontal direction
|
||||||
|
pub const ROOMS_VERTICAL: usize = 7;
|
||||||
|
|
||||||
|
/// the width of a room in the grid of rooms (number of characters)
|
||||||
|
pub const ROOM_WIDTH: usize = 9;
|
||||||
|
/// the height of a room in the grid of rooms (number of characters)
|
||||||
|
pub const ROOM_HEIGHT: usize = 6;
|
||||||
|
|
||||||
|
// the minmal width of a room
|
||||||
|
pub const ROOM_MIN_WIDTH: usize = 4;
|
||||||
|
|
||||||
|
// the minmal height of a room
|
||||||
|
pub const ROOM_MIN_HEIGHT: usize = 4;
|
||||||
|
|
||||||
|
/// How many levels does the dungeon have?
|
||||||
|
pub const LEVELS: usize = 4;
|
||||||
|
|
||||||
|
/// length of a game frame in ms
|
||||||
|
pub const FRAME_LENGTH: u64 = 100;
|
||||||
|
|
||||||
|
/// define the minimum width of a terminal to run the game, must be at least the level width plus some space for stats and messages
|
||||||
|
pub const MIN_WIDTH: u16 = 120;
|
||||||
|
/// define the minimum height of a terminal to run the game, this must be at least the level height!
|
||||||
|
pub const MIN_HEIGHT: u16 = LEVEL_HEIGHT as u16;
|
||||||
|
|
||||||
|
/// the calculated width of a level
|
||||||
|
pub const LEVEL_WIDTH: usize = 1 + ROOMS_HORIZONTAL * ROOM_WIDTH;
|
||||||
|
/// the calculated height of a level
|
||||||
|
pub const LEVEL_HEIGHT: usize = 1 + ROOMS_VERTICAL * ROOM_HEIGHT;
|
||||||
|
|
||||||
|
pub fn get_monsters_per_level() -> Vec<HashMap<MonsterTypes, std::ops::RangeInclusive<u8>>> {
|
||||||
|
let tmp = [
|
||||||
|
// level 1
|
||||||
|
vec![(MonsterTypes::Rat, 50), (MonsterTypes::Spider, 50)],
|
||||||
|
// level 2
|
||||||
|
vec![(MonsterTypes::Rat, 50), (MonsterTypes::Snake, 50)],
|
||||||
|
// level 3
|
||||||
|
vec![
|
||||||
|
(MonsterTypes::Orc, 34),
|
||||||
|
(MonsterTypes::Skeleton, 33),
|
||||||
|
(MonsterTypes::Snake, 33),
|
||||||
|
],
|
||||||
|
// level 4
|
||||||
|
vec![
|
||||||
|
(MonsterTypes::Orc, 34),
|
||||||
|
(MonsterTypes::Skeleton, 33),
|
||||||
|
(MonsterTypes::Snake, 33),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
if tmp.len() < LEVELS {
|
||||||
|
panic!(
|
||||||
|
"Only {} monster sets defined for {} levels!",
|
||||||
|
tmp.len(),
|
||||||
|
LEVELS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let mut result: Vec<HashMap<MonsterTypes, std::ops::RangeInclusive<u8>>> = vec![];
|
||||||
|
for (idx, level) in tmp.iter().enumerate() {
|
||||||
|
let mut sum = 0;
|
||||||
|
let mut map: HashMap<MonsterTypes, RangeInclusive<u8>> = HashMap::new();
|
||||||
|
for monster in level {
|
||||||
|
map.insert(monster.0, RangeInclusive::new(sum + 1, sum + monster.1));
|
||||||
|
sum += monster.1;
|
||||||
|
}
|
||||||
|
if sum != 100 {
|
||||||
|
panic!(
|
||||||
|
"all percentages must add to 100 (was {}) per level, error in level {}!",
|
||||||
|
sum,
|
||||||
|
idx + 1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
result.push(map);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_room_type_per_level() -> Vec<HashMap<RoomType, std::ops::RangeInclusive<u8>>> {
|
||||||
|
let tmp = [
|
||||||
|
// level 1
|
||||||
|
vec![
|
||||||
|
(RoomType::EmptyRoom, 50),
|
||||||
|
(RoomType::ArtifactRoom, 10),
|
||||||
|
(RoomType::MonsterRoom, 20),
|
||||||
|
(RoomType::BasicRoom, 20),
|
||||||
|
],
|
||||||
|
// level 2
|
||||||
|
vec![
|
||||||
|
(RoomType::EmptyRoom, 50),
|
||||||
|
(RoomType::BasicRoom, 25),
|
||||||
|
(RoomType::MonsterRoom, 12),
|
||||||
|
(RoomType::ArtifactRoom, 13),
|
||||||
|
],
|
||||||
|
// level 3
|
||||||
|
vec![
|
||||||
|
(RoomType::EmptyRoom, 50),
|
||||||
|
(RoomType::BasicRoom, 25),
|
||||||
|
(RoomType::MonsterRoom, 25),
|
||||||
|
],
|
||||||
|
// level 4
|
||||||
|
vec![
|
||||||
|
(RoomType::BasicRoom, 33),
|
||||||
|
(RoomType::MonsterRoom, 33),
|
||||||
|
(RoomType::ArtifactRoom, 34),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
if tmp.len() < LEVELS {
|
||||||
|
panic!(
|
||||||
|
"Only {} room sets defined for {} levels!",
|
||||||
|
tmp.len(),
|
||||||
|
LEVELS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let mut result: Vec<HashMap<RoomType, std::ops::RangeInclusive<u8>>> = vec![];
|
||||||
|
for (idx, level) in tmp.iter().enumerate() {
|
||||||
|
let mut sum = 0;
|
||||||
|
let mut map: HashMap<RoomType, RangeInclusive<u8>> = HashMap::new();
|
||||||
|
for room in level {
|
||||||
|
map.insert(room.0, RangeInclusive::new(sum + 1, sum + room.1));
|
||||||
|
sum += room.1;
|
||||||
|
}
|
||||||
|
if sum != 100 {
|
||||||
|
panic!(
|
||||||
|
"all percentages must add to 100 (was {}) per level, error in level {}!",
|
||||||
|
sum,
|
||||||
|
idx + 1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
result.push(map);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
35
src/game.rs
35
src/game.rs
@@ -1,10 +1,9 @@
|
|||||||
|
use crate::constants::LEVELS;
|
||||||
use crate::level::{Level, StructureElement};
|
use crate::level::{Level, StructureElement};
|
||||||
use crate::level_generator::LevelGenerator;
|
use crate::level_generator::LevelGenerator;
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
use crate::position::{HasPosition, Position};
|
use crate::position::{HasPosition, Position};
|
||||||
|
|
||||||
pub const LEVELS: usize = 10;
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
/// represents a state of a game
|
/// represents a state of a game
|
||||||
pub enum GameState {
|
pub enum GameState {
|
||||||
@@ -22,6 +21,7 @@ pub struct Game {
|
|||||||
player: Player,
|
player: Player,
|
||||||
/// the levels of the game
|
/// the levels of the game
|
||||||
levels: Vec<Level>,
|
levels: Vec<Level>,
|
||||||
|
/// messages that are displayed in the ui
|
||||||
pub messages: Vec<String>,
|
pub messages: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ impl Game {
|
|||||||
let (next_level, x, y) = self.next_start();
|
let (next_level, x, y) = self.next_start();
|
||||||
player_level = next_level;
|
player_level = next_level;
|
||||||
self.messages
|
self.messages
|
||||||
.insert(0, format!("you climb down to level {}.", next_level));
|
.insert(0, format!("you climb down to level {}.", next_level + 1));
|
||||||
self.get_mutable_player()
|
self.get_mutable_player()
|
||||||
.get_position()
|
.get_position()
|
||||||
.set(next_level, x, y);
|
.set(next_level, x, y);
|
||||||
@@ -140,7 +140,7 @@ impl Game {
|
|||||||
let (next_level, x, y) = self.prev_end();
|
let (next_level, x, y) = self.prev_end();
|
||||||
player_level = next_level;
|
player_level = next_level;
|
||||||
self.messages
|
self.messages
|
||||||
.insert(0, format!("you climb up to level {}.", next_level));
|
.insert(0, format!("you climb up to level {}.", next_level + 1));
|
||||||
self.get_mutable_player()
|
self.get_mutable_player()
|
||||||
.get_position()
|
.get_position()
|
||||||
.set(next_level, x, y);
|
.set(next_level, x, y);
|
||||||
@@ -164,25 +164,32 @@ impl Game {
|
|||||||
match m {
|
match m {
|
||||||
None => {}
|
None => {}
|
||||||
Some(m) => {
|
Some(m) => {
|
||||||
// TODO fight the monster
|
|
||||||
|
|
||||||
let monster_dmg = m.damage() as i16;
|
|
||||||
let player_dmg = self.player.damage();
|
let player_dmg = self.player.damage();
|
||||||
self.player.change_life(-monster_dmg);
|
|
||||||
m.decrease_life(player_dmg);
|
m.decrease_life(player_dmg);
|
||||||
|
if m.is_dead() {
|
||||||
// inform player
|
self.player.gain_experience(m.get_experience_gain());
|
||||||
self.messages.insert(
|
self.messages.insert(
|
||||||
0,
|
0,
|
||||||
format!("{} hits you for {} damage.", m.get_name(), monster_dmg).to_string(),
|
format!(
|
||||||
|
"you hit {} for {} damage and kill it.",
|
||||||
|
m.get_name(),
|
||||||
|
player_dmg
|
||||||
|
)
|
||||||
|
.to_string(),
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
self.messages.insert(
|
self.messages.insert(
|
||||||
0,
|
0,
|
||||||
format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(),
|
format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(),
|
||||||
);
|
);
|
||||||
// monster died, player gains experience
|
let monster_dmg = m.damage() as i16;
|
||||||
if m.is_dead() {
|
self.player.change_life(-monster_dmg);
|
||||||
self.player.gain_experience(m.get_experience_gain());
|
|
||||||
|
self.messages.insert(
|
||||||
|
0,
|
||||||
|
format!("{} hits you for {} damage.", m.get_name(), monster_dmg)
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return m.is_dead();
|
return m.is_dead();
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/level.rs
30
src/level.rs
@@ -6,6 +6,8 @@ use rand::Rng;
|
|||||||
use crate::artifacts::Artifact;
|
use crate::artifacts::Artifact;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::artifacts::{Chest, Potion};
|
use crate::artifacts::{Chest, Potion};
|
||||||
|
use crate::constants::LEVEL_HEIGHT;
|
||||||
|
use crate::constants::LEVEL_WIDTH;
|
||||||
use crate::monster::Monster;
|
use crate::monster::Monster;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::monster::{Orc, Rat};
|
use crate::monster::{Orc, Rat};
|
||||||
@@ -13,9 +15,6 @@ use crate::player::Player;
|
|||||||
use crate::position::HasPosition;
|
use crate::position::HasPosition;
|
||||||
use crate::position::Position;
|
use crate::position::Position;
|
||||||
|
|
||||||
pub const LEVEL_WIDTH: usize = 50;
|
|
||||||
pub const LEVEL_HEIGHT: usize = 25;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
pub enum StructureElement {
|
pub enum StructureElement {
|
||||||
Start,
|
Start,
|
||||||
@@ -58,6 +57,7 @@ impl Level {
|
|||||||
return (None, None, None);
|
return (None, None, None);
|
||||||
}
|
}
|
||||||
if !self.discovered[x][y] {
|
if !self.discovered[x][y] {
|
||||||
|
// #[cfg(test)]
|
||||||
return (Some(StructureElement::Unknown), None, None);
|
return (Some(StructureElement::Unknown), None, None);
|
||||||
}
|
}
|
||||||
let search_pos = &Position::new(self.level, x, y);
|
let search_pos = &Position::new(self.level, x, y);
|
||||||
@@ -147,12 +147,8 @@ impl Level {
|
|||||||
if player.get_immutable_position().get_x() == new_x
|
if player.get_immutable_position().get_x() == new_x
|
||||||
&& player.get_immutable_position().get_y() == new_y
|
&& player.get_immutable_position().get_y() == new_y
|
||||||
{
|
{
|
||||||
// TODO handle fight between monster and player
|
|
||||||
let monster_dmg = self.monsters[index].damage() as i16;
|
let monster_dmg = self.monsters[index].damage() as i16;
|
||||||
let player_dmg = player.damage();
|
|
||||||
self.monsters[index].decrease_life(player_dmg);
|
|
||||||
player.change_life(-monster_dmg);
|
player.change_life(-monster_dmg);
|
||||||
|
|
||||||
messages.insert(
|
messages.insert(
|
||||||
0,
|
0,
|
||||||
format!(
|
format!(
|
||||||
@@ -162,6 +158,23 @@ impl Level {
|
|||||||
)
|
)
|
||||||
.to_string(),
|
.to_string(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let player_dmg = player.damage();
|
||||||
|
self.monsters[index].decrease_life(player_dmg);
|
||||||
|
|
||||||
|
// if the attack did not kill the opponent, back down
|
||||||
|
if !player.is_dead() {
|
||||||
|
messages.insert(
|
||||||
|
0,
|
||||||
|
format!(
|
||||||
|
"you hit {} for {} damage and kill it.",
|
||||||
|
self.monsters[index].get_name(),
|
||||||
|
player_dmg
|
||||||
|
)
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
self.monsters[index].get_position().change(-dx, -dy);
|
||||||
|
} else {
|
||||||
messages.insert(
|
messages.insert(
|
||||||
0,
|
0,
|
||||||
format!(
|
format!(
|
||||||
@@ -171,9 +184,6 @@ impl Level {
|
|||||||
)
|
)
|
||||||
.to_string(),
|
.to_string(),
|
||||||
);
|
);
|
||||||
// if the attack did not kill the opponent, back down
|
|
||||||
if !player.is_dead() {
|
|
||||||
self.monsters[index].get_position().change(-dx, -dy);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,14 @@
|
|||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
use ratatui::style::Color;
|
use ratatui::style::{Color, Modifier, Style};
|
||||||
use ratatui::widgets::{StatefulWidget, Widget};
|
use ratatui::widgets::{StatefulWidget, Widget};
|
||||||
|
|
||||||
use crate::game::Game;
|
use crate::game::Game;
|
||||||
use crate::level::StructureElement;
|
use crate::level::StructureElement;
|
||||||
use crate::position::HasPosition;
|
use crate::position::HasPosition;
|
||||||
|
|
||||||
const FG_BROWN: Color = Color::Rgb(186, 74, 0);
|
const FG_BROWN: Color = Color::Rgb(140, 34, 0);
|
||||||
|
const BACKGROUND: Color = Color::Black;
|
||||||
|
|
||||||
pub struct LevelWidget {}
|
pub struct LevelWidget {}
|
||||||
|
|
||||||
@@ -15,6 +16,13 @@ impl LevelWidget {
|
|||||||
fn set_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
|
fn set_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
|
||||||
buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg);
|
buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg);
|
||||||
}
|
}
|
||||||
|
fn set_bold_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
|
||||||
|
buf[(x, y)]
|
||||||
|
.set_symbol(symbol)
|
||||||
|
.set_bg(bg)
|
||||||
|
.set_fg(fg)
|
||||||
|
.set_style(Style::new().add_modifier(Modifier::BOLD));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StatefulWidget for LevelWidget {
|
impl StatefulWidget for LevelWidget {
|
||||||
@@ -39,36 +47,36 @@ impl StatefulWidget for LevelWidget {
|
|||||||
(Some(structure_element), None, None) => {
|
(Some(structure_element), None, None) => {
|
||||||
match structure_element {
|
match structure_element {
|
||||||
StructureElement::Start => {
|
StructureElement::Start => {
|
||||||
self.set_cell(buf, x, y, "α", Color::Black, Color::Gray);
|
self.set_cell(buf, x, y, "α", Color::White, BACKGROUND);
|
||||||
}
|
}
|
||||||
StructureElement::End => {
|
StructureElement::End => {
|
||||||
self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray);
|
self.set_cell(buf, x, y, "Ω", Color::White, BACKGROUND);
|
||||||
}
|
}
|
||||||
StructureElement::Wall => {
|
StructureElement::Wall => {
|
||||||
// TODO add fancy walls
|
// TODO add fancy walls with https://en.wikipedia.org/wiki/Box-drawing_characters
|
||||||
self.set_cell(buf, x, y, "#", FG_BROWN, Color::Gray);
|
self.set_cell(buf, x, y, "#", FG_BROWN, FG_BROWN);
|
||||||
}
|
}
|
||||||
StructureElement::Floor => {
|
StructureElement::Floor => {
|
||||||
self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray);
|
self.set_cell(buf, x, y, " ", FG_BROWN, BACKGROUND);
|
||||||
}
|
}
|
||||||
StructureElement::StairDown => {
|
StructureElement::StairDown => {
|
||||||
self.set_cell(buf, x, y, ">", Color::Black, Color::Gray);
|
self.set_cell(buf, x, y, ">", Color::White, BACKGROUND);
|
||||||
}
|
}
|
||||||
StructureElement::StairUp => {
|
StructureElement::StairUp => {
|
||||||
self.set_cell(buf, x, y, "<", Color::Black, Color::Gray);
|
self.set_cell(buf, x, y, "<", Color::White, BACKGROUND);
|
||||||
}
|
}
|
||||||
StructureElement::Unknown => {
|
StructureElement::Unknown => {
|
||||||
self.set_cell(buf, x, y, "▒", Color::DarkGray, Color::Gray);
|
self.set_cell(buf, x, y, "▒", Color::Gray, BACKGROUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(_, Some(m), _) => {
|
(_, Some(m), _) => {
|
||||||
let (s, c) = m.get_representation();
|
let (s, c) = m.get_representation();
|
||||||
self.set_cell(buf, x, y, s, c, Color::Gray);
|
self.set_cell(buf, x, y, s, c, BACKGROUND);
|
||||||
}
|
}
|
||||||
(_, _, Some(t)) => {
|
(_, _, Some(t)) => {
|
||||||
let (s, c) = t.get_representation();
|
let (s, c) = t.get_representation();
|
||||||
self.set_cell(buf, x, y, s, c, Color::Gray);
|
self.set_bold_cell(buf, x, y, s, c, BACKGROUND);
|
||||||
}
|
}
|
||||||
(None, None, None) => {}
|
(None, None, None) => {}
|
||||||
};
|
};
|
||||||
@@ -80,7 +88,7 @@ impl StatefulWidget for LevelWidget {
|
|||||||
let player_pos = player.get_immutable_position();
|
let player_pos = player.get_immutable_position();
|
||||||
let player_x = al + player_pos.get_x() as u16;
|
let player_x = al + player_pos.get_x() as u16;
|
||||||
let player_y = at + player_pos.get_y() as u16;
|
let player_y = at + player_pos.get_y() as u16;
|
||||||
self.set_cell(buf, player_x, player_y, "8", Color::Red, Color::Gray);
|
self.set_cell(buf, player_x, player_y, "8", Color::LightRed, BACKGROUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
82
src/main.rs
82
src/main.rs
@@ -2,6 +2,12 @@ use std::io::stdout;
|
|||||||
use std::io::Result;
|
use std::io::Result;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use constants::FRAME_LENGTH;
|
||||||
|
use constants::LEVELS;
|
||||||
|
use constants::LEVEL_HEIGHT;
|
||||||
|
use constants::LEVEL_WIDTH;
|
||||||
|
use constants::MIN_HEIGHT;
|
||||||
|
use constants::MIN_WIDTH;
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
event::{self, KeyCode, KeyEventKind},
|
event::{self, KeyCode, KeyEventKind},
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||||
@@ -22,6 +28,7 @@ use crate::player::Player;
|
|||||||
use crate::position::HasPosition;
|
use crate::position::HasPosition;
|
||||||
|
|
||||||
mod artifacts;
|
mod artifacts;
|
||||||
|
mod constants;
|
||||||
mod game;
|
mod game;
|
||||||
mod level;
|
mod level;
|
||||||
mod level_generator;
|
mod level_generator;
|
||||||
@@ -29,9 +36,7 @@ mod level_widget;
|
|||||||
mod monster;
|
mod monster;
|
||||||
mod player;
|
mod player;
|
||||||
mod position;
|
mod position;
|
||||||
|
mod room;
|
||||||
/// length of a game frame in ms
|
|
||||||
pub const FRAME_LENGTH: u64 = 100;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
@@ -54,42 +59,45 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
// don't draw stuff except an info box if the terminal is too small (less than 80x25)
|
// don't draw stuff except an info box if the terminal is too small (less than 80x25)
|
||||||
// to prevent the read drawing code from crashing the game.
|
// to prevent the read drawing code from crashing the game.
|
||||||
if area.width < 80 || area.height < 25 {
|
if area.width < MIN_WIDTH || area.height < MIN_HEIGHT {
|
||||||
let block = Block::default()
|
let block = Block::default()
|
||||||
.title("Info")
|
.title("Info")
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.border_style(Style::default().fg(Color::White))
|
.border_style(Style::default().fg(Color::White))
|
||||||
.border_type(BorderType::Rounded)
|
.border_type(BorderType::Rounded)
|
||||||
.style(Style::default().bg(Color::Black));
|
.style(Style::default().bg(Color::Black));
|
||||||
let paragraph = Paragraph::new("Terminal needs to be at leas 80x25!")
|
let paragraph = Paragraph::new(format!(
|
||||||
|
"Terminal needs to be at leas {}x{}!",
|
||||||
|
MIN_WIDTH, MIN_HEIGHT
|
||||||
|
))
|
||||||
.block(block)
|
.block(block)
|
||||||
.wrap(Wrap { trim: true });
|
.wrap(Wrap { trim: true });
|
||||||
frame.render_widget(paragraph, area);
|
frame.render_widget(paragraph, area);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if area.width > 80 {
|
if area.width > MIN_WIDTH {
|
||||||
area.x = (area.width - 80) / 2;
|
area.x = (area.width - MIN_WIDTH) / 2;
|
||||||
area.width = 80;
|
area.width = MIN_WIDTH;
|
||||||
}
|
}
|
||||||
if area.height > 25 {
|
if area.height > MIN_HEIGHT {
|
||||||
area.y = (area.height - 25) / 2;
|
area.y = (area.height - LEVEL_HEIGHT as u16) / 2;
|
||||||
area.height = 25;
|
area.height = LEVEL_HEIGHT as u16;
|
||||||
}
|
}
|
||||||
|
|
||||||
let map_area = Rect {
|
let map_area = Rect {
|
||||||
x: area.x,
|
x: area.x,
|
||||||
y: area.y,
|
y: area.y,
|
||||||
width: level::LEVEL_WIDTH as u16,
|
width: LEVEL_WIDTH as u16,
|
||||||
height: level::LEVEL_HEIGHT as u16,
|
height: LEVEL_HEIGHT as u16,
|
||||||
};
|
};
|
||||||
frame.render_stateful_widget(LevelWidget {}, map_area, &mut game);
|
frame.render_stateful_widget(LevelWidget {}, map_area, &mut game);
|
||||||
|
|
||||||
let stats_area = Rect {
|
let stats_area = Rect {
|
||||||
x: area.x + 50,
|
x: area.x + map_area.width,
|
||||||
y: area.y,
|
y: area.y,
|
||||||
width: 30,
|
width: MIN_WIDTH - map_area.width,
|
||||||
height: 15,
|
height: map_area.height / 2 + 1,
|
||||||
};
|
};
|
||||||
let block = Block::default()
|
let block = Block::default()
|
||||||
.title(
|
.title(
|
||||||
@@ -103,22 +111,25 @@ fn main() -> Result<()> {
|
|||||||
.style(Style::default().bg(Color::Blue));
|
.style(Style::default().bg(Color::Blue));
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
Paragraph::new(format!(
|
Paragraph::new(format!(
|
||||||
"Health: {}/{}\nExp: {}\nGold: {}\nLevel: {}",
|
"Health: {} of {}\nExp: {}\nGold: {}\nLevel: {} of {}\nInventory used: {} of {}",
|
||||||
game.get_player().get_life(),
|
game.get_player().get_life(),
|
||||||
game.get_player().get_max_life(),
|
game.get_player().get_max_life(),
|
||||||
game.get_player().get_experience(),
|
game.get_player().get_experience(),
|
||||||
game.get_player().get_gold(),
|
game.get_player().get_gold(),
|
||||||
game.get_player().get_immutable_position().get_level()+1
|
game.get_player().get_immutable_position().get_level() + 1,
|
||||||
|
LEVELS,
|
||||||
|
game.get_player().inventory_size().0,
|
||||||
|
game.get_player().inventory_size().1,
|
||||||
))
|
))
|
||||||
.block(block)
|
.block(block)
|
||||||
.wrap(Wrap { trim: true }),
|
.wrap(Wrap { trim: true }),
|
||||||
stats_area,
|
stats_area,
|
||||||
);
|
);
|
||||||
let messages_area = Rect {
|
let messages_area = Rect {
|
||||||
x: area.x + 50,
|
x: area.x + map_area.width,
|
||||||
y: area.y + 15,
|
y: area.y + map_area.height / 2 + 1,
|
||||||
width: 30,
|
width: MIN_WIDTH - map_area.width,
|
||||||
height: 10,
|
height: map_area.height / 2,
|
||||||
};
|
};
|
||||||
// Display the latest messages from the game to the user
|
// Display the latest messages from the game to the user
|
||||||
let block = Block::default()
|
let block = Block::default()
|
||||||
@@ -146,16 +157,32 @@ fn main() -> Result<()> {
|
|||||||
})?;
|
})?;
|
||||||
if event::poll(std::time::Duration::from_millis(FRAME_LENGTH))? {
|
if event::poll(std::time::Duration::from_millis(FRAME_LENGTH))? {
|
||||||
if let event::Event::Key(key) = event::read()? {
|
if let event::Event::Key(key) = event::read()? {
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('v') {
|
if key.kind == KeyEventKind::Press {
|
||||||
|
match key.code {
|
||||||
|
KeyCode::Char('v') => {
|
||||||
game.messages.insert(
|
game.messages.insert(
|
||||||
0,
|
0,
|
||||||
format!("You are playing version '{}'.", env!("GIT_HASH")).to_string(),
|
format!("You are playing version '{}'.", env!("GIT_HASH"))
|
||||||
|
.to_string(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
KeyCode::Char('p') => {
|
||||||
|
let gained_health = game.get_mutable_player().consume_inventory();
|
||||||
|
if gained_health > 0 {
|
||||||
|
game.messages.insert(
|
||||||
|
0,
|
||||||
|
format!(
|
||||||
|
"used a potion from inventory and gained {} health.",
|
||||||
|
gained_health
|
||||||
|
)
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char('q') => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if key.kind == KeyEventKind::Press {
|
KeyCode::Left | KeyCode::Down | KeyCode::Right | KeyCode::Up => {
|
||||||
let new_pos = match key.code {
|
let new_pos = match key.code {
|
||||||
KeyCode::Left => game.move_player(-1, 0),
|
KeyCode::Left => game.move_player(-1, 0),
|
||||||
KeyCode::Right => game.move_player(1, 0),
|
KeyCode::Right => game.move_player(1, 0),
|
||||||
@@ -169,6 +196,9 @@ fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
game.player_collects_artifact();
|
game.player_collects_artifact();
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
game.update_level(ticks);
|
game.update_level(ticks);
|
||||||
|
|||||||
128
src/monster.rs
128
src/monster.rs
@@ -1,10 +1,10 @@
|
|||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
|
use crate::position::{HasPosition, Position};
|
||||||
|
use macros::CreateMonsters;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use ratatui::prelude::Color;
|
use ratatui::prelude::Color;
|
||||||
|
|
||||||
use crate::position::{HasPosition, Position};
|
|
||||||
|
|
||||||
pub trait Monster: HasPosition {
|
pub trait Monster: HasPosition {
|
||||||
fn get_name(&self) -> &str;
|
fn get_name(&self) -> &str;
|
||||||
fn is_dead(&self) -> bool;
|
fn is_dead(&self) -> bool;
|
||||||
@@ -18,90 +18,92 @@ pub trait Monster: HasPosition {
|
|||||||
fn damage(&self) -> usize;
|
fn damage(&self) -> usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! create_monster {
|
#[derive(CreateMonsters, PartialEq, Eq, Hash, Clone, Copy)]
|
||||||
($($t:ident),+ $(,)?) => ($(
|
pub enum MonsterTypes {
|
||||||
pub struct $t {
|
Rat,
|
||||||
name: String,
|
Orc,
|
||||||
life: usize,
|
Snake,
|
||||||
position: Position,
|
Skeleton,
|
||||||
symbol: String,
|
Spider,
|
||||||
color: Color,
|
|
||||||
experience_gain: usize,
|
|
||||||
ticks_between_steps: u128,
|
|
||||||
damage_range: RangeInclusive<usize>,
|
|
||||||
}
|
|
||||||
impl Monster for $t {
|
|
||||||
fn get_name(&self) -> &str { &self.name }
|
|
||||||
fn is_dead(&self) -> bool { self.life <= 0 }
|
|
||||||
fn get_experience_gain(&self) -> usize { self.experience_gain }
|
|
||||||
fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) }
|
|
||||||
fn decrease_life(&mut self, by: usize) {
|
|
||||||
self.life = self.life.saturating_sub(by);
|
|
||||||
}
|
|
||||||
fn get_ticks_between_steps(&self) -> u128 { self.ticks_between_steps }
|
|
||||||
fn damage(&self) -> usize { rand::thread_rng().gen_range(self.damage_range.clone()) }
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
fn get_life(&self) -> usize { self.life }
|
|
||||||
}
|
|
||||||
impl HasPosition for $t {
|
|
||||||
fn get_position(&mut self) -> &mut Position {
|
|
||||||
&mut self.position
|
|
||||||
}
|
|
||||||
fn get_immutable_position(&self) -> &Position {
|
|
||||||
&self.position
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)+)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rat {
|
macro_rules! create_monster {
|
||||||
|
($t:ident $(, $k:ident : $v:expr)*$(,)?) => (
|
||||||
|
impl $t {
|
||||||
pub fn new_with_position(position: Position) -> Self {
|
pub fn new_with_position(position: Position) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: "rat".to_string(),
|
|
||||||
life: 2,
|
|
||||||
position,
|
position,
|
||||||
symbol: String::from("R"),
|
$($k: $v,)*
|
||||||
color: Color::Black,
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
create_monster!(
|
||||||
|
Rat,
|
||||||
|
name:"rat".to_string(),
|
||||||
|
life: 2,
|
||||||
|
symbol: String::from("r"),
|
||||||
|
color: Color::White,
|
||||||
experience_gain: 5,
|
experience_gain: 5,
|
||||||
ticks_between_steps: 5,
|
ticks_between_steps: 5,
|
||||||
damage_range: 1..=2,
|
damage_range: 1..=2,
|
||||||
}
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
create_monster!(Rat);
|
|
||||||
|
|
||||||
impl Orc {
|
create_monster!(
|
||||||
pub fn new_with_position(position: Position) -> Self {
|
Spider,
|
||||||
Self {
|
name:"spider".to_string(),
|
||||||
|
life: 3,
|
||||||
|
symbol: String::from("s"),
|
||||||
|
color: Color::Yellow,
|
||||||
|
experience_gain: 7,
|
||||||
|
ticks_between_steps: 7,
|
||||||
|
damage_range: 2..=3,
|
||||||
|
);
|
||||||
|
|
||||||
|
create_monster!(
|
||||||
|
Orc,
|
||||||
name: "orc".to_string(),
|
name: "orc".to_string(),
|
||||||
life: 4,
|
life: 4,
|
||||||
position,
|
|
||||||
symbol: String::from("O"),
|
symbol: String::from("O"),
|
||||||
color: Color::DarkGray,
|
color: Color::Gray,
|
||||||
experience_gain: 10,
|
experience_gain: 10,
|
||||||
ticks_between_steps: 10,
|
ticks_between_steps: 10,
|
||||||
damage_range: 2..=3,
|
damage_range: 2..=3,
|
||||||
}
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
create_monster!(Orc);
|
|
||||||
|
|
||||||
impl Snake {
|
create_monster!(
|
||||||
pub fn new_with_position(position: Position) -> Self {
|
Snake,
|
||||||
Self {
|
|
||||||
name: "snake".to_string(),
|
name: "snake".to_string(),
|
||||||
life: 3,
|
life: 3,
|
||||||
position,
|
symbol: String::from("s"),
|
||||||
symbol: String::from("S"),
|
color: Color::White,
|
||||||
color: Color::DarkGray,
|
|
||||||
experience_gain: 10,
|
experience_gain: 10,
|
||||||
ticks_between_steps: 20,
|
ticks_between_steps: 20,
|
||||||
damage_range: 1..=4,
|
damage_range: 1..=4,
|
||||||
}
|
);
|
||||||
|
|
||||||
|
create_monster!(
|
||||||
|
Skeleton,
|
||||||
|
name: "skeleton".to_string(),
|
||||||
|
life: 3,
|
||||||
|
symbol: String::from("S"),
|
||||||
|
color: Color::Gray,
|
||||||
|
experience_gain: 20,
|
||||||
|
ticks_between_steps: 10,
|
||||||
|
damage_range: 1..=5,
|
||||||
|
);
|
||||||
|
|
||||||
|
pub fn create_monster_by_type(monster_type: &MonsterTypes, position: Position) -> Box<dyn Monster> {
|
||||||
|
match monster_type {
|
||||||
|
MonsterTypes::Rat => Box::new(Rat::new_with_position(position)),
|
||||||
|
MonsterTypes::Orc => Box::new(Orc::new_with_position(position)),
|
||||||
|
MonsterTypes::Snake => Box::new(Snake::new_with_position(position)),
|
||||||
|
MonsterTypes::Skeleton => Box::new(Skeleton::new_with_position(position)),
|
||||||
|
MonsterTypes::Spider => Box::new(Spider::new_with_position(position)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
create_monster!(Snake);
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn monsters_can_move() {
|
fn monsters_can_move() {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
|
use crate::artifacts::Potion;
|
||||||
use crate::position::{HasPosition, Position};
|
use crate::position::{HasPosition, Position};
|
||||||
|
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
@@ -10,6 +11,8 @@ pub struct Player {
|
|||||||
max_life: i16,
|
max_life: i16,
|
||||||
gold: usize,
|
gold: usize,
|
||||||
experience: usize,
|
experience: usize,
|
||||||
|
inventory: Vec<Potion>,
|
||||||
|
inventory_slots: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
@@ -21,6 +24,8 @@ impl Player {
|
|||||||
max_life,
|
max_life,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
experience: 0,
|
experience: 0,
|
||||||
|
inventory: vec![],
|
||||||
|
inventory_slots: 2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_name(&self) -> String {
|
pub fn get_name(&self) -> String {
|
||||||
@@ -65,6 +70,30 @@ impl Player {
|
|||||||
pub fn damage(&self) -> usize {
|
pub fn damage(&self) -> usize {
|
||||||
rand::thread_rng().gen_range(1..4)
|
rand::thread_rng().gen_range(1..4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_to_inventory(&mut self, potion: &Potion) -> bool {
|
||||||
|
if self.inventory.len() < self.inventory_slots {
|
||||||
|
self.inventory.push(*potion);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn inventory_size(&self) -> (usize, usize) {
|
||||||
|
(self.inventory.len(), self.inventory_slots)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn consume_inventory(&mut self) -> usize {
|
||||||
|
if self.is_healthy() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if let Some(potion) = self.inventory.pop() {
|
||||||
|
self.change_life(potion.get_health() as i16);
|
||||||
|
return potion.get_health();
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasPosition for Player {
|
impl HasPosition for Player {
|
||||||
@@ -85,6 +114,8 @@ fn test_get_name() {
|
|||||||
max_life: 10,
|
max_life: 10,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
experience: 0,
|
experience: 0,
|
||||||
|
inventory: vec![],
|
||||||
|
inventory_slots: 1,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_name(), "Teddy Tester");
|
assert_eq!(p.get_name(), "Teddy Tester");
|
||||||
}
|
}
|
||||||
@@ -108,6 +139,8 @@ fn test_change_life() {
|
|||||||
max_life: 10,
|
max_life: 10,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
experience: 0,
|
experience: 0,
|
||||||
|
inventory: vec![],
|
||||||
|
inventory_slots: 1,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_life(), 5);
|
assert_eq!(p.get_life(), 5);
|
||||||
p.change_life(-2);
|
p.change_life(-2);
|
||||||
@@ -141,6 +174,8 @@ fn test_max_life() {
|
|||||||
max_life: 10,
|
max_life: 10,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
experience: 0,
|
experience: 0,
|
||||||
|
inventory: vec![],
|
||||||
|
inventory_slots: 1,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_max_life(), 10);
|
assert_eq!(p.get_max_life(), 10);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ pub trait HasPosition {
|
|||||||
fn get_immutable_position(&self) -> &Position;
|
fn get_immutable_position(&self) -> &Position;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug, Clone, Copy)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
level: usize,
|
level: usize,
|
||||||
x: usize,
|
x: usize,
|
||||||
|
|||||||
280
src/room.rs
Normal file
280
src/room.rs
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
use std::{
|
||||||
|
cmp::{max, min},
|
||||||
|
ops::RangeInclusive,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
constants::{
|
||||||
|
LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_MIN_HEIGHT, ROOM_MIN_WIDTH, ROOM_WIDTH,
|
||||||
|
},
|
||||||
|
level::StructureElement,
|
||||||
|
};
|
||||||
|
use rand::rngs::ThreadRng;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Copy, Clone, Eq, Hash, Debug)]
|
||||||
|
pub enum RoomType {
|
||||||
|
Start,
|
||||||
|
End,
|
||||||
|
StairUp,
|
||||||
|
StairDown,
|
||||||
|
BasicRoom,
|
||||||
|
ArtifactRoom,
|
||||||
|
MonsterRoom,
|
||||||
|
EmptyRoom,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub struct Connection {
|
||||||
|
pub start_pos: (usize, usize),
|
||||||
|
pub end_pos: (usize, usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Connection {
|
||||||
|
pub fn render(
|
||||||
|
&self,
|
||||||
|
rng: &mut ThreadRng,
|
||||||
|
tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
||||||
|
) {
|
||||||
|
fn calc_range(a: usize, b: usize) -> RangeInclusive<usize> {
|
||||||
|
if a > b {
|
||||||
|
b..=a
|
||||||
|
} else {
|
||||||
|
a..=b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the tuples are (col, row)
|
||||||
|
let abs_d_col =
|
||||||
|
max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
|
||||||
|
|
||||||
|
let abs_d_row =
|
||||||
|
max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
|
||||||
|
|
||||||
|
if abs_d_col == 0 {
|
||||||
|
for row in calc_range(self.end_pos.1, self.start_pos.1) {
|
||||||
|
tgt[self.end_pos.0][row] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
} else if abs_d_row == 0 {
|
||||||
|
for col in calc_range(self.end_pos.0, self.start_pos.0) {
|
||||||
|
tgt[col][self.end_pos.1] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let d_row = self.end_pos.1 as i32 - self.start_pos.1 as i32;
|
||||||
|
if d_row > 0 {
|
||||||
|
// more up/down
|
||||||
|
let d_row = self.end_pos.1 - self.start_pos.1;
|
||||||
|
let vert_offset = if d_row > 3 {
|
||||||
|
rng.gen_range(1..=d_row - 1)
|
||||||
|
} else {
|
||||||
|
d_row / 2
|
||||||
|
};
|
||||||
|
for r in self.start_pos.1..=(self.start_pos.1 + vert_offset) {
|
||||||
|
tgt[self.start_pos.0][r] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
for c in calc_range(self.end_pos.0, self.start_pos.0) {
|
||||||
|
tgt[c][self.start_pos.1 + vert_offset] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
for r in (self.start_pos.1 + vert_offset)..=self.end_pos.1 {
|
||||||
|
tgt[self.end_pos.0][r] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// more left/right
|
||||||
|
let d_col = self.end_pos.0 - self.start_pos.0;
|
||||||
|
let horizont_offset = if d_col > 3 {
|
||||||
|
rng.gen_range(1..=d_col - 1)
|
||||||
|
} else {
|
||||||
|
d_col / 2
|
||||||
|
};
|
||||||
|
for tgt_col in tgt
|
||||||
|
.iter_mut()
|
||||||
|
.skip(self.start_pos.0)
|
||||||
|
.take(horizont_offset + 1)
|
||||||
|
{
|
||||||
|
tgt_col[self.start_pos.1] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
for r in calc_range(self.end_pos.1, self.start_pos.1) {
|
||||||
|
tgt[self.start_pos.0 + horizont_offset][r] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
for tgt_col in tgt
|
||||||
|
.iter_mut()
|
||||||
|
.take(self.end_pos.0 + 1)
|
||||||
|
.skip(self.start_pos.0 + horizont_offset)
|
||||||
|
{
|
||||||
|
tgt_col[self.end_pos.1] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub struct Room {
|
||||||
|
pub kind: RoomType,
|
||||||
|
pub offset_x: usize,
|
||||||
|
pub offset_y: usize,
|
||||||
|
pub width: usize,
|
||||||
|
pub height: usize,
|
||||||
|
pub special: (usize, usize),
|
||||||
|
pub connection_down: Option<Connection>,
|
||||||
|
pub connection_right: Option<Connection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Room {
|
||||||
|
pub fn new(rng: &mut ThreadRng) -> Self {
|
||||||
|
let width = rng.gen_range(ROOM_MIN_WIDTH..ROOM_WIDTH);
|
||||||
|
let height = rng.gen_range(ROOM_MIN_HEIGHT..ROOM_HEIGHT);
|
||||||
|
let offset_x = rng.gen_range(0..(ROOM_WIDTH - width));
|
||||||
|
let offset_y = rng.gen_range(0..(ROOM_HEIGHT - height));
|
||||||
|
let sx = offset_x + rng.gen_range(1..width - 1);
|
||||||
|
let sy = offset_y + rng.gen_range(1..height - 1);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
kind: RoomType::EmptyRoom,
|
||||||
|
offset_x,
|
||||||
|
offset_y,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
special: (sx, sy),
|
||||||
|
connection_down: None,
|
||||||
|
connection_right: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn get_x_range(&self) -> Range<usize> {
|
||||||
|
// self.offset_x..self.offset_x + self.width
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn get_y_range(&self) -> Range<usize> {
|
||||||
|
// self.offset_y..self.offset_y + self.height
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub fn render(
|
||||||
|
&self,
|
||||||
|
tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
||||||
|
col: usize,
|
||||||
|
row: usize,
|
||||||
|
) -> (usize, usize) {
|
||||||
|
let top = 1 + row * ROOM_HEIGHT;
|
||||||
|
let left = 1 + col * ROOM_WIDTH;
|
||||||
|
for col in tgt.iter_mut().skip(left).take(ROOM_WIDTH) {
|
||||||
|
//left..left + ROOM_WIDTH {
|
||||||
|
for row_element in col.iter_mut().skip(top).take(ROOM_HEIGHT) {
|
||||||
|
// top..top + ROOM_HEIGHT {
|
||||||
|
*row_element = StructureElement::Wall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.kind == RoomType::EmptyRoom {
|
||||||
|
return (0, 0);
|
||||||
|
}
|
||||||
|
// render floor elements
|
||||||
|
for x in 0..self.width {
|
||||||
|
for y in 0..self.height {
|
||||||
|
tgt[left + self.offset_x + x][top + self.offset_y + y] = StructureElement::Floor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.kind {
|
||||||
|
RoomType::Start => {
|
||||||
|
tgt[left + self.special.0][top + self.special.1] = StructureElement::Start;
|
||||||
|
}
|
||||||
|
RoomType::End => {
|
||||||
|
tgt[left + self.special.0][top + self.special.1] = StructureElement::End;
|
||||||
|
}
|
||||||
|
RoomType::StairUp => {
|
||||||
|
tgt[left + self.special.0][top + self.special.1] = StructureElement::StairUp;
|
||||||
|
}
|
||||||
|
RoomType::StairDown => {
|
||||||
|
tgt[left + self.special.0][top + self.special.1] = StructureElement::StairDown;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
(left + self.special.0, top + self.special.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_room_creation() {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let room = Room::new(&mut rng);
|
||||||
|
assert_eq!(room.kind, RoomType::EmptyRoom);
|
||||||
|
for _ in 0..1000 {
|
||||||
|
let room = Room::new(&mut rng);
|
||||||
|
assert!((3..=ROOM_WIDTH).contains(&room.width));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
fn all_wall(
|
||||||
|
tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
||||||
|
col: usize,
|
||||||
|
row: usize,
|
||||||
|
) -> bool {
|
||||||
|
let top = 1 + row * ROOM_HEIGHT;
|
||||||
|
let left = 1 + col * ROOM_WIDTH;
|
||||||
|
for x in left..left + ROOM_WIDTH {
|
||||||
|
for y in top..top + ROOM_HEIGHT {
|
||||||
|
if tgt[x][y] != StructureElement::Wall {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
fn has_structure_element(
|
||||||
|
tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
||||||
|
col: usize,
|
||||||
|
row: usize,
|
||||||
|
element: StructureElement,
|
||||||
|
) -> bool {
|
||||||
|
let top = 1 + row * ROOM_HEIGHT;
|
||||||
|
let left = 1 + col * ROOM_WIDTH;
|
||||||
|
for x in left..left + ROOM_WIDTH {
|
||||||
|
for y in top..top + ROOM_HEIGHT {
|
||||||
|
if tgt[x][y] == element {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_room_render_empty() {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let room = Room::new(&mut rng);
|
||||||
|
assert_eq!(room.kind, RoomType::EmptyRoom);
|
||||||
|
let mut structure = [[StructureElement::Floor; LEVEL_HEIGHT]; LEVEL_WIDTH];
|
||||||
|
|
||||||
|
room.render(&mut structure, 0, 0);
|
||||||
|
assert!(all_wall(&mut structure, 0, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_room_render_basic() {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let mut room = Room::new(&mut rng);
|
||||||
|
room.kind = RoomType::BasicRoom;
|
||||||
|
let mut structure = [[StructureElement::Floor; LEVEL_HEIGHT]; LEVEL_WIDTH];
|
||||||
|
|
||||||
|
room.render(&mut structure, 1, 3);
|
||||||
|
assert!(!all_wall(&mut structure, 1, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_room_render_start() {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let mut room = Room::new(&mut rng);
|
||||||
|
room.kind = RoomType::Start;
|
||||||
|
let mut structure = [[StructureElement::Floor; LEVEL_HEIGHT]; LEVEL_WIDTH];
|
||||||
|
|
||||||
|
room.render(&mut structure, 1, 3);
|
||||||
|
assert!(!all_wall(&mut structure, 1, 3));
|
||||||
|
assert!(has_structure_element(
|
||||||
|
&mut structure,
|
||||||
|
1,
|
||||||
|
3,
|
||||||
|
StructureElement::Start
|
||||||
|
));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user