ba5e47bc77
1. 应用界面增加了返回主页的按钮 2. 修复了gif渲染内存泄漏的严重bug 3. 将PetDao当中的cJSON API替换为cpp_json,完美通过测试 4. 整合已经实现的各种上层建筑,实现了一个宠物对话基本业务应用,用于样品测试展示用 5. 重构了音频播放类,使其更modern,更加便于移植和拓展
108 lines
3.5 KiB
C++
108 lines
3.5 KiB
C++
//
|
|
// Created by misaki on 2025/9/14.
|
|
//
|
|
#pragma once
|
|
|
|
#include "PetBaseClass.h"
|
|
#include "SDFileManager.h"
|
|
#include "cpp_json.h"
|
|
#include <string>
|
|
|
|
// 辅助函数:枚举类型与字符串的转换
|
|
namespace PetEnumConverter {
|
|
// PetStageType 转换
|
|
std::string stageTypeToString(PetStageType stage);
|
|
PetStageType stringToStageType(const std::string &str);
|
|
// PetActionType 转换
|
|
std::string actionTypeToString(PetActionType action);
|
|
PetActionType stringToActionType(const std::string &str);
|
|
}
|
|
|
|
// PetDAO 类 - 负责宠物的数据持久化
|
|
class PetDAO {
|
|
public:
|
|
// 构造函数,需要SDFileManager实例
|
|
explicit PetDAO(SDFileManager *fileManager);
|
|
|
|
// 保存宠物数据到文件
|
|
bool savePet(const std::shared_ptr<PetBase> &pet, const std::string &filename);
|
|
|
|
// 从文件加载宠物数据
|
|
std::shared_ptr<PetBase> loadPet(const std::string &filename);
|
|
|
|
// 获取所有保存的宠物文件列表
|
|
std::vector<std::string> listPetFiles();
|
|
|
|
// 删除宠物文件
|
|
bool deletePetFile(const std::string &filename);
|
|
|
|
private:
|
|
// 将宠物数据转换为JSON对象
|
|
cppjson::Json petToJson(const std::shared_ptr<PetBase> &pet); // 返回 cppjson::Json
|
|
|
|
// 从JSON对象创建宠物
|
|
std::shared_ptr<PetBase> petFromJson(const cppjson::Json &json); // 参数改为 cppjson::Json
|
|
|
|
// 将阶段策略转换为JSON对象
|
|
cppjson::Json stageStrategyToJson(const std::shared_ptr<PetStageStrategy> &strategy);
|
|
|
|
// 从JSON对象创建阶段策略
|
|
std::shared_ptr<PetStageStrategy> stageStrategyFromJson(const cppjson::Json &json);
|
|
|
|
// 将动作策略转换为JSON对象
|
|
cppjson::Json actionStrategyToJson(const std::shared_ptr<PetActionStrategy> &strategy);
|
|
|
|
// 从JSON对象创建动作策略
|
|
std::shared_ptr<PetActionStrategy> actionStrategyFromJson(const cppjson::Json &json);
|
|
|
|
// 文件管理器实例
|
|
SDFileManager *fileManager;
|
|
|
|
// 宠物数据存储目录
|
|
static constexpr const char *PET_DATA_DIR = "/sdcard/pet_data";
|
|
};
|
|
|
|
/**
|
|
* 宠物数据结构(JSON)
|
|
{
|
|
"name": "芝士雪豹",
|
|
"hp": 85,
|
|
"density": 120,
|
|
"identity": "我是顶真,是妈妈省的",
|
|
"stage_strategy": {
|
|
"current_stage": "PET_STAGE_ADULT",
|
|
"stage_model_map": {
|
|
"PET_STAGE_YOUNG": "/models/snow_leopard_young.gif",
|
|
"PET_STAGE_ADULT": "/models/snow_leopard_adult.gif",
|
|
"PET_STAGE_OLD": "/models/snow_leopard_old.gif"
|
|
},
|
|
"stage_audio_map": {
|
|
"PET_STAGE_YOUNG": "/audio/snow_leopard_young.mp3",
|
|
"PET_STAGE_ADULT": "/audio/snow_leopard_adult.mp3",
|
|
"PET_STAGE_OLD": "/audio/snow_leopard_old.mp3"
|
|
}
|
|
},
|
|
"action_strategy": {
|
|
"current_action": "PET_ACTION_SLEEP",
|
|
"action_model_map": {
|
|
"PET_ACTION_SLEEP": "/models/actions/sleep.gif",
|
|
"PET_ACTION_EAT": "/models/actions/eat.gif",
|
|
"PET_ACTION_HAPPY": "/models/actions/happy.gif",
|
|
"PET_ACTION_ANGRY": "/models/actions/angry.gif",
|
|
"PET_ACTION_SAD": "/models/actions/sad.gif",
|
|
"PET_ACTION_EVOLVE": "/models/actions/evolve.gif",
|
|
"PET_ACTION_TOUCH": "/models/actions/touch.gif"
|
|
},
|
|
"action_audio_map": {
|
|
"PET_ACTION_SLEEP": "/audio/actions/sleep.mp3",
|
|
"PET_ACTION_EAT": "/audio/actions/eat.mp3",
|
|
"PET_ACTION_HAPPY": "/audio/actions/happy.mp3",
|
|
"PET_ACTION_ANGRY": "/audio/actions/angry.mp3",
|
|
"PET_ACTION_SAD": "/audio/actions/sad.mp3",
|
|
"PET_ACTION_EVOLVE": "/audio/actions/evolve.mp3",
|
|
"PET_ACTION_TOUCH": "/audio/actions/touch.mp3"
|
|
}
|
|
}
|
|
}
|
|
*/
|