C:/DevKitPro/!MDuel/source/wp_magnet.cpp

Go to the documentation of this file.
00001 /*
00002  * Marshmallow Duel DS v2
00003  * Copyright © 2007 Sam Pospischil http://pospi.spadgos.com
00004  * 
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  */
00019 
00020 #include "wp_magnet.h"
00021 #include "player.h"
00022 #include "gameManager.h"
00023 #include "pickup.h"
00024 #include "pickupSpawner.h"
00025 #include "weaponIncludes.h"
00026 
00027 #include <math.h>
00028 
00029 wp_magnet::~wp_magnet()
00030 {
00031         if (isFiring() || wasFiring())
00032                 pawn->updateAnimation();
00033 }
00034 
00035 void wp_magnet::weaponTick()
00036 {       
00037         if ((!pawn->isOnGround() || pawn->isCrouched()) && isFiring())
00038                 stopFiring();
00039         
00040         if (!isFiring() && wasFiring())
00041                 pawn->updateAnimation();
00042                 
00043         Weapon::weaponTick();
00044 }
00045 
00046 void wp_magnet::weaponFireAction()
00047 {
00048         #ifdef __WITHSOUND
00049         if (!pawn->hasSoundLooping())
00050                 pawn->playSound(&pawn->gm->magnetLoop, true);
00051         #endif
00052         
00053         if (!wasFiring())
00054                 pawn->setAnim(64, 65, spriteObject::ANIMSPEED);
00055         pawn->setInputInterrupt(1);
00056         
00057         //use global pickups array for a much faster loop than if we went through all gameSprites
00058         for (unsigned int i=0; i < pawn->gm->pickups.size(); ++i)
00059         {
00060                 Pickup *p = pawn->gm->pickups[i];
00061                 if (p->getLifetime() == -1)                     //disabled so dont touch it
00062                         continue;
00063 
00064                 s32 magnetx = pawn->getx();
00065                 s32 magnety = pawn->getTop();
00066                 s32 pickupx = p->getx();
00067                 s32 pickupy = p->gety();
00068                 
00069                 s32 dist = (s32)sqrt((pickupx-magnetx)*(pickupx-magnetx)+(pickupy-magnety)*(pickupy-magnety));
00070                 
00071                 p->setRawSpeed(p->getRawvx()-((pickupx-magnetx)/(dist/PULLSTR)), p->getRawvy()-((pickupy-magnety)/(dist/PULLSTR)));
00072         }
00073         
00074         //now for player weapon grabbing
00075         Player *p;
00076         if (pawn == pawn->gm->player1)
00077                 p = pawn->gm->player2;
00078         else
00079                 p = pawn->gm->player1;
00080         if (p->pointCollision(p->getx(), pawn->gety()) && p->getWeapon() != NULL &&
00081                 (pawn->getFlippedh() ? pawn->getx() > p->getx() : pawn->getx() < p->getx()))
00082                 popOutPawnWeapon(p);
00083 }
00084 
00085 void wp_magnet::stopFiring()
00086 {
00087         Weapon::stopFiring();
00088         #ifdef __WITHSOUND
00089         if (pawn->hasSoundLooping())
00090                 pawn->stopSound();
00091         #endif
00092 }
00093 
00094 void wp_magnet::popOutPawnWeapon(Player *p)
00095 {
00096         if (p->getWeapon() == NULL)
00097                 return;
00098         u8 lifetime = PA_RandMinMax(pickupSpawner::PICKUPLIFEMIN, pickupSpawner::PICKUPLIFEMAX);
00099         s8 offset = 13;
00100         s16 speed = Player::WALKSPEED>>8;
00101         if (p->getx() > pawn->getx())
00102         {
00103                 offset *= -1;
00104                 speed *= -1;
00105         }
00106         
00107         Pickup *pk = new Pickup(pawn->gm, NULL, lifetime, p->getx()+offset, p->gety(), 
00108                                                         p->getWeapon()->getType(), p->gm->getNextAvailablePickupGFXSet());
00109 
00110         if (pk != NULL)
00111         {
00112                 pk->setSpeed(speed, 0);
00113         }
00114         p->clearWeapon();
00115 }

Generated on Tue Mar 13 23:27:53 2007 for MDuel DS by  doxygen 1.5.1-p1