mirror of
https://github.com/ChuXunYu/OnlineRpg.git
synced 2026-01-31 12:05:47 +00:00
2
This commit is contained in:
134
include/server/BattleRoom.h
Normal file
134
include/server/BattleRoom.h
Normal file
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
#include "ICharacter.h"
|
||||
#include "HistoryLog.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
// 前向声明
|
||||
class ClientHandler;
|
||||
|
||||
/**
|
||||
* @brief 战斗房间
|
||||
*
|
||||
* 技术点:
|
||||
* 1. 多态 - ICharacter* 和 ISkill*
|
||||
* 2. 模板+链表 - HistoryLog<string>
|
||||
* 3. STL - std::vector, std::mutex
|
||||
*/
|
||||
class BattleRoom {
|
||||
private:
|
||||
int m_roomId;
|
||||
|
||||
// 战斗双方
|
||||
std::shared_ptr<ClientHandler> m_player1;
|
||||
std::shared_ptr<ClientHandler> m_player2;
|
||||
|
||||
// 角色 (多态)
|
||||
std::shared_ptr<ICharacter> m_char1;
|
||||
std::shared_ptr<ICharacter> m_char2;
|
||||
|
||||
// 战斗日志 (模板+链表)
|
||||
HistoryLog<std::string> m_historyLog;
|
||||
|
||||
// 战斗状态
|
||||
bool m_isRunning;
|
||||
int m_currentTurn; // 0 = player1, 1 = player2
|
||||
int m_turnCount;
|
||||
|
||||
std::mutex m_mutex;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
*/
|
||||
BattleRoom(int roomId,
|
||||
std::shared_ptr<ClientHandler> player1,
|
||||
std::shared_ptr<ClientHandler> player2,
|
||||
std::shared_ptr<ICharacter> char1,
|
||||
std::shared_ptr<ICharacter> char2);
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~BattleRoom();
|
||||
|
||||
/**
|
||||
* @brief 启动战斗
|
||||
*/
|
||||
void start();
|
||||
|
||||
/**
|
||||
* @brief 处理玩家行动
|
||||
*/
|
||||
bool handleAction(const std::string& username,
|
||||
int skillIndex,
|
||||
const std::string& targetName);
|
||||
|
||||
/**
|
||||
* @brief 检查战斗是否结束
|
||||
*/
|
||||
bool isFinished() const;
|
||||
|
||||
/**
|
||||
* @brief 获取胜利者
|
||||
*/
|
||||
std::string getWinner() const;
|
||||
|
||||
/**
|
||||
* @brief 获取战斗日志
|
||||
*/
|
||||
std::vector<std::string> getBattleLog() const;
|
||||
|
||||
/**
|
||||
* @brief 获取房间ID
|
||||
*/
|
||||
int getRoomId() const { return m_roomId; }
|
||||
|
||||
/**
|
||||
* @brief 检查是否轮到某玩家
|
||||
*/
|
||||
bool isPlayerTurn(const std::string& username) const;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief 执行一回合
|
||||
*/
|
||||
void executeTurn();
|
||||
|
||||
/**
|
||||
* @brief 广播给双方玩家
|
||||
*/
|
||||
void broadcastToBoth(const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 发送给玩家1
|
||||
*/
|
||||
void sendToPlayer1(const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 发送给玩家2
|
||||
*/
|
||||
void sendToPlayer2(const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 添加战斗日志
|
||||
*/
|
||||
void addLog(const std::string& log);
|
||||
|
||||
/**
|
||||
* @brief 结束战斗
|
||||
*/
|
||||
void endBattle();
|
||||
|
||||
/**
|
||||
* @brief 发送回合通知
|
||||
*/
|
||||
void notifyTurn();
|
||||
|
||||
/**
|
||||
* @brief 发送战斗状态
|
||||
*/
|
||||
void sendBattleStatus();
|
||||
};
|
||||
Reference in New Issue
Block a user