mirror of
https://github.com/ChuXunYu/OnlineRpg.git
synced 2026-01-31 08:31:26 +00:00
2
This commit is contained in:
130
include/client/GameClient.h
Normal file
130
include/client/GameClient.h
Normal file
@@ -0,0 +1,130 @@
|
||||
#pragma once
|
||||
#include "SocketWrapper.h"
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
/**
|
||||
* @brief 游戏客户端
|
||||
*
|
||||
* 技术点:
|
||||
* 1. STL - std::thread (多线程)
|
||||
* 2. 网络编程 - Socket Client
|
||||
*
|
||||
* 设计:
|
||||
* - 主线程: 处理用户输入和发送
|
||||
* - 接收线程: 处理服务器消息接收和显示
|
||||
*/
|
||||
class GameClient {
|
||||
private:
|
||||
std::string m_serverAddress;
|
||||
int m_serverPort;
|
||||
SocketWrapper m_socket;
|
||||
|
||||
std::thread m_recvThread;
|
||||
std::atomic<bool> m_isRunning;
|
||||
std::atomic<bool> m_isConnected;
|
||||
std::atomic<bool> m_isAuthenticated;
|
||||
|
||||
std::string m_username;
|
||||
std::atomic<bool> m_inBattle;
|
||||
std::atomic<bool> m_waitingForTurn;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
*/
|
||||
GameClient(const std::string& serverAddress, int serverPort);
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~GameClient();
|
||||
|
||||
/**
|
||||
* @brief 连接到服务器
|
||||
*/
|
||||
bool connect();
|
||||
|
||||
/**
|
||||
* @brief 断开连接
|
||||
*/
|
||||
void disconnect();
|
||||
|
||||
/**
|
||||
* @brief 运行客户端主循环
|
||||
*/
|
||||
void run();
|
||||
|
||||
/**
|
||||
* @brief 发送消息
|
||||
*/
|
||||
bool sendMessage(const std::string& message);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief 接收线程主循环
|
||||
*/
|
||||
void recvLoop();
|
||||
|
||||
/**
|
||||
* @brief 处理服务器消息
|
||||
*/
|
||||
void handleServerMessage(const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 显示主菜单
|
||||
*/
|
||||
void showMainMenu();
|
||||
|
||||
/**
|
||||
* @brief 显示大厅菜单
|
||||
*/
|
||||
void showLobbyMenu();
|
||||
|
||||
/**
|
||||
* @brief 显示战斗菜单
|
||||
*/
|
||||
void showBattleMenu();
|
||||
|
||||
/**
|
||||
* @brief 处理注册
|
||||
*/
|
||||
void handleRegister();
|
||||
|
||||
/**
|
||||
* @brief 处理登录
|
||||
*/
|
||||
void handleLogin();
|
||||
|
||||
/**
|
||||
* @brief 处理大厅输入
|
||||
*/
|
||||
void handleLobbyInput(const std::string& input);
|
||||
|
||||
/**
|
||||
* @brief 处理战斗输入
|
||||
*/
|
||||
void handleBattleInput(const std::string& input);
|
||||
|
||||
/**
|
||||
* @brief 显示提示符
|
||||
*/
|
||||
void showPrompt();
|
||||
|
||||
/**
|
||||
* @brief 清屏
|
||||
*/
|
||||
void clearScreen();
|
||||
|
||||
/**
|
||||
* @brief 打印分隔线
|
||||
*/
|
||||
void printSeparator();
|
||||
|
||||
/**
|
||||
* @brief 打印标题
|
||||
*/
|
||||
void printTitle(const std::string& title);
|
||||
};
|
||||
Reference in New Issue
Block a user