1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | /*
|
---|
42 | * KAsteroids - Copyright (c) Martin R. Jones 1997
|
---|
43 | *
|
---|
44 | * Part of the KDE project
|
---|
45 | */
|
---|
46 |
|
---|
47 | #ifndef __AST_VIEW_H__
|
---|
48 | #define __AST_VIEW_H__
|
---|
49 |
|
---|
50 | #include <qwidget.h>
|
---|
51 | #include <q3ptrlist.h>
|
---|
52 | #include <q3intdict.h>
|
---|
53 | #include <qtimer.h>
|
---|
54 | #include <QGraphicsScene>
|
---|
55 | #include <QGraphicsView>
|
---|
56 | //Added by qt3to4:
|
---|
57 | #include <QTimerEvent>
|
---|
58 | #include <QShowEvent>
|
---|
59 | #include <QResizeEvent>
|
---|
60 | #include "sprites.h"
|
---|
61 |
|
---|
62 | #define MAX_POWER_LEVEL 1000
|
---|
63 |
|
---|
64 | class KAsteroidsView : public QWidget
|
---|
65 | {
|
---|
66 | Q_OBJECT
|
---|
67 | public:
|
---|
68 | KAsteroidsView( QWidget *parent = 0, const char *name = 0 );
|
---|
69 | virtual ~KAsteroidsView();
|
---|
70 |
|
---|
71 | int refreshRate;
|
---|
72 |
|
---|
73 | void reset();
|
---|
74 | void setRockSpeed( double rs ) { rockSpeed = rs; }
|
---|
75 | void addRocks( int num );
|
---|
76 | void newGame();
|
---|
77 | void endGame();
|
---|
78 | void newShip();
|
---|
79 |
|
---|
80 | void rotateLeft( bool r ) { rotateL = r; rotateSlow = 5; }
|
---|
81 | void rotateRight( bool r ) { rotateR = r; rotateSlow = 5; }
|
---|
82 | void thrust( bool t ) { thrustShip = t && shipPower > 0; }
|
---|
83 | void shoot( bool s ) { shootShip = s; shootDelay = 0; }
|
---|
84 | void setShield( bool s );
|
---|
85 | void teleport( bool te) { teleportShip = te && mTeleportCount; }
|
---|
86 | void brake( bool b );
|
---|
87 | void pause( bool p);
|
---|
88 |
|
---|
89 | void showText( const QString &text, const QColor &color, bool scroll=TRUE );
|
---|
90 | void hideText();
|
---|
91 |
|
---|
92 | int shots() const { return shotsFired; }
|
---|
93 | int hits() const { return shotsHit; }
|
---|
94 | int power() const { return shipPower; }
|
---|
95 |
|
---|
96 | int teleportCount() const { return mTeleportCount; }
|
---|
97 | int brakeCount() const { return mBrakeCount; }
|
---|
98 | int shieldCount() const { return mShieldCount; }
|
---|
99 | int shootCount() const { return mShootCount; }
|
---|
100 |
|
---|
101 | signals:
|
---|
102 | void shipKilled();
|
---|
103 | void rockHit( int size );
|
---|
104 | void rocksRemoved();
|
---|
105 | void updateVitals();
|
---|
106 |
|
---|
107 | private slots:
|
---|
108 | void hideShield();
|
---|
109 |
|
---|
110 | protected:
|
---|
111 | bool readSprites();
|
---|
112 | void wrapSprite( QGraphicsItem * );
|
---|
113 | void rockHit( AnimatedPixmapItem * );
|
---|
114 | void reducePower( int val );
|
---|
115 | void addExhaust( double x, double y, double dx, double dy, int count );
|
---|
116 | void processMissiles();
|
---|
117 | void processShip();
|
---|
118 | void processPowerups();
|
---|
119 | void processShield();
|
---|
120 | double randDouble();
|
---|
121 | int randInt( int range );
|
---|
122 |
|
---|
123 | virtual void resizeEvent( QResizeEvent *event );
|
---|
124 | virtual void timerEvent( QTimerEvent * );
|
---|
125 |
|
---|
126 | void showEvent( QShowEvent * );
|
---|
127 |
|
---|
128 | private:
|
---|
129 | QGraphicsScene field;
|
---|
130 | QGraphicsView view;
|
---|
131 | QMap<int, QList<QPixmap> > animation;
|
---|
132 | Q3PtrList<AnimatedPixmapItem> rocks;
|
---|
133 | Q3PtrList<KMissile> missiles;
|
---|
134 | Q3PtrList<KBit> bits;
|
---|
135 | Q3PtrList<KExhaust> exhaust;
|
---|
136 | Q3PtrList<KPowerup> powerups;
|
---|
137 | KShield *shield;
|
---|
138 | AnimatedPixmapItem *ship;
|
---|
139 | QGraphicsTextItem *textSprite;
|
---|
140 |
|
---|
141 | bool rotateL;
|
---|
142 | bool rotateR;
|
---|
143 | bool thrustShip;
|
---|
144 | bool shootShip;
|
---|
145 | bool teleportShip;
|
---|
146 | bool brakeShip;
|
---|
147 | bool pauseShip;
|
---|
148 | bool shieldOn;
|
---|
149 |
|
---|
150 | bool vitalsChanged;
|
---|
151 |
|
---|
152 | int shipAngle;
|
---|
153 | int rotateSlow;
|
---|
154 | int rotateRate;
|
---|
155 | int shipPower;
|
---|
156 |
|
---|
157 | int shotsFired;
|
---|
158 | int shotsHit;
|
---|
159 | int shootDelay;
|
---|
160 |
|
---|
161 | int mBrakeCount;
|
---|
162 | int mShieldCount;
|
---|
163 | int mTeleportCount;
|
---|
164 | int mShootCount;
|
---|
165 |
|
---|
166 | double shipDx;
|
---|
167 | double shipDy;
|
---|
168 |
|
---|
169 | int textDy;
|
---|
170 | int mFrameNum;
|
---|
171 | bool mPaused;
|
---|
172 | int mTimerId;
|
---|
173 |
|
---|
174 | double rockSpeed;
|
---|
175 | double powerupSpeed;
|
---|
176 |
|
---|
177 | bool can_destroy_powerups;
|
---|
178 |
|
---|
179 | QTimer *shieldTimer;
|
---|
180 | bool initialized;
|
---|
181 | };
|
---|
182 |
|
---|
183 | #endif
|
---|