1 #include "scene_if.h"
2 #include "baseitem.h"
3 
4 #include "gamestock.h"
5 #include "gamesound.h"
6 #include "gamebonus.h"
7 #include "gametools.h"
8 #include "gameprofile.h"
9 #include "gamewidget.h"
10 
11 #include "particlecontrol.h"
12 
13 extern ParticleControl *partControl;
14 
15 ////////////////////////////////////////////////////////////////////////////////
16 
IScene()17 IScene::IScene() :
18     time(0),
19     bonus(0), bonus_time(0),
20     max_level(0)
21 {
22   scene = this;
23 }
24 
25 IScene * scene = nullptr;
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 
getSchemePath() const29 QString IScene::getSchemePath() const
30 { return GameWidget::getResourcePath() + "schemes/" + gameProfile->currentTheme(); }
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 
loadPixmap(QPixmap & pm,const QString & name,int dx,int dy)34 bool IScene::loadPixmap(QPixmap &pm, const QString &name, int dx, int dy)
35 {
36   pm = QPixmap(getSchemePath() + "/" + name);
37 
38   if (pm.isNull())
39   {
40     pm = QPixmap(":/images/" + name);
41 
42     if (pm.isNull())
43       return false;
44   }
45 
46   pm = pm.scaled(dx,dy, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
47 
48   return true;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 
createScorePopup(int x,int y,int score,bool prepend)53 void IScene::createScorePopup(int x, int y, int score, bool prepend)
54 {
55   createStaticPopup(QRect(x,y,X56,Y56),
56                   QString::number(score),
57                   Qt::AlignCenter,
58                   gameStock->Font12,
59                   Qt::white,
60                   1,
61                   0, 20,
62                   0, -1,
63                   prepend);
64 }
65 
createPixmapPopup(int x,int y,int dx,int dy,const QPixmap & pm,int steps,bool prepend)66 void IScene::createPixmapPopup(int x, int y, int dx, int dy, const QPixmap &pm,
67                                int steps, bool prepend)
68 {
69   PixmapItem *bi = new PixmapItem(x,y,dx,dy,pm,steps);
70 
71   if (prepend)
72     tempItems.prepend(bi);
73   else
74     tempItems.append(bi);
75 }
76 
createStaticPopup(QRect rect,const QString & text,int textFlags,const QFont & font,QColor color,qreal opacity,int staysteps,int steps,int dx,int dy,bool prepend)77 void IScene::createStaticPopup(QRect rect, const QString &text, int textFlags,
78                        const QFont &font, QColor color,
79                        qreal opacity, int staysteps, int steps,
80                        int dx, int dy,
81                        bool prepend)
82 {
83   TextItem *ti = new TextItem(rect,
84                               text,
85                               textFlags,
86                               font,
87                               color,
88                               opacity,
89                               staysteps,
90                               steps,
91                               dx, dy);
92 
93   if (prepend)
94     tempItems.prepend(ti);
95   else
96     tempItems.append(ti);
97 }
98 
createStaticPopup(const QPoint & centerPos,const QString & text,int textFlags,const QFont & font,QColor color,qreal opacity,int staysteps,int steps,int dx,int dy,bool prepend)99 void IScene::createStaticPopup(const QPoint &centerPos, const QString &text, int textFlags,
100                        const QFont &font, QColor color,
101                        qreal opacity, int staysteps, int steps,
102                        int dx, int dy,
103                        bool prepend)
104 {
105   QFontMetrics fm(font);
106   QRect rect = fm.boundingRect(text);
107   rect.setWidth(rect.width() + rect.width()/4);
108   rect.moveCenter(centerPos);
109 
110   TextItem *ti = new TextItem(rect,
111                               text,
112                               textFlags,
113                               font,
114                               color,
115                               opacity,
116                               staysteps,
117                               steps,
118                               dx, dy);
119 
120   if (prepend)
121     tempItems.prepend(ti);
122   else
123     tempItems.append(ti);
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 
removeBlock(PlaceInfo & pi,int row,int col)128 void IScene::removeBlock(PlaceInfo &pi, int row, int col)
129 {
130   bool b1 = true;
131 
132   if (pi.place & Block2)
133   {
134     b1 = false;
135     pi.place = (pi.place & ~Block2) | Block1;
136   }
137   else
138   {
139     pi.place = (pi.place & ~Block1);
140   }
141 
142   sndEngine->playSound(GameSound::sndUnblock);
143 
144   // create ghost
145   createPixmapPopup(col2x(col), row2y(row), (qrand() % 3)-1, 10 + (qrand() % 5)-2,
146                               gameStock->Block1,
147                               50);
148 }
149 
removeTarget(PlaceInfo & pi,int row,int col)150 void IScene::removeTarget(PlaceInfo &pi, int row, int col)
151 {
152   bool t1 = true;
153 
154   if (pi.place & Target2)
155   {
156     t1 = false;
157     pi.place = (pi.place & ~Target2) | Target1;
158   }
159   else
160     pi.place = (pi.place & ~Target1);
161 
162 //  sndEngine->playSound(GameSound::sndTarget);
163 
164   // create ghost
165   createPixmapPopup(col2x(col), row2y(row), (qrand() % 3)-1, 10 + (qrand() % 5)-2,
166                               t1 ? gameStock->Target1: gameStock->Target2,
167                               50);
168 
169 //  // update bonus
170 //  bonus++;
171 //  bonus_time = 10 + gameBonus->bonusSpeed();
172 //  bonusTimer->start();
173 }
174 
removeAndCountItem(int row,int col)175 void IScene::removeAndCountItem(int row, int col)
176 {
177   PlaceInfo &pi = data(row,col);
178 
179   // it was already scheduled and counted
180   if (pi.isProcessed())
181     return;
182 
183   pi.setProcessed();
184 
185   bool remove = true;
186 //  int upscore = 0;
187 
188 //  if (pi.hasBlock())
189 //  {
190 //    remove = false;
191 //
192 //    removeBlock(pi, row, col);
193 //  }
194 //  else
195 //  if (pi.hasTarget())
196 //  {
197 //    targets--;
198 //
199 //    // score for target
200 //    upscore = TARGET_SCORE + toolset->bonusScore();
201 //
202 //    removeTarget(pi, row, col);
203 //  }
204 //  else
205 //  if (!pi.item->isNotAlive())   // do not count again
206 //  {
207 //    upscore = ITEM_SCORE;  // + toolset->bonusScore();
208 //  }
209 //
210 //  if (upscore)
211 //  {
212 //    // increase score
213 //    upscore *= bonus;
214 //
215 //    score += upscore;
216 ////    toolset->addScore(upscore);
217 //
218 //    // add score popup
219 //    createScorePopup(col2x(col), row2y(row), upscore);
220 //  }
221 
222   if (remove && !pi.item->isNotAlive()) // do not remove again
223   {
224     //gameBonus->addItemScore(pi.item->id(), ITEM_SCORE);
225 
226     pi.item->scheduleDeath();
227 
228     // emit particles
229     ParticleEmitter *pe = new ParticleEmitter(
230         col2x(col)/*+X56/2*/,
231         row2y(row)/*+X56/2*/);
232     pe->setDuration(80);
233     pe->setCount(10);
234     pe->setEmitRadius(20,20);
235     partControl->addEmitter(pe);
236   }
237 }
238 
removeAndCountItemOnly(int row,int col)239 void IScene::removeAndCountItemOnly(int row, int col)
240 {
241   PlaceInfo &pi = data(row,col);
242 
243   // it was already scheduled and counted
244   if (pi.isProcessed())
245     return;
246 
247   pi.setProcessed();
248 
249   if (!pi.item->isNotAlive())   // do not count again
250   {
251     // increase score
252     int upscore = ITEM_SCORE; // + toolset->bonusScore();
253 
254     score += upscore;
255 //    toolset->addScore(upscore);
256 
257     // add score popup
258     createScorePopup(col2x(col), row2y(row), upscore);
259 
260     //gameBonus->addItemScore(pi.item->id(), ITEM_SCORE);
261 
262     pi.item->scheduleDeath();
263   }
264 }
265 
266 ////////////////////////////////////////////////////////////////////////////////
267 
addTime(int timeAdd)268 void IScene::addTime(int timeAdd)
269 {
270   time += timeAdd;
271 
272   createStaticPopup(QRect(DX(975), DY(75), DX(100), DY(30)),
273                   QString("+%1").arg(timeAdd),
274                   Qt::AlignLeft | Qt::AlignTop,
275                   gameStock->Font20,
276                   Qt::yellow,
277                   1,
278                   0, 20,
279                   0, -1
280                   );
281 }
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 
setDefaultGameCursor()285 void IScene::setDefaultGameCursor()
286 {
287   QApplication::setOverrideCursor(gameStock->GameCursor);
288 }
289 
restoreCursor()290 void IScene::restoreCursor()
291 {
292     lastCursor = QApplication::overrideCursor() ? (* QApplication::overrideCursor()) : QCursor();
293     while (QApplication::overrideCursor())
294         QApplication::restoreOverrideCursor();
295 
296     setDefaultGameCursor();
297 }
298 
299 ////////////////////////////////////////////////////////////////////////////////
300 
drawTransRect(QPainter & p,const QRect & r,QColor borderColor,QColor bgColor,qreal op)301 void IScene::drawTransRect(QPainter &p, const QRect &r, QColor borderColor, QColor bgColor, qreal op)
302 {
303     p.setOpacity(op);
304     p.setPen(QPen(borderColor, 2));
305     p.setBrush(bgColor);
306 #if QT_VERSION >= 0x040400
307     p.drawRoundedRect(r, 5,5);
308 #else
309     p.drawRect(r);
310 #endif
311 }
312 
drawTextHint(QPainter & p)313 void IScene::drawTextHint(QPainter &p)
314 {
315   p.setOpacity(1);
316   p.setPen(QPen(Qt::yellow));
317   p.setFont(gameStock->Font12);
318   //p.drawText(0,DY(690),DX(860),DY(30), Qt::AlignRight, hintText);
319   p.drawText(0,DY(675),WIDTH,DY(30), Qt::AlignCenter, hintText);
320 }
321