-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJewelBoard.cpp
203 lines (168 loc) · 4.83 KB
/
JewelBoard.cpp
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
/**************************************************************************
** Qt Creator license header template
** Special keywords: dpinol 03/03/2014 2014
** Environment variables:
** To protect a percent sign, use '%'.
**************************************************************************/
#include "JewelBoard.h"
#include "JewelObject.h"
#include "TextureManager.h"
#include "InputHandler.h"
#include "Game.h"
#include <model/JewelSwap.h>
#include "utils/utils.h"
#include "utils/log.h"
#include <assert.h>
#include <algorithm>
#include <SDL_events.h>
JewelBoard::JewelBoard() : BoardObject(),
m_model(*this),
m_offset(350, 100),
m_bottomDown(m_offset + Vector2D(JewelObject::WIDTH * BoardPos::NUM_COLS, JewelObject::HEIGHT * (BoardPos::NUM_ROWS + 1)) ),
m_drag(*this),
m_strike(m_model, this)
{
TheTextureManager::Instance()->load("assets/jewels.png", "jewels", TheGame::Instance()->getRenderer());
createInitialJewelsBoard();
}
void JewelBoard::createInitialJewelsBoard()
{
forAllPos([&](BoardPos const pos)
{
Jewel &jewel = m_model.getJewel(pos, true);
JewelObject *jo = new JewelObject(jewel, pos.m_row == 0);
m_jewels[pos.m_row][pos.m_col] = jo;
Vector2D pixel = getJewelPixel(pos);
jo->getPixel() = pixel;
});
}
void JewelBoard::kill(BoardPos pos)
{
getJewel(pos).kill(); //.setColor(Jewel::NO_COLOR);
}
bool JewelBoard::isAlive(BoardPos pos) const
{
return !getJewel(pos).isDead() && !getJewel(pos).isDying();
}
void JewelBoard::load(std::unique_ptr<LoaderParams> const &pParams)
{
//@todo load dimensions from xml
}
/******* Model *******/
#ifdef NDEBUG
inline static void assertBoardPos(BoardPos const)
{
}
#else
inline static void assertBoardPos(BoardPos const pos)
{
if (!pos.isValid(true))
throw std::runtime_error("BoardPos " + dani::toString(pos) + "is not valid");
}
#endif
JewelObject& JewelBoard::getJewel(BoardPos const pos)
{
assertBoardPos(pos);
return *m_jewels[pos.m_row][pos.m_col];
}
JewelObject const& JewelBoard::getJewel(BoardPos const pos) const
{
assertBoardPos(pos);
return *m_jewels[pos.m_row][pos.m_col];
}
/******* Model *******/
void JewelBoard::draw()
{
forAll([&](JewelObject &jewel)
{
//if (jewel.getPixel().getY() + jewel.getHeight() >= m_offset.getY())
if (!jewel.isDead())
jewel.draw();
});
}
Vector2D JewelBoard::getJewelPixel(BoardPos pos) const
{
return Vector2D(m_offset.getX() + JewelObject::WIDTH * pos.m_col,
m_offset.getY() + JewelObject::HEIGHT * pos.m_row);
}
BoardPos JewelBoard::getJewelAt(Vector2D const &v) const
{
if (!v.isInside(m_offset, m_bottomDown))
return BoardPos();
return BoardPos((v.getX() - m_offset.getX()) / JewelObject::WIDTH,
(v.getY() - m_offset.getY()) / JewelObject::HEIGHT);
}
bool JewelBoard::swap(BoardPos const pos1, BoardPos const pos2)
{
JewelSwap sw(m_model);
sw.setPositions(pos1, pos2);
return sw.run();
}
void JewelBoard::pureSwap(BoardPos pos, BoardPos pos2)
{
/*getJewel(BoardPos(pos.m_col, pos.m_row + 1)) = jo;
getJewel(BoardPos(pos.m_col, pos.m_row)).resetFalling();*/
std::swap( m_jewels[pos.m_row][pos.m_col], m_jewels[pos2.m_row][pos2.m_col]);
}
void JewelBoard::shiftDown(BoardPos pos)
{
//JewelObject &jo = getJewel(pos);
assert(pos.m_row < BoardPos::NUM_ROWS);
{
BoardPos next = pos.getBelow();
//@bug still would rarely fails.pending to investigate
//assert(getJewel(next).isDead());
LOG_INFO("jewel " << next << " popped up ");
pureSwap(pos, next);
m_model.pureSwap(pos, next);
//it will be set to falling again if lower jewel is detected to be empty
getJewel(next).setFalling(false);
getJewel(pos).setFalling(false);
}
}
void JewelBoard::update()
{
bool clicked = TheInputHandler::Instance()->getMouseButtonState(LEFT);
if (clicked)
m_drag.drag();
else
m_drag.drop();
m_model.update();
forAllPos([&](BoardPos pos)
{
JewelObject &jo = getJewel(pos);
if (jo.isFallDone())
shiftDown(pos);
if (pos.m_row > 0 && !jo.isDying() && (jo.isFalling() || jo.isDead()))
{
BoardPos const upperPos = BoardPos(pos.m_col, pos.m_row - 1);
JewelObject &upper = getJewel(upperPos);
if (!upper.isFalling())
{
upper.setFalling();
//resurrect so that it will fall
if (pos.m_row == 1 && upper.isDead())
{
LOG_INFO("Resurrecting " << upperPos);
upper.resurrect();
upper.getPixel() = getJewelPixel(upperPos);
}
}
}
jo.update();
});
//mostly works, but eventually a falling jewel overtakes lower one and assert at shiftDown fails
//or jewels stop at halfway
forAllPos([&](BoardPos pos)
{
m_strike.findMatch(pos, getJewel(pos).getModel().getColor());
}, false);
}
void JewelBoard::clean()
{
forAll([&](JewelObject &jewel)
{
jewel.clean();
});
BoardObject::clean();
}