feat: add Piece HP and ATK

This commit is contained in:
2026-02-04 17:11:36 +08:00
parent ede8dfbb26
commit ac58e1bfb7
6 changed files with 105 additions and 11 deletions

View File

@@ -195,4 +195,22 @@ std::unordered_set<int> 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;
}

View File

@@ -53,4 +53,8 @@ public:
std::unordered_set<int> getOrthogonalNeighbors(int PieceID) const;
// 获取扩散范围
std::unordered_set<int> getSporeRange(int PieceID) const;
bool changeHP(int row, int col, int num);
bool changeATK(int row, int col, int num);
};

View File

@@ -4,7 +4,7 @@
GameSession::GameSession()
{
m_board = std::make_unique<Board>(7, 7);
m_board = std::make_unique<Board>(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) {

View File

@@ -27,6 +27,10 @@ private:
GamePieceEventCallback m_gamePieceEventCallback;
int GameRounds = 1;
public:
GameSession();
~GameSession();
@@ -66,4 +70,6 @@ public:
m_gamePieceEventCallback = callback;
}
};

View File

@@ -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
};
};
namespace Stat {
constexpr int DefaultHP = 20;
constexpr int DefaultATK = 10;
constexpr int HealthRegenPerTurn = 2;
}

View File

@@ -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;
}
};