mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-09 22:06:09 +08:00
feat: add Piece HP and ATK
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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) {
|
||||
|
||||
@@ -27,6 +27,10 @@ private:
|
||||
|
||||
GamePieceEventCallback m_gamePieceEventCallback;
|
||||
|
||||
int GameRounds = 1;
|
||||
|
||||
|
||||
|
||||
public:
|
||||
GameSession();
|
||||
~GameSession();
|
||||
@@ -66,4 +70,6 @@ public:
|
||||
m_gamePieceEventCallback = callback;
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user