~ubuntu-branches/ubuntu/natty/kdegames/natty-proposed

« back to all changes in this revision

Viewing changes to kblocks/KBlocksScene.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2010-05-28 20:16:42 UTC
  • mfrom: (1.2.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20100528201642-vh75rp06hh5hjt6l
Tags: 4:4.4.80-0ubuntu1
* New upstream beta release
* New package for kajongg in debian/control
* Custom deb flag for kajongg in debian/rules
* New install file for kajongg
* Remove README.source from debian/
* Bump dep on kde-sc-dev-latest to 4.4.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
*   KBlocks, a falling blocks game for KDE                                *
 
3
*   Copyright (C) 2010 Mauricio Piacentini <mauricio@tabuleiro.com>       *
 
4
*                      Zhongjie Cai <squall.leonhart.cai@gmail.com>       *
 
5
*                                                                         *
 
6
*   This program is free software; you can redistribute it and/or modify  *
 
7
*   it under the terms of the GNU General Public License as published by  *
 
8
*   the Free Software Foundation; either version 2 of the License, or     *
 
9
*   (at your option) any later version.                                   *
 
10
***************************************************************************/
 
11
#include "KBlocksScene.h"
 
12
 
 
13
#include "settings.h"
 
14
 
 
15
#include <QVarLengthArray>
 
16
 
 
17
KBlocksScene::KBlocksScene(GameLogicInterface * p, int capacity)
 
18
{
 
19
    mpGameLogic = p;
 
20
    
 
21
    mSnapshotMode = false;
 
22
    mTopGameLevel = 0;
 
23
    mGroupCount = 0;
 
24
    mMaxCapacity = capacity;
 
25
    mSceneGamesPerLine = 4;
 
26
    mGameAnimEnabled = true;
 
27
    mWaitForAllUpdate = false;
 
28
    
 
29
    maGroupList = new KBlocksItemGroup*[capacity]();
 
30
    maGameScoreList = new KBlocksScore*[capacity]();
 
31
    maGameReadySignal = new bool[capacity]();
 
32
    
 
33
    QString themeFile(Settings::theme());
 
34
    mpGrafx = new KBlocksGraphics(themeFile);
 
35
    mpSnd = new KBlocksSound(themeFile);
 
36
    
 
37
    int width = (capacity >= mSceneGamesPerLine) ? mSceneGamesPerLine : (capacity % mSceneGamesPerLine);
 
38
    int height = (int)(capacity / (mSceneGamesPerLine + 1)) + 1;
 
39
    setSceneRect(0, 0, mpGrafx->m_View_Size_Width * width,
 
40
                       mpGrafx->m_View_Size_Height * height);
 
41
    
 
42
    setItemIndexMethod(NoIndex);
 
43
    
 
44
    mUpdateInterval = 50;
 
45
    mUpdateTimer.setInterval(mUpdateInterval);
 
46
    connect(&mUpdateTimer, SIGNAL(timeout()), SLOT(updateGame()));
 
47
    mUpdateTimer.stop();
 
48
}
 
49
 
 
50
KBlocksScene::~KBlocksScene()
 
51
{
 
52
    delete [] maGameReadySignal;
 
53
    delete [] maGameScoreList;
 
54
    delete [] maGroupList;
 
55
    
 
56
    delete mpGrafx;
 
57
    delete mpSnd;
 
58
}
 
59
 
 
60
KBlocksItemGroup* KBlocksScene::getItemGroup(int index)
 
61
{
 
62
    return maGroupList[index];
 
63
}
 
64
 
 
65
KBlocksScore* KBlocksScene::getScoreHandler(int index)
 
66
{
 
67
    return maGameScoreList[index];
 
68
}
 
69
 
 
70
void KBlocksScene::createGameItemGroups(int groupCount, bool snapshotMode)
 
71
{
 
72
    if (groupCount > mMaxCapacity)
 
73
    {
 
74
        mGroupCount = mMaxCapacity;
 
75
    }
 
76
    mGroupCount = groupCount;
 
77
    mSnapshotMode = snapshotMode;
 
78
    mTopGameLevel = 0;
 
79
    
 
80
    for(int i = 0; i < mGroupCount; i++)
 
81
    {
 
82
        maGroupList[i] = new KBlocksItemGroup(i, mpGameLogic->getSingleGame(i), mpGrafx, mpSnd, snapshotMode);
 
83
        maGroupList[i]->setUpdateInterval(mUpdateInterval);
 
84
        maGroupList[i]->setGameAnimEnabled(mGameAnimEnabled);
 
85
        maGroupList[i]->setWaitForAllUpdate(mWaitForAllUpdate);
 
86
        addItem(maGroupList[i]);
 
87
        
 
88
        maGameScoreList[i] = new KBlocksScore();
 
89
        maGameScoreList[i]->setLevelUpFactor(KBlocksScore_Level_x_Level_x_Factor, 1000);
 
90
        maGameScoreList[i]->setScoreUpFactor(10);
 
91
        
 
92
        maGameReadySignal[i] = false;
 
93
        connect(maGroupList[i], SIGNAL(readyForAction(int)), this, SLOT(readyForAction(int)));
 
94
    }
 
95
    
 
96
    updateDimensions();
 
97
        
 
98
    //Our Message Item, hidden by default
 
99
    mMessageBox = new KGamePopupItem();
 
100
    mMessageBox->setMessageOpacity(0.9);
 
101
    addItem(mMessageBox);
 
102
}
 
103
 
 
104
void KBlocksScene::deleteGameItemGroups()
 
105
{
 
106
    removeItem(mMessageBox);
 
107
    delete mMessageBox;
 
108
 
 
109
    for(int i = 0; i < mGroupCount; i++)
 
110
    {
 
111
        disconnect(maGroupList[i], SIGNAL(readyForAction(int)), this, SLOT(readyForAction(int)));
 
112
        
 
113
        delete maGameScoreList[i];
 
114
        maGameScoreList[i] = 0;
 
115
        
 
116
        removeItem(maGroupList[i]);
 
117
        delete maGroupList[i];
 
118
        maGroupList[i] = 0;
 
119
    }
 
120
    mGroupCount = 0;
 
121
}
 
122
 
 
123
void KBlocksScene::setGamesPerLine(int count)
 
124
{
 
125
    mSceneGamesPerLine = count;
 
126
}
 
127
 
 
128
void KBlocksScene::setGameAnimEnabled(bool flag)
 
129
{
 
130
    mGameAnimEnabled = flag;
 
131
    for(int i = 0; i < mGroupCount; i++)
 
132
    {
 
133
        maGroupList[i]->setGameAnimEnabled(flag);
 
134
    }
 
135
}
 
136
 
 
137
void KBlocksScene::setWaitForAllUpdate(bool flag)
 
138
{
 
139
    mWaitForAllUpdate = flag;
 
140
    for(int i = 0; i < mGroupCount; i++)
 
141
    {
 
142
        maGroupList[i]->setWaitForAllUpdate(flag);
 
143
    }
 
144
}
 
145
 
 
146
void KBlocksScene::setUpdateInterval(int interval)
 
147
{
 
148
    mUpdateInterval = interval;
 
149
    mUpdateTimer.setInterval(mUpdateInterval);
 
150
    for(int i = 0; i < mGroupCount; i++)
 
151
    {
 
152
        maGroupList[i]->setUpdateInterval(mUpdateInterval);
 
153
    }
 
154
}
 
155
 
 
156
void KBlocksScene::setSoundsEnabled(bool enabled)
 
157
{
 
158
    mpSnd->setSoundsEnabled(enabled);
 
159
}
 
160
 
 
161
void KBlocksScene::readSettings(const QSize & viewSize)
 
162
{
 
163
    if (mpGrafx->theme()->fileName()!=Settings::theme())
 
164
    {
 
165
        mpGrafx->loadTheme(Settings::theme());
 
166
        mpSnd->loadTheme(Settings::theme());
 
167
        mpGrafx->adjustForSize(viewSize);
 
168
        updateDimensions();
 
169
    }
 
170
}
 
171
 
 
172
void KBlocksScene::viewScaled(const QSize& /*newsize*/)
 
173
{
 
174
    /*
 
175
    //Temporarily halt game timer while resizing elements
 
176
    if (gameState==Game_Active) stepTimer.stop();
 
177
    grafx->adjustForSize(newsize);
 
178
    updateDimensions();
 
179
    //Do not restart if game was paused
 
180
    if (gameState==Game_Active) stepTimer.start();
 
181
    */
 
182
}
 
183
 
 
184
void KBlocksScene::startGame()
 
185
{
 
186
    mTopGameLevel = 0;
 
187
    for(int i = 0; i < mGroupCount; i++)
 
188
    {
 
189
        maGroupList[i]->startGame();
 
190
    }
 
191
    
 
192
    if (!mSnapshotMode)
 
193
    {
 
194
        mUpdateTimer.start();
 
195
        QTimer::singleShot(500, this, SLOT(greetPlayer()));
 
196
    }
 
197
}
 
198
 
 
199
void KBlocksScene::stopGame()
 
200
{
 
201
    for(int i = 0; i < mGroupCount; i++)
 
202
    {
 
203
        maGroupList[i]->stopGame();
 
204
    }
 
205
    
 
206
    mUpdateTimer.stop();
 
207
}
 
208
 
 
209
void KBlocksScene::pauseGame(bool flag, bool fromUI)
 
210
{
 
211
    QString resuming(i18n("Game Resumed!"));
 
212
    QString pausing(i18n("Game Paused!"));
 
213
    
 
214
    for(int i = 0; i < mGroupCount; i++)
 
215
    {
 
216
        maGroupList[i]->pauseGame(flag);
 
217
    }
 
218
    
 
219
    if (!mSnapshotMode)
 
220
    {
 
221
        if (flag)
 
222
        {
 
223
            mUpdateTimer.stop();
 
224
            if (!fromUI)
 
225
            {
 
226
                showMessage(pausing, 2000);
 
227
            }
 
228
        }
 
229
        else
 
230
        {
 
231
            mUpdateTimer.start();
 
232
            if (!fromUI)
 
233
            {
 
234
                showMessage(resuming, 2000);
 
235
            }
 
236
        }
 
237
    }
 
238
}
 
239
 
 
240
void KBlocksScene::addScore(int gameIndex, int lineCount)
 
241
{
 
242
    if (!mSnapshotMode)
 
243
    {
 
244
        return;
 
245
    }
 
246
    maGameScoreList[gameIndex]->addScore(lineCount);
 
247
    emit scoreChanged(gameIndex, maGameScoreList[gameIndex]->getScorePoint(),
 
248
                                 maGameScoreList[gameIndex]->getLineCount(), 
 
249
                                 maGameScoreList[gameIndex]->getGameLevel());
 
250
}
 
251
 
 
252
void KBlocksScene::updateDimensions()
 
253
{
 
254
    // TODO : Reset item position and scale
 
255
    int width = (mGroupCount >= mSceneGamesPerLine) ? mSceneGamesPerLine : (mGroupCount % mSceneGamesPerLine);
 
256
    int height = (int)(mGroupCount / (mSceneGamesPerLine + 1)) + 1;
 
257
    
 
258
    setSceneRect(0, 0, mpGrafx->m_View_Size_Width * width,
 
259
                       mpGrafx->m_View_Size_Height * height);
 
260
    
 
261
    for(int i = 0; i < mGroupCount; i++)
 
262
    {
 
263
        int left = mpGrafx->m_View_Size_Width * (i % mSceneGamesPerLine);
 
264
        int top = mpGrafx->m_View_Size_Height * ((int)(i / mSceneGamesPerLine));
 
265
        
 
266
        maGroupList[i]->setPos(left, top);
 
267
        maGroupList[i]->refreshPosition();
 
268
    }
 
269
}
 
270
 
 
271
void KBlocksScene::greetPlayer()
 
272
{
 
273
    QString greets(i18n("Game Start!")); 
 
274
    showMessage( greets, 2000 );
 
275
}
 
276
 
 
277
void KBlocksScene::gameOverPlayer()
 
278
{
 
279
    QString greets(i18n("Game Over!")); 
 
280
    showMessage( greets, 2000 );
 
281
}
 
282
 
 
283
void KBlocksScene::gameOverMultiWin()
 
284
{
 
285
    QString gameOver(i18n("You Win!")); 
 
286
    showMessage( gameOver, 2000 );
 
287
}
 
288
 
 
289
void KBlocksScene::gameOverMultiLose()
 
290
{
 
291
    QString gameOver(i18n("You Lose!")); 
 
292
    showMessage( gameOver, 2000 );
 
293
}
 
294
 
 
295
void KBlocksScene::showMessage(const QString& message, int ms)
 
296
{
 
297
    mMessageBox->setMessageTimeout( ms );
 
298
    mMessageBox->showMessage( message, KGamePopupItem::TopLeft );
 
299
}
 
300
 
 
301
void KBlocksScene::updateGame()
 
302
{
 
303
    if (mSnapshotMode)
 
304
    {
 
305
        return;
 
306
    }
 
307
    
 
308
    QVarLengthArray<int, 16> removedLines(mGroupCount);
 
309
    int gameCount = mpGameLogic->updateGame(removedLines.data());
 
310
    
 
311
    for(int i = 0; i < mGroupCount; i++)
 
312
    {
 
313
        if (removedLines[i] > 0)
 
314
        {
 
315
            if (maGameScoreList[i]->addScore(removedLines[i]))
 
316
            {
 
317
                int tmpLevel = maGameScoreList[i]->getGameLevel();
 
318
                if (mTopGameLevel < tmpLevel)
 
319
                {
 
320
                    mpGameLogic->levelUpGame(tmpLevel - mTopGameLevel);
 
321
                    mTopGameLevel = tmpLevel;
 
322
                }
 
323
            }
 
324
            emit scoreChanged(i, maGameScoreList[i]->getScorePoint(),
 
325
                                 maGameScoreList[i]->getLineCount(), 
 
326
                                 maGameScoreList[i]->getGameLevel());
 
327
        }
 
328
        else if (removedLines[i] == -1)
 
329
        {
 
330
            maGroupList[i]->stopGame();
 
331
            if (mGroupCount == 1)
 
332
            {
 
333
                QTimer::singleShot(500, this, SLOT(gameOverPlayer()));
 
334
                emit isHighscore(0, maGameScoreList[0]->getScorePoint(),
 
335
                                    maGameScoreList[0]->getGameLevel());
 
336
            }
 
337
            else
 
338
            {
 
339
                if (i == 0)
 
340
                {
 
341
                    for (int j = 0; j < mGroupCount; j++)
 
342
                    {
 
343
                        maGroupList[j]->stopGame();
 
344
                    }
 
345
                    QTimer::singleShot(500, this, SLOT(gameOverMultiLose()));
 
346
                    emit isHighscore(0, maGameScoreList[0]->getScorePoint(),
 
347
                                        maGameScoreList[0]->getGameLevel());
 
348
                }
 
349
                else if (gameCount <= 1)
 
350
                {
 
351
                    maGroupList[0]->stopGame();
 
352
                    QTimer::singleShot(500, this, SLOT(gameOverMultiWin()));
 
353
                    emit isHighscore(0, maGameScoreList[0]->getScorePoint(),
 
354
                                        maGameScoreList[0]->getGameLevel());
 
355
                }
 
356
            }
 
357
        }
 
358
    }
 
359
}
 
360
 
 
361
void KBlocksScene::readyForAction(int groupID)
 
362
{
 
363
    maGameReadySignal[groupID] = true;
 
364
    bool allReady = true;
 
365
    for(int i = 0; i < mGroupCount; i++)
 
366
    {
 
367
        if (!maGameReadySignal[i])
 
368
        {
 
369
            allReady = false;
 
370
        }
 
371
    }
 
372
    if (allReady)
 
373
    {
 
374
        for(int i = 0; i < mGroupCount; i++)
 
375
        {
 
376
            if (mpGameLogic->getSingleGame(i)->isGameRunning())
 
377
            {
 
378
                maGameReadySignal[i] = false;
 
379
            }
 
380
        }
 
381
        mpGameLogic->continueGame();
 
382
    }
 
383
}
 
384
 
 
385
void KBlocksScene::drawBackground( QPainter * painter, const QRectF & rect )
 
386
{
 
387
    if (mpGrafx->renderer()->isValid())
 
388
    {
 
389
        mpGrafx->renderer()->render(painter, QString("BACKGROUND"), rect);
 
390
    }
 
391
}
 
392
 
 
393