1. 历时两天,完整且完美的设计了宠物类,使用到了多种设计模式,完成了低耦合,高内聚的完美代码,测试也完美通过。

2. 顺便完善了底层通信类的封装,基于websocket,基准测试通过,但存在一点很小的线程bug,似乎是来自于esp32 idf底层的问题,待解决
This commit is contained in:
Misaki
2025-09-15 01:49:09 +08:00
parent 97fe13da26
commit dc420c3b7a
12 changed files with 1466 additions and 74 deletions
+110
View File
@@ -4,8 +4,118 @@
#include "CppHandle.h"
#include "OTAClass.h"
#include "PetBaseClass.h"
#include "PetDao.h"
#include <iostream>
void testPetSystem() {
std::cout << "Test point1" << std::endl;
// 创建阶段策略
auto stageStrategy = std::make_unique<PetStageStrategy>();
stageStrategy->addStage(PetStageType::PET_STAGE_YOUNG, "models/young.obj");
stageStrategy->addStage(PetStageType::PET_STAGE_ADULT, "models/adult.obj");
stageStrategy->addStage(PetStageType::PET_STAGE_OLD, "models/old.obj");
stageStrategy->addStageAudio(PetStageType::PET_STAGE_YOUNG, "audio/young.mp3");
stageStrategy->addStageAudio(PetStageType::PET_STAGE_ADULT, "audio/adult.mp3");
stageStrategy->addStageAudio(PetStageType::PET_STAGE_OLD, "audio/old.mp3");
// 创建动作策略
auto actionStrategy = std::make_unique<PetActionStrategy>();
actionStrategy->addAction(PetActionType::PET_ACTION_EAT, "models/eat.obj");
actionStrategy->addAction(PetActionType::PET_ACTION_HAPPY, "models/happy.obj");
actionStrategy->addAction(PetActionType::PET_ACTION_SLEEP, "models/sleep.obj");
actionStrategy->addAction(PetActionType::PET_ACTION_ANGRY, "models/angry.obj");
actionStrategy->addAction(PetActionType::PET_ACTION_SAD, "models/sad.obj");
actionStrategy->addAction(PetActionType::PET_ACTION_EVOLVE, "models/evolve.obj");
actionStrategy->addAction(PetActionType::PET_ACTION_TOUCH, "models/touch.obj");
actionStrategy->addActionAudio(PetActionType::PET_ACTION_EAT, "audio/eat.mp3");
actionStrategy->addActionAudio(PetActionType::PET_ACTION_HAPPY, "audio/happy.mp3");
actionStrategy->addActionAudio(PetActionType::PET_ACTION_SLEEP, "audio/sleep.mp3");
std::cout << "Test point2" << std::endl;
// 创建宠物信息
PetBaseInfo info;
info.pet_name = "芝士雪豹";
info.pet_hp = 100;
info.pet_density = 50;
info.pet_identity = "我是顶真";
// 创建宠物
auto pet = std::make_shared<PetBase>(info, std::move(stageStrategy), std::move(actionStrategy));
std::cout << "Test point3" << std::endl;
// 创建音频观察者
auto audioStrategy = std::make_shared<PetAudioStrategy>();
audioStrategy->setAudioCallback([](const std::string& audioPath) {
std::cout << "Playing audio: " << audioPath << std::endl;
});
audioStrategy->subscribe(pet);
std::cout << "Test point4" << std::endl;
// 创建渲染器观察者
auto rendererStrategy = std::make_shared<PetRendererStrategy>(
pet->getStageStrategy(),
pet->getActionStrategy()
);
rendererStrategy->setRenderCallback([](const std::string& modelPath) {
std::cout << "Rendering model: " << modelPath << std::endl;
});
rendererStrategy->subscribe(pet);
std::cout << "Test point5" << std::endl;
// 执行一些动作
std::cout << "=== Testing basic actions ===" << std::endl;
pet->feed();
pet->play();
pet->touch();
std::cout << "Test point6" << std::endl;
// 检查当前状态
std::cout << "Current HP: " << pet->getPetInfo().pet_hp << std::endl;
std::cout << "Current density: " << pet->getPetInfo().pet_density << std::endl;
std::cout << "Current stage: " << static_cast<int>(pet->getCurrentStage()) << std::endl;
std::cout << "Current action: " << static_cast<int>(pet->getCurrentAction()) << std::endl;
std::cout << "Test point7" << std::endl;
// 测试进化
std::cout << "\n=== Testing evolution ===" << std::endl;
// 直接修改亲密度来测试进化
PetBaseInfo newInfo = pet->getPetInfo();
newInfo.pet_density = 100; // 达到进化条件
pet->setPetInfo(newInfo);
std::cout << "Test point8" << std::endl;
if (pet->checkEvolution()) {
std::cout << "Evolution successful!" << std::endl;
} else {
std::cout << "Evolution failed!" << std::endl;
}
// 进一步增加亲密度到150,尝试再次进化
newInfo.pet_density = 150;
pet->setPetInfo(newInfo);
if (pet->checkEvolution()) {
std::cout << "Second evolution successful!" << std::endl;
} else {
std::cout << "Second evolution failed!" << std::endl;
}
std::cout << "Final stage: " << static_cast<int>(pet->getCurrentStage()) << std::endl;
PetDAO petDAO(SDFileManager::getInstance());
petDAO.savePet(pet, "my_pet.json");
// 列出所有宠物文件
auto petFiles = petDAO.listPetFiles();
for (const auto& file : petFiles) {
std::cout << "Pet file: " << file << std::endl;
}
std::cout << SDFileManager::getInstance()->catCommand("/sdcard/pet_data/my_pet.json") << std::endl;
}
void Cpp_Hand() {
testPetSystem();
OTAClass oc;
oc.Init();
}