mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 14:24:10 +08:00
Added OnlineGameScene and OnlineGameUIManager class
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
#include "GameApplication.h"
|
#include "GameApplication.h"
|
||||||
#include "utils/Tools.h"
|
#include "utils/Tools.h"
|
||||||
#include "Time.h"
|
#include "Time.h"
|
||||||
GameApplication::GameApplication() {
|
|
||||||
|
|
||||||
|
GameApplication::GameApplication()
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,6 +27,9 @@ bool GameApplication::initialize() {
|
|||||||
SDL_Log("无法加载json");
|
SDL_Log("无法加载json");
|
||||||
}
|
}
|
||||||
Time::init();
|
Time::init();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 输入管理
|
// 输入管理
|
||||||
m_inputManager = std::make_unique<InputManager>();
|
m_inputManager = std::make_unique<InputManager>();
|
||||||
// 窗口管理
|
// 窗口管理
|
||||||
@@ -96,3 +103,4 @@ void GameApplication::run() {
|
|||||||
|
|
||||||
m_windowManager->Present();
|
m_windowManager->Present();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ private:
|
|||||||
std::unique_ptr<DebugManager> m_debugManager;
|
std::unique_ptr<DebugManager> m_debugManager;
|
||||||
Config m_config;
|
Config m_config;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GameApplication();
|
GameApplication();
|
||||||
~GameApplication();
|
~GameApplication();
|
||||||
bool initialize();
|
bool initialize();
|
||||||
SDL_AppResult handleInputEvent(SDL_Event* event);
|
SDL_AppResult handleInputEvent(SDL_Event* event);
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -38,6 +38,9 @@ void SceneManager::registerAllScene() {
|
|||||||
registerSceneFactory("GameScene", []() -> std::shared_ptr<Scene> {
|
registerSceneFactory("GameScene", []() -> std::shared_ptr<Scene> {
|
||||||
return std::make_shared<GameScene>();
|
return std::make_shared<GameScene>();
|
||||||
});
|
});
|
||||||
|
registerSceneFactory("OnlineGameScene", []() -> std::shared_ptr<Scene> {
|
||||||
|
return std::make_shared<OnlineGameScene>();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Scene> SceneManager::createScene(const std::string& sceneName) {
|
std::shared_ptr<Scene> SceneManager::createScene(const std::string& sceneName) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "Scene.h"
|
#include "Scene.h"
|
||||||
#include "scenes/gameplay/GameScene.h"
|
#include "scenes/gameplay/GameScene.h"
|
||||||
#include "scenes/menu/MainMenuScene.h"
|
#include "scenes/menu/MainMenuScene.h"
|
||||||
|
#include "scenes/gameplay/OnlineGameScene.h"
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ void GameScene::renderUI() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GameScene::handleClick(int logicalX, int logicalY) {
|
void GameScene::handleClick(int logicalX, int logicalY) {
|
||||||
if (m_gameUIManager->handleClick(logicalX, logicalY)) {
|
if (m_gameUIManager && m_gameUIManager->handleClick(logicalX, logicalY)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,12 +21,12 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
void restartGame();
|
void restartGame();
|
||||||
private:
|
protected:
|
||||||
|
|
||||||
std::unique_ptr<BoardRenderer> m_boardRenderer;
|
std::unique_ptr<BoardRenderer> m_boardRenderer;
|
||||||
std::unique_ptr<CoordinateConverter> m_CoordinateConverter;
|
std::unique_ptr<CoordinateConverter> m_CoordinateConverter;
|
||||||
std::unique_ptr<GameSession> m_gameSession;
|
std::unique_ptr<GameSession> m_gameSession;
|
||||||
|
private:
|
||||||
std::unique_ptr<GameUIManager> m_gameUIManager;
|
std::unique_ptr<GameUIManager> m_gameUIManager;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
82
src/scenes/gameplay/OnlineGameScene.cpp
Normal file
82
src/scenes/gameplay/OnlineGameScene.cpp
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
#include "OnlineGameScene.h"
|
||||||
|
#include "core/GameApplication.h"
|
||||||
|
#include <iostream>
|
||||||
|
void OnlineGameScene::onEnter(SDL_Renderer* renderer, int WIDTH, int HEIGHT, UIRenderer* uiRenderer) {
|
||||||
|
m_renderer = renderer;
|
||||||
|
m_uiRenderer = uiRenderer;
|
||||||
|
m_gameUIManager = std::make_unique<OnlineGameUIManager>(
|
||||||
|
[this](const std::string& sceneName) {
|
||||||
|
if (m_eventCallback) {
|
||||||
|
SceneEvent event{SceneEventType::ChangeScene, sceneName};
|
||||||
|
m_eventCallback(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
m_networkManager = std::make_unique<NetworkManager>();
|
||||||
|
|
||||||
|
m_gameUIManager->init();
|
||||||
|
m_gameUIManager->setCallback([this]() {
|
||||||
|
this->restartGame();
|
||||||
|
});
|
||||||
|
|
||||||
|
m_gameUIManager->setOnlineTypeCallback(
|
||||||
|
[this](NetType type){
|
||||||
|
//std::cout << "try to init networkmanager\n";
|
||||||
|
if (!m_networkManager) {
|
||||||
|
std::cerr << "networkmanager is null\n";
|
||||||
|
}
|
||||||
|
m_networkManager->init(type);
|
||||||
|
if (type == NetType::CLIENT) {
|
||||||
|
m_isMyTurn = false;
|
||||||
|
m_myPlayerID = PlayerID::P2;
|
||||||
|
}
|
||||||
|
if (type == NetType::HOST) {
|
||||||
|
m_isMyTurn = true;
|
||||||
|
m_myPlayerID = PlayerID::P1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
m_boardRenderer = std::make_unique<BoardRenderer>(WIDTH, HEIGHT, renderer);
|
||||||
|
m_gameSession = std::make_unique<GameSession>();
|
||||||
|
m_CoordinateConverter = std::make_unique<CoordinateConverter>(renderer);
|
||||||
|
m_gameSession->initialize();
|
||||||
|
|
||||||
|
m_boardRenderer->setBoard(m_gameSession->getBoard());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnlineGameScene::renderWorld() {
|
||||||
|
GameScene::renderWorld();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnlineGameScene::renderUI() {
|
||||||
|
m_uiRenderer->renderUI(m_gameUIManager->getUIRenderData());
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnlineGameScene::handleClick(int logicalX, int logicalY) {
|
||||||
|
if (m_gameUIManager && m_gameUIManager->handleClick(logicalX, logicalY)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 如果当前是自己的回合就处理点击
|
||||||
|
// 否则忽略
|
||||||
|
if (!m_isMyTurn) {\
|
||||||
|
std::cout << "it is not your turn\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GameScene::handleClick(logicalX, logicalY);
|
||||||
|
if (m_gameSession->getCurrentPlayer() != m_myPlayerID) {
|
||||||
|
m_isMyTurn = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnlineGameScene::handleNetworkClick(int logicalX, int logicalY) {
|
||||||
|
if (m_isMyTurn) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GameScene::handleClick(logicalX, logicalY);
|
||||||
|
if (m_gameSession->getCurrentPlayer() == m_myPlayerID) {
|
||||||
|
m_isMyTurn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/scenes/gameplay/OnlineGameScene.h
Normal file
22
src/scenes/gameplay/OnlineGameScene.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "GameScene.h"
|
||||||
|
#include "ui/managers/OnlineGameUIManager.h"
|
||||||
|
#include "network/NetworkManager.h"
|
||||||
|
class OnlineGameScene : public GameScene {
|
||||||
|
public:
|
||||||
|
|
||||||
|
void onEnter(SDL_Renderer* renderer, int WIDTH, int HEIGHT, UIRenderer* uiRenderer) override;
|
||||||
|
|
||||||
|
void renderWorld() override;
|
||||||
|
|
||||||
|
void handleClick(int logicalX, int logicalY) override;
|
||||||
|
|
||||||
|
void handleNetworkClick(int logicalX, int logicalY);
|
||||||
|
|
||||||
|
void renderUI() override;
|
||||||
|
private:
|
||||||
|
PlayerID m_myPlayerID;
|
||||||
|
bool m_isMyTurn;
|
||||||
|
std::unique_ptr<NetworkManager> m_networkManager;
|
||||||
|
std::unique_ptr<OnlineGameUIManager> m_gameUIManager;
|
||||||
|
};
|
||||||
@@ -23,7 +23,7 @@ public:
|
|||||||
void updateGameState(GameState state);
|
void updateGameState(GameState state);
|
||||||
|
|
||||||
bool handleClick(int logicalX, int logicalY);
|
bool handleClick(int logicalX, int logicalY);
|
||||||
private:
|
protected:
|
||||||
std::function<void()> m_restartCallback;
|
std::function<void()> m_restartCallback;
|
||||||
void setupUIComponents();
|
void setupUIComponents();
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,13 @@ void MainMenuUIManager::init() {
|
|||||||
|
|
||||||
m_buttons.emplace(startButton->getNameHash(), std::move(startButton));
|
m_buttons.emplace(startButton->getNameHash(), std::move(startButton));
|
||||||
|
|
||||||
|
auto onlineStartButton = UIWidgetFactory::createStandardButton(
|
||||||
|
"OnlineStartButton", "联机对战", 275, 250,
|
||||||
|
[this]() {m_eventCallback("OnlineGameScene"); }
|
||||||
|
);
|
||||||
|
|
||||||
|
m_buttons.emplace(onlineStartButton->getNameHash(), std::move(onlineStartButton));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainMenuUIManager::CollectRenderData() {
|
void MainMenuUIManager::CollectRenderData() {
|
||||||
|
|||||||
13
src/ui/managers/OnlineGameUIManager.h
Normal file
13
src/ui/managers/OnlineGameUIManager.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "GameUIManager.h"
|
||||||
|
#include "network/NetData.h"
|
||||||
|
class OnlineGameUIManager : public GameUIManager {
|
||||||
|
public:
|
||||||
|
using OnlineTypeEvent = std::function<void(NetType )>;
|
||||||
|
OnlineGameUIManager(SceneEventCallback eventCallback);
|
||||||
|
~OnlineGameUIManager();
|
||||||
|
void init() override;
|
||||||
|
void setOnlineTypeCallback(OnlineTypeEvent type);
|
||||||
|
private:
|
||||||
|
OnlineTypeEvent m_onlineTypeEvent;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user