mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-09 22:06:09 +08:00
feat: add texture destory function
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
GameSession::GameSession()
|
||||
{
|
||||
m_board = std::make_unique<Board>(7, 7);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +62,7 @@ bool GameSession::executeAction(int toRow, int toCol) {
|
||||
if (m_currentActionType == ActionType::GROW) {
|
||||
if (Rule::canGrow(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) {
|
||||
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
||||
m_gamePieceEventCallback(GamePieceEvent::PLACE_PIECE, toRow, toCol);
|
||||
// 如果执行了操作就擦除
|
||||
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
||||
return true;
|
||||
@@ -69,7 +71,9 @@ 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_board->removePieceAt(fromRow, fromCol);
|
||||
m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol);
|
||||
m_board->removePieceAt(toRow, toCol);
|
||||
m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, toRow, toCol);
|
||||
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
||||
|
||||
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
||||
@@ -79,8 +83,9 @@ bool GameSession::executeAction(int toRow, int toCol) {
|
||||
if (m_currentActionType == ActionType::SPORE) {
|
||||
if (Rule::canSpore(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) {
|
||||
m_board->removePieceAt(fromRow, fromCol);
|
||||
m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol);
|
||||
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
||||
|
||||
m_gamePieceEventCallback(GamePieceEvent::PLACE_PIECE, toRow, toCol);
|
||||
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3,14 +3,16 @@
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
|
||||
#include <functional>
|
||||
#include "Board.h"
|
||||
|
||||
|
||||
|
||||
|
||||
class GameSession {
|
||||
|
||||
private:
|
||||
using GamePieceEventCallback = std::function<void(GamePieceEvent, int x, int y)>;
|
||||
std::unique_ptr<Board> m_board;
|
||||
PlayerID m_currentPlayer = PlayerID::P1;
|
||||
ActionType m_currentActionType = ActionType::GROW;
|
||||
@@ -23,6 +25,8 @@ private:
|
||||
|
||||
GameState m_gameState = GameState::GAME_RUNING;
|
||||
|
||||
GamePieceEventCallback m_gamePieceEventCallback;
|
||||
|
||||
public:
|
||||
GameSession();
|
||||
~GameSession();
|
||||
@@ -58,4 +62,8 @@ public:
|
||||
|
||||
GameState getGameState() const;
|
||||
|
||||
void setGamePieceEventCallback(GamePieceEventCallback callback) {
|
||||
m_gamePieceEventCallback = callback;
|
||||
}
|
||||
|
||||
};
|
||||
@@ -26,4 +26,9 @@ enum class GameMode {
|
||||
VS_AI, // 玩家 vs AI
|
||||
ONLINE_PVP, // 网络对战
|
||||
TUTORIAL_MODE // 教程(可视为特殊模式)
|
||||
};
|
||||
|
||||
enum class GamePieceEvent {
|
||||
REMOVE_PIECE,
|
||||
PLACE_PIECE
|
||||
};
|
||||
@@ -282,4 +282,24 @@ void BoardRenderer::renderBlackOverlay() {
|
||||
|
||||
// 恢复原来的混合模式
|
||||
SDL_SetRenderDrawBlendMode(m_renderer, SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
void BoardRenderer::handleGamePieceEvent(GamePieceEvent event, int row, int col) {
|
||||
auto area = getBoardArea();
|
||||
float x = area.x + col * area.cellSize;
|
||||
float y = area.y + row * area.cellSize;
|
||||
float pieceRadius = m_cellSize * m_pieceRadiusRatio / 2.0f;
|
||||
switch (event) {
|
||||
case (GamePieceEvent::REMOVE_PIECE):
|
||||
//SDL_Log("BoardRenderer: try to destory texture\n");
|
||||
m_textureManager->destoryTexture(
|
||||
x + (area.cellSize - pieceRadius * 2) / 2.0f,
|
||||
y + (area.cellSize - pieceRadius * 2) / 2.0f);
|
||||
break;
|
||||
case (GamePieceEvent::PLACE_PIECE):
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <utility>
|
||||
#include <unordered_set>
|
||||
#include "graphics/texture/TextureManager.h"
|
||||
#include "game/GameTypes.h"
|
||||
struct PlayerColors {
|
||||
SDL_Color P1 = {255, 0, 0, 255}; // 红色
|
||||
SDL_Color P2 = {0, 0, 255, 255}; // 蓝色
|
||||
@@ -65,6 +66,8 @@ public:
|
||||
void setGameState(GameState state);
|
||||
|
||||
void renderBlackOverlay();
|
||||
|
||||
void handleGamePieceEvent(GamePieceEvent event, int row, int col);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -81,3 +81,17 @@ SDL_Texture* TextureManager::getTexture(int x, int y) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool TextureManager::destoryTexture(int x, int y) {
|
||||
int key = makeHash(x, y);
|
||||
auto it = m_cacheTexture.find(key);
|
||||
if (it == m_cacheTexture.end()) {
|
||||
SDL_Log("can't find the texture\n");
|
||||
return false;
|
||||
|
||||
}
|
||||
SDL_DestroyTexture(it->second);
|
||||
m_cacheTexture.erase(key);
|
||||
SDL_Log("TextureManager: destory texture sucessfully\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ public:
|
||||
|
||||
SDL_Texture* getTexture(int x, int y);
|
||||
|
||||
bool destoryTexture(int x, int y);
|
||||
|
||||
private:
|
||||
|
||||
SDL_Renderer* m_renderer = nullptr;
|
||||
|
||||
@@ -71,7 +71,12 @@ void GameScene::onEnter(SDL_Renderer* renderer, int WIDTH, int HEIGHT, UIRendere
|
||||
|
||||
m_boardRenderer->setBoard(m_gameSession->getBoard());
|
||||
|
||||
|
||||
m_gameSession->setGamePieceEventCallback(
|
||||
[this](GamePieceEvent evnet, int row, int col) {
|
||||
std::cout << "GameScene: recevie the event piece at " << row << " " << col << "\n";
|
||||
m_boardRenderer->handleGamePieceEvent(evnet, row, col);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user