Skip to content

Commit 4d34fef

Browse files
committed
effects chained. does not display effects
1 parent 3afd75f commit 4d34fef

10 files changed

+137
-42
lines changed

JewelBoard.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ inline static void assertBoardPos(BoardPos const pos)
7676
}
7777
#endif
7878

79+
Board const &JewelBoard::getModel() const
80+
{
81+
return m_model;
82+
}
83+
84+
Board &JewelBoard::getModel()
85+
{
86+
return m_model;
87+
}
88+
7989
JewelObject& JewelBoard::getJewel(BoardPos const pos)
8090
{
8191
assertBoardPos(pos);
@@ -116,13 +126,22 @@ BoardPos JewelBoard::getJewelAt(Vector2D const &v) const
116126
(v.getY() - m_offset.getY()) / JewelObject::HEIGHT);
117127
}
118128

129+
/**
130+
* @brief wa swaps jewels at specified positions
131+
* but only if final position achieves a strike
132+
* @param pos1
133+
* @param pos2
134+
* @return whether they could be swap
135+
136+
bool swap(BoardPos const pos1, BoardPos const pos2);
137+
119138
bool JewelBoard::swap(BoardPos const pos1, BoardPos const pos2)
120139
{
121140
JewelSwap sw(m_model);
122141
sw.setPositions(pos1, pos2);
123142
return sw.run();
124143
}
125-
144+
*/
126145
void JewelBoard::pureSwap(BoardPos pos, BoardPos pos2)
127146
{
128147
/*getJewel(BoardPos(pos.m_col, pos.m_row + 1)) = jo;

JewelBoard.h

+4-9
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,8 @@ class JewelBoard : public BoardObject, BoardCallback
3737
//BoardCallback
3838
void kill(BoardPos pos) override;
3939
bool isAlive(BoardPos pos) const override;
40+
//
4041

41-
42-
/**
43-
* @brief swap swaps jewels at specified positions
44-
* but only if final position achieves a strike
45-
* @param pos1
46-
* @param pos2
47-
* @return whether they could be swap
48-
*/
49-
bool swap(BoardPos const pos1, BoardPos const pos2);
5042
/**
5143
* @brief getJewel
5244
* @param row 0 to SIZE
@@ -86,6 +78,9 @@ class JewelBoard : public BoardObject, BoardCallback
8678
BoardPos getJewelAt(const Vector2D &pixel) const;
8779
Vector2D getJewelPixel(BoardPos pos) const;
8880

81+
Board &getModel();
82+
Board const &getModel() const;
83+
8984
private:
9085
/**
9186
* Just swap the 2 pointers to the Jewels

JewelDrag.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
#include "JewelObject.h"
1010
#include "JewelBoard.h"
1111
#include <utils/log.h>
12+
#include <utils/Effect.h>
1213
#include "InputHandler.h"
1314

1415
JewelDrag::JewelDrag(JewelBoard &board)
15-
:m_board(board)
16+
:m_board(board),
17+
m_modelSwap(board.getModel()),
18+
m_swapEffect(NULL)
1619
{
1720
}
1821

@@ -42,13 +45,13 @@ void JewelDrag::drag()
4245
BoardPos shiftPos;
4346
int RATIO = 3;
4447
if (shift.getX() > JewelObject::WIDTH / RATIO)
45-
shiftPos = BoardPos(1,0);
48+
shiftPos = BoardPos(1,0);
4649
else if (shift.getX() < -JewelObject::WIDTH / RATIO)
47-
shiftPos = BoardPos(-1,0);
50+
shiftPos = BoardPos(-1,0);
4851
else if (shift.getY() > JewelObject::HEIGHT / RATIO)
49-
shiftPos = BoardPos(0, 1);
52+
shiftPos = BoardPos(0, 1);
5053
else if (shift.getY() < -JewelObject::HEIGHT / RATIO)
51-
shiftPos = BoardPos(0, -1);
54+
shiftPos = BoardPos(0, -1);
5255
else
5356
{
5457
LOG_DEBUG("shift too small: " << shift);
@@ -59,9 +62,16 @@ void JewelDrag::drag()
5962
bool returnBack;
6063
if (m_toPos.isValid())
6164
{
62-
returnBack = !m_board.swap(m_fromPos, m_toPos);
65+
m_modelSwap.setPositions(m_fromPos, m_toPos);
66+
returnBack = m_modelSwap.isValid();
67+
6368
m_board.getJewel(m_fromPos).swapWith(shiftPos, returnBack);
64-
m_board.getJewel(m_toPos).swapWith(-shiftPos, returnBack);
69+
m_swapEffect = &m_board.getJewel(m_toPos).swapWith(-shiftPos, returnBack);
70+
m_swapEffect->setNext([&]()
71+
{
72+
m_modelSwap.run();
73+
m_swapEffect->setNext(NULL);
74+
});
6575
}
6676
else
6777
LOG_DEBUG("to_pos not valid: " << m_toPos);

JewelDrag.h

+7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010

1111
#include <utils/Vector2D.h>
1212
#include <model/BoardPos.h>
13+
#include <model/JewelSwap.h>
14+
1315

1416
class JewelBoard;
17+
namespace dani {
18+
class Effect;
19+
}
1520

1621
/**
1722
* @brief The JewelDrag class mimicks Candy Crush swipe.
@@ -35,6 +40,8 @@ class JewelDrag
3540
/** redundant wrt m_fromPixel to avoid recalculation*/
3641
BoardPos m_fromPos;
3742
BoardPos m_toPos;
43+
JewelSwap m_modelSwap;
44+
dani::Effect *m_swapEffect;
3845
// BoardPos m_selected;
3946

4047
};

JewelObject.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
//
88

99
#include "JewelObject.h"
10-
1110
#include "TextureManager.h"
1211
#include "Game.h"
12+
#include <utils/ValueEffect.h>
1313

1414
const short JewelObject::FALLING_STEPS = 20;
1515

1616
JewelObject::JewelObject(Jewel &jewel, bool firstRow) :
1717
m_model(&jewel),
1818
m_fallingStep(0)
1919
{
20+
m_effects->addChild(m_swapper);
21+
m_effects->addChild(m_dier);
22+
2023
m_swapper.setPeriod(3000);
2124
m_swapper.setPaused(true);
2225
m_dier.setPaused(true);
@@ -40,7 +43,7 @@ Jewel& JewelObject::getModel()
4043
return *m_model;
4144
}
4245

43-
void JewelObject::swapWith(BoardPos relativeShift, bool andReturn)
46+
dani::Effect &JewelObject::swapWith(BoardPos relativeShift, bool andReturn)
4447
{
4548
m_swapper.setPaused(false);
4649
Vector2D vShift(relativeShift.m_col * WIDTH, relativeShift.m_row * HEIGHT);
@@ -54,6 +57,7 @@ void JewelObject::swapWith(BoardPos relativeShift, bool andReturn)
5457
{
5558
m_swapper.setStopPhase(M_PI / 2);
5659
}
60+
return m_swapper;
5761
}
5862

5963

@@ -96,6 +100,7 @@ void JewelObject::draw()
96100
// apply velocity to current position
97101
void JewelObject::update()
98102
{
103+
m_effects->update();
99104
if (isDying())
100105
{
101106
m_dyingCounter++;

JewelObject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class JewelObject : public BoardObject
5353
void setFalling(bool falling =true);
5454
void fallStep();
5555

56-
void swapWith(BoardPos relativeShift, bool andReturn);
56+
dani::Effect &swapWith(BoardPos relativeShift, bool andReturn);
5757

5858
static constexpr short WIDTH = 35;
5959
static constexpr short HEIGHT = 35;

MovingObject.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ MovingObject::~MovingObject()
5252
void MovingObject::setRandomSize(float amplitudeRatio)
5353
{
5454
m_scale.setMean(1, amplitudeRatio);
55-
m_scale.setPaused();
5655
}
5756

5857
void MovingObject::setAlphaOscillation(float minAlphaRatio)
5958
{
6059
//m_minAlphaPerc = minAlphaRatio;
6160
//m_alphaDegree = 0;
6261
m_alpha.setRange( 255 * minAlphaRatio, 255);
63-
m_alpha.setPaused();
6462
}
6563

6664
void MovingObject::setTrajectory(Trajectory &trajectory)

utils/Effect.cpp

+49-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,46 @@ namespace dani
1515

1616
Effect::Effect(bool paused)
1717
:m_paused(paused),
18+
m_isDone(false),
1819
m_slave(NULL),
19-
m_next(NULL)
20+
m_next(NULL),
21+
m_callback(NULL)
2022
{
2123
}
24+
#if 0
25+
/**
26+
* @brief restart
27+
* resets m_isDone and calls restartImpl
28+
*/
29+
void restart();
30+
31+
void Effect::restart()
32+
{
33+
m_isDone = false;
34+
restartImpl();
35+
}
36+
#endif
2237

38+
void Effect::update()
39+
{
40+
if (isDone())
41+
{
42+
if (!m_isDone)
43+
{
44+
if (m_next)
45+
m_next->setPaused(false);
46+
if (m_callback)
47+
m_callback();
48+
m_isDone = true;
49+
}
50+
}
51+
else
52+
{
53+
m_isDone = false;
54+
if (!isPaused())
55+
updateImpl();
56+
}
57+
}
2358

2459
void Effect::setSlave(Effect *slaveEffect)
2560
{
@@ -31,6 +66,11 @@ namespace dani
3166
m_next = nextEffect;
3267
}
3368

69+
void Effect::setNext(std::function<void (void)> callback)
70+
{
71+
m_callback = callback;
72+
}
73+
3474
void Effect::setFPS(int FPS)
3575
{
3676
m_FPS = FPS;
@@ -69,7 +109,7 @@ namespace dani
69109
}
70110

71111
/******** Composite Effect****/
72-
void CompositeEffect::update()
112+
void CompositeEffect::updateImpl()
73113
{
74114
for(auto &effect: m_children)
75115
{
@@ -85,6 +125,11 @@ namespace dani
85125
}
86126
}
87127

128+
void CompositeEffect::clearChildren()
129+
{
130+
m_children.clear();
131+
}
132+
88133
void CompositeEffect::addChild(Effect &child)
89134
{
90135
m_children.push_back(&child);
@@ -97,12 +142,12 @@ namespace dani
97142
{return e->isDone();});
98143
}
99144

100-
void CompositeEffect::restart()
145+
/* void CompositeEffect::restartImpl()
101146
{
102147
for(auto &effect: m_children)
103148
{
104149
effect->restart();
105150
}
106151
}
107-
152+
*/
108153
}

0 commit comments

Comments
 (0)