From ac58e1bfb76fad6b505f512abec230c3a3793f1d Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Wed, 4 Feb 2026 17:11:36 +0800 Subject: [PATCH] feat: add Piece HP and ATK --- src/game/Board.cpp | 18 ++++++++++++ src/game/Board.h | 4 +++ src/game/GameSession.cpp | 59 ++++++++++++++++++++++++++++++++++------ src/game/GameSession.h | 6 ++++ src/game/GameTypes.h | 13 +++++++-- src/game/Piece.h | 16 ++++++++++- 6 files changed, 105 insertions(+), 11 deletions(-) diff --git a/src/game/Board.cpp b/src/game/Board.cpp index 6bd3659..fc42e93 100644 --- a/src/game/Board.cpp +++ b/src/game/Board.cpp @@ -195,4 +195,22 @@ std::unordered_set Board::getSporeRange(int PieceID) const { return SporeRegion; +} + +bool Board::changeHP(int row, int col, int num) { + int pieceID = getPieceID(row, col); + if (!m_grid[pieceID]) { + return false; + } + m_grid[pieceID]->changeHP(num); + return true; +} + +bool Board::changeATK(int row, int col, int num) { + int pieceID = getPieceID(row, col); + if (!m_grid[pieceID]) { + return false; + } + m_grid[pieceID]->changeATK(num); + return true; } \ No newline at end of file diff --git a/src/game/Board.h b/src/game/Board.h index 0a51356..b5d05c5 100644 --- a/src/game/Board.h +++ b/src/game/Board.h @@ -53,4 +53,8 @@ public: std::unordered_set getOrthogonalNeighbors(int PieceID) const; // 获取扩散范围 std::unordered_set getSporeRange(int PieceID) const; + + bool changeHP(int row, int col, int num); + + bool changeATK(int row, int col, int num); }; \ No newline at end of file diff --git a/src/game/GameSession.cpp b/src/game/GameSession.cpp index e6f5e44..50509f8 100644 --- a/src/game/GameSession.cpp +++ b/src/game/GameSession.cpp @@ -4,7 +4,7 @@ GameSession::GameSession() { - m_board = std::make_unique(7, 7); + m_board = std::make_unique(ROWS, COLS); } @@ -73,14 +73,43 @@ bool GameSession::executeAction(int toRow, int toCol) { } if (m_currentActionType == ActionType::MOVE) { if (Rule::canMove(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) { - m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol, -1, -1); - m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, toRow, toCol, -1, -1); - m_gamePieceEventCallback(GamePieceEvent::MOVE_PIECE, fromRow, fromCol, toRow, toCol); - m_board->removePieceAt(fromRow, fromCol); + //m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol, -1, -1); + //m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, toRow, toCol, -1, -1); + //m_gamePieceEventCallback(GamePieceEvent::MOVE_PIECE, fromRow, fromCol, toRow, toCol); + auto fromPiece = m_board->getPieceAt(fromRow, fromCol); + auto toPiece = m_board->getPieceAt(toRow, toCol); + + if (!fromPiece) { + std::cout << "GameSession: fromPiece is null\n"; + exit(EXIT_FAILURE); + } - m_board->removePieceAt(toRow, toCol); + if (!toPiece) { + m_gamePieceEventCallback(GamePieceEvent::MOVE_PIECE, fromRow, fromCol, toRow, toCol); + m_board->removePieceAt(fromRow, fromCol); + m_board->placePieceAt(toRow, toCol, m_currentPlayer); + markComponentAsUsed(getOldComponentID(fromRow, fromCol)); + return true; + } + + m_board->changeHP(toRow, toCol, -fromPiece->getATK()); + m_board->changeHP(fromRow, fromCol, -toPiece->getATK() * 0.5); + + if (fromPiece->getHP() <= 0) { + m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol, -1, -1); + m_board->removePieceAt(fromRow, fromCol); + } + if (toPiece->getHP() <= 0) { + m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, toRow, toCol, -1, -1); + m_board->removePieceAt(toRow, toCol); + + if (fromPiece->getHP() > 0) { + m_gamePieceEventCallback(GamePieceEvent::MOVE_PIECE, fromRow, fromCol, toRow, toCol); + m_board->removePieceAt(fromRow, fromCol); + m_board->placePieceAt(toRow, toCol, m_currentPlayer); + } + } - m_board->placePieceAt(toRow, toCol, m_currentPlayer); markComponentAsUsed(getOldComponentID(fromRow, fromCol)); @@ -122,13 +151,27 @@ void GameSession::printBoard() const { } void GameSession::nextTurn() { - std::cout << "switch to " << ((m_currentPlayer == PlayerID::P1) ? "P2" : "P1") << "\n"; + std::cout << "GameSession: switch to " << ((m_currentPlayer == PlayerID::P1) ? "P2" : "P1") << "\n"; m_seletedPiece = std::nullopt; m_currentPlayer = (m_currentPlayer == PlayerID::P1) ? PlayerID::P2 : PlayerID::P1; resetOldPieceIDtoComponent(); resetActionableComponents(); m_currentActionType = ActionType::GROW; + if (m_currentPlayer == PlayerID::P1) { + GameRounds++; + std::cout << "GameSession: Current Round is " << GameRounds << "\n"; + } + + // 回合结束增加生命值 + + for (int i = 0; i < ROWS; i++) { + for (int j = 0; j < COLS; j++) { + //int pieceID = m_board->getPieceID(i, j); + m_board->changeHP(i, j, Stat::HealthRegenPerTurn); + } + } + } bool GameSession::handleCoordinateInput(int row, int col) { diff --git a/src/game/GameSession.h b/src/game/GameSession.h index 4b0204c..97dfde5 100644 --- a/src/game/GameSession.h +++ b/src/game/GameSession.h @@ -27,6 +27,10 @@ private: GamePieceEventCallback m_gamePieceEventCallback; + int GameRounds = 1; + + + public: GameSession(); ~GameSession(); @@ -66,4 +70,6 @@ public: m_gamePieceEventCallback = callback; } + + }; \ No newline at end of file diff --git a/src/game/GameTypes.h b/src/game/GameTypes.h index c8c1ab8..c963ab6 100644 --- a/src/game/GameTypes.h +++ b/src/game/GameTypes.h @@ -1,7 +1,8 @@ #pragma once // 这里存储游戏的各种状态 - +constexpr int ROWS = 7; +constexpr int COLS = 7; enum class PlayerID { P1, @@ -34,4 +35,12 @@ enum class GamePieceEvent { MOVE_PIECE, GROW_PIECE, SPORE_PIECE -}; \ No newline at end of file +}; + + +namespace Stat { + constexpr int DefaultHP = 20; + constexpr int DefaultATK = 10; + constexpr int HealthRegenPerTurn = 2; +} + diff --git a/src/game/Piece.h b/src/game/Piece.h index faa0d6a..cb5dbb8 100644 --- a/src/game/Piece.h +++ b/src/game/Piece.h @@ -5,10 +5,24 @@ class Piece { private: PlayerID m_owner; + int HP = 20; + int ATK = 10; public: Piece(PlayerID ID); ~Piece(); PlayerID getPieceOwner() const; - + void changeHP(int num) { + HP += num; + } + void changeATK(int num) { + ATK += num; + } + + int getHP() const { + return HP; + } + int getATK() const { + return ATK; + } }; \ No newline at end of file