-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJewelBoard.cpp
175 lines (146 loc) · 3.91 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
//
// JewelBoard.cpp
// SDL Game Programming Book
//
// Created by shaun mitchell on 26/03/2013.
// Copyright (c) 2013 shaun mitchell. All rights reserved.
//
#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 <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)
{
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);
m_jewels[pos.m_row][pos.m_col] = jo;
Vector2D pixel = getJewelPixel(pos);
jo->getPixel() = pixel;
}, true);
}
void JewelBoard::kill(BoardPos pos)
{
getJewel(pos).kill(); //.setColor(Jewel::NO_COLOR);
}
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())
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::shiftDown(BoardPos pos)
{
JewelObject &jo = getJewel(pos);
//it will be reset to falling if lower jewel is detected to be empty
jo.setFalling(false);
if (pos.m_row < BoardPos::NUM_ROWS)
getJewel(BoardPos(pos.m_col, pos.m_row + 1)).getModel() = jo.getModel();
else
{
BoardPos fallPos(pos.m_col, BoardPos::NUM_ROWS);
JewelObject &last = getJewel(fallPos);
while(fallPos.m_row > 0)
{
m_jewels[fallPos.m_row][fallPos.m_col] = m_jewels[fallPos.m_row - 1][fallPos.m_col];
fallPos.m_row--;
}
m_jewels[0][fallPos.m_col] = &last;
m_jewels[0][fallPos.m_col]->getModel().setColor(random() % Jewel::NUM_COLORS);
}
}
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.getFallingStep() == 10)
shiftDown(pos);
if (jo.isFalling() || jo.isDead())
{
getJewel(BoardPos(pos.m_col, pos.m_row - 1)).setFalling(true);
}
jo.update();
if (jo.isFalling())
{
m_pixel.setY(m_pixel.getY() + m_height / 10.0); //jo.getFallingStep() *
}
});
}
void JewelBoard::clean()
{
forAll([&](JewelObject &jewel)
{
jewel.clean();
});
BoardObject::clean();
}