Skip to content

Commit 36be671

Browse files
committed
implementing jewels fall and rollup
1 parent c7f0812 commit 36be671

6 files changed

+116
-70
lines changed

JewelBoard.cpp

+42-11
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ JewelBoard::JewelBoard() : BoardObject(),
3030
createInitialJewelsBoard();
3131
}
3232

33+
3334
void JewelBoard::createInitialJewelsBoard()
3435
{
35-
for (unsigned y = 0 ; y <= BoardPos::BoardPos::NUM_ROWS ; ++y)
36-
{
37-
for (unsigned x = 0 ; x < BoardPos::BoardPos::NUM_COLS ; ++x)
36+
forAllPos([&](BoardPos const pos)
3837
{
39-
Jewel &jewel = m_model.getJewel(BoardPos(x, y), true);
38+
Jewel &jewel = m_model.getJewel(pos, true);
4039
JewelObject *jo = new JewelObject(jewel);
41-
m_jewels[y][x] = jo;
42-
jo->getPixel().setX(m_offset.getX() + jo->getWidth() * x);
43-
jo->getPixel().setY(m_offset.getY() + jo->getHeight() * y);
44-
}
45-
}
40+
m_jewels[pos.m_row][pos.m_col] = jo;
41+
Vector2D pixel = getJewelPixel(pos);
42+
jo->getPixel() = pixel;
43+
}, true);
4644
}
4745

4846
void JewelBoard::kill(BoardPos pos)
@@ -96,6 +94,12 @@ void JewelBoard::draw()
9694
});
9795
}
9896

97+
Vector2D JewelBoard::getJewelPixel(BoardPos pos) const
98+
{
99+
return Vector2D(m_offset.getX() + JewelObject::WIDTH * pos.m_col,
100+
m_offset.getY() + JewelObject::HEIGHT * pos.m_row);
101+
}
102+
99103
BoardPos JewelBoard::getJewelAt(Vector2D const &v) const
100104
{
101105
if (!v.isInside(m_offset, m_bottomDown))
@@ -111,6 +115,26 @@ bool JewelBoard::swap(BoardPos const pos1, BoardPos const pos2)
111115
return sw.run();
112116
}
113117

118+
void JewelBoard::shiftDown(BoardPos pos)
119+
{
120+
JewelObject &jo = getJewel(pos);
121+
//it will be reset to falling if lower jewel is detected to be empty
122+
jo.setFalling(false);
123+
if (pos.m_row < BoardPos::NUM_ROWS)
124+
getJewel(BoardPos(pos.m_col, pos.m_row + 1)).getModel() = jo.getModel();
125+
else
126+
{
127+
BoardPos fallPos(pos.m_col, BoardPos::NUM_ROWS);
128+
JewelObject &last = getJewel(fallPos);
129+
while(fallPos.m_row > 0)
130+
{
131+
m_jewels[fallPos.m_row][fallPos.m_col] = m_jewels[fallPos.m_row - 1][fallPos.m_col];
132+
fallPos.m_row--;
133+
}
134+
m_jewels[0][fallPos.m_col] = &last;
135+
m_jewels[0][fallPos.m_col]->getModel().setColor(random() % Jewel::NUM_COLORS);
136+
}
137+
}
114138

115139

116140
void JewelBoard::update()
@@ -125,11 +149,18 @@ void JewelBoard::update()
125149
forAllPos([&](BoardPos pos)
126150
{
127151
JewelObject &jo = getJewel(pos);
128-
if ((jo.isFalling() || jo.isDead()) && pos.m_row < BoardPos::NUM_ROWS)
152+
if(jo.getFallingStep() == 10)
153+
shiftDown(pos);
154+
if (jo.isFalling() || jo.isDead())
129155
{
130-
getJewel(BoardPos(pos.m_col, pos.m_row + 1)).setFalling(true);
156+
getJewel(BoardPos(pos.m_col, pos.m_row - 1)).setFalling(true);
131157
}
132158
jo.update();
159+
if (jo.isFalling())
160+
{
161+
m_pixel.setY(m_pixel.getY() + m_height / 10.0); //jo.getFallingStep() *
162+
}
163+
133164
});
134165
}
135166

JewelBoard.h

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//
2-
// JewelBoard.h
3-
// SDL Game Programming Book
4-
//
5-
// Created by shaun mitchell on 26/03/2013.
6-
// Copyright (c) 2013 shaun mitchell. All rights reserved.
7-
//
1+
/**************************************************************************
2+
** Qt Creator license header template
3+
** Special keywords: dpinol 03/03/2014 2014
4+
** Environment variables:
5+
** To protect a percent sign, use '%'.
6+
**************************************************************************/
87

9-
#ifndef __SDL_Game_Programming_Book__JewelBoard__
10-
#define __SDL_Game_Programming_Book__JewelBoard__
8+
9+
#ifndef JEWEL_BOARD
10+
#define JEWEL_BOARD
1111

1212
#include <iostream>
1313
#include "model/BoardPos.h"
@@ -77,9 +77,11 @@ class JewelBoard : public BoardObject, BoardCallback
7777
* @return not valid board if not withn board
7878
*/
7979
BoardPos getJewelAt(const Vector2D &pixel) const;
80+
Vector2D getJewelPixel(BoardPos pos) const;
8081

8182
private:
82-
83+
/** Move jewel to next row*/
84+
void shiftDown(BoardPos pos);
8385

8486
//extra row is for falling new jewels
8587
JewelObject* m_jewels[BoardPos::NUM_ROWS + 1][ BoardPos::NUM_COLS];
@@ -105,4 +107,4 @@ class JewelBoardCreator : public BaseCreator
105107
}
106108
};
107109

108-
#endif /* defined(__SDL_Game_Programming_Book__JewelBoard__) */
110+
#endif /* JEWEL_BOARD */

JewelObject.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
JewelObject::JewelObject(Jewel &jewel) :
1515
m_model(&jewel),
16-
m_isFalling(false)
16+
m_fallingStep(0)
1717
{
1818
m_pixel = Vector2D(0,0);
1919
m_currentRow = 0;
@@ -89,11 +89,19 @@ void JewelObject::doDyingAnimation()
8989

9090
bool JewelObject::isFalling() const
9191
{
92-
return m_isFalling;
92+
return m_fallingStep != 0;
93+
}
94+
95+
short JewelObject::getFallingStep() const
96+
{
97+
return m_fallingStep;
9398
}
9499

95100
void JewelObject::setFalling(bool falling)
96101
{
97-
m_isFalling = falling;
102+
if (falling)
103+
m_fallingStep = 1;
104+
else
105+
m_fallingStep = 0;
98106
}
99107

JewelObject.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//
2-
// SDLGameObject.h
3-
// SDL Game Programming Book
4-
//
5-
// Created by shaun mitchell on 19/01/2013.
6-
// Copyright (c) 2013 shaun mitchell. All rights reserved.
7-
//
1+
/**************************************************************************
2+
** Qt Creator license header template
3+
** Special keywords: dpinol 03/03/2014 2014
4+
** Environment variables:
5+
** To protect a percent sign, use '%'.
6+
**************************************************************************/
87

9-
#ifndef __SDL_Game_Programming_Book__SDLGameObject__
10-
#define __SDL_Game_Programming_Book__SDLGameObject__
8+
9+
#ifndef JEWEL_OBJECT_H
10+
#define JEWEL_OBJECT_H
1111

1212
#include <SDL.h>
1313
#include "BoardObject.h"
@@ -41,7 +41,7 @@ class JewelObject : public BoardObject
4141
virtual void doDyingAnimation() override;
4242
bool isFalling() const;
4343
void setFalling(bool falling);
44-
44+
short getFallingStep() const;
4545

4646

4747
static constexpr short WIDTH = 35;
@@ -51,7 +51,7 @@ class JewelObject : public BoardObject
5151

5252
//friend class JewelBoard;
5353
Jewel *m_model;
54-
bool m_isFalling;
54+
short m_fallingStep;
5555

5656
};
57-
#endif /* defined(__SDL_Game_Programming_Book__SDLGameObject__) */
57+
#endif /* JEWEL_OBJECT_H */

main.cpp

+36-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Game.h"
22
#include <utils/init.h>
3+
#include <utils/log.h>
34
//#include "windows.h"
45
#include <iostream>
56
#include <stdlib.h>
@@ -10,41 +11,45 @@ const int DELAY_TIME = 1000.0f / FPS;
1011
int main(int argc, char const **argv)
1112
{
1213
dani::init::init(argc, argv);
13-
if (argc > 1 && strcmp(argv[1], "--randomize") == 0)
14-
srandom(time(NULL));
15-
16-
// AllocConsole();
14+
if (argc > 1 && strcmp(argv[1], "--randomize") == 0)
15+
{
16+
time_t t = time(NULL);
17+
//useful in case we want to repeat a given board
18+
LOG_ERROR("Randomizing with value " << t);
19+
srandom(t);
20+
}
21+
// AllocConsole();
1722
//freopen("CON", "w", stdout);
18-
Uint32 frameStart, frameTime;
23+
Uint32 frameStart, frameTime;
1924

20-
std::cout << "game init attempt...\n";
21-
if(TheGame::Instance()->init("SDL_test", 100, 100, 755, 600, false))
22-
{
23-
std::cout << "game init success!\n";
24-
while(TheGame::Instance()->running())
25-
{
26-
frameStart = SDL_GetTicks();
27-
28-
TheGame::Instance()->handleEvents();
29-
TheGame::Instance()->update();
30-
TheGame::Instance()->render();
31-
32-
frameTime = SDL_GetTicks() - frameStart;
33-
34-
if(frameTime < DELAY_TIME)
35-
{
36-
SDL_Delay((int)(DELAY_TIME - frameTime));
37-
}
38-
}
39-
}
40-
else
25+
std::cout << "game init attempt...\n";
26+
if(TheGame::Instance()->init("SDL_test", 100, 100, 755, 600, false))
27+
{
28+
std::cout << "game init success!\n";
29+
while(TheGame::Instance()->running())
4130
{
42-
std::cout << "game init failure - " << SDL_GetError() << "\n";
43-
return -1;
31+
frameStart = SDL_GetTicks();
32+
33+
TheGame::Instance()->handleEvents();
34+
TheGame::Instance()->update();
35+
TheGame::Instance()->render();
36+
37+
frameTime = SDL_GetTicks() - frameStart;
38+
39+
if(frameTime < DELAY_TIME)
40+
{
41+
SDL_Delay((int)(DELAY_TIME - frameTime));
42+
}
4443
}
44+
}
45+
else
46+
{
47+
std::cout << "game init failure - " << SDL_GetError() << "\n";
48+
return -1;
49+
}
4550

46-
std::cout << "game closing...\n";
47-
TheGame::Instance()->clean();
51+
std::cout << "game closing...\n";
52+
TheGame::Instance()->clean();
4853

49-
return 0;
54+
return 0;
5055
}

model/JewelSwap.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ bool JewelSwap::isValid() const
4646
m_valid = true;
4747
}
4848
m_validated = true;
49-
if (!m_valid)
50-
LOG_DEBUG("Swap " << m_positions[0] << "-" << m_positions[1] <<" not valid because it would not form a strike");
49+
//if (!m_valid)
50+
// LOG_DEBUG("Swap " << m_positions[0] << "-" << m_positions[1] <<" not valid because it would not form a strike");
5151
return m_valid;
5252
}
5353

0 commit comments

Comments
 (0)