Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions include/basic_ai_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,19 @@

namespace cpprl {
class Engine;
class GameEntity;
class Entity;

class BasicAIComponent {
class AIComponent {
public:
virtual ~BasicAIComponent() = default;
virtual void update(Engine& engine) = 0;
AIComponent(){};
virtual ~AIComponent() = default;
virtual void update(Engine& engine, Entity* entity) = 0;
};

class BaseBasicAI : BasicAIComponent {
class HostileAI final : public AIComponent {
public:
BaseBasicAI(GameEntity& entity) : entity_(entity) {}
virtual ~BaseBasicAI() = default;
virtual void update(Engine& engine) = 0;

protected:
GameEntity& entity_;
};

class HostileAI : BaseBasicAI {
public:
HostileAI(GameEntity& entity) : BaseBasicAI(entity) {}
virtual ~HostileAI() = default;
virtual void update(Engine& engine) override;
HostileAI() : AIComponent(){};
void update(Engine& engine, Entity* entity) override;
};
} // namespace cpprl

Expand Down
5 changes: 5 additions & 0 deletions include/colours.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ static constexpr auto BLUE = tcod::ColorRGB{0, 0, 255};
static constexpr auto DARK_BLUE = tcod::ColorRGB{0, 0, 191};

static constexpr auto BAR_TEXT = WHITE;
static constexpr auto INVALID = tcod::ColorRGB{0xFF, 0xFF, 0x00};
static constexpr auto IMPOSSIBLE = tcod::ColorRGB(0x80, 0x80, 0x80);
static constexpr auto ERROR = tcod::ColorRGB(0xFF, 0x40, 0x40);
static constexpr auto HEALTH_RECOVERED = tcod::ColorRGB(0x0, 0xFF, 0x0);

} // namespace cpprl
7 changes: 4 additions & 3 deletions include/combat_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#include "game_entity.hpp"

namespace cpprl::combat_system {
auto handle_attack = [](GameEntity& attacker, GameEntity& target) -> int {
int damage = attacker.get_attack_component().damage - target.get_defense_component().defense;
auto handle_attack = [](Entity& attacker, Entity& target) -> int {
int damage = attacker.get_attack_component()->get_damage() -
target.get_defense_component()->get_defense();
if (damage > 0) {
target.take_damage(damage);
target.get_defense_component()->take_damage(damage);
return damage;
}
return 0;
Expand Down
91 changes: 79 additions & 12 deletions include/components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,93 @@
#include "types/math.hpp"

namespace cpprl {
class Entity;
struct ActionResult {
bool success;
std::string message;
};
class AttackComponent {
public:
AttackComponent(int damage) : damage_(damage) {}
int get_damage() { return damage_; }

struct AttackComponent {
int damage;
private:
int damage_;
};

struct DefenseComponent {
DefenseComponent(int defense, int maxHp) : defense(defense), hp(maxHp), max_hp(maxHp) {}
class DefenseComponent {
public:
DefenseComponent(int defense, int maxHp)
: defense(defense), hp_(maxHp), max_hp_(maxHp) {}

int get_hp() { return hp_; }
int get_max_hp() { return max_hp_; }
int get_defense() { return defense; }

void take_damage(int damage) { hp_ -= damage; }
int heal(int amount);
bool is_dead() { return hp_ <= 0; }
bool is_not_dead() { return !is_dead(); }
void die(Entity& owner);

private:
int defense;
int hp;
int max_hp;
int hp_;
int max_hp_;
};

struct TransformComponent {
Vector2D position;
class TransformComponent {
public:
TransformComponent(int x, int y) : position_({x, y}) {}
Vector2D get_position() { return position_; }
void move(Vector2D new_position) { position_ = new_position; }

private:
Vector2D position_;
};

// Or ASCII component?
struct SpriteComponent {
std::string_view symbol;
tcod::ColorRGB colour;
class ASCIIComponent {
public:
ASCIIComponent(std::string_view symbol, tcod::ColorRGB colour, int layer)
: symbol_(symbol), colour_(colour), layer_(layer) {}

std::string_view get_symbol() { return symbol_; }
tcod::ColorRGB get_colour() { return colour_; }
int get_layer() { return layer_; }

private:
std::string_view symbol_;
tcod::ColorRGB colour_;
int layer_;
};

class Container {
private:
size_t size_;
std::vector<Entity*> inventory_;

public:
Container(int size);
bool add(Entity* actor);
void remove(Entity* actor);
std::vector<Entity*> get_inventory() { return inventory_; }
int get_size() { return size_; }
};

class ConsumableComponent {
public:
virtual ~ConsumableComponent() = default;
bool pick_up(Entity* owner, Entity* wearer);
virtual ActionResult use(Entity* owner, Entity* wearer);
};

class HealingConsumable final : public ConsumableComponent {
public:
HealingConsumable(int amount);
ActionResult use(Entity* owner, Entity* wearer);

private:
int amount_;
};
} // namespace cpprl
#endif
5 changes: 4 additions & 1 deletion include/dungeon.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#ifndef DUNGEON_HPP
#define DUNGEON_HPP

#include <vector>

Expand All @@ -24,3 +25,5 @@ class Dungeon {
Map* generate(DungeonConfig config);
};
} // namespace cpprl

#endif
24 changes: 14 additions & 10 deletions include/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@
#include "dungeon.hpp"
#include "entity_manager.hpp"
#include "globals.hpp"
#include "gui.hpp"
#include "message_log.hpp"
#include "rendering.hpp"
#include "ui_window.hpp"

namespace cpprl {

class InputHandler;
class EventHandler;
class GameInputHandler;
class GameEntity;
class GameActor;
class Map;

class Engine {
private:
std::unique_ptr<Dungeon> dungeon_;
std::unique_ptr<EntityManager> entities_;
GameEntity* player_;
Entity* player_;
UiWindow* health_bar_;
Map* map_;
std::unique_ptr<MessageLog> message_log_;
UiWindow* history_window_;
std::unique_ptr<InputHandler> input_handler_;
UiWindow* current_window_;
EventHandler* input_handler_;
std::unique_ptr<Renderer> renderer_;
tcod::Context context_;
Controller controller_;
bool is_paused_ = false;
bool game_over_ = false;
bool show_history_view_ = false;
bool show_view_ = false;

void generate_map(int width, int height);
void handle_enemy_turns();
Expand All @@ -47,14 +47,18 @@ class Engine {
EntityManager& get_entities() { return *entities_; };
void render();
Map* get_map() { return map_; }
GameEntity& get_player() { return *player_; }
Entity* get_player() { return player_; }
void handle_player_death();
void reset_game();
MessageLog& get_message_log() { return *message_log_; }
Controller& get_controller() { return controller_; }
UiWindow& get_current_view() { return *current_window_; }
void toggle_pause() { is_paused_ = !is_paused_; }
void toggle_history_view() { show_history_view_ = !show_history_view_; }
void set_input_handler(std::unique_ptr<InputHandler> input_handler);
void toggle_view() { show_view_ = !show_view_; }
void set_input_handler(EventHandler* input_handler);
void set_current_view(UiWindow* current_window) {
current_window_ = current_window;
};
void scroll_current_view(int scroll_amount);
};
} // namespace cpprl
Expand Down
23 changes: 13 additions & 10 deletions include/entity_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ class EntityManager {
public:
EntityManager() : entities_(){};
void clear();
GameEntity* get_blocking_entity_at(Vector2D position);
std::vector<GameEntity*> get_entities_at(Vector2D position);
void place_entities(RectangularRoom room, int max_monsters_per_room);
GameEntity& spawn(const GameEntity& entity);
GameEntity& spawn(const GameEntity& entity, Vector2D position);
GameEntity& spawn_player(Vector2D position);
Entity* get_blocking_entity_at(Vector2D position);
Entity* get_non_blocking_entity_at(Vector2D position);
std::vector<Entity*> get_entities_at(Vector2D position);
void place_entities(
RectangularRoom room, int max_monsters_per_room, int max_items_per_room);
Entity* spawn(Entity* entity);
Entity* spawn(Entity* entity, Vector2D position);
void reserve(size_t size) { entities_.reserve(size); }
void shrink_to_fit() { entities_.shrink_to_fit(); }
void remove(Entity* entity);

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

using iterator = std::vector<GameEntity>::iterator;
using const_iterator = std::vector<GameEntity>::const_iterator;
using iterator = std::vector<Entity*>::iterator;
using const_iterator = std::vector<Entity*>::const_iterator;

iterator begin() { return entities_.begin(); }

Expand All @@ -33,6 +36,6 @@ class EntityManager {
const_iterator end() const { return entities_.end(); }

private:
std::vector<GameEntity> entities_;
std::vector<Entity*> entities_;
};
} // namespace cpprl
57 changes: 51 additions & 6 deletions include/events/command.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,65 @@
#ifndef COMMAND_H
#define COMMAND_H

#include "../engine.hpp"
#include "../game_entity.hpp"
#include "../types/map.hpp"
#include "engine.hpp"
#include "engine_event.hpp"
#include "game_entity.hpp"
#include "types/map.hpp"

namespace cpprl {
class Command : public EngineEvent {
protected:
GameEntity& entity_;
Entity* entity_;

public:
Command(Engine& engine, GameEntity& entity) : EngineEvent(engine), entity_(entity) {}
Command(Engine& engine, Entity* entity)
: EngineEvent(engine), entity_(entity) {}
virtual ~Command() {}
virtual void execute() = 0;
virtual void execute() = 0;
};

class ScrollCommand : public EngineEvent {
public:
ScrollCommand(Engine& engine, int scroll_amount)
: EngineEvent(engine), scroll_amount_(scroll_amount){};
virtual void execute() ;

private:
int scroll_amount_;
};

class ViewHistoryCommand : public EngineEvent {
public:
ViewHistoryCommand(Engine& engine) : EngineEvent(engine){};
virtual void execute() ;
};

class PickupCommand : public Command {
public:
PickupCommand(Engine& engine, Entity* entity) : Command(engine, entity){};
virtual void execute() ;
};

class InventoryCommand final : public Command {
public:
InventoryCommand(Engine& engine, Entity* entity) : Command(engine, entity) {}
void execute() override;
};

class SelectItemCommand final : public Command {
public:
SelectItemCommand(Engine& engine, Entity* entity) : Command(engine, entity) {}
void execute() override;
};

class UseItemCommand final : public Command {
private:
int item_index_;

public:
UseItemCommand(Engine& engine, Entity* entity, int item_index)
: Command(engine, entity), item_index_(item_index) {}
void execute() override;
};
} // namespace cpprl
#endif
11 changes: 6 additions & 5 deletions include/events/die_event.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#ifndef INCLUDE_DIE_EVENT_HPP_
#define INCLUDE_DIE_EVENT_HPP_

#include "../engine.hpp"
#include "../game_entity.hpp"
#include "engine_event.hpp"
#include "engine.hpp"
#include "events/engine_event.hpp"
#include "game_entity.hpp"

namespace cpprl {
class DieEvent : public EngineEvent {
public:
DieEvent(Engine& engine, GameEntity& entity) : EngineEvent(engine), entity_(entity) {}
DieEvent(Engine& engine, Entity& entity)
: EngineEvent(engine), entity_(entity) {}
virtual ~DieEvent() = default;
virtual void execute() override;

private:
GameEntity& entity_;
Entity& entity_;
};
} // namespace cpprl

Expand Down
2 changes: 1 addition & 1 deletion include/events/directional_command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DirectionalCommand : public Command {
Vector2D move_vector_;

public:
DirectionalCommand(Engine& engine, GameEntity& entity, Vector2D move_vector)
DirectionalCommand(Engine& engine, Entity* entity, Vector2D move_vector)
: Command(engine, entity), move_vector_(move_vector){};
virtual void execute();
};
Expand Down
Loading