-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.cpp
More file actions
229 lines (192 loc) · 5.64 KB
/
Copy pathGameController.cpp
File metadata and controls
229 lines (192 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "GameController.h"
#include "Command.h"
#include <memory>
#include <algorithm>
GameController* GameController::create(GameView* view)
{
GameController* controller = new GameController(view);
if (controller)
{
return controller;
}
CC_SAFE_DELETE(controller);
return nullptr;
}
GameController::GameController(GameView* view)
: _gameView(view)
, _isAnimating(false)
{
_gameData = std::unique_ptr<GameData>(new GameData());
_commandManager = std::unique_ptr<CommandManager>(new CommandManager());
// 设置视图回调
if (_gameView)
{
_gameView->setTableauCardClickCallback(
[this](int index) { this->onTableauCardClicked(index); });
_gameView->setHandCardClickCallback(
[this](int index) { this->onHandCardClicked(index); });
_gameView->setUndoCallback(
[this]() { this->onUndo(); });
}
}
GameController::~GameController()
{
}
void GameController::initialize()
{
_gameData->initialize();
_commandManager->clearHistory();
_isAnimating = false;
updateView();
if (_gameView)
{
_gameView->updateScore(0);
_gameView->updateCombo(0);
}
}
void GameController::resetGame()
{
initialize();
}
void GameController::onTableauCardClicked(int index)
{
if (_isAnimating) return;
// 需求2:检查是否可以匹配(桌面牌和手牌顶部牌点数差1)
if (_gameData->canMatchTableauCard(index))
{
executeMatchWithAnimation(index);
}
else
{
// 显示不可匹配提示(可以添加视觉效果)
}
}
// 需求1:手牌区翻牌替换
void GameController::onHandCardClicked(int index)
{
if (_isAnimating) return;
// 获取当前手牌堆信息
int topIndex = _gameData->getHandPileCount() - 1;
// 不能点击顶部牌自己
if (index == topIndex) return;
// 执行翻牌替换
executeFlipWithAnimation(index);
}
// 需求1:执行翻牌替换并播放动画
void GameController::executeFlipWithAnimation(int handIndex)
{
_isAnimating = true;
// 翻牌会中断连击
int previousScore = _gameData->getScore();
int previousCombo = _gameData->getCombo();
_gameData->resetCombo();
if (_gameView)
{
_gameView->updateCombo(0);
}
// 创建并执行翻牌替换命令
auto command = std::unique_ptr<FlipCardCommand>(new FlipCardCommand(_gameData.get(), handIndex, previousScore, previousCombo));
if (_commandManager->executeCommand(std::move(command)))
{
_gameView->updateUndoButton(_commandManager->canUndo());
refreshHandPileView();
_isAnimating = false;
}
else
{
_isAnimating = false;
}
}
void GameController::onUndo()
{
if (_isAnimating) return;
if (_commandManager->canUndo())
{
_commandManager->undoLastCommand();
updateView();
// 更新回退按钮状态
if (_gameView)
{
_gameView->updateUndoButton(_commandManager->canUndo());
}
}
}
void GameController::executeMatchWithAnimation(int tableauIndex)
{
_isAnimating = true;
// 保存被匹配的牌信息
Card matchedCard = _gameData->getTableauCard(tableauIndex);
const Card* handTopCard = _gameData->getHandTopCard();
int originalIndex = tableauIndex;
// 检查是否涉及大小王
bool isJokerMatch = matchedCard.isJoker() || (handTopCard && handTopCard->isJoker());
// 保存执行前的状态
int previousScore = _gameData->getScore();
int previousCombo = _gameData->getCombo();
// 计算得分
int score = _gameData->calculateMatchScore(tableauIndex);
// 增加连击
_gameData->incrementCombo();
_gameData->addScore(score);
if (_gameView)
{
_gameView->showScorePopup(score, isJokerMatch);
_gameView->updateScore(_gameData->getScore());
_gameView->updateCombo(_gameData->getCombo());
}
// 创建并执行匹配命令
auto command = std::unique_ptr<MatchCardCommand>(new MatchCardCommand(
_gameData.get(), tableauIndex,
previousScore, previousCombo, score, isJokerMatch));
if (_commandManager->executeCommand(std::move(command)))
{
_gameView->updateUndoButton(_commandManager->canUndo());
CardView* cardView = _gameView->getTableauCardView(originalIndex);
if (cardView)
{
_gameView->moveCardToHandTop(cardView, [this, originalIndex]() {
this->refreshTableauView();
this->refreshHandPileView();
_isAnimating = false;
});
}
else
{
refreshTableauView();
refreshHandPileView();
_isAnimating = false;
}
}
else
{
_isAnimating = false;
}
}
void GameController::updateView()
{
refreshTableauView();
refreshHandPileView();
if (_gameView && _gameData)
{
_gameView->updateScore(_gameData->getScore());
_gameView->updateCombo(_gameData->getCombo());
}
}
void GameController::refreshTableauView()
{
if (_gameView && _gameData)
{
_gameView->updateTableauCards(_gameData->getTableauCards());
}
}
void GameController::refreshHandPileView()
{
if (_gameView && _gameData)
{
// 需求1:更新手牌堆显示
_gameView->updateHandPileCards(_gameData->getHandPile(), _gameData->getHandPileCount() - 1);
// 同时更新顶部牌
const Card* topCard = _gameData->getHandTopCard();
_gameView->updateHandTopCard(topCard);
}
}