diff --git a/src/core/CoreData.h b/src/core/CoreData.h index b87bd19..c219609 100644 --- a/src/core/CoreData.h +++ b/src/core/CoreData.h @@ -1,7 +1,9 @@ #pragma once #include "ui/managers/debug/DebugData.h" +#include "input/InputState.h" struct CoreData { DebugData debugData; + InputState inputState; }; \ No newline at end of file diff --git a/src/core/GameApplication.cpp b/src/core/GameApplication.cpp index eab3b99..396dc08 100644 --- a/src/core/GameApplication.cpp +++ b/src/core/GameApplication.cpp @@ -31,7 +31,7 @@ bool GameApplication::initialize() { // 输入管理 - m_inputManager = std::make_unique(); + m_inputManager = std::make_unique(m_coreData.inputState); // 窗口管理 m_windowManager = std::make_unique(); @@ -79,6 +79,8 @@ SDL_AppResult GameApplication::handleInputEvent(SDL_Event* event) { } auto pos = Tools::physicalToLogical(input.mouseCurrentPosition.first, input.mouseCurrentPosition.second, m_windowManager->getViewport()); + m_coreData.inputState.mouseCurrentLogicalPosition = pos; + m_debugManager->updateMousePos(pos.first, pos.second, input); m_windowManager->setFullscreen(input.isFullscreen); diff --git a/src/game/GameSession.cpp b/src/game/GameSession.cpp index e6a3a61..d09b6fa 100644 --- a/src/game/GameSession.cpp +++ b/src/game/GameSession.cpp @@ -291,4 +291,21 @@ int GameSession::getOldComponentID(int row, int col) { GameState GameSession::getGameState() const { return m_gameState; + } + + PieceInfo GameSession::getPieceInfo(int row, int col) const { + PieceInfo pieceInfo; + + auto piece = m_board->getPieceAt(row, col); + + if (!piece) { + pieceInfo.hasPiece = false; + return pieceInfo; + } + pieceInfo.hasPiece = true; + pieceInfo.HP = piece->getHP(); + pieceInfo.ATK = piece->getATK(); + + return pieceInfo; + } \ No newline at end of file diff --git a/src/game/GameSession.h b/src/game/GameSession.h index 5037a02..a3d9b5a 100644 --- a/src/game/GameSession.h +++ b/src/game/GameSession.h @@ -74,4 +74,6 @@ public: return m_gameRound; } + PieceInfo getPieceInfo(int row, int col) const; + }; \ No newline at end of file diff --git a/src/game/GameTypes.h b/src/game/GameTypes.h index c963ab6..8ff8e97 100644 --- a/src/game/GameTypes.h +++ b/src/game/GameTypes.h @@ -37,6 +37,12 @@ enum class GamePieceEvent { SPORE_PIECE }; +struct PieceInfo { + bool hasPiece = false; + int HP = 0; + int ATK = 0; +}; + namespace Stat { constexpr int DefaultHP = 20; diff --git a/src/input/InputManager.cpp b/src/input/InputManager.cpp index 98e3f33..e8163e9 100644 --- a/src/input/InputManager.cpp +++ b/src/input/InputManager.cpp @@ -1,5 +1,11 @@ #include "InputManager.h" +InputManager::InputManager(InputState& inputState) : + m_currentInputState(inputState) +{ + +} + SDL_AppResult InputManager::handleInputEvent(const SDL_Event* event) { switch (event->type) { diff --git a/src/input/InputManager.h b/src/input/InputManager.h index ecef5a7..9934791 100644 --- a/src/input/InputManager.h +++ b/src/input/InputManager.h @@ -5,10 +5,10 @@ #include "InputState.h" class InputManager { public: - + InputManager(InputState& inputState); SDL_AppResult handleInputEvent(const SDL_Event* event); InputState GetInputState() const; private: - InputState m_currentInputState; + InputState& m_currentInputState; }; \ No newline at end of file diff --git a/src/input/InputState.h b/src/input/InputState.h index 8c33d7f..c796718 100644 --- a/src/input/InputState.h +++ b/src/input/InputState.h @@ -5,5 +5,6 @@ struct InputState std::pair mouseCilckOn; bool leftMouseDown = false; std::pair mouseCurrentPosition; + std::pair mouseCurrentLogicalPosition; bool isFullscreen = false; }; diff --git a/src/scenes/gameplay/GameScene.cpp b/src/scenes/gameplay/GameScene.cpp index 181bb34..85813f8 100644 --- a/src/scenes/gameplay/GameScene.cpp +++ b/src/scenes/gameplay/GameScene.cpp @@ -82,7 +82,7 @@ void GameScene::onEnter(SDL_Renderer* renderer, int WIDTH, int HEIGHT, UIRendere } void GameScene::update() { - + updatePieceInfo(); } void GameScene::renderWorld() { @@ -149,4 +149,30 @@ void GameScene::restartGame() { } ); +} + +void GameScene::updatePieceInfo() { + auto [mouseX, mouseY] = m_coreData->inputState.mouseCurrentLogicalPosition; + auto click = m_CoordinateConverter->ScreenToBoard(mouseX, mouseY, m_boardRenderer->getBoardArea()); + + if (click == std::nullopt) { + m_gameUIManager->setLabel("PieceInfoLabel", false); + return; + } + + auto [row, col] = click.value(); + + PieceInfo pieceInfo = m_gameSession->getPieceInfo(row, col); + if (!pieceInfo.hasPiece) { + m_gameUIManager->setLabel("PieceInfoLabel", false); + return; + } + std::string text = "HP: " + std::to_string(pieceInfo.HP) + "\n" + "ATK: " + std::to_string(pieceInfo.ATK); + m_gameUIManager->setLabel("PieceInfoLabel", true); + m_gameUIManager->setLabel("PieceInfoLabel", text); + + m_gameUIManager->setLabel("PieceInfoLabel", mouseX, mouseY); + + + } \ No newline at end of file diff --git a/src/scenes/gameplay/GameScene.h b/src/scenes/gameplay/GameScene.h index eddcd4a..edf2722 100644 --- a/src/scenes/gameplay/GameScene.h +++ b/src/scenes/gameplay/GameScene.h @@ -31,6 +31,8 @@ protected: virtual void handleBoardClick(int row, int col); + void updatePieceInfo(); + // 公共成员,子类可以直接访问 std::unique_ptr m_boardRenderer; std::unique_ptr m_CoordinateConverter; diff --git a/src/ui/base/UIWidgetFactory.h b/src/ui/base/UIWidgetFactory.h index 1c2fe33..745992f 100644 --- a/src/ui/base/UIWidgetFactory.h +++ b/src/ui/base/UIWidgetFactory.h @@ -21,6 +21,7 @@ public: label->setText(text, {"SourceHanSansSC-Regular.otf", UI::UI_NORMAL_FONT_SIZE, {0, 0, 0, 255}}); label->setName(name); label->setVisible(true); + label->setBackgroundColor({0, 0, 0, 0}); return label; } diff --git a/src/ui/managers/GameUIManager.cpp b/src/ui/managers/GameUIManager.cpp index 1371293..2d04136 100644 --- a/src/ui/managers/GameUIManager.cpp +++ b/src/ui/managers/GameUIManager.cpp @@ -177,6 +177,15 @@ void GameUIManager::setupUIComponents() { ); m_labels.emplace(playerLabel->getNameHash(), std::move(playerLabel)); + auto pieceInfoLabel = UIWidgetFactory::createStandardLabel( + "PieceInfoLabel", + "HP: 0\nATK: 0", + 120, + 0 + ); + pieceInfoLabel->setVisible(false); + m_labels.emplace(pieceInfoLabel->getNameHash(), std::move(pieceInfoLabel)); + }