-
Notifications
You must be signed in to change notification settings - Fork 0
/
JewelObject.cpp
175 lines (147 loc) · 3.66 KB
/
JewelObject.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
//
// SDLGameObject.cpp
// SDL Game Programming Book
//
// Created by shaun mitchell on 19/01/2013.
// Copyright (c) 2013 shaun mitchell. All rights reserved.
//
#include "JewelObject.h"
#include "TextureManager.h"
#include "Game.h"
#include <utils/ValueEffect.h>
#include <utils/log.h>
const short JewelObject::FALLING_STEPS = 20;
JewelObject::JewelObject(Jewel &jewel, bool firstRow) :
m_model(&jewel),
m_fallingStep(0)
{
m_effects->addChild(m_swapper);
m_effects->addChild(m_dier);
m_swapper.setPeriod(3000);
m_swapper.setPaused(true);
m_dier.setPaused(true);
m_bfalling = false;
if (firstRow)
m_bDead = true;
m_pixel = Vector2D(0,0);
m_currentRow = 0;
//we don't use it so far
m_currentFrame = -1; //jewel.getColor();
// get drawing variables
m_width = SPRITE_SIZE;
m_height = SPRITE_SIZE;
m_textureID = "jewels";
}
Jewel& JewelObject::getModel()
{
return *m_model;
}
dani::Effect &JewelObject::swapWith(BoardPos relativeShift, bool andReturn)
{
m_swapper.setPaused(false);
Vector2D vShift(relativeShift.m_col * WIDTH, relativeShift.m_row * HEIGHT);
m_swapper.setMean({0,0}, vShift);
m_swapper.setStartPhase(0);
if (andReturn)
{
m_swapper.setStopPhase(M_PI);
}
else
{
m_swapper.setStopPhase(M_PI / 2);
}
return m_swapper;
}
void JewelObject::load(std::unique_ptr<LoaderParams> const &pParams)
{
// get position
m_pixel = Vector2D(pParams->getX(),pParams->getY());
// get drawing variables
m_width = pParams->getWidth();
m_height = pParams->getHeight();
m_textureID = pParams->getTextureID();
m_numFrames = pParams->getNumFrames();
}
// draw the object to the screen
void JewelObject::draw()
{
//pending dying animation
int alpha = 255;
//@todo migrate to m_dier
if (isDying())
alpha = 255.0 * (m_dyingTime - m_dyingCounter) / m_dyingTime;
if(getModel().getColor() != Jewel::NO_COLOR) // && !isDead())
{
Uint32 x = (Uint32)m_pixel.getX() + MARGIN;
Uint32 y = (Uint32)m_pixel.getY() + MARGIN;
if (!m_swapper.isDone() && !m_swapper.isPaused())
{
Vector2D swapper = m_swapper.get();
x += swapper.getX();
y += swapper.getY();
LOG_DEBUG("swapper in action" << getPixel() << " " << swapper << "col " <<getModel().getColor());
}
TextureManager::Instance()->drawFrame(m_textureID, x, y,
m_width, m_height, m_currentRow, getModel().getColor(),
NULL, 0, alpha);
}
//TheGame::Instance()->getRenderer(), m_angle, m_alpha);
}
// apply velocity to current position
void JewelObject::update()
{
m_effects->update();
if (isDying())
{
m_dyingCounter++;
if (m_dyingCounter == m_dyingTime)
{
m_bDead = true;
m_bDying = false;
}
} else if (isFalling())
{
fallStep();
}
}
void JewelObject::setMovement(JewelMove const &m)
{
}
void JewelObject::resurrect()
{
m_bDying = false;
m_bDead = false;
//@todo we should allow direct strike. fo
getModel().setColor(random() % Jewel::NUM_COLORS);
}
void JewelObject::kill()
{
if (!m_bDying)
{
// m_model->setColor(Jewel::NO_COLOR);
m_bDying = true;
m_dyingTime = 60;
m_dyingCounter = 0;
m_fallingStep = 0;
}
}
void JewelObject::setFalling(bool falling)
{
m_bfalling = falling;
m_fallingStep = 0;
}
bool JewelObject::isFalling() const
{
return m_bfalling; //m_fallingStep != 0;
}
bool JewelObject::isFallDone() const
{
return m_fallingStep >= FALLING_STEPS;
}
void JewelObject::fallStep()
{
m_fallingStep++;
getPixel().setY(getPixel().getY() + float(HEIGHT) / FALLING_STEPS); //jo.getFallingStep() *
//if (m_fallingStep == FALLING_STEPS)
// m_fallingStep = 0;
}