Compare commits
11 Commits
use_potion
...
d6f4fdaa5b
| Author | SHA1 | Date | |
|---|---|---|---|
| d6f4fdaa5b | |||
| 8dfe56a3c2 | |||
| b3d85ac7b3 | |||
| cefb0cc5f2 | |||
| efc4cdd363 | |||
| 210d590e38 | |||
| e4018d9cce | |||
| f6bb0a9297 | |||
| b6d61e4bd1 | |||
| 85496e3200 | |||
| 4770a21abe |
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
|
||||||
265
Cargo.lock
generated
265
Cargo.lock
generated
@@ -50,6 +50,15 @@ version = "0.3.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
|
checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "castaway"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0abae9be0aaf9ea96a3b1b8b1b55c602ca751eba1b1500220cea4ecbafe7c0d5"
|
||||||
|
dependencies = [
|
||||||
|
"rustversion",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@@ -57,16 +66,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossterm"
|
name = "compact_str"
|
||||||
version = "0.27.0"
|
version = "0.8.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df"
|
checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644"
|
||||||
|
dependencies = [
|
||||||
|
"castaway",
|
||||||
|
"cfg-if",
|
||||||
|
"itoa",
|
||||||
|
"rustversion",
|
||||||
|
"ryu",
|
||||||
|
"static_assertions",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossterm"
|
||||||
|
version = "0.28.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.4.1",
|
"bitflags 2.4.1",
|
||||||
"crossterm_winapi",
|
"crossterm_winapi",
|
||||||
"libc",
|
|
||||||
"mio",
|
"mio",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
|
"rustix",
|
||||||
"signal-hook",
|
"signal-hook",
|
||||||
"signal-hook-mio",
|
"signal-hook-mio",
|
||||||
"winapi",
|
"winapi",
|
||||||
@@ -104,6 +127,16 @@ version = "1.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
|
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "errno"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fixedbitset"
|
name = "fixedbitset"
|
||||||
version = "0.4.2"
|
version = "0.4.2"
|
||||||
@@ -133,9 +166,15 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
version = "0.4.1"
|
version = "0.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
@@ -148,20 +187,30 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indoc"
|
name = "instability"
|
||||||
version = "2.0.4"
|
version = "0.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8"
|
checksum = "b23a0c8dfe501baac4adf6ebbfa6eddf8f0c07f56b058cc1288017e32397846c"
|
||||||
|
dependencies = [
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.11.0"
|
version = "0.13.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
|
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"either",
|
"either",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.66"
|
version = "0.3.66"
|
||||||
@@ -173,9 +222,15 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.149"
|
version = "0.2.161"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
|
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "linux-raw-sys"
|
||||||
|
version = "0.4.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
@@ -204,10 +259,11 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "0.8.9"
|
version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0"
|
checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
"libc",
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
"wasi",
|
"wasi",
|
||||||
@@ -238,9 +294,9 @@ checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall 0.4.1",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"windows-targets",
|
"windows-targets 0.48.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -251,9 +307,9 @@ checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "petgraph"
|
name = "petgraph"
|
||||||
version = "0.6.4"
|
version = "0.6.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9"
|
checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fixedbitset",
|
"fixedbitset",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
@@ -315,19 +371,22 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ratatui"
|
name = "ratatui"
|
||||||
version = "0.24.0"
|
version = "0.28.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0ebc917cfb527a566c37ecb94c7e3fd098353516fb4eb6bea17015ade0182425"
|
checksum = "fdef7f9be5c0122f890d58bdf4d964349ba6a6161f705907526d891efabba57d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.4.1",
|
"bitflags 2.4.1",
|
||||||
"cassowary",
|
"cassowary",
|
||||||
|
"compact_str",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
"indoc",
|
"instability",
|
||||||
"itertools",
|
"itertools",
|
||||||
"lru",
|
"lru",
|
||||||
"paste",
|
"paste",
|
||||||
"strum",
|
"strum",
|
||||||
|
"strum_macros",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
|
"unicode-truncate",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -340,12 +399,40 @@ dependencies = [
|
|||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "redox_syscall"
|
||||||
|
version = "0.5.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.4.1",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustix"
|
||||||
|
version = "0.38.37"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.4.1",
|
||||||
|
"errno",
|
||||||
|
"libc",
|
||||||
|
"linux-raw-sys",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustversion"
|
name = "rustversion"
|
||||||
version = "1.0.14"
|
version = "1.0.14"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4"
|
checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
@@ -364,9 +451,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signal-hook-mio"
|
name = "signal-hook-mio"
|
||||||
version = "0.2.3"
|
version = "0.2.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
|
checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
@@ -389,19 +476,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
|
checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strum"
|
name = "static_assertions"
|
||||||
version = "0.25.0"
|
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 = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125"
|
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strum"
|
||||||
|
version = "0.26.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"strum_macros",
|
"strum_macros",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strum_macros"
|
name = "strum_macros"
|
||||||
version = "0.25.3"
|
version = "0.26.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0"
|
checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
@@ -434,10 +527,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
|
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-width"
|
name = "unicode-truncate"
|
||||||
version = "0.1.11"
|
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 = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
|
checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf"
|
||||||
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
|
"unicode-segmentation",
|
||||||
|
"unicode-width",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-width"
|
||||||
|
version = "0.1.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
@@ -451,6 +555,12 @@ version = "0.11.0+wasi-snapshot-preview1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasite"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen"
|
name = "wasm-bindgen"
|
||||||
version = "0.2.89"
|
version = "0.2.89"
|
||||||
@@ -517,11 +627,12 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "whoami"
|
name = "whoami"
|
||||||
version = "1.4.1"
|
version = "1.5.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50"
|
checksum = "372d5b87f58ec45c384ba03563b03544dc5fadc3983e434b286913f5b4a9bb6d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"wasm-bindgen",
|
"redox_syscall 0.5.7",
|
||||||
|
"wasite",
|
||||||
"web-sys",
|
"web-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -549,11 +660,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-sys"
|
name = "windows-sys"
|
||||||
version = "0.48.0"
|
version = "0.52.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-targets",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -562,13 +673,29 @@ version = "0.48.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows_aarch64_gnullvm",
|
"windows_aarch64_gnullvm 0.48.5",
|
||||||
"windows_aarch64_msvc",
|
"windows_aarch64_msvc 0.48.5",
|
||||||
"windows_i686_gnu",
|
"windows_i686_gnu 0.48.5",
|
||||||
"windows_i686_msvc",
|
"windows_i686_msvc 0.48.5",
|
||||||
"windows_x86_64_gnu",
|
"windows_x86_64_gnu 0.48.5",
|
||||||
"windows_x86_64_gnullvm",
|
"windows_x86_64_gnullvm 0.48.5",
|
||||||
"windows_x86_64_msvc",
|
"windows_x86_64_msvc 0.48.5",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm 0.52.6",
|
||||||
|
"windows_aarch64_msvc 0.52.6",
|
||||||
|
"windows_i686_gnu 0.52.6",
|
||||||
|
"windows_i686_gnullvm",
|
||||||
|
"windows_i686_msvc 0.52.6",
|
||||||
|
"windows_x86_64_gnu 0.52.6",
|
||||||
|
"windows_x86_64_gnullvm 0.52.6",
|
||||||
|
"windows_x86_64_msvc 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -577,42 +704,90 @@ version = "0.48.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.48.5"
|
version = "0.48.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.48.5"
|
version = "0.48.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
version = "0.48.5"
|
version = "0.48.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
version = "0.48.5"
|
version = "0.48.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
version = "0.48.5"
|
version = "0.48.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
version = "0.48.5"
|
version = "0.48.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zerocopy"
|
name = "zerocopy"
|
||||||
version = "0.7.19"
|
version = "0.7.19"
|
||||||
|
|||||||
19
Cargo.toml
19
Cargo.toml
@@ -6,8 +6,19 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ratatui = "0.24.0"
|
ratatui = "0.28.1"
|
||||||
crossterm = "0.27.0"
|
crossterm = "0.28.1"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
petgraph = "0.6.4"
|
petgraph = "0.6.5"
|
||||||
whoami = "1.4.1"
|
whoami = "1.5.2"
|
||||||
|
|
||||||
|
[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
build.rs
Normal file
10
build.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let output = Command::new("git")
|
||||||
|
.args(["rev-parse", "--short", "HEAD"])
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
let git_hash = String::from_utf8(output.stdout).unwrap();
|
||||||
|
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use rand::Rng;
|
||||||
use ratatui::style::Color;
|
use ratatui::style::Color;
|
||||||
|
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
@@ -9,7 +10,7 @@ pub trait Artifact {
|
|||||||
/// 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;
|
||||||
/// call to apply the effects of the artifact to the player
|
/// call to apply the effects of the artifact to the player
|
||||||
fn collect(&mut self, player: &mut Player);
|
fn collect(&mut self, player: &mut Player, messages: &mut Vec<String>);
|
||||||
/// returns if the artifact was collected and can be removed from the level
|
/// returns if the artifact was collected and can be removed from the level
|
||||||
fn was_collected(&self) -> bool;
|
fn was_collected(&self) -> bool;
|
||||||
}
|
}
|
||||||
@@ -22,11 +23,11 @@ pub struct Chest {
|
|||||||
|
|
||||||
impl Chest {
|
impl Chest {
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,9 +37,15 @@ impl Artifact for Chest {
|
|||||||
("C", Color::Blue)
|
("C", Color::Blue)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_immutable_position(&self) -> &Position { &self.position }
|
fn get_immutable_position(&self) -> &Position {
|
||||||
fn collect(&mut self, player: &mut Player) {
|
&self.position
|
||||||
|
}
|
||||||
|
fn collect(&mut self, player: &mut Player, messages: &mut Vec<String>) {
|
||||||
player.retrieve_gold(self.gold);
|
player.retrieve_gold(self.gold);
|
||||||
|
messages.insert(
|
||||||
|
0,
|
||||||
|
format!("opened chest and collected {} gold.", self.gold).to_string(),
|
||||||
|
);
|
||||||
self.gold = 0;
|
self.gold = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,9 +62,11 @@ pub struct Potion {
|
|||||||
|
|
||||||
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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,12 +75,25 @@ 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 { &self.position }
|
fn get_immutable_position(&self) -> &Position {
|
||||||
fn collect(&mut self, player: &mut Player) {
|
&self.position
|
||||||
|
}
|
||||||
|
fn collect(&mut self, player: &mut Player, messages: &mut Vec<String>) {
|
||||||
// only consume potion of the player can gain at least one health point
|
// only consume potion of the player can gain at least one health point
|
||||||
if !player.is_healthy() {
|
if !player.is_healthy() {
|
||||||
|
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();
|
||||||
|
messages.insert(
|
||||||
|
0,
|
||||||
|
format!("picked up potion and gained {} health.", new - old).to_string(),
|
||||||
|
);
|
||||||
self.health = 0;
|
self.health = 0;
|
||||||
|
} else {
|
||||||
|
messages.insert(
|
||||||
|
0,
|
||||||
|
"not using the potion because you're healthy.".to_string(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
94
src/game.rs
94
src/game.rs
@@ -9,20 +9,20 @@ pub const LEVELS: usize = 10;
|
|||||||
/// represents a state of a game
|
/// represents a state of a game
|
||||||
pub enum GameState {
|
pub enum GameState {
|
||||||
/// the game is ongoing (neither won or lost)
|
/// the game is ongoing (neither won or lost)
|
||||||
RUNNING,
|
Running,
|
||||||
/// the player died
|
/// the player died
|
||||||
LOST,
|
Lost,
|
||||||
/// the player reached the Ω
|
/// the player reached the Ω
|
||||||
WON,
|
Won,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// the main structure to hold all information about the ongoing game
|
/// the main structure to hold all information about the ongoing game
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
/// the player
|
/// the player
|
||||||
player: Player,
|
player: Player,
|
||||||
/// the levels of the game
|
/// the levels of the game
|
||||||
levels: Vec<Level>,
|
levels: Vec<Level>,
|
||||||
|
pub messages: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
@@ -34,12 +34,14 @@ impl Game {
|
|||||||
let mut g = Game {
|
let mut g = Game {
|
||||||
player: p,
|
player: p,
|
||||||
levels: v,
|
levels: v,
|
||||||
|
messages: Vec::with_capacity(10),
|
||||||
};
|
};
|
||||||
let start = {
|
let start = { g.get_level(0).start };
|
||||||
g.get_level(0).start
|
g.get_mutable_player()
|
||||||
};
|
.get_position()
|
||||||
g.get_mutable_player().get_position().set(0, start.0, start.1);
|
.set(0, start.0, start.1);
|
||||||
g.get_mutable_level(0).discover(&Position::new(0, start.0, start.1));
|
g.get_mutable_level(0)
|
||||||
|
.discover(&Position::new(0, start.0, start.1));
|
||||||
g
|
g
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,9 +50,8 @@ impl Game {
|
|||||||
match self.next_element(0, 0) {
|
match self.next_element(0, 0) {
|
||||||
None => {}
|
None => {}
|
||||||
Some(a) => {
|
Some(a) => {
|
||||||
match a {
|
if a == StructureElement::End {
|
||||||
StructureElement::End => { return true; }
|
return true;
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -59,12 +60,12 @@ impl Game {
|
|||||||
/// returns the state of the game (depending on player's life and position)
|
/// returns the state of the game (depending on player's life and position)
|
||||||
pub fn get_game_state(&mut self) -> GameState {
|
pub fn get_game_state(&mut self) -> GameState {
|
||||||
if self.player.is_dead() {
|
if self.player.is_dead() {
|
||||||
return GameState::LOST;
|
return GameState::Lost;
|
||||||
}
|
}
|
||||||
if self.player_reached_goal() {
|
if self.player_reached_goal() {
|
||||||
return GameState::WON;
|
return GameState::Won;
|
||||||
}
|
}
|
||||||
GameState::RUNNING
|
GameState::Running
|
||||||
}
|
}
|
||||||
pub fn get_player(&self) -> &Player {
|
pub fn get_player(&self) -> &Player {
|
||||||
&self.player
|
&self.player
|
||||||
@@ -72,8 +73,12 @@ impl Game {
|
|||||||
pub fn get_mutable_player(&mut self) -> &mut Player {
|
pub fn get_mutable_player(&mut self) -> &mut Player {
|
||||||
&mut self.player
|
&mut self.player
|
||||||
}
|
}
|
||||||
pub fn get_level(&self, level: usize) -> &Level { &self.levels[level] }
|
pub fn get_level(&self, level: usize) -> &Level {
|
||||||
pub fn get_mutable_level(&mut self, level: usize) -> &mut Level { &mut self.levels[level] }
|
&self.levels[level]
|
||||||
|
}
|
||||||
|
pub fn get_mutable_level(&mut self, level: usize) -> &mut Level {
|
||||||
|
&mut self.levels[level]
|
||||||
|
}
|
||||||
/// check if the player can move in the given direction. This only checks for structure element
|
/// check if the player can move in the given direction. This only checks for structure element
|
||||||
/// limitation as walls.
|
/// limitation as walls.
|
||||||
fn can_move(&mut self, dx: i16, dy: i16) -> bool {
|
fn can_move(&mut self, dx: i16, dy: i16) -> bool {
|
||||||
@@ -118,32 +123,39 @@ impl Game {
|
|||||||
let structure = self.next_element(dx, dy);
|
let structure = self.next_element(dx, dy);
|
||||||
|
|
||||||
// use stairs if walked onto one
|
// use stairs if walked onto one
|
||||||
match structure {
|
if let Some(e) = structure {
|
||||||
Some(e) => {
|
|
||||||
match e {
|
match e {
|
||||||
StructureElement::StairDown => {
|
StructureElement::StairDown => {
|
||||||
(dx, dy) = (0, 0);
|
(dx, dy) = (0, 0);
|
||||||
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.get_mutable_player().get_position().set(next_level, x, y);
|
self.messages
|
||||||
|
.insert(0, format!("you climb down to level {}.", next_level));
|
||||||
|
self.get_mutable_player()
|
||||||
|
.get_position()
|
||||||
|
.set(next_level, x, y);
|
||||||
}
|
}
|
||||||
StructureElement::StairUp => {
|
StructureElement::StairUp => {
|
||||||
(dx, dy) = (0, 0);
|
(dx, dy) = (0, 0);
|
||||||
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.get_mutable_player().get_position().set(next_level, x, y);
|
self.messages
|
||||||
}
|
.insert(0, format!("you climb up to level {}.", next_level));
|
||||||
_ => {}
|
self.get_mutable_player()
|
||||||
}
|
.get_position()
|
||||||
|
.set(next_level, x, y);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// update position and discover area on new position
|
// update position and discover area on new position
|
||||||
let (new_x, new_y) = self.get_mutable_player().get_position().change(dx, dy);
|
let (new_x, new_y) = self.get_mutable_player().get_position().change(dx, dy);
|
||||||
self.get_mutable_level(player_level).discover(&Position::new(player_level, new_x, new_y));
|
self.get_mutable_level(player_level)
|
||||||
|
.discover(&Position::new(player_level, new_x, new_y));
|
||||||
(-dx, -dy)
|
(-dx, -dy)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn player_fights_monster(&mut self) -> bool {
|
pub fn player_fights_monster(&mut self) -> bool {
|
||||||
let player_pos = &self.player.get_immutable_position();
|
let player_pos = &self.player.get_immutable_position();
|
||||||
let player_level = player_pos.get_level();
|
let player_level = player_pos.get_level();
|
||||||
@@ -152,12 +164,32 @@ impl Game {
|
|||||||
match m {
|
match m {
|
||||||
None => {}
|
None => {}
|
||||||
Some(m) => {
|
Some(m) => {
|
||||||
// TODO fight the monster
|
let player_dmg = self.player.damage();
|
||||||
self.player.change_life(-1);
|
m.decrease_life(player_dmg);
|
||||||
m.decrease_life(1);
|
|
||||||
// monster died, player gains experience
|
|
||||||
if m.is_dead() {
|
if m.is_dead() {
|
||||||
self.player.gain_experience(m.get_experience_gain());
|
self.player.gain_experience(m.get_experience_gain());
|
||||||
|
self.messages.insert(
|
||||||
|
0,
|
||||||
|
format!(
|
||||||
|
"you hit {} for {} damage and kill it.",
|
||||||
|
m.get_name(),
|
||||||
|
player_dmg
|
||||||
|
)
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
self.messages.insert(
|
||||||
|
0,
|
||||||
|
format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(),
|
||||||
|
);
|
||||||
|
let monster_dmg = m.damage() as i16;
|
||||||
|
self.player.change_life(-monster_dmg);
|
||||||
|
|
||||||
|
self.messages.insert(
|
||||||
|
0,
|
||||||
|
format!("{} hits you for {} damage.", m.get_name(), monster_dmg)
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return m.is_dead();
|
return m.is_dead();
|
||||||
}
|
}
|
||||||
@@ -172,7 +204,7 @@ impl Game {
|
|||||||
match a {
|
match a {
|
||||||
None => {}
|
None => {}
|
||||||
Some(a) => {
|
Some(a) => {
|
||||||
a.collect(&mut self.player);
|
a.collect(&mut self.player, &mut self.messages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,7 +213,7 @@ impl Game {
|
|||||||
let player_pos = &self.player.get_immutable_position();
|
let player_pos = &self.player.get_immutable_position();
|
||||||
let player_level = player_pos.get_level();
|
let player_level = player_pos.get_level();
|
||||||
let level = &mut self.levels[player_level];
|
let level = &mut self.levels[player_level];
|
||||||
level.update(ticks, &mut self.player);
|
level.update(ticks, &mut self.player, &mut self.messages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
92
src/level.rs
92
src/level.rs
@@ -1,14 +1,14 @@
|
|||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
use rand::Rng;
|
|
||||||
use rand::rngs::ThreadRng;
|
use rand::rngs::ThreadRng;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
use crate::artifacts::{Chest, Potion};
|
|
||||||
use crate::artifacts::Artifact;
|
use crate::artifacts::Artifact;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::monster::{Orc, Rat};
|
use crate::artifacts::{Chest, Potion};
|
||||||
use crate::monster::Monster;
|
use crate::monster::Monster;
|
||||||
|
#[cfg(test)]
|
||||||
|
use crate::monster::{Orc, Rat};
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
use crate::position::HasPosition;
|
use crate::position::HasPosition;
|
||||||
use crate::position::Position;
|
use crate::position::Position;
|
||||||
@@ -27,6 +27,9 @@ pub enum StructureElement {
|
|||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PossibleArtifact<'a> = Option<&'a mut Box<(dyn Artifact + 'static)>>;
|
||||||
|
type PossibleMonster<'a> = Option<&'a mut Box<(dyn Monster + 'static)>>;
|
||||||
|
|
||||||
pub struct Level {
|
pub struct Level {
|
||||||
pub(crate) level: usize,
|
pub(crate) level: usize,
|
||||||
pub(crate) structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
pub(crate) structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
||||||
@@ -41,7 +44,11 @@ pub struct Level {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Level {
|
impl Level {
|
||||||
pub fn get_element(&mut self, x: i16, y: i16) -> (Option<StructureElement>, Option<&mut Box<(dyn Monster + 'static)>>, Option<&mut Box<(dyn Artifact + 'static)>>) {
|
pub fn get_element(
|
||||||
|
&mut self,
|
||||||
|
x: i16,
|
||||||
|
y: i16,
|
||||||
|
) -> (Option<StructureElement>, PossibleMonster, PossibleArtifact) {
|
||||||
if x < 0 || y < 0 {
|
if x < 0 || y < 0 {
|
||||||
return (None, None, None);
|
return (None, None, None);
|
||||||
}
|
}
|
||||||
@@ -112,7 +119,7 @@ impl Level {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn update(&mut self, ticks: u128, player: &mut Player) {
|
pub fn update(&mut self, ticks: u128, player: &mut Player, messages: &mut Vec<String>) {
|
||||||
for (index, a) in &mut self.artifacts.iter().enumerate() {
|
for (index, a) in &mut self.artifacts.iter().enumerate() {
|
||||||
if a.was_collected() {
|
if a.was_collected() {
|
||||||
self.artifacts.remove(index);
|
self.artifacts.remove(index);
|
||||||
@@ -129,20 +136,54 @@ impl Level {
|
|||||||
loop {
|
loop {
|
||||||
// calculate the direction the monster will try to walk
|
// calculate the direction the monster will try to walk
|
||||||
let (dx, dy) = match self.rng.gen_range(0..5) {
|
let (dx, dy) = match self.rng.gen_range(0..5) {
|
||||||
1 => { (1, 0) }
|
1 => (1, 0),
|
||||||
2 => { (-1, 0) }
|
2 => (-1, 0),
|
||||||
3 => { (0, 1) }
|
3 => (0, 1),
|
||||||
4 => { (0, -1) }
|
4 => (0, -1),
|
||||||
_ => { (0, 0) }
|
_ => (0, 0),
|
||||||
};
|
};
|
||||||
if self.can_monster_move(self.monsters[index].as_ref(), dx, dy) {
|
if self.can_monster_move(self.monsters[index].as_ref(), dx, dy) {
|
||||||
let (new_x, new_y) = self.monsters[index].get_position().change(dx, dy);
|
let (new_x, new_y) = self.monsters[index].get_position().change(dx, dy);
|
||||||
if player.get_immutable_position().get_x() == new_x && player.get_immutable_position().get_y() == new_y {
|
if player.get_immutable_position().get_x() == new_x
|
||||||
self.monsters[index].decrease_life(1);
|
&& player.get_immutable_position().get_y() == new_y
|
||||||
player.change_life(-1);
|
{
|
||||||
|
let monster_dmg = self.monsters[index].damage() as i16;
|
||||||
|
player.change_life(-monster_dmg);
|
||||||
|
messages.insert(
|
||||||
|
0,
|
||||||
|
format!(
|
||||||
|
"{} hits you for {} damage.",
|
||||||
|
self.monsters[index].get_name(),
|
||||||
|
monster_dmg
|
||||||
|
)
|
||||||
|
.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 the attack did not kill the opponent, back down
|
||||||
if !player.is_dead() {
|
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);
|
self.monsters[index].get_position().change(-dx, -dy);
|
||||||
|
} else {
|
||||||
|
messages.insert(
|
||||||
|
0,
|
||||||
|
format!(
|
||||||
|
"you hit {} for {} damage.",
|
||||||
|
self.monsters[index].get_name(),
|
||||||
|
player_dmg
|
||||||
|
)
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -162,13 +203,16 @@ impl Level {
|
|||||||
let new_x: usize = (agent_pos.get_x() as i16 + dx) as usize;
|
let new_x: usize = (agent_pos.get_x() as i16 + dx) as usize;
|
||||||
let new_y: usize = (agent_pos.get_y() as i16 + dy) as usize;
|
let new_y: usize = (agent_pos.get_y() as i16 + dy) as usize;
|
||||||
|
|
||||||
|
// verify we cannot leave the playing field
|
||||||
if new_x >= LEVEL_WIDTH || new_y >= LEVEL_HEIGHT {
|
if new_x >= LEVEL_WIDTH || new_y >= LEVEL_HEIGHT {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for index in 0..self.monsters.len() {
|
for index in 0..self.monsters.len() {
|
||||||
let pos = self.monsters[index].get_immutable_position();
|
let pos = self.monsters[index].get_immutable_position();
|
||||||
if pos.get_x() == new_x && pos.get_y() == new_y { return false; }
|
if pos.get_x() == new_x && pos.get_y() == new_y {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.structure[new_x][new_y] != StructureElement::Wall
|
self.structure[new_x][new_y] != StructureElement::Wall
|
||||||
}
|
}
|
||||||
@@ -176,6 +220,12 @@ impl Level {
|
|||||||
let agent_pos = agent.get_immutable_position();
|
let agent_pos = agent.get_immutable_position();
|
||||||
let new_x: usize = (agent_pos.get_x() as i16 + dx) as usize;
|
let new_x: usize = (agent_pos.get_x() as i16 + dx) as usize;
|
||||||
let new_y: usize = (agent_pos.get_y() as i16 + dy) as usize;
|
let new_y: usize = (agent_pos.get_y() as i16 + dy) as usize;
|
||||||
|
|
||||||
|
// verify we cannot leave the playing field
|
||||||
|
if new_x >= LEVEL_WIDTH || new_y >= LEVEL_HEIGHT {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
self.structure[new_x][new_y] != StructureElement::Wall
|
self.structure[new_x][new_y] != StructureElement::Wall
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -230,8 +280,16 @@ fn test_get_element() {
|
|||||||
let mut l = Level::new(0);
|
let mut l = Level::new(0);
|
||||||
assert_eq!(l.get_element(-1, -1).0, None);
|
assert_eq!(l.get_element(-1, -1).0, None);
|
||||||
assert_eq!(l.get_element(0, 0).0.unwrap(), StructureElement::Unknown);
|
assert_eq!(l.get_element(0, 0).0.unwrap(), StructureElement::Unknown);
|
||||||
assert_eq!(l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1).0.unwrap(), StructureElement::Unknown);
|
assert_eq!(
|
||||||
assert_eq!(l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16).0, None);
|
l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1)
|
||||||
|
.0
|
||||||
|
.unwrap(),
|
||||||
|
StructureElement::Unknown
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16).0,
|
||||||
|
None
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ use petgraph::data::*;
|
|||||||
use petgraph::graph::Graph;
|
use petgraph::graph::Graph;
|
||||||
use petgraph::graph::UnGraph;
|
use petgraph::graph::UnGraph;
|
||||||
use rand::prelude::SliceRandom;
|
use rand::prelude::SliceRandom;
|
||||||
use rand::Rng;
|
|
||||||
use rand::rngs::ThreadRng;
|
use rand::rngs::ThreadRng;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
use crate::artifacts::{Artifact, Chest, Potion};
|
use crate::artifacts::{Artifact, Chest, Potion};
|
||||||
use crate::level::{Level, StructureElement};
|
use crate::level::{Level, StructureElement};
|
||||||
use crate::monster::{Monster, Orc, Rat};
|
use crate::monster::{Monster, Orc, Rat, Snake};
|
||||||
use crate::position::Position;
|
use crate::position::Position;
|
||||||
|
|
||||||
const ROOMS_VERTICAL: usize = 7;
|
const ROOMS_VERTICAL: usize = 7;
|
||||||
@@ -93,7 +93,8 @@ impl LevelGenerator {
|
|||||||
rooms[0][0].kind = RoomType::BasicRoom;
|
rooms[0][0].kind = RoomType::BasicRoom;
|
||||||
|
|
||||||
while !LevelGenerator::rooms_connectable(&rooms) {
|
while !LevelGenerator::rooms_connectable(&rooms) {
|
||||||
let mut room_types: Vec<RoomType> = Vec::with_capacity(ROOMS_HORIZONTAL * ROOMS_VERTICAL);
|
let mut room_types: Vec<RoomType> =
|
||||||
|
Vec::with_capacity(ROOMS_HORIZONTAL * ROOMS_VERTICAL);
|
||||||
// level 0 contains a start room, all others contain a stair up
|
// level 0 contains a start room, all others contain a stair up
|
||||||
if first {
|
if first {
|
||||||
room_types.push(RoomType::Start);
|
room_types.push(RoomType::Start);
|
||||||
@@ -112,21 +113,21 @@ impl LevelGenerator {
|
|||||||
for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL {
|
for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL {
|
||||||
match rng.gen_range(1..=100) {
|
match rng.gen_range(1..=100) {
|
||||||
// TODO tune room type distribution
|
// TODO tune room type distribution
|
||||||
1..=33 => { room_types.push(RoomType::EmptyRoom) }
|
1..=33 => room_types.push(RoomType::EmptyRoom),
|
||||||
34..=66 => { room_types.push(RoomType::TreasureRoom) }
|
34..=66 => room_types.push(RoomType::TreasureRoom),
|
||||||
67..=90 => { room_types.push(RoomType::MonsterRoom) }
|
67..=90 => room_types.push(RoomType::MonsterRoom),
|
||||||
_ => { room_types.push(RoomType::BasicRoom) }
|
_ => room_types.push(RoomType::BasicRoom),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
room_types.shuffle(&mut rng);
|
room_types.shuffle(&mut rng);
|
||||||
|
|
||||||
graph.clear();
|
graph.clear();
|
||||||
// place the rooms in the array an add nodes to the graph for every non empty room
|
// place the rooms in the array an add nodes to the graph for every non empty room
|
||||||
for c in 0..ROOMS_VERTICAL {
|
for (c, rs) in rooms.iter_mut().enumerate().take(ROOMS_VERTICAL) {
|
||||||
for r in 0..ROOMS_HORIZONTAL {
|
for (r, room) in rs.iter_mut().enumerate().take(ROOMS_HORIZONTAL) {
|
||||||
rooms[c][r].kind = room_types.pop().unwrap();
|
room.kind = room_types.pop().unwrap();
|
||||||
if rooms[c][r].kind != RoomType::EmptyRoom {
|
if room.kind != RoomType::EmptyRoom {
|
||||||
rooms[c][r].random(&mut rng);
|
room.random(&mut rng);
|
||||||
graph.add_node((c, r));
|
graph.add_node((c, r));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,15 +143,21 @@ impl LevelGenerator {
|
|||||||
let src_index = graph.node_indices().find(|i| graph[*i] == (c, r)).unwrap();
|
let src_index = graph.node_indices().find(|i| graph[*i] == (c, r)).unwrap();
|
||||||
for r_1 in r + 1..ROOMS_HORIZONTAL {
|
for r_1 in r + 1..ROOMS_HORIZONTAL {
|
||||||
if rooms[c][r_1].kind != RoomType::EmptyRoom {
|
if rooms[c][r_1].kind != RoomType::EmptyRoom {
|
||||||
let tgt_index = graph.node_indices().find(|i| graph[*i] == (c, r_1)).unwrap();
|
let tgt_index = graph
|
||||||
|
.node_indices()
|
||||||
|
.find(|i| graph[*i] == (c, r_1))
|
||||||
|
.unwrap();
|
||||||
// todo use random weight for edge
|
// todo use random weight for edge
|
||||||
graph.add_edge(src_index, tgt_index, 1);
|
graph.add_edge(src_index, tgt_index, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for c_1 in c + 1..ROOMS_VERTICAL {
|
for (c_1, rs) in rooms.iter().enumerate().take(ROOMS_VERTICAL).skip(c + 1) {
|
||||||
if rooms[c_1][r].kind != RoomType::EmptyRoom {
|
if rs[r].kind != RoomType::EmptyRoom {
|
||||||
let tgt_index = graph.node_indices().find(|i| graph[*i] == (c_1, r)).unwrap();
|
let tgt_index = graph
|
||||||
|
.node_indices()
|
||||||
|
.find(|i| graph[*i] == (c_1, r))
|
||||||
|
.unwrap();
|
||||||
// todo use random weight for edge
|
// todo use random weight for edge
|
||||||
graph.add_edge(src_index, tgt_index, 1);
|
graph.add_edge(src_index, tgt_index, 1);
|
||||||
break;
|
break;
|
||||||
@@ -160,7 +167,8 @@ impl LevelGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// calculate a minimum spanning tree
|
// calculate a minimum spanning tree
|
||||||
let mst: Graph<(usize, usize), u16, petgraph::Undirected> = Graph::from_elements(min_spanning_tree(&graph));
|
let mst: Graph<(usize, usize), u16, petgraph::Undirected> =
|
||||||
|
Graph::from_elements(min_spanning_tree(&graph));
|
||||||
for edge in mst.raw_edges() {
|
for edge in mst.raw_edges() {
|
||||||
let src = mst[edge.source()];
|
let src = mst[edge.source()];
|
||||||
let tgt = mst[edge.target()];
|
let tgt = mst[edge.target()];
|
||||||
@@ -170,24 +178,31 @@ impl LevelGenerator {
|
|||||||
|
|
||||||
// cols are the same, either up or down
|
// cols are the same, either up or down
|
||||||
if src.0 == tgt.0 {
|
if src.0 == tgt.0 {
|
||||||
let range = LevelGenerator::range_overlap(src_room.get_x_range(), tgt_room.get_x_range());
|
let range =
|
||||||
let position: usize;
|
LevelGenerator::range_overlap(src_room.get_x_range(), tgt_room.get_x_range());
|
||||||
if range.is_empty() {
|
let position: usize = if range.is_empty() {
|
||||||
position = range.start;
|
range.start
|
||||||
} else {
|
} else {
|
||||||
position = rng.gen_range(range);
|
rng.gen_range(range)
|
||||||
}
|
};
|
||||||
if src.1 < tgt.1 {
|
if src.1 < tgt.1 {
|
||||||
// src to tgt
|
// src to tgt
|
||||||
rooms[src.0][src.1].connection_down = Some(ConnectionInfo { offset: position, distance: tgt.1 - src.1 });
|
rooms[src.0][src.1].connection_down = Some(ConnectionInfo {
|
||||||
|
offset: position,
|
||||||
|
distance: tgt.1 - src.1,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// tgt to src
|
// tgt to src
|
||||||
tgt_room.connection_down = Some(ConnectionInfo { offset: position, distance: src.1 - tgt.1 });
|
tgt_room.connection_down = Some(ConnectionInfo {
|
||||||
|
offset: position,
|
||||||
|
distance: src.1 - tgt.1,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// rows are the same, either left or right
|
// rows are the same, either left or right
|
||||||
if src.1 == tgt.1 {
|
if src.1 == tgt.1 {
|
||||||
let range = LevelGenerator::range_overlap(src_room.get_y_range(), tgt_room.get_y_range());
|
let range =
|
||||||
|
LevelGenerator::range_overlap(src_room.get_y_range(), tgt_room.get_y_range());
|
||||||
let mut position: usize;
|
let mut position: usize;
|
||||||
if range.is_empty() {
|
if range.is_empty() {
|
||||||
position = range.start;
|
position = range.start;
|
||||||
@@ -199,18 +214,21 @@ impl LevelGenerator {
|
|||||||
}
|
}
|
||||||
if src.0 < tgt.0 {
|
if src.0 < tgt.0 {
|
||||||
// src to tgt
|
// src to tgt
|
||||||
rooms[src.0][src.1].connection_right = Some(ConnectionInfo { offset: position, distance: tgt.0 - src.0 });
|
rooms[src.0][src.1].connection_right = Some(ConnectionInfo {
|
||||||
|
offset: position,
|
||||||
|
distance: tgt.0 - src.0,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// tgt to src
|
// tgt to src
|
||||||
tgt_room.connection_right = Some(ConnectionInfo { offset: position, distance: src.1 - tgt.1 });
|
tgt_room.connection_right = Some(ConnectionInfo {
|
||||||
|
offset: position,
|
||||||
|
distance: src.1 - tgt.1,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LevelGenerator {
|
LevelGenerator { rooms, level }
|
||||||
rooms,
|
|
||||||
level,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn range_overlap(r1: Range<usize>, r2: Range<usize>) -> Range<usize> {
|
fn range_overlap(r1: Range<usize>, r2: Range<usize>) -> Range<usize> {
|
||||||
@@ -224,8 +242,8 @@ impl LevelGenerator {
|
|||||||
for r in 0..ROOMS_HORIZONTAL {
|
for r in 0..ROOMS_HORIZONTAL {
|
||||||
if rooms[c][r].kind != RoomType::EmptyRoom {
|
if rooms[c][r].kind != RoomType::EmptyRoom {
|
||||||
let mut connected = 0;
|
let mut connected = 0;
|
||||||
for c1 in 0..ROOMS_VERTICAL {
|
for room in rooms.iter().take(ROOMS_VERTICAL) {
|
||||||
if rooms[c1][r].kind != RoomType::EmptyRoom {
|
if room[r].kind != RoomType::EmptyRoom {
|
||||||
connected += 1;
|
connected += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -240,11 +258,12 @@ impl LevelGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
true
|
||||||
}
|
}
|
||||||
pub fn render(&self) -> Level {
|
pub fn render(&self) -> Level {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let mut structure = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH];
|
let mut structure = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT];
|
||||||
|
1 + ROOMS_VERTICAL * ROOM_WIDTH];
|
||||||
let mut artifacts: Vec<Box<dyn Artifact>> = Vec::with_capacity(10);
|
let mut artifacts: Vec<Box<dyn Artifact>> = Vec::with_capacity(10);
|
||||||
let mut enemies: Vec<Box<dyn Monster>> = Vec::with_capacity(10);
|
let mut enemies: Vec<Box<dyn Monster>> = Vec::with_capacity(10);
|
||||||
let mut start_x: usize = 0;
|
let mut start_x: usize = 0;
|
||||||
@@ -258,7 +277,8 @@ impl LevelGenerator {
|
|||||||
let room = self.rooms[c][r];
|
let room = self.rooms[c][r];
|
||||||
for x in 0..room.width {
|
for x in 0..room.width {
|
||||||
for y in 0..room.height {
|
for y in 0..room.height {
|
||||||
structure[left + room.offset_x + x][top + room.offset_y + y] = StructureElement::Floor;
|
structure[left + room.offset_x + x][top + room.offset_y + y] =
|
||||||
|
StructureElement::Floor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if room.kind == RoomType::TreasureRoom {
|
if room.kind == RoomType::TreasureRoom {
|
||||||
@@ -266,8 +286,14 @@ impl LevelGenerator {
|
|||||||
let t_y = top + room.offset_y + rng.gen_range(0..room.height);
|
let t_y = top + room.offset_y + rng.gen_range(0..room.height);
|
||||||
// TODO randomize artifacts
|
// TODO randomize artifacts
|
||||||
match rng.gen_range(1..=100) {
|
match rng.gen_range(1..=100) {
|
||||||
1..=50 => { artifacts.push(Box::new(Chest::new(Position::new(self.level, t_x, t_y)))); }
|
1..=50 => {
|
||||||
_ => { artifacts.push(Box::new(Potion::new(Position::new(self.level, t_x, t_y)))); }
|
artifacts
|
||||||
|
.push(Box::new(Chest::new(Position::new(self.level, t_x, t_y))));
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
artifacts
|
||||||
|
.push(Box::new(Potion::new(Position::new(self.level, t_x, t_y))));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if room.kind == RoomType::MonsterRoom {
|
if room.kind == RoomType::MonsterRoom {
|
||||||
@@ -275,11 +301,20 @@ impl LevelGenerator {
|
|||||||
let t_y = top + room.offset_y + rng.gen_range(0..room.height);
|
let t_y = top + room.offset_y + rng.gen_range(0..room.height);
|
||||||
// TODO randomize enemies here
|
// TODO randomize enemies here
|
||||||
match rng.gen_range(1..=100) {
|
match rng.gen_range(1..=100) {
|
||||||
1..=50 => {
|
1..=30 => {
|
||||||
enemies.push(Box::new(Orc::new_with_position(Position::new(self.level, t_x, t_y))));
|
enemies.push(Box::new(Orc::new_with_position(Position::new(
|
||||||
|
self.level, t_x, t_y,
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
31..=60 => {
|
||||||
|
enemies.push(Box::new(Snake::new_with_position(Position::new(
|
||||||
|
self.level, t_x, t_y,
|
||||||
|
))));
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
enemies.push(Box::new(Rat::new_with_position(Position::new(self.level, t_x, t_y))));
|
enemies.push(Box::new(Rat::new_with_position(Position::new(
|
||||||
|
self.level, t_x, t_y,
|
||||||
|
))));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -315,7 +350,10 @@ impl LevelGenerator {
|
|||||||
let tgt_room = self.rooms[c][r + x_conn.distance];
|
let tgt_room = self.rooms[c][r + x_conn.distance];
|
||||||
let top = 1 + r * ROOM_HEIGHT + src_room.offset_y;
|
let top = 1 + r * ROOM_HEIGHT + src_room.offset_y;
|
||||||
let left = 1 + c * ROOM_WIDTH + x_conn.offset;
|
let left = 1 + c * ROOM_WIDTH + x_conn.offset;
|
||||||
let bottom = 1 + (r + x_conn.distance) * ROOM_HEIGHT + tgt_room.offset_y + tgt_room.height;
|
let bottom = 1
|
||||||
|
+ (r + x_conn.distance) * ROOM_HEIGHT
|
||||||
|
+ tgt_room.offset_y
|
||||||
|
+ tgt_room.height;
|
||||||
for i in top..bottom {
|
for i in top..bottom {
|
||||||
if structure[left][i] == StructureElement::Wall {
|
if structure[left][i] == StructureElement::Wall {
|
||||||
structure[left][i] = StructureElement::Floor;
|
structure[left][i] = StructureElement::Floor;
|
||||||
@@ -327,10 +365,11 @@ impl LevelGenerator {
|
|||||||
|
|
||||||
let top = 1 + r * ROOM_HEIGHT + src_room.offset_y + y_conn.offset - 1;
|
let top = 1 + r * ROOM_HEIGHT + src_room.offset_y + y_conn.offset - 1;
|
||||||
let left = 1 + c * ROOM_WIDTH + src_room.offset_x;
|
let left = 1 + c * ROOM_WIDTH + src_room.offset_x;
|
||||||
let right = 1 + (c + y_conn.distance) * ROOM_WIDTH + tgt_room.offset_x + tgt_room.width;
|
let right =
|
||||||
for i in left..right {
|
1 + (c + y_conn.distance) * ROOM_WIDTH + tgt_room.offset_x + tgt_room.width;
|
||||||
if structure[i][top] == StructureElement::Wall {
|
for room in structure.iter_mut().take(right).skip(left) {
|
||||||
structure[i][top] = StructureElement::Floor;
|
if room[top] == StructureElement::Wall {
|
||||||
|
room[top] = StructureElement::Floor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -339,7 +378,8 @@ impl LevelGenerator {
|
|||||||
Level {
|
Level {
|
||||||
level: self.level,
|
level: self.level,
|
||||||
structure,
|
structure,
|
||||||
discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH],
|
discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT];
|
||||||
|
1 + ROOMS_VERTICAL * ROOM_WIDTH],
|
||||||
monsters: enemies,
|
monsters: enemies,
|
||||||
artifacts,
|
artifacts,
|
||||||
start: (start_x, start_y),
|
start: (start_x, start_y),
|
||||||
|
|||||||
@@ -13,11 +13,7 @@ pub struct LevelWidget {}
|
|||||||
|
|
||||||
impl LevelWidget {
|
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.
|
buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg);
|
||||||
get_mut(x, y).
|
|
||||||
set_symbol(symbol).
|
|
||||||
set_bg(bg).
|
|
||||||
set_fg(fg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +45,7 @@ impl StatefulWidget for LevelWidget {
|
|||||||
self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray);
|
self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray);
|
||||||
}
|
}
|
||||||
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, Color::Gray);
|
||||||
}
|
}
|
||||||
StructureElement::Floor => {
|
StructureElement::Floor => {
|
||||||
|
|||||||
130
src/main.rs
130
src/main.rs
@@ -1,42 +1,41 @@
|
|||||||
use std::io::Result;
|
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
|
use std::io::Result;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
event::{self, KeyCode, KeyEventKind},
|
event::{self, KeyCode, KeyEventKind},
|
||||||
ExecutableCommand,
|
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||||
};
|
ExecutableCommand,
|
||||||
use ratatui::{
|
|
||||||
prelude::{CrosstermBackend, Stylize, Terminal},
|
|
||||||
widgets::Paragraph,
|
|
||||||
};
|
};
|
||||||
use ratatui::prelude::*;
|
use ratatui::prelude::*;
|
||||||
use ratatui::widgets::{Block, Borders, BorderType, Wrap};
|
|
||||||
use ratatui::widgets::block::{Position, Title};
|
use ratatui::widgets::block::{Position, Title};
|
||||||
|
use ratatui::widgets::{Block, BorderType, Borders, Wrap};
|
||||||
|
use ratatui::{
|
||||||
|
prelude::{CrosstermBackend, Terminal},
|
||||||
|
widgets::Paragraph,
|
||||||
|
};
|
||||||
use whoami::realname;
|
use whoami::realname;
|
||||||
|
|
||||||
use crate::game::{Game, GameState};
|
use crate::game::{Game, GameState};
|
||||||
use crate::level_widget::LevelWidget;
|
use crate::level_widget::LevelWidget;
|
||||||
// use crate::level_widget::LevelWidget;
|
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
use crate::position::HasPosition;
|
use crate::position::HasPosition;
|
||||||
|
|
||||||
mod game;
|
|
||||||
mod player;
|
|
||||||
mod level;
|
|
||||||
mod position;
|
|
||||||
mod level_widget;
|
|
||||||
mod level_generator;
|
|
||||||
mod artifacts;
|
mod artifacts;
|
||||||
|
mod game;
|
||||||
|
mod level;
|
||||||
|
mod level_generator;
|
||||||
|
mod level_widget;
|
||||||
mod monster;
|
mod monster;
|
||||||
|
mod player;
|
||||||
|
mod position;
|
||||||
|
|
||||||
/// length of a game frame in ms
|
/// length of a game frame in ms
|
||||||
pub const FRAME_LENGTH: u64 = 100;
|
pub const FRAME_LENGTH: u64 = 100;
|
||||||
|
|
||||||
//
|
//
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut game = Game::new(Player::new(realname().as_str(), 10));
|
let mut game = Game::new(Player::new(realname().as_str(), 30));
|
||||||
|
|
||||||
stdout().execute(EnterAlternateScreen)?;
|
stdout().execute(EnterAlternateScreen)?;
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
@@ -47,8 +46,11 @@ fn main() -> Result<()> {
|
|||||||
let mut ticks = 0;
|
let mut ticks = 0;
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(|frame| {
|
terminal.draw(|frame| {
|
||||||
let mut area = frame.size();
|
let mut area = frame.area();
|
||||||
frame.render_widget(Block::default().style(Style::default().bg(Color::Green)), area);
|
frame.render_widget(
|
||||||
|
Block::default().style(Style::default().bg(Color::Green)),
|
||||||
|
area,
|
||||||
|
);
|
||||||
|
|
||||||
// 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.
|
||||||
@@ -87,33 +89,79 @@ fn main() -> Result<()> {
|
|||||||
x: area.x + 50,
|
x: area.x + 50,
|
||||||
y: area.y,
|
y: area.y,
|
||||||
width: 30,
|
width: 30,
|
||||||
height: 25,
|
height: 15,
|
||||||
};
|
};
|
||||||
|
let block = Block::default()
|
||||||
|
.title(
|
||||||
|
Title::from(format!(" {} ", game.get_player().get_name()))
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.position(Position::Top),
|
||||||
|
)
|
||||||
|
.borders(Borders::TOP)
|
||||||
|
.border_style(Style::default().fg(Color::White))
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.style(Style::default().bg(Color::Blue));
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
Paragraph::new(format!("{}\nHealth: {}/{}\nExp: {}\nGold: {}\nLevel: {}",
|
Paragraph::new(format!(
|
||||||
game.get_player().get_name(),
|
"Health: {}/{}\nExp: {}\nGold: {}\nLevel: {}",
|
||||||
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()))
|
game.get_player().get_immutable_position().get_level() + 1
|
||||||
.white()
|
))
|
||||||
.on_blue(),
|
.block(block)
|
||||||
|
.wrap(Wrap { trim: true }),
|
||||||
stats_area,
|
stats_area,
|
||||||
);
|
);
|
||||||
|
let messages_area = Rect {
|
||||||
|
x: area.x + 50,
|
||||||
|
y: area.y + 15,
|
||||||
|
width: 30,
|
||||||
|
height: 10,
|
||||||
|
};
|
||||||
|
// Display the latest messages from the game to the user
|
||||||
|
let block = Block::default()
|
||||||
|
.title(
|
||||||
|
Title::from(" messages ")
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.position(Position::Top),
|
||||||
|
)
|
||||||
|
.borders(Borders::TOP)
|
||||||
|
.border_style(Style::default().fg(Color::White))
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.style(Style::default().bg(Color::Blue));
|
||||||
|
|
||||||
|
let paragraph1 = if game.messages.is_empty() {
|
||||||
|
"".to_string()
|
||||||
|
} else {
|
||||||
|
format!("> {}", game.messages.join("\n> "))
|
||||||
|
};
|
||||||
|
frame.render_widget(
|
||||||
|
Paragraph::new(paragraph1)
|
||||||
|
.block(block)
|
||||||
|
.wrap(Wrap { trim: true }),
|
||||||
|
messages_area,
|
||||||
|
);
|
||||||
})?;
|
})?;
|
||||||
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') {
|
||||||
|
game.messages.insert(
|
||||||
|
0,
|
||||||
|
format!("You are playing version '{}'.", env!("GIT_HASH")).to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if key.kind == KeyEventKind::Press {
|
if key.kind == KeyEventKind::Press {
|
||||||
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),
|
||||||
KeyCode::Up => { game.move_player(0, -1) }
|
KeyCode::Up => game.move_player(0, -1),
|
||||||
KeyCode::Down => { game.move_player(0, 1) }
|
KeyCode::Down => game.move_player(0, 1),
|
||||||
_ => { (0, 0) }
|
_ => (0, 0),
|
||||||
};
|
};
|
||||||
if !game.player_fights_monster() {
|
if !game.player_fights_monster() {
|
||||||
// player attacked monster but did not kill it
|
// player attacked monster but did not kill it
|
||||||
@@ -124,7 +172,7 @@ fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
game.update_level(ticks);
|
game.update_level(ticks);
|
||||||
if game.get_game_state() != GameState::RUNNING {
|
if game.get_game_state() != GameState::Running {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ticks += 1;
|
ticks += 1;
|
||||||
@@ -132,7 +180,7 @@ fn main() -> Result<()> {
|
|||||||
let playtime = start_time.elapsed();
|
let playtime = start_time.elapsed();
|
||||||
loop {
|
loop {
|
||||||
let _ = terminal.draw(|frame| {
|
let _ = terminal.draw(|frame| {
|
||||||
let mut area = frame.size();
|
let mut area = frame.area();
|
||||||
let w = area.width / 2;
|
let w = area.width / 2;
|
||||||
let h = area.height / 2;
|
let h = area.height / 2;
|
||||||
area.x += w - 20;
|
area.x += w - 20;
|
||||||
@@ -140,24 +188,33 @@ fn main() -> Result<()> {
|
|||||||
area.width = 40;
|
area.width = 40;
|
||||||
area.height = 20;
|
area.height = 20;
|
||||||
let block = Block::default()
|
let block = Block::default()
|
||||||
.title("Game ended")
|
.title(
|
||||||
|
Title::from(" Game ended ")
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.position(Position::Top),
|
||||||
|
)
|
||||||
.title(Title::from("Press `q` to quit!").position(Position::Bottom))
|
.title(Title::from("Press `q` to quit!").position(Position::Bottom))
|
||||||
.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 mut text = match game.get_game_state() {
|
let mut text = match game.get_game_state() {
|
||||||
GameState::RUNNING => {
|
GameState::Running => {
|
||||||
"Quitting is for cowards! You'll better try again!".to_string()
|
"Quitting is for cowards! You'll better try again!".to_string()
|
||||||
}
|
}
|
||||||
GameState::LOST => {
|
GameState::Lost => {
|
||||||
"Sorry, you died in the dungeon. Better luck next time!".to_string()
|
"Sorry, you died in the dungeon. Better luck next time!".to_string()
|
||||||
}
|
}
|
||||||
GameState::WON => {
|
GameState::Won => {
|
||||||
"Congratulation! You mastered your way through the dungeon and won the game.".to_string()
|
"Congratulation! You mastered your way through the dungeon and won the game."
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
text += format!("\nYou gained {} experience.", game.get_player().get_experience()).as_str();
|
text += format!(
|
||||||
|
"\nYou gained {} experience.",
|
||||||
|
game.get_player().get_experience()
|
||||||
|
)
|
||||||
|
.as_str();
|
||||||
text += format!("\nYou collected {} gold.", game.get_player().get_gold()).as_str();
|
text += format!("\nYou collected {} gold.", game.get_player().get_gold()).as_str();
|
||||||
text += format!("\nYou played {} seconds.", playtime.as_secs()).as_str();
|
text += format!("\nYou played {} seconds.", playtime.as_secs()).as_str();
|
||||||
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
|
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
|
||||||
@@ -171,7 +228,6 @@ fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout().execute(LeaveAlternateScreen)?;
|
stdout().execute(LeaveAlternateScreen)?;
|
||||||
disable_raw_mode()?;
|
disable_raw_mode()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
use ratatui::prelude::Color;
|
use ratatui::prelude::Color;
|
||||||
|
|
||||||
use crate::position::{HasPosition, Position};
|
use crate::position::{HasPosition, Position};
|
||||||
|
|
||||||
pub trait Monster: HasPosition {
|
pub trait Monster: HasPosition {
|
||||||
|
fn get_name(&self) -> &str;
|
||||||
fn is_dead(&self) -> bool;
|
fn is_dead(&self) -> bool;
|
||||||
fn get_representation(&self) -> (&str, Color);
|
fn get_representation(&self) -> (&str, Color);
|
||||||
fn decrease_life(&mut self, by: usize);
|
fn decrease_life(&mut self, by: usize);
|
||||||
@@ -11,11 +15,23 @@ pub trait Monster: HasPosition {
|
|||||||
fn get_ticks_between_steps(&self) -> u128;
|
fn get_ticks_between_steps(&self) -> u128;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn get_life(&self) -> usize;
|
fn get_life(&self) -> usize;
|
||||||
|
fn damage(&self) -> usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! default_monster {
|
macro_rules! create_monster {
|
||||||
($($t:ty),+ $(,)?) => ($(
|
($($t:ident),+ $(,)?) => ($(
|
||||||
|
pub struct $t {
|
||||||
|
name: String,
|
||||||
|
life: usize,
|
||||||
|
position: Position,
|
||||||
|
symbol: String,
|
||||||
|
color: Color,
|
||||||
|
experience_gain: usize,
|
||||||
|
ticks_between_steps: u128,
|
||||||
|
damage_range: RangeInclusive<usize>,
|
||||||
|
}
|
||||||
impl Monster for $t {
|
impl Monster for $t {
|
||||||
|
fn get_name(&self) -> &str { &self.name }
|
||||||
fn is_dead(&self) -> bool { self.life <= 0 }
|
fn is_dead(&self) -> bool { self.life <= 0 }
|
||||||
fn get_experience_gain(&self) -> usize { self.experience_gain }
|
fn get_experience_gain(&self) -> usize { self.experience_gain }
|
||||||
fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) }
|
fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) }
|
||||||
@@ -23,6 +39,7 @@ macro_rules! default_monster {
|
|||||||
self.life = self.life.saturating_sub(by);
|
self.life = self.life.saturating_sub(by);
|
||||||
}
|
}
|
||||||
fn get_ticks_between_steps(&self) -> u128 { self.ticks_between_steps }
|
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)]
|
#[cfg(test)]
|
||||||
fn get_life(&self) -> usize { self.life }
|
fn get_life(&self) -> usize { self.life }
|
||||||
@@ -38,53 +55,53 @@ macro_rules! default_monster {
|
|||||||
)+)
|
)+)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Rat {
|
|
||||||
life: usize,
|
|
||||||
position: Position,
|
|
||||||
symbol: String,
|
|
||||||
color: Color,
|
|
||||||
experience_gain: usize,
|
|
||||||
ticks_between_steps: u128,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Rat {
|
impl Rat {
|
||||||
pub fn new_with_position(position: Position) -> Self {
|
pub fn new_with_position(position: Position) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
name: "rat".to_string(),
|
||||||
life: 2,
|
life: 2,
|
||||||
position,
|
position,
|
||||||
symbol: String::from("R"),
|
symbol: String::from("R"),
|
||||||
color: Color::Black,
|
color: Color::Black,
|
||||||
experience_gain: 5,
|
experience_gain: 5,
|
||||||
ticks_between_steps: 5,
|
ticks_between_steps: 5,
|
||||||
|
damage_range: 1..=2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default_monster!(Rat);
|
create_monster!(Rat);
|
||||||
|
|
||||||
pub struct Orc {
|
|
||||||
life: usize,
|
|
||||||
position: Position,
|
|
||||||
symbol: String,
|
|
||||||
color: Color,
|
|
||||||
experience_gain: usize,
|
|
||||||
ticks_between_steps: u128,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Orc {
|
impl Orc {
|
||||||
pub fn new_with_position(position: Position) -> Self {
|
pub fn new_with_position(position: Position) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
name: "orc".to_string(),
|
||||||
life: 4,
|
life: 4,
|
||||||
position,
|
position,
|
||||||
symbol: String::from("O"),
|
symbol: String::from("O"),
|
||||||
color: Color::DarkGray,
|
color: Color::DarkGray,
|
||||||
experience_gain: 10,
|
experience_gain: 10,
|
||||||
ticks_between_steps: 10,
|
ticks_between_steps: 10,
|
||||||
|
damage_range: 2..=3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
create_monster!(Orc);
|
||||||
|
|
||||||
default_monster!(Orc);
|
impl Snake {
|
||||||
|
pub fn new_with_position(position: Position) -> Self {
|
||||||
|
Self {
|
||||||
|
name: "snake".to_string(),
|
||||||
|
life: 3,
|
||||||
|
position,
|
||||||
|
symbol: String::from("S"),
|
||||||
|
color: Color::DarkGray,
|
||||||
|
experience_gain: 10,
|
||||||
|
ticks_between_steps: 20,
|
||||||
|
damage_range: 1..=4,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
create_monster!(Snake);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn monsters_can_move() {
|
fn monsters_can_move() {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use rand::Rng;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
use crate::position::{HasPosition, Position};
|
use crate::position::{HasPosition, Position};
|
||||||
@@ -23,7 +24,7 @@ impl Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_name(&self) -> String {
|
pub fn get_name(&self) -> String {
|
||||||
return self.name.clone();
|
self.name.clone()
|
||||||
}
|
}
|
||||||
pub fn change_life(&mut self, by: i16) {
|
pub fn change_life(&mut self, by: i16) {
|
||||||
self.life = max(0, min(self.max_life, self.life + by));
|
self.life = max(0, min(self.max_life, self.life + by));
|
||||||
@@ -32,22 +33,38 @@ impl Player {
|
|||||||
self.life
|
self.life
|
||||||
}
|
}
|
||||||
/// returns true if the player is dead (life <= 0)
|
/// returns true if the player is dead (life <= 0)
|
||||||
pub fn is_dead(&self) -> bool { self.life <= 0 }
|
pub fn is_dead(&self) -> bool {
|
||||||
|
self.life <= 0
|
||||||
|
}
|
||||||
/// returns true if the player's life is at maximum
|
/// returns true if the player's life is at maximum
|
||||||
pub fn is_healthy(&self) -> bool { self.life == self.max_life }
|
pub fn is_healthy(&self) -> bool {
|
||||||
|
self.life == self.max_life
|
||||||
|
}
|
||||||
pub fn get_max_life(&self) -> i16 {
|
pub fn get_max_life(&self) -> i16 {
|
||||||
self.max_life
|
self.max_life
|
||||||
}
|
}
|
||||||
|
|
||||||
/// add the given amount to the players gold stash
|
/// add the given amount to the players gold stash
|
||||||
pub fn retrieve_gold(&mut self, amount: usize) { self.gold += amount }
|
pub fn retrieve_gold(&mut self, amount: usize) {
|
||||||
|
self.gold += amount
|
||||||
|
}
|
||||||
|
|
||||||
/// return the size of the players gold stash
|
/// return the size of the players gold stash
|
||||||
pub fn get_gold(&self) -> usize { self.gold }
|
pub fn get_gold(&self) -> usize {
|
||||||
|
self.gold
|
||||||
|
}
|
||||||
|
|
||||||
pub fn gain_experience(&mut self, amount: usize) { self.experience += amount }
|
pub fn gain_experience(&mut self, amount: usize) {
|
||||||
|
self.experience += amount
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_experience(&self) -> usize { self.experience }
|
pub fn get_experience(&self) -> usize {
|
||||||
|
self.experience
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn damage(&self) -> usize {
|
||||||
|
rand::thread_rng().gen_range(1..4)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasPosition for Player {
|
impl HasPosition for Player {
|
||||||
|
|||||||
@@ -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,
|
||||||
@@ -17,11 +17,7 @@ pub struct Position {
|
|||||||
|
|
||||||
impl Position {
|
impl Position {
|
||||||
pub fn new(level: usize, x: usize, y: usize) -> Self {
|
pub fn new(level: usize, x: usize, y: usize) -> Self {
|
||||||
Self {
|
Self { level, x, y }
|
||||||
level,
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn change(&mut self, dx: i16, dy: i16) -> (usize, usize) {
|
pub fn change(&mut self, dx: i16, dy: i16) -> (usize, usize) {
|
||||||
self.x = max((self.x as i16) + dx, 0) as usize;
|
self.x = max((self.x as i16) + dx, 0) as usize;
|
||||||
@@ -42,7 +38,9 @@ impl Position {
|
|||||||
self.x
|
self.x
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_y(&self) -> usize { self.y }
|
pub fn get_y(&self) -> usize {
|
||||||
|
self.y
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user