summaryrefslogtreecommitdiff
path: root/noncore/games/qasteroids
Unidiff
Diffstat (limited to 'noncore/games/qasteroids') (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/qasteroids/.cvsignore2
-rw-r--r--noncore/games/qasteroids/Makefile.in155
-rw-r--r--noncore/games/qasteroids/ledmeter.cpp135
-rw-r--r--noncore/games/qasteroids/ledmeter.h72
-rw-r--r--noncore/games/qasteroids/main.cpp36
-rw-r--r--noncore/games/qasteroids/qasteroids.pro11
-rw-r--r--noncore/games/qasteroids/qpe-qasteroids.control9
-rw-r--r--noncore/games/qasteroids/sprites.h147
-rw-r--r--noncore/games/qasteroids/toplevel.cpp514
-rw-r--r--noncore/games/qasteroids/toplevel.h99
-rw-r--r--noncore/games/qasteroids/view.cpp884
-rw-r--r--noncore/games/qasteroids/view.h156
12 files changed, 2220 insertions, 0 deletions
diff --git a/noncore/games/qasteroids/.cvsignore b/noncore/games/qasteroids/.cvsignore
new file mode 100644
index 0000000..c152c55
--- a/dev/null
+++ b/noncore/games/qasteroids/.cvsignore
@@ -0,0 +1,2 @@
1moc_*.cpp
2Makefile
diff --git a/noncore/games/qasteroids/Makefile.in b/noncore/games/qasteroids/Makefile.in
new file mode 100644
index 0000000..7312743
--- a/dev/null
+++ b/noncore/games/qasteroids/Makefile.in
@@ -0,0 +1,155 @@
1#############################################################################
2
3####### Compiler, tools and options
4
5 CXX =$(SYSCONF_CXX) $(QT_CXX_MT)
6 CXXFLAGS=$(SYSCONF_CXXFLAGS_QT) $(SYSCONF_CXXFLAGS)
7 CC =$(SYSCONF_CC) $(QT_C_MT)
8 CFLAGS =$(SYSCONF_CFLAGS)
9 INCPATH =-I$(QPEDIR)/include
10 LFLAGS =$(SYSCONF_LFLAGS_QT) $(SYSCONF_RPATH_QT) $(SYSCONF_LFLAGS) $(QT_LFLAGS_MT)
11 LIBS =$(SUBLIBS) -lqpe $(SYSCONF_LIBS_QT) $(SYSCONF_LIBS) $(SYSCONF_LIBS_QTAPP)
12 MOC =$(SYSCONF_MOC)
13 UIC =$(SYSCONF_UIC)
14
15####### Target
16
17DESTDIR = $(QPEDIR)/bin/
18VER_MAJ = 1
19VER_MIN = 0
20VER_PATCH = 0
21 TARGET= qasteroids
22TARGET1 = lib$(TARGET).so.$(VER_MAJ)
23
24####### Files
25
26 HEADERS =ledmeter.h \
27 sprites.h \
28 toplevel.h \
29 view.h
30 SOURCES =ledmeter.cpp \
31 toplevel.cpp \
32 view.cpp \
33 main.cpp
34 OBJECTS =ledmeter.o \
35 toplevel.o \
36 view.o \
37 main.o
38INTERFACES =
39UICDECLS =
40UICIMPLS =
41 SRCMOC =moc_ledmeter.cpp \
42 moc_toplevel.cpp \
43 moc_view.cpp
44 OBJMOC =moc_ledmeter.o \
45 moc_toplevel.o \
46 moc_view.o
47
48
49####### Implicit rules
50
51.SUFFIXES: .cpp .cxx .cc .C .c
52
53.cpp.o:
54 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
55
56.cxx.o:
57 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
58
59.cc.o:
60 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
61
62.C.o:
63 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
64
65.c.o:
66 $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
67
68####### Build rules
69
70
71all: $(DESTDIR)$(TARGET)
72
73$(DESTDIR)$(TARGET): $(UICDECLS) $(OBJECTS) $(OBJMOC) $(SUBLIBS)
74 $(SYSCONF_LINK) $(LFLAGS) -o $(DESTDIR)$(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS)
75
76moc: $(SRCMOC)
77
78tmake:
79 tmake qasteroids.pro
80
81clean:
82 -rm -f $(OBJECTS) $(OBJMOC) $(SRCMOC) $(UICIMPLS) $(UICDECLS)
83 -rm -f *~ core
84 -rm -f allmoc.cpp
85
86####### Extension Modules
87
88listpromodules:
89 @echo
90
91listallmodules:
92 @echo
93
94listaddonpromodules:
95 @echo
96
97listaddonentmodules:
98 @echo
99
100
101REQUIRES=
102
103####### Sub-libraries
104
105
106###### Combined headers
107
108
109
110####### Compile
111
112ledmeter.o: ledmeter.cpp \
113 ledmeter.h
114
115toplevel.o: toplevel.cpp \
116 toplevel.h \
117 view.h \
118 sprites.h \
119 ledmeter.h \
120 $(QPEDIR)/include/qpe/qpeapplication.h \
121 $(QPEDIR)/include/qpe/resource.h
122
123view.o: view.cpp \
124 view.h \
125 sprites.h \
126 $(QPEDIR)/include/qpe/resource.h
127
128main.o: main.cpp \
129 toplevel.h \
130 view.h \
131 sprites.h \
132 $(QPEDIR)/include/qpe/qpeapplication.h
133
134moc_ledmeter.o: moc_ledmeter.cpp \
135 ledmeter.h
136
137moc_toplevel.o: moc_toplevel.cpp \
138 toplevel.h \
139 view.h \
140 sprites.h
141
142moc_view.o: moc_view.cpp \
143 view.h \
144 sprites.h
145
146moc_ledmeter.cpp: ledmeter.h
147 $(MOC) ledmeter.h -o moc_ledmeter.cpp
148
149moc_toplevel.cpp: toplevel.h
150 $(MOC) toplevel.h -o moc_toplevel.cpp
151
152moc_view.cpp: view.h
153 $(MOC) view.h -o moc_view.cpp
154
155
diff --git a/noncore/games/qasteroids/ledmeter.cpp b/noncore/games/qasteroids/ledmeter.cpp
new file mode 100644
index 0000000..f4d4d1a
--- a/dev/null
+++ b/noncore/games/qasteroids/ledmeter.cpp
@@ -0,0 +1,135 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************//*
20 * KAsteroids - Copyright (c) Martin R. Jones 1997
21 *
22 * Part of the KDE project
23 */
24
25#include <qpainter.h>
26#include "ledmeter.h"
27
28KALedMeter::KALedMeter( QWidget *parent ) : QFrame( parent )
29{
30 mCRanges.setAutoDelete( TRUE );
31 mRange = 100;
32 mCount = 20;
33 mCurrentCount = 0;
34 mValue = 0;
35 setMinimumWidth( mCount * 2 + frameWidth() );
36}
37
38void KALedMeter::setRange( int r )
39{
40 mRange = r;
41 if ( mRange < 1 )
42 mRange = 1;
43 setValue( mValue );
44 update();
45}
46
47void KALedMeter::setCount( int c )
48{
49 mCount = c;
50 if ( mCount < 1 )
51 mCount = 1;
52 setMinimumWidth( mCount * 2 + frameWidth() );
53 calcColorRanges();
54 setValue( mValue );
55 update();
56}
57
58void KALedMeter::setValue( int v )
59{
60 mValue = v;
61 if ( mValue > mRange )
62 mValue = mRange;
63 else if ( mValue < 0 )
64 mValue = 0;
65 int c = ( mValue + mRange / mCount - 1 ) * mCount / mRange;
66 if ( c != mCurrentCount )
67 {
68 mCurrentCount = c;
69 update();
70 }
71}
72
73void KALedMeter::addColorRange( int pc, const QColor &c )
74{
75 ColorRange *cr = new ColorRange;
76 cr->mPc = pc;
77 cr->mColor = c;
78 mCRanges.append( cr );
79 calcColorRanges();
80}
81
82void KALedMeter::resizeEvent( QResizeEvent *e )
83{
84 QFrame::resizeEvent( e );
85 int w = ( width() - frameWidth() - 2 ) / mCount * mCount;
86 w += frameWidth() + 2;
87 setFrameRect( QRect( 0, 0, w, height() ) );
88}
89
90void KALedMeter::drawContents( QPainter *p )
91{
92 QRect b = contentsRect();
93
94 unsigned cidx = 0;
95 int ncol = mCount;
96 QColor col = colorGroup().foreground();
97
98 if ( !mCRanges.isEmpty() )
99 {
100 col = mCRanges.at( cidx )->mColor;
101 ncol = mCRanges.at( cidx )->mValue;
102 }
103 p->setBrush( col );
104 p->setPen( col );
105
106 int lw = b.width() / mCount;
107 int lx = b.left() + 1;
108 for ( int i = 0; i < mCurrentCount; i++, lx += lw )
109 {
110 if ( i > ncol )
111 {
112 if ( ++cidx < mCRanges.count() )
113 {
114 col = mCRanges.at( cidx )->mColor;
115 ncol = mCRanges.at( cidx )->mValue;
116 p->setBrush( col );
117 p->setPen( col );
118 }
119 }
120
121 p->drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 );
122 }
123}
124
125void KALedMeter::calcColorRanges()
126{
127 int prev = 0;
128 ColorRange *cr;
129 for ( cr = mCRanges.first(); cr; cr = mCRanges.next() )
130 {
131 cr->mValue = prev + cr->mPc * mCount / 100;
132 prev = cr->mValue;
133 }
134}
135
diff --git a/noncore/games/qasteroids/ledmeter.h b/noncore/games/qasteroids/ledmeter.h
new file mode 100644
index 0000000..b2f06c1
--- a/dev/null
+++ b/noncore/games/qasteroids/ledmeter.h
@@ -0,0 +1,72 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************//*
20 * KAsteroids - Copyright (c) Martin R. Jones 1997
21 *
22 * Part of the KDE project
23 */
24
25#ifndef __LEDMETER_H__
26#define __LEDMETER_H__
27
28#include <qframe.h>
29#include <qlist.h>
30
31#define QPtrList QList
32
33class KALedMeter : public QFrame
34{
35 Q_OBJECT
36public:
37 KALedMeter( QWidget *parent );
38
39 int range() const { return mRange; }
40 void setRange( int r );
41
42 int count() const { return mCount; }
43 void setCount( int c );
44
45 int value () const { return mValue; }
46
47 void addColorRange( int pc, const QColor &c );
48
49public slots:
50 void setValue( int v );
51
52protected:
53 virtual void resizeEvent( QResizeEvent * );
54 virtual void drawContents( QPainter * );
55 void calcColorRanges();
56
57protected:
58 struct ColorRange
59 {
60 int mPc;
61 int mValue;
62 QColor mColor;
63 };
64
65 int mRange;
66 int mCount;
67 int mCurrentCount;
68 int mValue;
69 QPtrList<ColorRange> mCRanges;
70};
71
72#endif
diff --git a/noncore/games/qasteroids/main.cpp b/noncore/games/qasteroids/main.cpp
new file mode 100644
index 0000000..7d1682c
--- a/dev/null
+++ b/noncore/games/qasteroids/main.cpp
@@ -0,0 +1,36 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "toplevel.h"
22
23#include <qpe/qpeapplication.h>
24
25int main( int argc, char *argv[] )
26{
27 QPEApplication app( argc, argv );
28
29 QPEApplication::grabKeyboard();
30
31 KAstTopLevel *mainWidget = new KAstTopLevel();
32 app.showMainWidget( mainWidget );
33
34 app.exec();
35}
36
diff --git a/noncore/games/qasteroids/qasteroids.pro b/noncore/games/qasteroids/qasteroids.pro
new file mode 100644
index 0000000..14a0901
--- a/dev/null
+++ b/noncore/games/qasteroids/qasteroids.pro
@@ -0,0 +1,11 @@
1 TEMPLATE= app
2 CONFIG += qt warn_on release
3 DESTDIR = $(QPEDIR)/bin
4 HEADERS = ledmeter.h sprites.h toplevel.h view.h
5 SOURCES = ledmeter.cpp toplevel.cpp view.cpp main.cpp
6 TARGET = qasteroids
7INCLUDEPATH += $(QPEDIR)/include
8 DEPENDPATH+= $(QPEDIR)/include
9LIBS += -lqpe
10
11TRANSLATIONS = ../i18n/de/qasteroids.ts
diff --git a/noncore/games/qasteroids/qpe-qasteroids.control b/noncore/games/qasteroids/qpe-qasteroids.control
new file mode 100644
index 0000000..c1b328e
--- a/dev/null
+++ b/noncore/games/qasteroids/qpe-qasteroids.control
@@ -0,0 +1,9 @@
1Files: bin/qasteroids apps/Games/qasteroids.desktop pics/qasteroids pics/Asteroids.png
2Priority: optional
3Section: qpe/games
4Maintainer: Martin Jones <mjones@trolltech.com>
5Architecture: arm
6Version: $QPE_VERSION-3
7Depends: qpe-base ($QPE_VERSION)
8Description: Game: shoot the asteroids
9 A game for the Qtopia environment.
diff --git a/noncore/games/qasteroids/sprites.h b/noncore/games/qasteroids/sprites.h
new file mode 100644
index 0000000..0827821
--- a/dev/null
+++ b/noncore/games/qasteroids/sprites.h
@@ -0,0 +1,147 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************//*
20 * KAsteroids - Copyright (c) Martin R. Jones 1997
21 *
22 * Part of the KDE project
23 */
24
25#ifndef __SPRITES_H__
26#define __SPRITES_H__
27
28#include <qcanvas.h>
29
30#define ID_ROCK_LARGE 1024
31#define ID_ROCK_MEDIUM 1025
32#define ID_ROCK_SMALL 1026
33
34#define ID_MISSILE 1030
35
36#define ID_BIT 1040
37#define ID_EXHAUST 1041
38
39#define ID_ENERGY_POWERUP 1310
40#define ID_TELEPORT_POWERUP 1311
41#define ID_BRAKE_POWERUP 1312
42#define ID_SHIELD_POWERUP 1313
43#define ID_SHOOT_POWERUP 1314
44
45#define ID_SHIP 1350
46#define ID_SHIELD 1351
47
48#define MAX_SHIELD_AGE 350
49#define MAX_POWERUP_AGE 500
50#define MAX_MISSILE_AGE 20
51
52class KMissile : public QCanvasSprite
53{
54public:
55 KMissile( QCanvasPixmapArray *s, QCanvas *c ) : QCanvasSprite( s, c )
56 { myAge = 0; }
57
58 virtual int rtti() const { return ID_MISSILE; }
59
60 void growOlder() { myAge++; }
61 bool expired() { return myAge > MAX_MISSILE_AGE; }
62
63private:
64 int myAge;
65};
66
67class KBit : public QCanvasSprite
68{
69public:
70 KBit( QCanvasPixmapArray *s, QCanvas *c ) : QCanvasSprite( s, c )
71 { death = 7; }
72
73 virtual int rtti() const { return ID_BIT; }
74
75 void setDeath( int d ) { death = d; }
76 void growOlder() { death--; }
77 bool expired() { return death <= 0; }
78
79private:
80 int death;
81};
82
83class KExhaust : public QCanvasSprite
84{
85public:
86 KExhaust( QCanvasPixmapArray *s, QCanvas *c ) : QCanvasSprite( s, c )
87 { death = 1; }
88
89 virtual int rtti() const { return ID_EXHAUST; }
90
91 void setDeath( int d ) { death = d; }
92 void growOlder() { death--; }
93 bool expired() { return death <= 0; }
94
95private:
96 int death;
97};
98
99class KPowerup : public QCanvasSprite
100{
101public:
102 KPowerup( QCanvasPixmapArray *s, QCanvas *c, int t ) : QCanvasSprite( s, c ),
103 myAge( 0 ), type(t) { }
104
105 virtual int rtti() const { return type; }
106
107 void growOlder() { myAge++; }
108 bool expired() const { return myAge > MAX_POWERUP_AGE; }
109
110protected:
111 int myAge;
112 int type;
113};
114
115class KRock : public QCanvasSprite
116{
117public:
118 KRock (QCanvasPixmapArray *s, QCanvas *c, int t, int sk, int st) : QCanvasSprite( s, c )
119 { type = t; skip = cskip = sk; step = st; }
120
121 void nextFrame()
122 {
123 if (cskip-- <= 0) {
124 setFrame( (frame()+step+frameCount())%frameCount() );
125 cskip = QABS(skip);
126 }
127 }
128
129 virtual int rtti() const { return type; }
130
131private:
132 int type;
133 int skip;
134 int cskip;
135 int step;
136};
137
138class KShield : public QCanvasSprite
139{
140public:
141 KShield( QCanvasPixmapArray *s, QCanvas *c )
142 : QCanvasSprite( s, c ) {}
143
144 virtual int rtti() const { return ID_SHIELD; }
145};
146
147#endif
diff --git a/noncore/games/qasteroids/toplevel.cpp b/noncore/games/qasteroids/toplevel.cpp
new file mode 100644
index 0000000..57242a0
--- a/dev/null
+++ b/noncore/games/qasteroids/toplevel.cpp
@@ -0,0 +1,514 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************//*
20 * KAsteroids - Copyright (c) Martin R. Jones 1997
21 *
22 * Part of the KDE project
23 */
24 //--- toplevel.cpp ---
25
26#include "toplevel.h"
27#include "ledmeter.h"
28
29#include <qpe/qpeapplication.h>
30#include <qpe/resource.h>
31
32#include <qaccel.h>
33#include <qlabel.h>
34#include <qlayout.h>
35#include <qlcdnumber.h>
36#include <qpushbutton.h>
37
38
39 #define SB_SCORE1
40 #define SB_LEVEL2
41 #define SB_SHIPS3
42
43struct SLevel
44{
45 int nrocks;
46 double rockSpeed;
47};
48
49 #define MAX_LEVELS16
50
51SLevel levels[MAX_LEVELS] =
52{
53 { 1, 0.4 },
54 { 1, 0.6 },
55 { 2, 0.5 },
56 { 2, 0.7 },
57 { 2, 0.8 },
58 { 3, 0.6 },
59 { 3, 0.7 },
60 { 3, 0.8 },
61 { 4, 0.6 },
62 { 4, 0.7 },
63 { 4, 0.8 },
64 { 5, 0.7 },
65 { 5, 0.8 },
66 { 5, 0.9 },
67 { 5, 1.0 }
68};
69
70const char *soundEvents[] =
71{
72 "ShipDestroyed",
73 "RockDestroyed",
74 0
75};
76
77const char *soundDefaults[] =
78{
79 "Explosion.wav",
80 "ploop.wav",
81 0
82};
83
84
85KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name )
86 : QMainWindow( parent, name )
87{
88 setCaption( tr("Asteroids") );
89 QWidget *border = new QWidget( this );
90 border->setBackgroundColor( black );
91 setCentralWidget( border );
92
93 QVBoxLayout *borderLayout = new QVBoxLayout( border );
94
95 QWidget *mainWin = new QWidget( border );
96 borderLayout->addWidget( mainWin, 2, AlignHCenter );
97
98 view = new KAsteroidsView( mainWin );
99 connect( view, SIGNAL( shipKilled() ), SLOT( slotShipKilled() ) );
100 connect( view, SIGNAL( rockHit(int) ), SLOT( slotRockHit(int) ) );
101 connect( view, SIGNAL( rocksRemoved() ), SLOT( slotRocksRemoved() ) );
102 connect( view, SIGNAL( updateVitals() ), SLOT( slotUpdateVitals() ) );
103
104 QVBoxLayout *vb = new QVBoxLayout( mainWin );
105 QHBoxLayout *hb = new QHBoxLayout;
106 QHBoxLayout *hbd = new QHBoxLayout;
107 vb->addLayout( hb );
108
109 QFont labelFont( "helvetica", 12 );
110 QColorGroup grp( darkGreen, black, QColor( 128, 128, 128 ),
111 QColor( 64, 64, 64 ), black, darkGreen, black );
112 QPalette pal( grp, grp, grp );
113
114 mainWin->setPalette( pal );
115
116 QLabel *label;
117 label = new QLabel( tr("Score"), mainWin );
118 label->setFont( labelFont );
119 label->setPalette( pal );
120// label->setFixedWidth( label->sizeHint().width() );
121 hb->addWidget( label );
122
123 scoreLCD = new QLCDNumber( 5, mainWin );
124 scoreLCD->setFrameStyle( QFrame::NoFrame );
125 scoreLCD->setSegmentStyle( QLCDNumber::Flat );
126 scoreLCD->setFixedHeight( 16 );
127 scoreLCD->setPalette( pal );
128 hb->addWidget( scoreLCD );
129 hb->addStretch( 1 );
130
131 label = new QLabel( tr("Level"), mainWin );
132 label->setFont( labelFont );
133 label->setPalette( pal );
134// label->setFixedWidth( label->sizeHint().width() );
135 hb->addWidget( label );
136
137 levelLCD = new QLCDNumber( 2, mainWin );
138 levelLCD->setFrameStyle( QFrame::NoFrame );
139 levelLCD->setSegmentStyle( QLCDNumber::Flat );
140 levelLCD->setFixedHeight( 16 );
141 levelLCD->setPalette( pal );
142 hb->addWidget( levelLCD );
143 hb->addStretch( 1 );
144
145 label = new QLabel( tr("Ships"), mainWin );
146 label->setFont( labelFont );
147// label->setFixedWidth( label->sizeHint().width() );
148 label->setPalette( pal );
149 hb->addWidget( label );
150
151 shipsLCD = new QLCDNumber( 1, mainWin );
152 shipsLCD->setFrameStyle( QFrame::NoFrame );
153 shipsLCD->setSegmentStyle( QLCDNumber::Flat );
154 shipsLCD->setFixedHeight( 16 );
155 shipsLCD->setPalette( pal );
156 hb->addWidget( shipsLCD );
157
158// hb->addStrut( 14 );
159
160 vb->addWidget( view, 10 );
161
162// -- bottom layout:
163 vb->addLayout( hbd );
164
165 QFont smallFont( "helvetica", 12 );
166 hbd->addSpacing( 5 );
167
168/*
169 label = new QLabel( tr( "T" ), mainWin );
170 label->setFont( smallFont );
171 label->setFixedWidth( label->sizeHint().width() );
172 label->setPalette( pal );
173 hbd->addWidget( label );
174
175 teleportsLCD = new QLCDNumber( 1, mainWin );
176 teleportsLCD->setFrameStyle( QFrame::NoFrame );
177 teleportsLCD->setSegmentStyle( QLCDNumber::Flat );
178 teleportsLCD->setPalette( pal );
179 teleportsLCD->setFixedHeight( 18 );
180 hbd->addWidget( teleportsLCD );
181
182 hbd->addSpacing( 10 );
183*/
184 label = new QLabel( mainWin );
185 label->setPixmap( Resource::loadPixmap("qasteroids/powerups/brake.png") );
186 label->setFixedWidth( 16 );
187 label->setPalette( pal );
188 hbd->addWidget( label );
189
190 brakesLCD = new QLCDNumber( 1, mainWin );
191 brakesLCD->setFrameStyle( QFrame::NoFrame );
192 brakesLCD->setSegmentStyle( QLCDNumber::Flat );
193 brakesLCD->setPalette( pal );
194 brakesLCD->setFixedHeight( 16 );
195 hbd->addWidget( brakesLCD );
196
197 hbd->addSpacing( 5 );
198
199 label = new QLabel( mainWin );
200 label->setPixmap( Resource::loadPixmap("qasteroids/powerups/shield.png") );
201 label->setFixedWidth( 16 );
202 label->setPalette( pal );
203 hbd->addWidget( label );
204
205 shieldLCD = new QLCDNumber( 1, mainWin );
206 shieldLCD->setFrameStyle( QFrame::NoFrame );
207 shieldLCD->setSegmentStyle( QLCDNumber::Flat );
208 shieldLCD->setPalette( pal );
209 shieldLCD->setFixedHeight( 16 );
210 hbd->addWidget( shieldLCD );
211
212 hbd->addSpacing( 5 );
213
214 label = new QLabel( mainWin );
215 label->setPixmap( Resource::loadPixmap("qasteroids/powerups/shoot.png") );
216 label->setFixedWidth( 16 );
217 label->setPalette( pal );
218 hbd->addWidget( label );
219
220 shootLCD = new QLCDNumber( 1, mainWin );
221 shootLCD->setFrameStyle( QFrame::NoFrame );
222 shootLCD->setSegmentStyle( QLCDNumber::Flat );
223 shootLCD->setPalette( pal );
224 shootLCD->setFixedHeight( 16 );
225 hbd->addWidget( shootLCD );
226
227 hbd->addStretch( 1 );
228
229 label = new QLabel( tr( "Fuel" ), mainWin );
230 label->setFont( smallFont );
231 label->setFixedWidth( label->sizeHint().width() + 5 );
232 label->setPalette( pal );
233 hbd->addWidget( label );
234
235 powerMeter = new KALedMeter( mainWin );
236 powerMeter->setFrameStyle( QFrame::Box | QFrame::Plain );
237 powerMeter->setRange( MAX_POWER_LEVEL );
238 powerMeter->addColorRange( 10, darkRed );
239 powerMeter->addColorRange( 20, QColor(160, 96, 0) );
240 powerMeter->addColorRange( 70, darkGreen );
241 powerMeter->setCount( 15 );
242 powerMeter->setPalette( pal );
243 powerMeter->setFixedSize( 60, 12 );
244 hbd->addWidget( powerMeter );
245
246 shipsRemain = 3;
247 showHiscores = FALSE;
248
249 actions.insert( Qt::Key_Up, Thrust );
250 actions.insert( Qt::Key_Left, RotateLeft );
251 actions.insert( Qt::Key_Right, RotateRight );
252 actions.insert( Qt::Key_Enter, Shoot );
253 actions.insert( Qt::Key_Z, Teleport );
254 actions.insert( Qt::Key_Down, Brake );
255 actions.insert( Qt::Key_P, Pause );
256 actions.insert( Key_F12, Launch );
257 actions.insert( Key_F11, Shield );
258 actions.insert( Key_F9, NewGame );
259
260// actions.insert( Qt::Key_S, Shield );
261// actions.insert( Qt::Key_X, Brake );
262// actions.insert( Qt::Key_L, Launch );
263 actions.insert( Qt::Key_Space, Shoot );
264
265 view->showText( tr( "Press Calendar to start playing" ), yellow );
266
267 setFocusPolicy( StrongFocus );
268
269 slotNewGame();
270}
271
272KAstTopLevel::~KAstTopLevel()
273{
274}
275
276void KAstTopLevel::playSound( const char * )
277{
278}
279
280void KAstTopLevel::keyPressEvent( QKeyEvent *event )
281{
282 if ( event->isAutoRepeat() || !actions.contains( event->key() ) )
283 {
284 event->ignore();
285 return;
286 }
287
288 Action a = actions[ event->key() ];
289
290 switch ( a )
291 {
292 case RotateLeft:
293 view->rotateLeft( TRUE );
294 break;
295
296 case RotateRight:
297 view->rotateRight( TRUE );
298 break;
299
300 case Thrust:
301 view->thrust( TRUE );
302 break;
303
304 case Shoot:
305 view->shoot( TRUE );
306 break;
307
308 case Shield:
309 view->setShield( TRUE );
310 break;
311
312 case Teleport:
313 view->teleport( TRUE );
314 break;
315
316 case Brake:
317 view->brake( TRUE );
318 break;
319
320 default:
321 event->ignore();
322 return;
323 }
324 event->accept();
325}
326
327void KAstTopLevel::keyReleaseEvent( QKeyEvent *event )
328{
329 if ( event->isAutoRepeat() || !actions.contains( event->key() ) )
330 {
331 event->ignore();
332 return;
333 }
334
335 Action a = actions[ event->key() ];
336
337 switch ( a )
338 {
339 case RotateLeft:
340 view->rotateLeft( FALSE );
341 break;
342
343 case RotateRight:
344 view->rotateRight( FALSE );
345 break;
346
347 case Thrust:
348 view->thrust( FALSE );
349 break;
350
351 case Shoot:
352 view->shoot( FALSE );
353 break;
354
355 case Brake:
356 view->brake( FALSE );
357 break;
358
359 case Shield:
360 view->setShield( FALSE );
361 break;
362
363 case Teleport:
364 view->teleport( FALSE );
365 break;
366
367 case Launch:
368 if ( waitShip )
369 {
370 view->newShip();
371 waitShip = FALSE;
372 view->hideText();
373 }
374 else
375 {
376 event->ignore();
377 return;
378 }
379 break;
380
381 case NewGame:
382 slotNewGame();
383 break;
384/*
385 case Pause:
386 {
387 view->pause( TRUE );
388 QMessageBox::information( this,
389 tr("KAsteroids is paused"),
390 tr("Paused") );
391 view->pause( FALSE );
392 }
393 break;
394*/
395 default:
396 event->ignore();
397 return;
398 }
399
400 event->accept();
401}
402
403void KAstTopLevel::showEvent( QShowEvent *e )
404{
405 QMainWindow::showEvent( e );
406 view->pause( FALSE );
407 setFocus();
408}
409
410void KAstTopLevel::hideEvent( QHideEvent *e )
411{
412 QMainWindow::hideEvent( e );
413 view->pause( TRUE );
414}
415
416void KAstTopLevel::focusInEvent( QFocusEvent * )
417{
418 view->pause( FALSE );
419 setFocus();
420}
421
422void KAstTopLevel::focusOutEvent( QFocusEvent * )
423{
424 view->pause( TRUE );
425}
426
427void KAstTopLevel::slotNewGame()
428{
429 shipsRemain = 3;
430 score = 0;
431 scoreLCD->display( 0 );
432 level = 0;
433 levelLCD->display( level+1 );
434 shipsLCD->display( shipsRemain-1 );
435 view->newGame();
436 view->setRockSpeed( levels[0].rockSpeed );
437 view->addRocks( levels[0].nrocks );
438 view->newShip();
439 waitShip = FALSE;
440 view->hideText();
441 isPaused = FALSE;
442}
443
444void KAstTopLevel::slotShipKilled()
445{
446 shipsRemain--;
447 shipsLCD->display( shipsRemain-1 );
448
449 playSound( "ShipDestroyed" );
450
451 if ( shipsRemain > 0 )
452 {
453 waitShip = TRUE;
454 view->showText( tr( "Ship Destroyed.\nPress Launch/Home key."), yellow );
455 }
456 else
457 {
458 view->endGame();
459 doStats();
460 }
461}
462
463void KAstTopLevel::slotRockHit( int size )
464{
465 switch ( size )
466 {
467 case 0:
468 score += 10;
469 break;
470
471 case 1:
472 score += 20;
473 break;
474
475 default:
476 score += 40;
477 }
478
479 playSound( "RockDestroyed" );
480
481 scoreLCD->display( score );
482}
483
484void KAstTopLevel::slotRocksRemoved()
485{
486 level++;
487
488 if ( level >= MAX_LEVELS )
489 level = MAX_LEVELS - 1;
490
491 view->setRockSpeed( levels[level-1].rockSpeed );
492 view->addRocks( levels[level-1].nrocks );
493
494 levelLCD->display( level+1 );
495}
496
497void KAstTopLevel::doStats()
498{
499 QString r( "0.00" );
500 if ( view->shots() )
501 r = QString::number( (double)view->hits() / view->shots() * 100.0,
502 'g', 2 );
503
504 view->showText( tr( "Game Over.\nPress Calendar for a new game." ), yellow, FALSE );
505}
506
507void KAstTopLevel::slotUpdateVitals()
508{
509 brakesLCD->display( view->brakeCount() );
510 shieldLCD->display( view->shieldCount() );
511 shootLCD->display( view->shootCount() );
512// teleportsLCD->display( view->teleportCount() );
513 powerMeter->setValue( view->power() );
514}
diff --git a/noncore/games/qasteroids/toplevel.h b/noncore/games/qasteroids/toplevel.h
new file mode 100644
index 0000000..4e1ac9c
--- a/dev/null
+++ b/noncore/games/qasteroids/toplevel.h
@@ -0,0 +1,99 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************//*
20 * KAsteroids - Copyright (c) Martin R. Jones 1997
21 *
22 * Part of the KDE project
23 */
24
25#ifndef __KAST_TOPLEVEL_H__
26#define __KAST_TOPLEVEL_H__
27
28#include <qmainwindow.h>
29#include <qdict.h>
30#include <qmap.h>
31
32#include "view.h"
33
34
35class KALedMeter;
36class QLCDNumber;
37
38class KAstTopLevel : public QMainWindow
39{
40 Q_OBJECT
41public:
42 KAstTopLevel( QWidget *parent=0, const char *name=0 );
43 virtual ~KAstTopLevel();
44
45private:
46 void playSound( const char *snd );
47 void readSoundMapping();
48 void doStats();
49
50protected:
51 virtual void showEvent( QShowEvent * );
52 virtual void hideEvent( QHideEvent * );
53 virtual void keyPressEvent( QKeyEvent *event );
54 virtual void keyReleaseEvent( QKeyEvent *event );
55 virtual void focusInEvent( QFocusEvent *event );
56 virtual void focusOutEvent( QFocusEvent *event );
57
58private slots:
59 void slotNewGame();
60
61 void slotShipKilled();
62 void slotRockHit( int size );
63 void slotRocksRemoved();
64
65 void slotUpdateVitals();
66
67private:
68 KAsteroidsView *view;
69 QLCDNumber *scoreLCD;
70 QLCDNumber *levelLCD;
71 QLCDNumber *shipsLCD;
72
73 QLCDNumber *teleportsLCD;
74// QLCDNumber *bombsLCD;
75 QLCDNumber *brakesLCD;
76 QLCDNumber *shieldLCD;
77 QLCDNumber *shootLCD;
78 KALedMeter *powerMeter;
79
80 bool sound;
81 QDict<QString> soundDict;
82
83 // waiting for user to press Enter to launch a ship
84 bool waitShip;
85 bool isPaused;
86
87 int shipsRemain;
88 int score;
89 int level;
90 bool showHiscores;
91
92 enum Action { Launch, Thrust, RotateLeft, RotateRight, Shoot, Teleport,
93 Brake, Shield, Pause, NewGame };
94
95 QMap<int,Action> actions;
96};
97
98#endif
99
diff --git a/noncore/games/qasteroids/view.cpp b/noncore/games/qasteroids/view.cpp
new file mode 100644
index 0000000..ef08343
--- a/dev/null
+++ b/noncore/games/qasteroids/view.cpp
@@ -0,0 +1,884 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************//*
20 * KAsteroids - Copyright (c) Martin R. Jones 1997
21 *
22 * Part of the KDE project
23 */
24
25#include "view.h"
26
27#include <qpe/resource.h>
28
29#include <qapplication.h>
30#include <qkeycode.h>
31#include <qaccel.h>
32
33#include <stdlib.h>
34#include <math.h>
35
36#define IMG_BACKGROUND "qasteroids/bg.png"
37
38#define REFRESH_DELAY 33
39#define SHIP_SPEED 0.3
40#define MISSILE_SPEED 10.0
41#define SHIP_STEPS 64
42#define ROTATE_RATE 2
43#define SHIELD_ON_COST 1
44#define SHIELD_HIT_COST 30
45#define BRAKE_ON_COST 4
46
47#define MAX_ROCK_SPEED 2.5
48#define MAX_POWERUP_SPEED 1.5
49 #define MAX_SHIP_SPEED 8
50#define MAX_BRAKES 5
51#define MAX_SHIELDS 5
52 #define MAX_FIREPOWER 5
53
54#define TEXT_SPEED 4
55
56#define PI_X_2 6.283185307
57#ifndef M_PI
58#define M_PI 3.141592654
59#endif
60
61struct
62{
63 int id;
64 const char *path;
65 int frames;
66}
67kas_animations [] =
68{
69// { ID_ROCK_LARGE, "rock1/rock1\%1.png", 32 },
70 { ID_ROCK_MEDIUM, "rock2/rock2\%1.png", 32 },
71 { ID_ROCK_SMALL, "rock3/rock3\%1.png", 32 },
72 { ID_SHIP, "ship/ship\%1.png", 32 },
73 { ID_MISSILE, "missile/missile.png", 1 },
74 { ID_BIT, "bits/bits\%1.png", 16 },
75 { ID_EXHAUST, "exhaust/exhaust.png", 1 },
76 { ID_ENERGY_POWERUP, "powerups/energy.png", 1 },
77// { ID_TELEPORT_POWERUP, "powerups/teleport%1.png", 12 },
78 { ID_BRAKE_POWERUP, "powerups/brake.png", 1 },
79 { ID_SHIELD_POWERUP, "powerups/shield.png", 1 },
80 { ID_SHOOT_POWERUP, "powerups/shoot.png", 1 },
81 { ID_SHIELD, "shield/shield\%1.png", 6 },
82 { 0, 0, 0 }
83};
84
85
86
87KAsteroidsView::KAsteroidsView( QWidget *parent, const char *name )
88 : QWidget( parent, name ),
89 field(200, 200),
90 view(&field,this)
91{
92 view.setVScrollBarMode( QScrollView::AlwaysOff );
93 view.setHScrollBarMode( QScrollView::AlwaysOff );
94 rocks.setAutoDelete( TRUE );
95 missiles.setAutoDelete( TRUE );
96 bits.setAutoDelete( TRUE );
97 powerups.setAutoDelete( TRUE );
98 exhaust.setAutoDelete( TRUE );
99
100 QPixmap pm( Resource::loadPixmap(IMG_BACKGROUND) );
101 field.setBackgroundPixmap( pm );
102
103 textSprite = new QCanvasText( &field );
104 QFont font( "helvetica", 14 );
105 textSprite->setFont( font );
106
107 shield = 0;
108 shieldOn = FALSE;
109 refreshRate = REFRESH_DELAY;
110
111 readSprites();
112
113 shieldTimer = new QTimer( this );
114 connect( shieldTimer, SIGNAL(timeout()), this, SLOT(hideShield()) );
115 mTimerId = -1;
116
117 shipPower = MAX_POWER_LEVEL;
118 vitalsChanged = TRUE;
119 can_destroy_powerups = FALSE;
120
121 mPaused = TRUE;
122}
123
124// - - -
125
126KAsteroidsView::~KAsteroidsView()
127{
128}
129
130// - - -
131
132void KAsteroidsView::reset()
133{
134 rocks.clear();
135 missiles.clear();
136 bits.clear();
137 powerups.clear();
138 exhaust.clear();
139
140 shotsFired = 0;
141 shotsHit = 0;
142
143 rockSpeed = 1.0;
144 powerupSpeed = 1.0;
145 mFrameNum = 0;
146 mPaused = FALSE;
147
148 ship->hide();
149 shield->hide();
150/*
151 if ( mTimerId >= 0 ) {
152 killTimer( mTimerId );
153 mTimerId = -1;
154 }
155*/
156}
157
158// - --
159
160void KAsteroidsView::newGame()
161{
162 if ( shieldOn )
163 {
164 shield->hide();
165 shieldOn = FALSE;
166 }
167 reset();
168 if ( mTimerId < 0 )
169 mTimerId = startTimer( REFRESH_DELAY );
170 emit updateVitals();
171}
172
173// - - -
174
175void KAsteroidsView::endGame()
176{
177}
178
179void KAsteroidsView::pause( bool p )
180{
181 if ( !mPaused && p ) {
182 if ( mTimerId >= 0 ) {
183 killTimer( mTimerId );
184 mTimerId = -1;
185 }
186 } else if ( mPaused && !p )
187 mTimerId = startTimer( REFRESH_DELAY );
188 mPaused = p;
189}
190
191// - - -
192
193void KAsteroidsView::newShip()
194{
195 ship->move( field.width()/2, field.height()/2, 0 );
196 shield->move( field.width()/2, field.height()/2, 0 );
197 ship->setVelocity( 0.0, 0.0 );
198 shipDx = 0;
199 shipDy = 0;
200 shipAngle = 0;
201 rotateL = FALSE;
202 rotateR = FALSE;
203 thrustShip = FALSE;
204 shootShip = FALSE;
205 brakeShip = FALSE;
206 teleportShip = FALSE;
207 shieldOn = TRUE;
208 shootDelay = 0;
209 shipPower = MAX_POWER_LEVEL;
210 rotateRate = ROTATE_RATE;
211 rotateSlow = 0;
212
213 mBrakeCount = 0;
214 mTeleportCount = 0;
215 mShootCount = 0;
216
217 ship->show();
218 shield->show();
219 mShieldCount = 1; // just in case the ship appears on a rock.
220 shieldTimer->start( 1000, TRUE );
221}
222
223void KAsteroidsView::setShield( bool s )
224{
225 if ( shieldTimer->isActive() && !s ) {
226 shieldTimer->stop();
227 hideShield();
228 } else {
229 shieldOn = s && mShieldCount;
230 }
231}
232
233void KAsteroidsView::brake( bool b )
234{
235 if ( mBrakeCount )
236 {
237 if ( brakeShip && !b )
238 {
239 rotateL = FALSE;
240 rotateR = FALSE;
241 thrustShip = FALSE;
242 rotateRate = ROTATE_RATE;
243 }
244
245 brakeShip = b;
246 }
247}
248
249// - - -
250
251void KAsteroidsView::readSprites()
252{
253 QString sprites_prefix = Resource::findPixmap( IMG_BACKGROUND );
254 int sep = sprites_prefix.findRev( "/" );
255
256 sprites_prefix.truncate( sep );
257
258 int i = 0;
259 while ( kas_animations[i].id )
260 {
261 animation.insert( kas_animations[i].id,
262 new QCanvasPixmapArray( sprites_prefix + "/" + kas_animations[i].path,
263 kas_animations[i].frames ) );
264 i++;
265 }
266
267 ship = new QCanvasSprite( animation[ID_SHIP], &field );
268 ship->hide();
269
270 shield = new KShield( animation[ID_SHIELD], &field );
271 shield->hide();
272}
273
274// - - -
275
276void KAsteroidsView::addRocks( int num )
277{
278 for ( int i = 0; i < num; i++ )
279 {
280 KRock *rock = new KRock( animation[ID_ROCK_MEDIUM], &field,
281 ID_ROCK_MEDIUM, randInt(2), randInt(2) ? -1 : 1 );
282 double dx = (2.0 - randDouble()*4.0) * rockSpeed;
283 double dy = (2.0 - randDouble()*4.0) * rockSpeed;
284 rock->setVelocity( dx, dy );
285 rock->setFrame( randInt( rock->frameCount() ) );
286 if ( dx > 0 )
287 {
288 if ( dy > 0 )
289 rock->move( 5, 5, 0 );
290 else
291 rock->move( 5, field.height() - 25, 0 );
292 }
293 else
294 {
295 if ( dy > 0 )
296 rock->move( field.width() - 25, 5, 0 );
297 else
298 rock->move( field.width() - 25, field.height() - 25, 0 );
299 }
300 rock->show( );
301 rocks.append( rock );
302 }
303}
304
305// - - -
306
307void KAsteroidsView::showText( const QString &text, const QColor &color, bool scroll )
308{
309 textSprite->setTextFlags( AlignLeft | AlignVCenter );
310 textSprite->setText( text );
311 textSprite->setColor( color );
312
313 if ( scroll ) {
314 textSprite->move( (field.width()-textSprite->boundingRect().width()) / 2,
315 -textSprite->boundingRect().height() );
316 textDy = TEXT_SPEED;
317 } else {
318 textSprite->move( (field.width()-textSprite->boundingRect().width()) / 2,
319 (field.height()-textSprite->boundingRect().height()) / 2 );
320 textDy = 0;
321 }
322 textSprite->show();
323}
324
325// - - -
326
327void KAsteroidsView::hideText()
328{
329 textDy = -TEXT_SPEED;
330}
331
332// - - -
333
334void KAsteroidsView::resizeEvent(QResizeEvent* event)
335{
336 QWidget::resizeEvent(event);
337 field.resize(width()-4, height()-4);
338 view.resize(width(),height());
339}
340
341// - - -
342
343void KAsteroidsView::timerEvent( QTimerEvent * )
344{
345 field.advance();
346
347 QCanvasSprite *rock;
348
349 // move rocks forward
350 for ( rock = rocks.first(); rock; rock = rocks.next() ) {
351 ((KRock *)rock)->nextFrame();
352 wrapSprite( rock );
353 }
354
355 wrapSprite( ship );
356
357 // check for missile collision with rocks.
358 processMissiles();
359
360 // these are generated when a ship explodes
361 for ( KBit *bit = bits.first(); bit; bit = bits.next() )
362 {
363 if ( bit->expired() )
364 {
365 bits.removeRef( bit );
366 }
367 else
368 {
369 bit->growOlder();
370 bit->setFrame( ( bit->frame()+1 ) % bit->frameCount() );
371 }
372 }
373
374 for ( KExhaust *e = exhaust.first(); e; e = exhaust.next() )
375 exhaust.removeRef( e );
376
377 // move / rotate ship.
378 // check for collision with a rock.
379 processShip();
380
381 // move powerups and check for collision with player and missiles
382 processPowerups();
383
384 if ( textSprite->visible() )
385 {
386 if ( textDy < 0 &&
387 textSprite->boundingRect().y() <= -textSprite->boundingRect().height() ) {
388 textSprite->hide();
389 } else {
390 textSprite->moveBy( 0, textDy );
391 }
392 if ( textSprite->boundingRect().y() > (field.height()-textSprite->boundingRect().height())/2 )
393 textDy = 0;
394 }
395
396 if ( vitalsChanged && !(mFrameNum % 10) ) {
397 emit updateVitals();
398 vitalsChanged = FALSE;
399 }
400
401 mFrameNum++;
402}
403
404void KAsteroidsView::wrapSprite( QCanvasItem *s )
405{
406 int x = int(s->x() + s->boundingRect().width() / 2);
407 int y = int(s->y() + s->boundingRect().height() / 2);
408
409 if ( x > field.width() )
410 s->move( s->x() - field.width(), s->y() );
411 else if ( x < 0 )
412 s->move( field.width() + s->x(), s->y() );
413
414 if ( y > field.height() )
415 s->move( s->x(), s->y() - field.height() );
416 else if ( y < 0 )
417 s->move( s->x(), field.height() + s->y() );
418}
419
420// - - -
421
422void KAsteroidsView::rockHit( QCanvasItem *hit )
423{
424 KPowerup *nPup = 0;
425 int rnd = static_cast<int>(randDouble()*30.0) % 30;
426 switch( rnd )
427 {
428 case 4:
429 case 5:
430 nPup = new KPowerup( animation[ID_ENERGY_POWERUP], &field,
431 ID_ENERGY_POWERUP );
432 break;
433 case 10:
434// nPup = new KPowerup( animation[ID_TELEPORT_POWERUP], &field,
435// ID_TELEPORT_POWERUP );
436 break;
437 case 15:
438 nPup = new KPowerup( animation[ID_BRAKE_POWERUP], &field,
439 ID_BRAKE_POWERUP );
440 break;
441 case 20:
442 nPup = new KPowerup( animation[ID_SHIELD_POWERUP], &field,
443 ID_SHIELD_POWERUP );
444 break;
445 case 24:
446 case 25:
447 nPup = new KPowerup( animation[ID_SHOOT_POWERUP], &field,
448 ID_SHOOT_POWERUP );
449 break;
450 }
451 if ( nPup )
452 {
453 double r = 0.5 - randDouble();
454 nPup->move( hit->x(), hit->y(), 0 );
455 nPup->setVelocity( hit->xVelocity() + r, hit->yVelocity() + r );
456 nPup->show( );
457 powerups.append( nPup );
458 }
459
460 if ( hit->rtti() == ID_ROCK_LARGE || hit->rtti() == ID_ROCK_MEDIUM )
461 {
462 // break into smaller rocks
463 double addx[4] = { 1.0, 1.0, -1.0, -1.0 };
464 double addy[4] = { -1.0, 1.0, -1.0, 1.0 };
465
466 double dx = hit->xVelocity();
467 double dy = hit->yVelocity();
468
469 double maxRockSpeed = MAX_ROCK_SPEED * rockSpeed;
470 if ( dx > maxRockSpeed )
471 dx = maxRockSpeed;
472 else if ( dx < -maxRockSpeed )
473 dx = -maxRockSpeed;
474 if ( dy > maxRockSpeed )
475 dy = maxRockSpeed;
476 else if ( dy < -maxRockSpeed )
477 dy = -maxRockSpeed;
478
479 QCanvasSprite *nrock;
480
481 for ( int i = 0; i < 4; i++ )
482 {
483 double r = rockSpeed/2 - randDouble()*rockSpeed;
484 if ( hit->rtti() == ID_ROCK_LARGE )
485 {
486 nrock = new KRock( animation[ID_ROCK_MEDIUM], &field,
487 ID_ROCK_MEDIUM, randInt(2), randInt(2) ? -1 : 1 );
488 emit rockHit( 0 );
489 }
490 else
491 {
492 nrock = new KRock( animation[ID_ROCK_SMALL], &field,
493 ID_ROCK_SMALL, randInt(2), randInt(2) ? -1 : 1 );
494 emit rockHit( 1 );
495 }
496
497 nrock->move( hit->x(), hit->y(), 0 );
498 nrock->setVelocity( dx+addx[i]*rockSpeed+r, dy+addy[i]*rockSpeed+r );
499 nrock->setFrame( randInt( nrock->frameCount() ) );
500 nrock->show( );
501 rocks.append( nrock );
502 }
503 }
504 else if ( hit->rtti() == ID_ROCK_SMALL )
505 emit rockHit( 2 );
506 rocks.removeRef( (QCanvasSprite *)hit );
507 if ( rocks.count() == 0 )
508 emit rocksRemoved();
509}
510
511void KAsteroidsView::reducePower( int val )
512{
513 shipPower -= val;
514 if ( shipPower <= 0 )
515 {
516 shipPower = 0;
517 thrustShip = FALSE;
518 if ( shieldOn )
519 {
520 shieldOn = FALSE;
521 shield->hide();
522 }
523 }
524 vitalsChanged = TRUE;
525}
526
527void KAsteroidsView::addExhaust( double x, double y, double dx,
528 double dy, int count )
529{
530 for ( int i = 0; i < count; i++ )
531 {
532 KExhaust *e = new KExhaust( animation[ID_EXHAUST], &field );
533 e->move( x + 2 - randDouble()*4, y + 2 - randDouble()*4 );
534 e->setVelocity( dx, dy );
535 e->show( );
536 exhaust.append( e );
537 }
538}
539
540void KAsteroidsView::processMissiles()
541{
542 KMissile *missile;
543
544 // if a missile has hit a rock, remove missile and break rock into smaller
545 // rocks or remove completely.
546 QPtrListIterator<KMissile> it(missiles);
547
548 for ( ; it.current(); ++it )
549 {
550 missile = it.current();
551 missile->growOlder();
552
553 if ( missile->expired() )
554 {
555 missiles.removeRef( missile );
556 continue;
557 }
558
559 wrapSprite( missile );
560
561 QCanvasItemList hits = missile->collisions( TRUE );
562 QCanvasItemList::Iterator hit;
563 for ( hit = hits.begin(); hit != hits.end(); ++hit )
564 {
565 if ( (*hit)->rtti() >= ID_ROCK_LARGE &&
566 (*hit)->rtti() <= ID_ROCK_SMALL )
567 {
568 shotsHit++;
569 rockHit( *hit );
570 missiles.removeRef( missile );
571 break;
572 }
573 }
574 }
575}
576
577// - - -
578
579void KAsteroidsView::processShip()
580{
581 if ( ship->visible() )
582 {
583 if ( shieldOn )
584 {
585 shield->show();
586 reducePower( SHIELD_ON_COST );
587 static int sf = 0;
588 sf++;
589
590 if ( sf % 2 )
591 shield->setFrame( (shield->frame()+1) % shield->frameCount() );
592 shield->move( ship->x() - 5, ship->y() - 5 );
593
594 QCanvasItemList hits = shield->collisions( TRUE );
595 QCanvasItemList::Iterator it;
596 for ( it = hits.begin(); it != hits.end(); ++it )
597 {
598 if ( (*it)->rtti() >= ID_ROCK_LARGE &&
599 (*it)->rtti() <= ID_ROCK_SMALL )
600 {
601 int factor;
602 switch ( (*it)->rtti() )
603 {
604 case ID_ROCK_LARGE:
605 factor = 3;
606 break;
607
608 case ID_ROCK_MEDIUM:
609 factor = 2;
610 break;
611
612 default:
613 factor = 1;
614 }
615
616 if ( factor > mShieldCount )
617 {
618 // shield not strong enough
619 shieldOn = FALSE;
620 break;
621 }
622 rockHit( *it );
623 // the more shields we have the less costly
624 reducePower( factor * (SHIELD_HIT_COST - mShieldCount*2) );
625 }
626 }
627 }
628
629 if ( !shieldOn )
630 {
631 shield->hide();
632 QCanvasItemList hits = ship->collisions( TRUE );
633 QCanvasItemList::Iterator it;
634 for ( it = hits.begin(); it != hits.end(); ++it )
635 {
636 if ( (*it)->rtti() >= ID_ROCK_LARGE &&
637 (*it)->rtti() <= ID_ROCK_SMALL )
638 {
639 KBit *bit;
640 for ( int i = 0; i < 8; i++ )
641 {
642 bit = new KBit( animation[ID_BIT], &field );
643 bit->move( ship->x() + 5 - randDouble() * 10,
644 ship->y() + 5 - randDouble() * 10,
645 randInt(bit->frameCount()) );
646 bit->setVelocity( 1-randDouble()*2,
647 1-randDouble()*2 );
648 bit->setDeath( 60 + randInt(60) );
649 bit->show( );
650 bits.append( bit );
651 }
652 ship->hide();
653 shield->hide();
654 emit shipKilled();
655 break;
656 }
657 }
658 }
659
660
661 if ( rotateSlow )
662 rotateSlow--;
663
664 if ( rotateL )
665 {
666 shipAngle -= rotateSlow ? 1 : rotateRate;
667 if ( shipAngle < 0 )
668 shipAngle += SHIP_STEPS;
669 }
670
671 if ( rotateR )
672 {
673 shipAngle += rotateSlow ? 1 : rotateRate;
674 if ( shipAngle >= SHIP_STEPS )
675 shipAngle -= SHIP_STEPS;
676 }
677
678 double angle = shipAngle * PI_X_2 / SHIP_STEPS;
679 double cosangle = cos( angle );
680 double sinangle = sin( angle );
681
682 if ( brakeShip )
683 {
684 thrustShip = FALSE;
685 rotateL = FALSE;
686 rotateR = FALSE;
687 rotateRate = ROTATE_RATE;
688 if ( fabs(shipDx) < 2.5 && fabs(shipDy) < 2.5 )
689 {
690 shipDx = 0.0;
691 shipDy = 0.0;
692 ship->setVelocity( shipDx, shipDy );
693 brakeShip = FALSE;
694 }
695 else
696 {
697 double motionAngle = atan2( -shipDy, -shipDx );
698 if ( angle > M_PI )
699 angle -= PI_X_2;
700 double angleDiff = angle - motionAngle;
701 if ( angleDiff > M_PI )
702 angleDiff = PI_X_2 - angleDiff;
703 else if ( angleDiff < -M_PI )
704 angleDiff = PI_X_2 + angleDiff;
705 double fdiff = fabs( angleDiff );
706 if ( fdiff > 0.08 )
707 {
708 if ( angleDiff > 0 )
709 rotateL = TRUE;
710 else if ( angleDiff < 0 )
711 rotateR = TRUE;
712 if ( fdiff > 0.6 )
713 rotateRate = mBrakeCount + 1;
714 else if ( fdiff > 0.4 )
715 rotateRate = 2;
716 else
717 rotateRate = 1;
718
719 if ( rotateRate > 5 )
720 rotateRate = 5;
721 }
722 else if ( fabs(shipDx) > 1 || fabs(shipDy) > 1 )
723 {
724 thrustShip = TRUE;
725 // we'll make braking a bit faster
726 shipDx += cosangle/6 * (mBrakeCount - 1);
727 shipDy += sinangle/6 * (mBrakeCount - 1);
728 reducePower( BRAKE_ON_COST );
729 addExhaust( ship->x() + 10 - cosangle*11,
730 ship->y() + 10 - sinangle*11,
731 shipDx-cosangle, shipDy-sinangle,
732 mBrakeCount+1 );
733 }
734 }
735 }
736
737 if ( thrustShip )
738 {
739 // The ship has a terminal velocity, but trying to go faster
740 // still uses fuel (can go faster diagonally - don't care).
741 double thrustx = cosangle/8;
742 double thrusty = sinangle/8;
743 if ( fabs(shipDx + thrustx) < MAX_SHIP_SPEED )
744 shipDx += thrustx;
745 if ( fabs(shipDy + thrusty) < MAX_SHIP_SPEED )
746 shipDy += thrusty;
747 ship->setVelocity( shipDx, shipDy );
748 reducePower( 1 );
749 addExhaust( ship->x() + 10 - cosangle*10,
750 ship->y() + 10 - sinangle*10,
751 shipDx-cosangle, shipDy-sinangle, 3 );
752 }
753
754 ship->setFrame( shipAngle >> 1 );
755
756 if ( shootShip )
757 {
758 if ( !shootDelay && (int)missiles.count() < mShootCount + 2 )
759 {
760 KMissile *missile = new KMissile( animation[ID_MISSILE], &field );
761 missile->move( 11+ship->x()+cosangle*11,
762 11+ship->y()+sinangle*11, 0 );
763 missile->setVelocity( shipDx + cosangle*MISSILE_SPEED,
764 shipDy + sinangle*MISSILE_SPEED );
765 missile->show( );
766 missiles.append( missile );
767 shotsFired++;
768 reducePower( 1 );
769
770 shootDelay = 5;
771 }
772
773 if ( shootDelay )
774 shootDelay--;
775 }
776
777 if ( teleportShip )
778 {
779 int ra = rand() % 10;
780 if( ra == 0 )
781 ra += rand() % 20;
782 int xra = ra * 60 + ( (rand() % 20) * (rand() % 20) );
783 int yra = ra * 50 - ( (rand() % 20) * (rand() % 20) );
784 ship->move( xra, yra );
785 }
786
787 vitalsChanged = TRUE;
788 }
789}
790
791// - - -
792
793void KAsteroidsView::processPowerups()
794{
795 if ( !powerups.isEmpty() )
796 {
797 // if player gets the powerup remove it from the screen, if option
798 // "Can destroy powerups" is enabled and a missile hits the powerup
799 // destroy it
800
801 KPowerup *pup;
802 QPtrListIterator<KPowerup> it( powerups );
803
804 for( ; it.current(); ++it )
805 {
806 pup = it.current();
807 pup->growOlder();
808
809 if( pup->expired() )
810 {
811 powerups.removeRef( pup );
812 continue;
813 }
814
815 wrapSprite( pup );
816
817 QCanvasItemList hits = pup->collisions( TRUE );
818 QCanvasItemList::Iterator it;
819 for ( it = hits.begin(); it != hits.end(); ++it )
820 {
821 if ( (*it) == ship )
822 {
823 switch( pup->rtti() )
824 {
825 case ID_ENERGY_POWERUP:
826 shipPower += 150;
827 if ( shipPower > MAX_POWER_LEVEL )
828 shipPower = MAX_POWER_LEVEL;
829 break;
830 case ID_TELEPORT_POWERUP:
831 mTeleportCount++;
832 break;
833 case ID_BRAKE_POWERUP:
834 if ( mBrakeCount < MAX_BRAKES )
835 mBrakeCount++;
836 break;
837 case ID_SHIELD_POWERUP:
838 if ( mShieldCount < MAX_SHIELDS )
839 mShieldCount++;
840 break;
841 case ID_SHOOT_POWERUP:
842 if ( mShootCount < MAX_FIREPOWER )
843 mShootCount++;
844 break;
845 }
846
847 powerups.removeRef( pup );
848 vitalsChanged = TRUE;
849 }
850 else if ( (*it) == shield )
851 {
852 powerups.removeRef( pup );
853 }
854 else if ( (*it)->rtti() == ID_MISSILE )
855 {
856 if ( can_destroy_powerups )
857 {
858 powerups.removeRef( pup );
859 }
860 }
861 }
862 }
863 } // -- if( powerups.isEmpty() )
864}
865
866// - - -
867
868void KAsteroidsView::hideShield()
869{
870 shield->hide();
871 mShieldCount = 0;
872 shieldOn = FALSE;
873}
874
875double KAsteroidsView::randDouble()
876{
877 int v = rand();
878 return (double)v / (double)RAND_MAX;
879}
880
881int KAsteroidsView::randInt( int range )
882{
883 return rand() % range;
884}
diff --git a/noncore/games/qasteroids/view.h b/noncore/games/qasteroids/view.h
new file mode 100644
index 0000000..0a7902b
--- a/dev/null
+++ b/noncore/games/qasteroids/view.h
@@ -0,0 +1,156 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************//*
20 * KAsteroids - Copyright (c) Martin R. Jones 1997
21 *
22 * Part of the KDE project
23 */
24
25#ifndef __AST_VIEW_H__
26#define __AST_VIEW_H__
27
28#include <qwidget.h>
29#include <qlist.h>
30#include <qintdict.h>
31#include <qtimer.h>
32#include <qcanvas.h>
33#include "sprites.h"
34
35#define QPtrList QList
36#define QPtrListIterator QListIterator
37
38#define MAX_POWER_LEVEL 1000
39
40class KAsteroidsView : public QWidget
41{
42 Q_OBJECT
43public:
44 KAsteroidsView( QWidget *parent = 0, const char *name = 0 );
45 virtual ~KAsteroidsView();
46
47 int refreshRate;
48
49 void reset();
50 void setRockSpeed( double rs ) { rockSpeed = rs; }
51 void addRocks( int num );
52 void newGame();
53 void endGame();
54 void newShip();
55
56 void rotateLeft( bool r ) { rotateL = r; rotateSlow = 5; }
57 void rotateRight( bool r ) { rotateR = r; rotateSlow = 5; }
58 void thrust( bool t ) { thrustShip = t && shipPower > 0; }
59 void shoot( bool s ) { shootShip = s; shootDelay = 0; }
60 void setShield( bool s );
61 void teleport( bool te) { teleportShip = te && mTeleportCount; }
62 void brake( bool b );
63 void pause( bool p);
64
65 void showText( const QString &text, const QColor &color, bool scroll=TRUE );
66 void hideText();
67
68 int shots() const { return shotsFired; }
69 int hits() const { return shotsHit; }
70 int power() const { return shipPower; }
71
72 int teleportCount() const { return mTeleportCount; }
73 int brakeCount() const { return mBrakeCount; }
74 int shieldCount() const { return mShieldCount; }
75 int shootCount() const { return mShootCount; }
76
77signals:
78 void shipKilled();
79 void rockHit( int size );
80 void rocksRemoved();
81 void updateVitals();
82
83private slots:
84 void hideShield();
85
86protected:
87 void readSprites();
88 void wrapSprite( QCanvasItem * );
89 void rockHit( QCanvasItem * );
90 void reducePower( int val );
91 void addExhaust( double x, double y, double dx, double dy, int count );
92 void processMissiles();
93 void processShip();
94 void processPowerups();
95 void processShield();
96 double randDouble();
97 int randInt( int range );
98
99 virtual void resizeEvent( QResizeEvent *event );
100 virtual void timerEvent( QTimerEvent * );
101
102private:
103 QCanvas field;
104 QCanvasView view;
105 QIntDict<QCanvasPixmapArray> animation;
106 QPtrList<QCanvasSprite> rocks;
107 QPtrList<KMissile> missiles;
108 QPtrList<KBit> bits;
109 QPtrList<KExhaust> exhaust;
110 QPtrList<KPowerup> powerups;
111 KShield *shield;
112 QCanvasSprite *ship;
113 QCanvasText *textSprite;
114
115 bool rotateL;
116 bool rotateR;
117 bool thrustShip;
118 bool shootShip;
119 bool teleportShip;
120 bool brakeShip;
121 bool pauseShip;
122 bool shieldOn;
123
124 bool vitalsChanged;
125
126 int shipAngle;
127 int rotateSlow;
128 int rotateRate;
129 int shipPower;
130
131 int shotsFired;
132 int shotsHit;
133 int shootDelay;
134
135 int mBrakeCount;
136 int mShieldCount;
137 int mTeleportCount;
138 int mShootCount;
139
140 double shipDx;
141 double shipDy;
142
143 int textDy;
144 int mFrameNum;
145 bool mPaused;
146 int mTimerId;
147
148 double rockSpeed;
149 double powerupSpeed;
150
151 bool can_destroy_powerups;
152
153 QTimer *shieldTimer;
154};
155
156#endif