Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

man.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include "codes.h"
00022 #include "man.h"
00023 #include "base.h"
00024 
00025 #include <opie2/oresource.h>
00026 
00027 int mancount;
00028 
00029 Man::Man(QCanvas* canvas) :
00030     QCanvasSprite(0, canvas),
00031     splat("lose") // No tr
00032 {
00033     manarray = new QCanvasPixmapArray();
00034     QString m0 = Opie::Core::OResource::findPixmap("parashoot/man0001");
00035     m0.replace(QRegExp("0001"),"%1");
00036     manarray->readPixmaps(m0, 7);
00037     setSequence(manarray);
00038     setAnimated(true);
00039     mancount++;
00040     dead = false;
00041     start();
00042 }
00043 
00044 Man::Man(QCanvas* canvas, int x, int y) :
00045     QCanvasSprite(0, canvas),
00046     splat("bang") // No tr
00047 {
00048     manarray = new QCanvasPixmapArray();
00049     QString m0 = Opie::Core::OResource::findPixmap("parashoot/man0001");
00050     m0.replace(QString("0001"),"%1");
00051     manarray->readPixmaps(m0, 7);
00052     setSequence(manarray);
00053     move(x, y);
00054     setFrame(5);
00055     setZ(300);
00056     show();
00057 
00058     static bool first_time = TRUE;
00059     if (first_time) {
00060         first_time = FALSE;
00061         QTime midnight(0, 0, 0);
00062         srand(midnight.secsTo(QTime::currentTime()) );
00063     }
00064     int yfallspeed = 0;
00065     yfallspeed = (rand() % 3) + 1;
00066     setVelocity(0, yfallspeed);
00067 
00068     mancount++;
00069     dead = false;
00070 }
00071 int f = 0;
00072 
00073 void Man::advance(int phase)
00074 {
00075     QCanvasSprite::advance(phase);
00076     if (phase == 0) {
00077         checkCollision();
00078         if (dead) {
00079             if (count < 10) {
00080                 setFrame(6);
00081                 setVelocity(0,0);
00082                 count++;
00083             } else {
00084                 delete this;
00085                 return;
00086             }
00087         }
00088         if (y() > canvas()->height()-43) {
00089             setFrame(f%5);
00090             f++;
00091             move(x(), canvas()->height()-26);
00092             setVelocity(-2, 0);
00093         } else if (xVelocity() == -2) {
00094             //
00095             // There's been a resize event while this Man has
00096             // been on the ground.  Move the man back to the
00097             // new ground location.  This is not neat.
00098             //
00099             move(x(), canvas()->height()-26);
00100         }
00101     }
00102 }
00103 
00104 void Man::setInitialCoords()
00105 {
00106     static bool first_time = TRUE;
00107     if (first_time) {
00108         first_time = FALSE;
00109         QTime midnight(0, 0, 0);
00110         srand(midnight.secsTo(QTime::currentTime()) );
00111     }
00112     dx = rand() % (canvas()->width()-16);
00113     dy = -43;  //height of a man off the screen
00114 }
00115 
00116 //check if man has reached the base
00117 void Man::checkCollision()
00118 {
00119     if ( (x() < 23) && (y() == canvas()->height()-26)) {
00120        QCanvasItem* item;
00121        QCanvasItemList l=collisions(FALSE);
00122           for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
00123              item = *it;
00124              if ( (item->rtti()== 1800) && (item->collidesWith(this)) ) {
00125                  Base* base = (Base*) item;
00126                  base->damageBase();
00127                  start();
00128              }
00129           }
00130     }
00131 
00132     //
00133     // resize events may cause Man objects to appear
00134     // outside the screen.  Get rid of them if this
00135     // is the case.
00136     //
00137     if ((x() < 0) || (x() > canvas()->width())) {
00138         delete this;
00139         return;
00140     }
00141 }
00142 
00143 void Man::start()
00144 {
00145    setInitialCoords();
00146    move(dx, dy);
00147    setFrame(5);
00148    setZ(300);
00149    show();
00150 
00151    static bool first_time = TRUE;
00152    if (first_time) {
00153       first_time = FALSE;
00154       QTime midnight(0, 0, 0);
00155       srand(midnight.secsTo(QTime::currentTime()) );
00156    }
00157    int yfallspeed = 0;
00158    yfallspeed = (rand() % 3) + 1;
00159    setVelocity(0, yfallspeed);
00160 }
00161 
00162 void Man::done()
00163 {
00164    splat.play();
00165    count = 0;
00166    dead = true;
00167    setFrame(6);
00168 }
00169 
00170 int Man::getManCount()
00171 {
00172    return mancount;
00173 }
00174 
00175 void Man::setManCount(int count)
00176 {
00177     mancount = count;
00178 }
00179 
00180 
00181 int Man::rtti() const
00182 {
00183    return man_rtti;
00184 }
00185 
00186 Man::~Man()
00187 {
00188    mancount--;
00189 }

Generated on Sat Nov 5 16:17:23 2005 for OPIE by  doxygen 1.4.2