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