This commit is contained in:
2025-10-26 20:44:58 +08:00
parent e287aadd3c
commit e73c08abf7
45 changed files with 280774 additions and 0 deletions

102
include/core/Characters.h Normal file
View File

@@ -0,0 +1,102 @@
#pragma once
#include "ICharacter.h"
/**
* @brief 战士职业
*
* 技术点: 多态 (Polymorphism) - 继承和方法重写
* 特点: 高生命值、高防御、物理攻击
*/
class Warrior : public ICharacter {
private:
int m_rage; // 怒气值 (战士特有资源)
int m_maxRage; // 最大怒气值
public:
/**
* @brief 构造函数
* @param name 角色名称
* @param level 等级
*/
Warrior(const std::string& name, int level = 1);
/**
* @brief 析构函数
*/
virtual ~Warrior() {}
/**
* @brief 受到伤害 (重写) - 战士有格挡机制
*/
virtual void takeDamage(int damage) override;
/**
* @brief 治疗 (重写)
*/
virtual void heal(int amount) override;
/**
* @brief 获取怒气值
*/
int getRage() const { return m_rage; }
/**
* @brief 增加怒气值
*/
void addRage(int amount);
/**
* @brief 消耗怒气值
*/
bool consumeRage(int amount);
};
/**
* @brief 法师职业
*
* 技术点: 多态 (Polymorphism) - 继承和方法重写
* 特点: 高魔法值、魔法攻击、低防御
*/
class Mage : public ICharacter {
private:
int m_shield; // 法力护盾
int m_maxShield; // 最大护盾值
public:
/**
* @brief 构造函数
* @param name 角色名称
* @param level 等级
*/
Mage(const std::string& name, int level = 1);
/**
* @brief 析构函数
*/
virtual ~Mage() {}
/**
* @brief 受到伤害 (重写) - 法师有法力护盾机制
*/
virtual void takeDamage(int damage) override;
/**
* @brief 治疗 (重写)
*/
virtual void heal(int amount) override;
/**
* @brief 获取护盾值
*/
int getShield() const { return m_shield; }
/**
* @brief 添加护盾
*/
void addShield(int amount);
/**
* @brief 重置护盾
*/
void resetShield();
};

193
include/core/HistoryLog.h Normal file
View File

@@ -0,0 +1,193 @@
#pragma once
#include <vector>
#include <stdexcept>
/**
* @brief 链表节点模板类
* @tparam T 节点存储的数据类型
*
* 技术点: 模板 (Template)
*/
template <typename T>
struct Node {
T data;
Node<T>* next;
explicit Node(const T& d) : data(d), next(nullptr) {}
};
/**
* @brief 历史日志记录器 - 使用模板和手写链表实现
* @tparam T 日志条目的数据类型
*
* 技术点:
* 1. 模板 (Template) - 泛型编程
* 2. 链表 (Linked List) - 手写数据结构
*
* 用途: 用于记录战斗日志,满足课程要求的"模板+链表"技术点
*/
template <typename T>
class HistoryLog {
private:
Node<T>* head; // 链表头指针
Node<T>* tail; // 链表尾指针 (用于快速追加)
int size; // 链表大小
public:
/**
* @brief 构造函数
*/
HistoryLog() : head(nullptr), tail(nullptr), size(0) {}
/**
* @brief 析构函数 - 必须正确释放所有节点内存
*/
~HistoryLog() {
clear();
}
/**
* @brief 拷贝构造函数 (深拷贝)
*/
HistoryLog(const HistoryLog& other) : head(nullptr), tail(nullptr), size(0) {
Node<T>* current = other.head;
while (current != nullptr) {
add(current->data);
current = current->next;
}
}
/**
* @brief 拷贝赋值运算符
*/
HistoryLog& operator=(const HistoryLog& other) {
if (this != &other) {
clear();
Node<T>* current = other.head;
while (current != nullptr) {
add(current->data);
current = current->next;
}
}
return *this;
}
/**
* @brief 在链表尾部添加新的日志条目
* @param logEntry 日志条目
*/
void add(const T& logEntry) {
Node<T>* newNode = new Node<T>(logEntry);
if (head == nullptr) {
// 空链表
head = newNode;
tail = newNode;
} else {
// 追加到尾部
tail->next = newNode;
tail = newNode;
}
size++;
}
/**
* @brief 在链表头部插入日志条目
* @param logEntry 日志条目
*/
void addFront(const T& logEntry) {
Node<T>* newNode = new Node<T>(logEntry);
if (head == nullptr) {
head = newNode;
tail = newNode;
} else {
newNode->next = head;
head = newNode;
}
size++;
}
/**
* @brief 获取所有日志条目
* @return std::vector<T> 包含所有日志条目的向量
*/
std::vector<T> getAllEntries() const {
std::vector<T> entries;
entries.reserve(size);
Node<T>* current = head;
while (current != nullptr) {
entries.push_back(current->data);
current = current->next;
}
return entries;
}
/**
* @brief 获取链表大小
* @return int 日志条目数量
*/
int getSize() const {
return size;
}
/**
* @brief 检查链表是否为空
* @return bool true 如果为空
*/
bool isEmpty() const {
return size == 0;
}
/**
* @brief 清空所有日志条目
*/
void clear() {
Node<T>* current = head;
while (current != nullptr) {
Node<T>* temp = current;
current = current->next;
delete temp;
}
head = nullptr;
tail = nullptr;
size = 0;
}
/**
* @brief 获取指定索引的日志条目
* @param index 索引 (0-based)
* @return const T& 日志条目的引用
* @throw std::out_of_range 如果索引超出范围
*/
const T& at(int index) const {
if (index < 0 || index >= size) {
throw std::out_of_range("Index out of range");
}
Node<T>* current = head;
for (int i = 0; i < index; i++) {
current = current->next;
}
return current->data;
}
/**
* @brief 打印所有日志 (仅用于调试)
*/
void print() const {
Node<T>* current = head;
int index = 0;
while (current != nullptr) {
// 这里假设 T 支持 << 运算符
// std::cout << "[" << index++ << "] " << current->data << std::endl;
current = current->next;
index++;
}
}
};

144
include/core/ICharacter.h Normal file
View File

@@ -0,0 +1,144 @@
#pragma once
#include <string>
#include <vector>
#include <memory>
// 前向声明
class ICharacter;
class ISkill;
/**
* @brief 技能接口 (抽象基类)
*
* 技术点: 多态 (Polymorphism) - 纯虚函数
*/
class ISkill {
protected:
std::string m_name; // 技能名称
std::string m_description; // 技能描述
int m_manaCost; // 消耗魔法值
int m_cooldown; // 冷却时间
public:
virtual ~ISkill() {}
/**
* @brief 执行技能
* @param caster 施法者
* @param target 目标
* @return std::string 战斗日志描述
*/
virtual std::string execute(ICharacter& caster, ICharacter& target) = 0;
/**
* @brief 获取技能名称
*/
virtual std::string getName() const { return m_name; }
/**
* @brief 获取技能描述
*/
virtual std::string getDescription() const { return m_description; }
/**
* @brief 获取魔法消耗
*/
virtual int getManaCost() const { return m_manaCost; }
/**
* @brief 获取冷却时间
*/
virtual int getCooldown() const { return m_cooldown; }
};
/**
* @brief 角色接口 (抽象基类)
*
* 技术点: 多态 (Polymorphism) - 纯虚函数、虚析构函数
*/
class ICharacter {
protected:
std::string m_name; // 角色名称
std::string m_className; // 职业名称
int m_level; // 等级
int m_hp; // 当前生命值
int m_maxHp; // 最大生命值
int m_mp; // 当前魔法值
int m_maxMp; // 最大魔法值
int m_attack; // 攻击力
int m_defense; // 防御力
int m_speed; // 速度 (决定行动顺序)
std::vector<std::shared_ptr<ISkill>> m_skills; // 技能列表 (STL)
public:
/**
* @brief 虚析构函数 (必须)
*/
virtual ~ICharacter() {}
// === Getters ===
virtual std::string getName() const { return m_name; }
virtual std::string getClassName() const { return m_className; }
virtual int getLevel() const { return m_level; }
virtual int getHp() const { return m_hp; }
virtual int getMaxHp() const { return m_maxHp; }
virtual int getMp() const { return m_mp; }
virtual int getMaxMp() const { return m_maxMp; }
virtual int getAttack() const { return m_attack; }
virtual int getDefense() const { return m_defense; }
virtual int getSpeed() const { return m_speed; }
/**
* @brief 检查角色是否存活
*/
virtual bool isAlive() const { return m_hp > 0; }
/**
* @brief 获取指定名称的技能
* @param skillName 技能名称
* @return ISkill* 技能指针,如果不存在返回 nullptr
*/
virtual ISkill* getSkill(const std::string& skillName);
/**
* @brief 获取所有技能
*/
virtual const std::vector<std::shared_ptr<ISkill>>& getSkills() const {
return m_skills;
}
/**
* @brief 受到伤害 (纯虚函数 - 必须在派生类中实现)
* @param damage 伤害值
*/
virtual void takeDamage(int damage) = 0;
/**
* @brief 治疗 (纯虚函数 - 必须在派生类中实现)
* @param amount 治疗量
*/
virtual void heal(int amount) = 0;
/**
* @brief 消耗魔法值
* @param amount 消耗量
* @return bool 是否成功消耗
*/
virtual bool consumeMana(int amount);
/**
* @brief 恢复魔法值
* @param amount 恢复量
*/
virtual void restoreMana(int amount);
/**
* @brief 获取角色状态字符串
*/
virtual std::string getStatus() const;
/**
* @brief 添加技能
*/
virtual void addSkill(std::shared_ptr<ISkill> skill);
};

72
include/core/Skills.h Normal file
View File

@@ -0,0 +1,72 @@
#pragma once
#include "ICharacter.h"
#include <random>
/**
* @brief 普通攻击技能
*/
class NormalAttack : public ISkill {
public:
NormalAttack();
virtual std::string execute(ICharacter& caster, ICharacter& target) override;
};
/**
* @brief 战士技能: 重击
* 造成高额伤害,消耗怒气
*/
class HeavyStrike : public ISkill {
public:
HeavyStrike();
virtual std::string execute(ICharacter& caster, ICharacter& target) override;
};
/**
* @brief 战士技能: 防御姿态
* 大幅提升防御,回复少量生命
*/
class DefensiveStance : public ISkill {
public:
DefensiveStance();
virtual std::string execute(ICharacter& caster, ICharacter& target) override;
};
/**
* @brief 法师技能: 火球术
* 造成魔法伤害
*/
class Fireball : public ISkill {
public:
Fireball();
virtual std::string execute(ICharacter& caster, ICharacter& target) override;
};
/**
* @brief 法师技能: 冰霜新星
* 造成伤害并降低目标速度
*/
class FrostNova : public ISkill {
public:
FrostNova();
virtual std::string execute(ICharacter& caster, ICharacter& target) override;
};
/**
* @brief 法师技能: 奥术护盾
* 为自己添加法力护盾
*/
class ArcaneShield : public ISkill {
public:
ArcaneShield();
virtual std::string execute(ICharacter& caster, ICharacter& target) override;
};
/**
* @brief 治疗术
* 恢复生命值
*/
class Heal : public ISkill {
public:
Heal();
virtual std::string execute(ICharacter& caster, ICharacter& target) override;
};