Skip to content

Commit 82db021

Browse files
authored
Merge pull request #11 from daniebker/feature/#10-persistence
Feature/#10 persistence
2 parents e00ca5d + cec5e43 commit 82db021

30 files changed

Lines changed: 664 additions & 127 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,5 @@ Temporary Items
120120
## Custom
121121
# Default build directory
122122
build
123+
124+
game.sav

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@
137137
"cinttypes": "cpp",
138138
"typeindex": "cpp",
139139
"valarray": "cpp",
140-
"__std_stream": "cpp"
140+
"__std_stream": "cpp",
141+
"__tree": "cpp"
141142
},
142143
"[cpp]": {
143144
"editor.defaultFormatter": "xaver.clang-format"

include/basic_ai_component.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
#ifndef INCLUDE_BASIC_AI_COMPONENT_HPP_
22
#define INCLUDE_BASIC_AI_COMPONENT_HPP_
33

4+
#include <libtcod.hpp>
5+
6+
#include "persistent.hpp"
47
#include "types/engine_fwd.hpp"
58
#include "types/entity_fwd.hpp"
69
#include "types/world_fwd.hpp"
710

811
namespace cpprl {
912

10-
class AIComponent {
13+
class AIComponent : public Persistent {
1114
public:
1215
AIComponent(){};
1316
virtual ~AIComponent() = default;
1417
virtual void update(World& world, Entity* entity) = 0;
18+
static AIComponent* create(TCODZip& zip);
19+
virtual void load(TCODZip& zip) = 0;
20+
virtual void save(TCODZip& zip) = 0;
21+
22+
protected:
23+
enum AiType { HOSTILE, CONFUSED };
1524
};
1625

1726
class HostileAI final : public AIComponent {
1827
public:
1928
HostileAI() : AIComponent(){};
2029
void update(World& world, Entity* entity) override;
30+
void load(TCODZip& zip) override;
31+
void save(TCODZip& zip) override;
2132
};
2233

2334
class ConfusionAI final : public AIComponent {
@@ -30,6 +41,8 @@ class ConfusionAI final : public AIComponent {
3041
: AIComponent(), num_turns_(num_turns), old_ai_(old_ai) {}
3142
~ConfusionAI() override = default;
3243
void update(World& world, Entity* entity) override;
44+
void load(TCODZip& zip) override;
45+
void save(TCODZip& zip) override;
3346
};
3447
} // namespace cpprl
3548

include/components.hpp

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,78 @@
55
#include <string_view>
66
#include <variant>
77

8+
#include "persistent.hpp"
89
#include "types/action_result.hpp"
910
#include "types/entity_fwd.hpp"
1011
#include "types/math.hpp"
1112
#include "types/world_fwd.hpp"
1213

1314
namespace cpprl {
1415

15-
class AttackComponent {
16+
class AttackComponent : public Persistent {
1617
public:
1718
AttackComponent(int damage) : damage_(damage) {}
1819
int get_damage() { return damage_; }
20+
void load(TCODZip& zip);
21+
void save(TCODZip& zip);
1922

2023
private:
2124
int damage_;
2225
};
2326

24-
class DefenseComponent {
27+
class DefenseComponent : public Persistent {
2528
public:
2629
DefenseComponent(int defense, int maxHp)
27-
: defense(defense), hp_(maxHp), max_hp_(maxHp) {}
30+
: defense_(defense), hp_(maxHp), max_hp_(maxHp) {}
2831

2932
int get_hp() { return hp_; }
3033
int get_max_hp() { return max_hp_; }
31-
int get_defense() { return defense; }
34+
int get_defense() { return defense_; }
3235

3336
void take_damage(int damage) { hp_ -= damage; }
3437
int heal(int amount);
3538
bool is_dead() { return hp_ <= 0; }
3639
bool is_not_dead() { return !is_dead(); }
3740
void die(Entity& owner);
41+
void load(TCODZip& zip);
42+
void save(TCODZip& zip);
3843

3944
private:
40-
int defense;
45+
int defense_;
4146
int hp_;
4247
int max_hp_;
4348
};
4449

45-
class TransformComponent {
50+
class TransformComponent : public Persistent {
4651
public:
4752
TransformComponent(int x, int y) : position_({x, y}) {}
4853
Vector2D get_position() { return position_; }
4954
void move(Vector2D new_position) { position_ = new_position; }
55+
void load(TCODZip& zip);
56+
void save(TCODZip& zip);
5057

5158
private:
5259
Vector2D position_;
5360
};
5461

55-
class ASCIIComponent {
62+
class ASCIIComponent : public Persistent {
5663
public:
5764
ASCIIComponent(std::string_view symbol, tcod::ColorRGB colour, int layer)
5865
: symbol_(symbol), colour_(colour), layer_(layer) {}
5966

6067
std::string_view get_symbol() { return symbol_; }
6168
tcod::ColorRGB get_colour() { return colour_; }
6269
int get_layer() { return layer_; }
70+
void load(TCODZip& zip);
71+
void save(TCODZip& zip);
6372

6473
private:
65-
std::string_view symbol_;
74+
std::string symbol_;
6675
tcod::ColorRGB colour_;
6776
int layer_;
6877
};
6978

70-
class Container {
79+
class Container : Persistent {
7180
private:
7281
size_t size_;
7382
std::vector<Entity*> inventory_;
@@ -78,22 +87,34 @@ class Container {
7887
void remove(Entity* actor);
7988
std::vector<Entity*> get_inventory() { return inventory_; }
8089
int get_size() { return size_; }
90+
91+
void load(TCODZip& zip);
92+
void save(TCODZip& zip);
8193
};
8294

83-
class ConsumableComponent {
95+
class ConsumableComponent : public Persistent {
8496
public:
8597
virtual ~ConsumableComponent() = default;
8698
// TODO: should also be an action result
8799
ActionResult pick_up(Entity* owner, Entity* wearer);
88100
ActionResult drop(Entity* owner, Entity* wearer);
89101
virtual ActionResult use(Entity* owner, Entity* wearer, World& world);
102+
virtual void load(TCODZip& zip) = 0;
103+
virtual void save(TCODZip& zip) = 0;
104+
static ConsumableComponent* create(TCODZip& zip);
105+
106+
protected:
107+
enum ConsumableType { HEALER, LIGHTNING_BOLT, CONFUSER, FIREBALL };
90108
};
91109

92110
class HealingConsumable final : public ConsumableComponent {
93111
public:
94112
HealingConsumable(int amount);
95113
ActionResult use(Entity* owner, Entity* wearer, World& world);
96114

115+
void load(TCODZip& zip);
116+
void save(TCODZip& zip);
117+
97118
private:
98119
int amount_;
99120
};
@@ -106,6 +127,9 @@ class LightningBolt final : public ConsumableComponent {
106127
LightningBolt(float range, float damage) : range_(range), damage_(damage) {}
107128
~LightningBolt() = default;
108129
ActionResult use(Entity* owner, Entity* wearer, World& world);
130+
131+
void load(TCODZip& zip);
132+
void save(TCODZip& zip);
109133
};
110134

111135
class FireSpell final : public ConsumableComponent {
@@ -118,6 +142,9 @@ class FireSpell final : public ConsumableComponent {
118142
~FireSpell() = default;
119143

120144
ActionResult use(Entity* owner, Entity* Wearer, World& world);
145+
146+
void load(TCODZip& zip);
147+
void save(TCODZip& zip);
121148
};
122149

123150
class ConfusionSpell final : public ConsumableComponent {
@@ -130,6 +157,9 @@ class ConfusionSpell final : public ConsumableComponent {
130157
~ConfusionSpell() = default;
131158

132159
ActionResult use(Entity* owner, Entity* wearer, World& world);
160+
161+
void load(TCODZip& zip);
162+
void save(TCODZip& zip);
133163
};
134164

135165
} // namespace cpprl

include/dungeon.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55

66
#include "game_entity.hpp"
7+
#include "persistent.hpp"
78
#include "types/map.hpp"
89

910
struct DungeonConfig {
@@ -16,14 +17,20 @@ struct DungeonConfig {
1617
};
1718

1819
namespace cpprl {
19-
class Dungeon {
20+
class Dungeon : public Persistent {
2021
private:
2122
std::vector<Vector2D> l_tunnel_between(Vector2D start, Vector2D end);
23+
TCODRandom* rng_;
24+
long seed_;
2225

2326
public:
24-
Dungeon(){};
25-
~Dungeon() = default;
27+
Dungeon() {
28+
seed_ = TCODRandom::getInstance()->getInt(0, 0x7FFFFFFF);
29+
};
30+
~Dungeon() { delete rng_; };
2631
Map* generate(DungeonConfig config);
32+
void save(TCODZip& zip) override;
33+
void load(TCODZip& zip) override;
2734
};
2835
} // namespace cpprl
2936

include/engine.hpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,8 @@ class State;
2323

2424
class Engine {
2525
private:
26-
// std::unique_ptr<Dungeon> dungeon_;
27-
// UiWindow* health_bar_;
28-
// std::unique_ptr<MessageLog> message_log_;
29-
// UiWindow* current_window_;
30-
// EventHandler* input_handler_;
3126
std::unique_ptr<Renderer> renderer_;
3227
tcod::Context context_;
33-
// Controller controller_;
34-
// bool is_paused_ = false;
35-
// bool game_over_ = false;
36-
// bool show_view_ = false;
37-
// bool targeting_mode_ = false;
3828
std::unique_ptr<World> world_;
3929
std::unique_ptr<State> engine_state_;
4030

@@ -45,25 +35,12 @@ class Engine {
4535
Engine(int argc, char** argv);
4636
~Engine();
4737
void handle_events();
48-
// EntityManager& get_entities() { return *entities_; };
4938
void render();
50-
// Map* get_map() { return map_; }
51-
// Entity* get_player() { return player_; }
5239
void handle_player_death();
5340
void reset_game();
54-
// MessageLog& get_message_log() { return *message_log_; }
55-
// Controller& get_controller() { return controller_; }
56-
// UiWindow& get_current_view() { return *current_window_; }
57-
// void toggle_pause() { is_paused_ = !is_paused_; }
58-
// void toggle_view() { show_view_ = !show_view_; }
59-
// void set_input_handler(EventHandler* input_handler);
60-
// void set_current_view(UiWindow* current_window) {
61-
// current_window_ = current_window;
62-
// };
63-
// void scroll_current_view(int scroll_amount);
64-
// void toggle_targeting_mode() { targeting_mode_ = !targeting_mode_; }
65-
// void set_targeting_tile(
66-
// float max_range = 0.0f, std::function<void()> callback = nullptr);
41+
void load();
42+
void save();
43+
void init();
6744
};
6845
} // namespace cpprl
6946
#endif

include/entity_manager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class EntityManager {
2222
void reserve(size_t size) { entities_.reserve(size); }
2323
void shrink_to_fit() { entities_.shrink_to_fit(); }
2424
void remove(Entity* entity);
25+
size_t size() const { return entities_.size(); }
2526

2627
Entity& at(int index) { return *entities_.at(index); }
2728

include/events/command.hpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,41 @@ class InventoryCommand final : public Command {
7272
StateResult execute() override;
7373
};
7474

75-
enum SubCommand { USE_ITEM, DROP_ITEM };
75+
class MainMenuCommand final : public EngineEvent {
76+
public:
77+
MainMenuCommand(World& world) : EngineEvent(world) {}
78+
StateResult execute() override;
79+
};
80+
81+
enum ItemSubCommand { USE_ITEM, DROP_ITEM };
7682
class SelectItemCommand final : public Command {
7783
private:
78-
SubCommand sub_command_;
84+
ItemSubCommand sub_command_;
7985
UiWindow& ui_window_;
8086

8187
public:
8288
SelectItemCommand(
83-
World& world, Entity* entity, UiWindow& ui_window, SubCommand sub_command)
89+
World& world,
90+
Entity* entity,
91+
UiWindow& ui_window,
92+
ItemSubCommand sub_command)
8493
: Command(world, entity),
8594
sub_command_(sub_command),
8695
ui_window_(ui_window) {}
8796
StateResult execute() override;
8897
};
8998

99+
enum MenuSubCommand { NEW_GAME, CONTINUE, QUIT };
100+
class SelectMenuItemCommand final : public EngineEvent {
101+
private:
102+
UiWindow& ui_window_;
103+
104+
public:
105+
SelectMenuItemCommand(World& world, UiWindow& ui_window)
106+
: EngineEvent(world), ui_window_(ui_window) {}
107+
StateResult execute() override;
108+
};
109+
90110
class UseItemCommand final : public Command {
91111
private:
92112
int item_index_;

include/game_entity.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <libtcod.hpp>
55
#include <string_view>
66

7+
#include "persistent.hpp"
78
#include "types/math.hpp"
89
#include "types/world_fwd.hpp"
910

@@ -73,6 +74,8 @@ class Entity {
7374
aiComponent_ = aiComponent;
7475
};
7576
void set_container(Container* container) { container_ = container; };
77+
void save(TCODZip& zip);
78+
void load(TCODZip& zip);
7679
};
7780

7881
} // namespace cpprl

0 commit comments

Comments
 (0)