21 #include <KLocalizedString>
51 m_pauseResumeText (0),
56 m_toolbarTileSize (10),
57 m_themeChanged (true),
60 m_mouse (new QCursor()),
61 m_fadingTimeLine (new QTimeLine (1000, this))
63 setItemIndexMethod(NoIndex);
65 m_tiles.fill (0, m_tilesWide * m_tilesHigh);
66 m_tileTypes.fill (
FREE, m_tilesWide * m_tilesHigh);
70 m_frame = addRect (0, 0, 100, 100);
71 m_frame->setVisible (
false);
73 m_spotlight = addRect (0, 0, 100, 100);
74 m_spotlight->setVisible (
false);
76 m_title =
new QGraphicsSimpleTextItem();
79 m_replayMessage =
new QGraphicsSimpleTextItem();
80 addItem (m_replayMessage);
81 m_replayMessage->setVisible (
false);
83 m_livesText =
new QGraphicsSimpleTextItem();
84 addItem (m_livesText);
86 m_scoreText =
new QGraphicsSimpleTextItem();
87 addItem (m_scoreText);
89 m_hasHintText =
new QGraphicsSimpleTextItem();
90 addItem (m_hasHintText);
92 m_pauseResumeText =
new QGraphicsSimpleTextItem();
93 addItem (m_pauseResumeText);
95 m_fadingTimeLine->setCurveShape (QTimeLine::EaseOutCurve);
96 m_fadingTimeLine->setUpdateInterval (50);
97 connect (m_fadingTimeLine, SIGNAL (valueChanged(qreal)),
98 this, SLOT (drawSpotlight(qreal)));
99 connect (m_fadingTimeLine, SIGNAL (finished()),
106 delete m_fadingTimeLine;
109 void KGrScene::redrawScene ()
111 qDebug() <<
"REDRAW: m_sizeChanged" << m_sizeChanged <<
"m_themeChanged" << m_themeChanged;
112 bool redrawToolbar =
false;
115 QSize size = m_view->size();
116 int tileSize = qMin (size.width() / m_tilesWide,
117 size.height() / m_tilesHigh);
118 m_topLeftX = (size.width() - m_tilesWide *
tileSize)/2.0;
119 m_topLeftY = (size.height() - m_tilesHigh *
tileSize)/2.0;
120 setSceneRect (0, 0, size.width(), size.height());
121 qDebug() <<
"SIZE" << size <<
"TL" << m_topLeftX << m_topLeftY <<
"TILE" << tileSize <<
"was" << m_tileSize << m_toolbarTileSize;
124 m_spotlight->setRect (m_topLeftX + 2 * tileSize - 1,
125 m_topLeftY + 2 * tileSize - 1,
126 (m_tilesWide - 4) * tileSize + 2,
127 (m_tilesHigh - 4) * tileSize + 2);
128 m_maxRadius = (5 * m_spotlight->rect().width() + 4) / 8;
129 m_spotlight->setPen (Qt::NoPen);
130 m_spotlight->setZValue (10);
131 m_spotlight->setVisible (
false);
134 QPointF center (width() * 0.5, height() * 0.5);
135 m_gradient.setCenter (center);
136 m_gradient.setFocalPoint (center);
137 m_gradient.setRadius (m_maxRadius);
138 m_gradient.setColorAt (1.00, QColor (0, 0, 0, 255));
139 m_gradient.setColorAt (0.85, QColor (0, 0, 0, 0));
144 setTile (tile, tileSize, index/m_tilesHigh, index%m_tilesHigh);
148 foreach (
KGrSprite * sprite, m_sprites) {
151 (m_topLeftX, m_topLeftY, tileSize);
155 if (m_tileSize != tileSize) {
158 m_toolbarTileSize = ((tileSize > m_tileSize) && (m_topLeftY == 0)) ?
159 m_tileSize : tileSize;
162 redrawToolbar = ((m_toolbarTileSize != m_tileSize) &&
163 (m_topLeftY > 0)) ?
true :
false;
165 m_sizeChanged =
false;
171 setTextFont (m_title, 0.6);
176 loadBackground (m_level);
180 m_frame->setVisible (
false);
187 if (m_themeChanged) {
199 int index = i * m_tilesHigh + j;
205 redrawToolbar =
true;
206 m_themeChanged =
false;
210 m_toolbarTileSize = m_tileSize;
217 m_themeChanged =
true;
223 m_sizeChanged =
true;
229 if (! m_title)
return;
231 m_title->setText (newTitle);
232 QRectF r = m_title->boundingRect();
233 m_title->setPos ((sceneRect().width() - r.width())/2,
234 m_topLeftY + (m_tileSize - r.height())/2);
239 m_replayMessage->setText (msg);
244 m_replayMessage->setVisible (onOff);
247 void KGrScene::placeTextItems()
249 setTextFont (m_replayMessage, 0.5);
250 setTextFont (m_livesText, 0.5);
251 setTextFont (m_scoreText, 0.5);
252 setTextFont (m_hasHintText, 0.5);
253 setTextFont (m_pauseResumeText, 0.5);
255 QRectF r = m_replayMessage->boundingRect();
256 m_replayMessage->setPos ((sceneRect().width() - r.width())/2,
257 m_topLeftY + 1.4 * m_tileSize - 0.5 * r.height());
258 m_replayMessage->setZValue (10);
260 qreal totalWidth = 0.0;
261 r = m_livesText->boundingRect();
262 qreal x = m_topLeftX + 2 * m_tileSize;
263 qreal y = sceneRect().height() - m_topLeftY - (m_tileSize + r.height())/2;
265 m_livesText->setPos (x, y);
266 totalWidth += r.width();
268 r = m_scoreText->boundingRect();
269 m_scoreText->setPos (x + totalWidth, y);
270 totalWidth += r.width();
272 r = m_hasHintText->boundingRect();
273 m_hasHintText->setPos (x + totalWidth, y);
274 totalWidth += r.width();
276 r = m_pauseResumeText->boundingRect();
277 m_pauseResumeText->setPos (x + totalWidth, y);
278 totalWidth += r.width();
280 qreal spacing = ((m_tilesWide - 4) * m_tileSize - totalWidth) / 3.0;
284 m_scoreText->moveBy (spacing, 0.0);
285 m_hasHintText->moveBy (2.0 * spacing, 0.0);
286 m_pauseResumeText->moveBy (3.0 * spacing, 0.0);
292 m_livesText->setText (i18n(
"Lives: %1", QString::number(lives)
293 .rightJustified(3,
'0')));
299 m_scoreText->setText (i18n(
"Score: %1", QString::number(score)
300 .rightJustified(7,
'0')));
306 m_hasHintText->setText (msg);
311 if (m_pauseResumeText)
312 m_pauseResumeText->setText (msg);
324 m_fadingTimeLine->setDirection (inOut ? QTimeLine::Forward
325 : QTimeLine::Backward);
326 m_fadingTimeLine->start();
329 void KGrScene::drawSpotlight (qreal ratio)
332 m_spotlight->setVisible (
false);
335 else if (ratio <= 0.01) {
336 m_spotlight->setBrush (Qt::black);
339 m_gradient.setRadius (ratio * m_maxRadius);
340 m_spotlight->setBrush (QBrush (m_gradient));
343 m_spotlight->setVisible (
true);
348 if (level == m_level) {
352 loadBackground (level);
355 void KGrScene::loadBackground (
const int level)
360 m_background = m_renderer->
getBackground (level, m_background);
362 m_background->setRenderSize (QSize ((m_tilesWide - 4) * m_tileSize,
363 (m_tilesHigh - 4) * m_tileSize));
364 m_background->setPos (m_topLeftX + 2 * m_tileSize,
365 m_topLeftY + 2 * m_tileSize);
367 m_background->setZValue (-1);
371 const int i,
const int j)
373 tile->setRenderSize (QSize (tileSize, tileSize));
374 tile->setPos (m_topLeftX + (i+1) * tileSize, m_topLeftY + (j+1) * tileSize);
377 void KGrScene::setBorderTile (
const QString spriteKey,
const int x,
const int y)
379 int index = x * m_tilesHigh + y;
385 setTile (t, m_tileSize, x, y);
389 void KGrScene::drawBorder()
392 setBorderTile (
"frame-topleft", 0, 0);
393 setBorderTile (
"frame-topright",
FIELDWIDTH + 1, 0);
394 setBorderTile (
"frame-bottomleft", 0,
FIELDHEIGHT + 1);
399 setBorderTile (
"frame-top", i, 0);
403 setBorderTile (
"frame-bottom", i,
FIELDHEIGHT + 1);
407 setBorderTile (
"frame-left", 0, i);
411 setBorderTile (
"frame-right",
FIELDWIDTH + 1, i);
414 void KGrScene::drawFrame()
416 int w = 0.05 * m_tileSize + 0.5;
419 m_topLeftX + (2 * m_tileSize) - (3 * w),
420 m_topLeftY + (2 * m_tileSize) - (3 * w),
423 kDebug() <<
"FRAME WIDTH" << w <<
"tile size" << m_tileSize <<
"rectangle" << m_frame->rect();
424 QPen pen = QPen (m_renderer->
textColor());
426 m_frame->setPen (pen);
427 m_frame->setVisible (
true);
432 int index = i * m_tilesHigh + j;
435 m_tileTypes[index] = type;
438 setTile (t, m_tileSize, i, j);
447 if (m_sprites.count(0) > 0 &&
448 ((spriteId = m_sprites.lastIndexOf (0)) >= 0)) {
450 m_sprites[spriteId] = sprite;
454 spriteId = m_sprites.count();
455 m_sprites.append (sprite);
481 sprite->setFrame (frame1);
484 sprite->
move (i, j, frame1);
490 foreach (
KGrSprite * sprite, m_sprites) {
498 const int i,
const int j,
const int time,
506 int nFrameChanges = 4;
547 m_sprites.at(
id)->setAnimation (repeating, i, j, frame, nFrames, dx, dy,
548 time, nFrameChanges);
552 const bool spriteHasGold,
const bool lost)
560 if (enemiesShowGold && (m_sprites.at(spriteId)->spriteType() ==
ENEMY)) {
561 m_sprites.at(spriteId)->setSpriteKey (spriteHasGold ?
"gold_enemy"
568 foreach (
int offset, ladders) {
569 int i = offset % width;
570 int j = offset / width;
577 QPointF loc = m_sprites.at(spriteId)->currentLoc();
578 bool brick = (m_sprites.at(spriteId)->spriteType() ==
BRICK);
580 delete m_sprites.at(spriteId);
581 m_sprites [spriteId] = 0;
591 qDeleteAll(m_sprites);
598 for (
int t = 0; t < 2; t++) {
600 sprite->setFrame (1);
601 sprite->setRenderSize (QSize (m_tileSize, m_tileSize));
602 int count = sprite->frameCount();
606 for (
int n = 1; n <= count; n++) {
607 sprite->setFrame (n);
615 m_mouse->setPos (m_view->mapToGlobal (QPoint (
616 m_topLeftX + (i + 1) * m_tileSize + m_tileSize/2,
617 m_topLeftY + (j + 1) * m_tileSize + m_tileSize/2)));
622 QPoint pos = m_view->mapFromGlobal (m_mouse->pos());
625 if (! m_view->isActiveWindow()) {
632 i = (i - m_topLeftX)/m_tileSize - 1;
633 j = (j - m_topLeftY)/m_tileSize - 1;
640 void KGrScene::setTextFont (QGraphicsSimpleTextItem * t,
double fontFraction)
643 f.setPixelSize ((
int) (m_tileSize * fontFraction + 0.5));
644 f.setWeight (QFont::Bold);
645 f.setStretch (QFont::Expanded);
650 #include "kgrscene.moc"
void getMousePos(int &i, int &j)
void showReplayMessage(bool onOff)
void setReplayMessage(const QString &msg)
QSize tileSize() const
Get the current size of the squared region occupied by a single visual element (characters, ladders, bricks etc.).
KGameRenderedItem * getTileItem(const char picType, KGameRenderedItem *currentTile)
void animate(bool missed)
QColor borderColor() const
void setTitle(const QString &newTitle)
Set the text for the title of the current level.
void gotGold(const int spriteId, const int i, const int j, const bool spriteHasGold, const bool lost=false)
void setPauseResumeText(const QString &msg)
const StartFrame animationStartFrames[nAnimationTypes]
void changeTheme()
Redraw the scene whenever the current theme has changed.
void setHasHintText(const QString &msg)
void changeSize()
Redraw the scene whenever the view widget is resized.
void setCoordinateSystem(int topLeftX, int topLeftY, int tileSize)
void showScore(long score)
void move(double x, double y, int frame)
void deleteSprite(const int id)
void setMousePos(const int i, const int j)
void showLives(long lives)
void setLevel(unsigned int level)
Set the current level number.
void preRenderSprites()
Just as the game starts, ensure that all frames of the "hero" and "enemy" sprites have been rendered...
void animate(bool missed)
KGameRenderedItem * getBorderItem(QString spriteKey, KGameRenderedItem *currentItem)
void startAnimation(const int id, const bool repeating, const int i, const int j, const int time, const Direction dirn, const AnimationType type)
Requests the view to display an animation of a runner or dug brick at a particular cell...
int makeSprite(const char type, int i, int j)
void showHiddenLadders(const QList< int > &ladders, const int width)
KGrSprite * getSpriteItem(const char picType, const int tickTime)
KGameRenderedItem * getBackground(const int level, KGameRenderedItem *currentBackground)
void changeCoordinateSystem(int topLeftX, int topLeftY, int tileSize)
void paintCell(const int i, const int j, const char type)
Requests the view to display a particular type of tile at a particular cell, or make it empty and sho...