1. 历时两天,完整且完美的设计了宠物类,使用到了多种设计模式,完成了低耦合,高内聚的完美代码,测试也完美通过。
2. 顺便完善了底层通信类的封装,基于websocket,基准测试通过,但存在一点很小的线程bug,似乎是来自于esp32 idf底层的问题,待解决
This commit is contained in:
@@ -2,5 +2,214 @@
|
||||
// Created by misaki on 2025/9/2.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "PetInterface.h"
|
||||
#include "PetObserver.h"
|
||||
// 宠物基类
|
||||
// 宠物基本信息
|
||||
struct PetBaseInfo {
|
||||
///<! 宠物名称
|
||||
std::string pet_name;
|
||||
///<! 宠物生命值(1~100)
|
||||
int pet_hp;
|
||||
///<! 宠物亲密度(1~150) 每过50为一个阶段
|
||||
int pet_density;
|
||||
///<! 宠物身份
|
||||
std::string pet_identity;
|
||||
};
|
||||
// 宠物阶段策略,继承自主题
|
||||
class PetBase : public PetSubject, public std::enable_shared_from_this<PetBase> {
|
||||
public:
|
||||
// 构造函数
|
||||
PetBase() = default;
|
||||
// 带参数的构造函数
|
||||
PetBase(PetBaseInfo info,
|
||||
std::shared_ptr<PetStageStrategy> stageStrategy,
|
||||
std::shared_ptr<PetActionStrategy> actionStrategy)
|
||||
: pet_info(std::move(info)),
|
||||
pet_stage_strategy(std::move(stageStrategy)),
|
||||
pet_action_strategy(std::move(actionStrategy)) {}
|
||||
virtual ~PetBase() = default;
|
||||
// 实现PetSubject接口
|
||||
/**
|
||||
* 添加观察者
|
||||
* @param observer 观察者
|
||||
*/
|
||||
void addObserver(std::shared_ptr<PetObserver> observer) override {
|
||||
observers.push_back(observer);
|
||||
}
|
||||
/**
|
||||
* 移除观察者
|
||||
* @param observer 观察者
|
||||
*/
|
||||
void removeObserver(std::shared_ptr<PetObserver> observer) override {
|
||||
observers.remove(observer);
|
||||
}
|
||||
/**
|
||||
* 通知动作
|
||||
* @param action 动作
|
||||
*/
|
||||
void notifyAction(PetActionType action) override {
|
||||
for (auto& observer : observers) {
|
||||
observer->onPetAction(action);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 通知阶段
|
||||
* @param oldStage 旧阶段
|
||||
* @param newStage 新阶段
|
||||
*/
|
||||
void notifyStageChange(PetStageType oldStage, PetStageType newStage) override {
|
||||
for (auto& observer : observers) {
|
||||
observer->onPetStageChange(oldStage, newStage);
|
||||
}
|
||||
}
|
||||
// 宠物行为方法
|
||||
// 喂食
|
||||
virtual void feed() {
|
||||
// 喂食会增加生命值和亲密度
|
||||
pet_info.pet_hp = std::min(100, pet_info.pet_hp + 10);
|
||||
pet_info.pet_density = std::min(150, pet_info.pet_density + 15);
|
||||
// 执行喂食动作并通知观察者
|
||||
if (pet_action_strategy->performAction(PetActionType::PET_ACTION_EAT)) {
|
||||
notifyAction(PetActionType::PET_ACTION_EAT);
|
||||
}
|
||||
// 检查是否可以进化
|
||||
checkEvolution();
|
||||
}
|
||||
// 玩耍
|
||||
virtual void play() {
|
||||
// 玩耍会增加亲密度
|
||||
pet_info.pet_density = std::min(150, pet_info.pet_density + 10);
|
||||
// 执行开心动作并通知观察者
|
||||
if (pet_action_strategy->performAction(PetActionType::PET_ACTION_HAPPY)) {
|
||||
notifyAction(PetActionType::PET_ACTION_HAPPY);
|
||||
}
|
||||
// 检查是否可以进化
|
||||
checkEvolution();
|
||||
}
|
||||
// 责骂
|
||||
virtual void scold() {
|
||||
// 责骂会减少亲密度
|
||||
pet_info.pet_density = std::max(0, pet_info.pet_density - 10);
|
||||
// 执行生气动作并通知观察者
|
||||
if (pet_action_strategy->performAction(PetActionType::PET_ACTION_ANGRY)) {
|
||||
notifyAction(PetActionType::PET_ACTION_ANGRY);
|
||||
}
|
||||
}
|
||||
// 忽视
|
||||
virtual void neglect() {
|
||||
// 忽视会减少生命值和亲密度
|
||||
pet_info.pet_hp = std::max(0, pet_info.pet_hp - 5);
|
||||
pet_info.pet_density = std::max(0, pet_info.pet_density - 8);
|
||||
// 执行沮丧动作并通知观察者
|
||||
if (pet_action_strategy->performAction(PetActionType::PET_ACTION_SAD)) {
|
||||
notifyAction(PetActionType::PET_ACTION_SAD);
|
||||
}
|
||||
}
|
||||
// 触摸
|
||||
virtual void touch() {
|
||||
// 触摸会增加亲密度
|
||||
pet_info.pet_density = std::min(150, pet_info.pet_density + 5);
|
||||
// 执行被触摸动作并通知观察者
|
||||
if (pet_action_strategy->performAction(PetActionType::PET_ACTION_TOUCH)) {
|
||||
notifyAction(PetActionType::PET_ACTION_TOUCH);
|
||||
}
|
||||
// 检查是否可以进化
|
||||
checkEvolution();
|
||||
}
|
||||
// 进化检查
|
||||
virtual bool checkEvolution() {
|
||||
// 获取当前阶段
|
||||
auto currentStage = pet_stage_strategy->getCurrentStageType();
|
||||
// 根据亲密度决定是否可以进化
|
||||
if (pet_info.pet_density >= 100 && currentStage == PetStageType::PET_STAGE_YOUNG) {
|
||||
// 从幼年进化到青年
|
||||
return evolveToStage(PetStageType::PET_STAGE_ADULT);
|
||||
} else if (pet_info.pet_density >= 150 && currentStage == PetStageType::PET_STAGE_ADULT) {
|
||||
// 从青年进化到成年
|
||||
return evolveToStage(PetStageType::PET_STAGE_OLD);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// 进化到指定阶段
|
||||
virtual bool evolveToStage(PetStageType newStage) {
|
||||
auto oldStage = pet_stage_strategy->getCurrentStageType(); // 获取当前阶段
|
||||
if (pet_stage_strategy->switchToStage(newStage)) {
|
||||
// 执行进化动作并通知观察者
|
||||
if (pet_action_strategy->performAction(PetActionType::PET_ACTION_EVOLVE)) {
|
||||
notifyAction(PetActionType::PET_ACTION_EVOLVE);
|
||||
}
|
||||
|
||||
// 通知阶段变化
|
||||
notifyStageChange(oldStage, newStage);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* 获取宠物信息
|
||||
* @return 宠物信息
|
||||
*/
|
||||
virtual const PetBaseInfo& getPetInfo() const {
|
||||
return pet_info;
|
||||
}
|
||||
/**
|
||||
* 设置宠物信息
|
||||
* @param info 宠物信息
|
||||
*/
|
||||
virtual void setPetInfo(const PetBaseInfo& info) {
|
||||
pet_info = info;
|
||||
}
|
||||
/**
|
||||
* 获取阶段策略
|
||||
* @return 阶段策略
|
||||
*/
|
||||
virtual std::shared_ptr<PetStageStrategy> getStageStrategy() const {
|
||||
return pet_stage_strategy;
|
||||
}
|
||||
/**
|
||||
* 设置阶段策略
|
||||
* @param strategy 阶段策略
|
||||
*/
|
||||
virtual void setStageStrategy(std::unique_ptr<PetStageStrategy> strategy) {
|
||||
pet_stage_strategy = std::move(strategy);
|
||||
}
|
||||
/**
|
||||
* 获取动作策略
|
||||
* @return 动作策略
|
||||
*/
|
||||
virtual std::shared_ptr<PetActionStrategy> getActionStrategy() const {
|
||||
return pet_action_strategy;
|
||||
}
|
||||
/**
|
||||
* 设置动作策略
|
||||
* @param strategy 动作策略
|
||||
*/
|
||||
virtual void setActionStrategy(std::unique_ptr<PetActionStrategy> strategy) {
|
||||
pet_action_strategy = std::move(strategy);
|
||||
}
|
||||
/**
|
||||
* 获取当前阶段
|
||||
* @return 当前阶段
|
||||
*/
|
||||
virtual PetStageType getCurrentStage() const {
|
||||
return pet_stage_strategy->getCurrentStageType();
|
||||
}
|
||||
/**
|
||||
* 获取当前动作
|
||||
* @return 当前动作
|
||||
*/
|
||||
virtual PetActionType getCurrentAction() const {
|
||||
return pet_action_strategy->getCurrentActionType();
|
||||
}
|
||||
public:
|
||||
///<! 宠物信息
|
||||
PetBaseInfo pet_info;
|
||||
///<! 宠物阶段策略
|
||||
std::shared_ptr<PetStageStrategy> pet_stage_strategy;
|
||||
///<! 宠物动作策略
|
||||
std::shared_ptr<PetActionStrategy> pet_action_strategy;
|
||||
///<! 观察者列表
|
||||
std::list<std::shared_ptr<PetObserver>> observers;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user