diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-29 21:30:38 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-29 21:30:38 +0100 |
commit | a04cb74fee079e3ee43ae5fae32fc2674409822c (patch) | |
tree | 403e681a768ee68b54569e93d98e741878cd7975 | |
parent | 9283e1eb66d6ff96b02f317e28cb6ff060953cdf (diff) |
implement movement
-rw-r--r-- | backend/CMakeLists.txt | 1 | ||||
-rw-r--r-- | backend/Dungeon.cpp | 8 | ||||
-rw-r--r-- | backend/Dungeon.h | 1 | ||||
-rw-r--r-- | backend/Location.h | 1 | ||||
-rw-r--r-- | backend/RNG.cpp | 32 | ||||
-rw-r--r-- | backend/RNG.h | 23 | ||||
-rw-r--r-- | frontend/Player.cpp | 8 | ||||
-rw-r--r-- | frontend/Player.h | 2 | ||||
-rw-r--r-- | frontend/cmd/go.cpp | 25 | ||||
-rw-r--r-- | frontend/cmd/query.cpp | 29 | ||||
-rw-r--r-- | frontend/load_dungeon.cpp | 2 |
11 files changed, 129 insertions, 3 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 9b1e251..b508654 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -6,5 +6,6 @@ target_sources(main PUBLIC Dungeon.cpp Location.cpp util.cpp + RNG.cpp ) diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp index 55dccec..3de2f11 100644 --- a/backend/Dungeon.cpp +++ b/backend/Dungeon.cpp @@ -1,4 +1,5 @@ #include "Dungeon.h" +#include "RNG.h" void Dungeon::update() { @@ -8,3 +9,10 @@ void Dungeon::add_location(Location * location) { this->locations.push_back(location); } +Location * Dungeon::get_start_location() { + size_t size = this->locations.size(); + if (size == 0) return nullptr; + size_t index = RNG::get().rand_int(size); + return this->locations[index]; +} + diff --git a/backend/Dungeon.h b/backend/Dungeon.h index 88328c8..2d5fa61 100644 --- a/backend/Dungeon.h +++ b/backend/Dungeon.h @@ -8,6 +8,7 @@ class Dungeon { public: void update(); void add_location(Location *); + Location * get_start_location(); private: List<Location *> locations; diff --git a/backend/Location.h b/backend/Location.h index 6068a99..f102728 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -11,6 +11,7 @@ enum Direction { SOUTH = 2, WEST = 3, }; +static constexpr const Direction DIRECTIONS[] = { NORTH, EAST, SOUTH, WEST }; class Location { public: diff --git a/backend/RNG.cpp b/backend/RNG.cpp new file mode 100644 index 0000000..bc3e57b --- /dev/null +++ b/backend/RNG.cpp @@ -0,0 +1,32 @@ +#include "RNG.h" + +using std::uniform_int_distribution; +using std::uniform_real_distribution; + +RNG::RNG() : dev(), rng(dev()) { } + +RNG & RNG::get() { + static RNG instance; + return instance; +} + +int RNG::rand_int(const int upper) { + return this->rand_int(0, upper); +} +int RNG::rand_int(const int lower, const int upper) { + uniform_int_distribution<int> random_dist(lower, upper); + return random_dist(rng); +} + +double RNG::rand_double() { + return this->rand_double(0.f, 1.f); +} +double RNG::rand_double(const double lower, const double upper) { + uniform_real_distribution<double> random_dist(lower, upper); + return random_dist(rng); +} + +bool RNG::rand_bool() { + return rand_int(0, 1); +} + diff --git a/backend/RNG.h b/backend/RNG.h new file mode 100644 index 0000000..ded337c --- /dev/null +++ b/backend/RNG.h @@ -0,0 +1,23 @@ +#pragma once + +#include <random> + +class RNG { +public: + static RNG & get(); + +public: + int rand_int(const int upper); + int rand_int(const int lower, const int upper); + double rand_double(); + double rand_double(const double lower, const double upper); + bool rand_bool(); + +private: + RNG(); + +private: + std::random_device dev; + std::mt19937 rng; +}; + diff --git a/frontend/Player.cpp b/frontend/Player.cpp index adf4e44..fddea95 100644 --- a/frontend/Player.cpp +++ b/frontend/Player.cpp @@ -2,6 +2,8 @@ #include "Player.h" #include "print.h" +#include "backend/Dungeon.h" + using namespace std; static string argv_pop(vector<string> & argv) { @@ -10,8 +12,10 @@ static string argv_pop(vector<string> & argv) { return el; } -Player::Player(Dungeon & dungeon) : dungeon(dungeon) { - this->cmds.clear(); +Player::Player(Dungeon & dungeon) : + dungeon(dungeon), + location(*dungeon.get_start_location()) + { cmdset_default(); } diff --git a/frontend/Player.h b/frontend/Player.h index ae3c520..40d47ad 100644 --- a/frontend/Player.h +++ b/frontend/Player.h @@ -5,6 +5,7 @@ #include <string> class Dungeon; +class Location; enum FollowupAction { NONE, @@ -24,6 +25,7 @@ private: // TODO: WeaponObject[] // TODO: GoldObject[] // TODO: ArmorObject[] + Location & location; public: Player(Dungeon & dungeon); diff --git a/frontend/cmd/go.cpp b/frontend/cmd/go.cpp index 2aaec08..cbe8e7c 100644 --- a/frontend/cmd/go.cpp +++ b/frontend/cmd/go.cpp @@ -1,6 +1,31 @@ +#include "backend/Location.h" + #include "../Player.h" +#include "../print.h" + +using namespace std; + +static const unordered_map<string, Direction> direction_map = { + { "noord", Direction::NORTH }, + { "oost", Direction::EAST }, + { "zuid", Direction::SOUTH }, + { "west", Direction::WEST }, +}; FollowupAction Player::cmd_go(Argv argv) { + if (argv.size() == 0 || !direction_map.contains(argv[0])) { + lprtf("Fout, gebruik: Ga <noord|zuid|oost|west>\n"); + return FollowupAction::NONE; + } + + Direction direction = direction_map.at(argv[0]); + Location * next_location = this->location.get_exit(direction); + if (next_location == nullptr) { + lprtf("Er is geen uitgang in deze richting!\n"); + return FollowupAction::NONE; + } + + this->location = *next_location; return FollowupAction::UPDATE; } diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp index 48cbb55..367d6cd 100644 --- a/frontend/cmd/query.cpp +++ b/frontend/cmd/query.cpp @@ -1,6 +1,35 @@ +#include "backend/Location.h" + #include "../Player.h" +#include "../print.h" + +using namespace std; + +static const unordered_map<Direction, string> direction_map = { + { Direction::NORTH, "Noord" }, + { Direction::EAST, "Oost" }, + { Direction::SOUTH, "Zuid" }, + { Direction::WEST, "West" }, +}; FollowupAction Player::cmd_query(Argv argv) { + lprtf("Je staat bij de locatie %s.\n", this->location.get_name()); + lprtf("%s\n", this->location.get_description()); + + // TODO: visible objects + + lprtf("Uitgangen: "); + bool first = true; + for (Direction direction : DIRECTIONS) { + if (this->location.get_exit(direction) == nullptr) continue; + if (!first) lprtf(", "); + lprtf("%s", direction_map.at(direction).c_str()); + first = false; + } + lprtf("\n"); + + // TODO: enemies + return FollowupAction::NONE; } diff --git a/frontend/load_dungeon.cpp b/frontend/load_dungeon.cpp index ef8cb54..e94cbcb 100644 --- a/frontend/load_dungeon.cpp +++ b/frontend/load_dungeon.cpp @@ -65,7 +65,7 @@ unique_ptr<Dungeon> load_dungeon(const string & filename) { // connect edges after creating all locations for (auto & [here, temp] : temp_map) { - for (Direction direction : { NORTH, EAST, SOUTH, WEST }) { + for (Direction direction : DIRECTIONS) { unsigned there = temp.edges[direction]; if (temp.edges[direction] == 0) continue; if (!temp_map.contains(there)) continue; |