Back to index

supertuxkart  0.5+dfsg1
attachment.cpp
Go to the documentation of this file.
00001 //  $Id: attachment.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Joerg Henrichs
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include <plib/ssg.h>
00021 
00022 #include "attachment.hpp"
00023 #include "attachment_manager.hpp"
00024 #include "projectile_manager.hpp"
00025 #include "kart.hpp"
00026 #include "constants.hpp"
00027 #include "world.hpp"
00028 #include "sound_manager.hpp"
00029 #include "stk_config.hpp"
00030 #include "user_config.hpp"
00031 
00032 Attachment::Attachment(Kart* _kart)
00033 {
00034     m_type           = ATTACH_NOTHING;
00035     m_time_left      = 0.0;
00036     m_kart           = _kart;
00037     m_holder         = new ssgSelector();
00038     m_previous_owner = NULL;
00039     m_kart->getModelTransform()->addKid(m_holder);
00040 
00041     for(int i=ATTACH_PARACHUTE; i<=ATTACH_TINYTUX; i++)
00042     {
00043         ssgEntity *p=attachment_manager->getModel((attachmentType)i);
00044         m_holder->addKid(p);
00045     }
00046     m_holder->select(0);
00047 }   // Attachment
00048 
00049 //-----------------------------------------------------------------------------
00050 Attachment::~Attachment()
00051 {
00052     ssgDeRefDelete(m_holder);
00053 }   // ~Attachment
00054 
00055 //-----------------------------------------------------------------------------
00056 void Attachment::set(attachmentType _type, float time, Kart *current_kart)
00057 {
00058     m_holder->selectStep(_type);
00059     m_type           = _type;
00060     m_time_left      = time;
00061     m_previous_owner = current_kart;
00062 }   // set
00063 
00064 // -----------------------------------------------------------------------------
00065 void Attachment::hitGreenHerring()
00066 {
00067     if(user_config->m_profile) return;
00068     int random_attachment;
00069     float leftover_time   = 0.0f;
00070     switch(getType())   // If there already is an attachment, make it worse :)
00071     {
00072     case ATTACH_BOMB:  projectile_manager->newExplosion(m_kart->getCoord());
00073                        // Best solution would probably be to trigger the
00074                        // explosion, and then to attach a new, random
00075                        // attachment. Unfortunately, handleExplosion() is not
00076                        // really severe enough, and forceRescue() attaches
00077                        // tinytux, so that the new attachment is immediately lost.
00078                        // m_kart->handleExplosion(m_kart->getCoord()->xyz, true);
00079                        m_kart->handleExplosion(m_kart->getPos(), /*direct_hit*/ true);
00080                        clear();
00081                        random_attachment = rand()%3;
00082                        break;
00083     case ATTACH_ANVIL :// if the kart already has an anvil, attach a new anvil, 
00084                        // and increase the overall time 
00085                        random_attachment = 2;
00086                        leftover_time     = m_time_left;
00087                        break;
00088     case ATTACH_PARACHUTE:
00089                        random_attachment = 2;  // anvil
00090                        leftover_time     = m_time_left;
00091                        break;
00092     default:           random_attachment = rand()%3;
00093     }   // switch
00094 
00095     switch (random_attachment)
00096     {
00097     case 0: set( ATTACH_PARACHUTE, stk_config->m_parachute_time+leftover_time);
00098         m_initial_speed = m_kart->getSpeed();
00099         // if ( m_kart == m_kart[0] )
00100         //   sound -> playSfx ( SOUND_SHOOMF ) ;
00101         break ;
00102     case 1: set( ATTACH_BOMB, stk_config->m_bomb_time+leftover_time);
00103         // if ( m_kart == m_kart[0] )
00104         //   sound -> playSfx ( SOUND_SHOOMF ) ;
00105         break ;
00106     case 2: set( ATTACH_ANVIL, stk_config->m_anvil_time+leftover_time);
00107         // if ( m_kart == m_kart[0] )
00108         //   sound -> playSfx ( SOUND_SHOOMF ) ;
00109         // Reduce speed once (see description above), all other changes are
00110         // handled in Kart::updatePhysics
00111         m_kart->adjustSpeedWeight(stk_config->m_anvil_speed_factor);
00112         break ;
00113     }   // switch rand()%3
00114 }   // hitGreenHerring
00115 
00116 //-----------------------------------------------------------------------------
00117 //** Moves a bomb from kart FROM to kart TO.
00118 void Attachment::moveBombFromTo(Kart *from, Kart *to)
00119 {
00120     to->setAttachmentType(ATTACH_BOMB,
00121                           from->getAttachment()->getTimeLeft()+
00122                           stk_config->m_bomb_time_increase, from);
00123     from->getAttachment()->clear();
00124 }   // moveBombFromTo
00125 
00126 //-----------------------------------------------------------------------------
00127 void Attachment::update(float dt)
00128 {
00129     if(m_type==ATTACH_NOTHING) return;
00130     m_time_left -=dt;
00131 
00132     switch (m_type)
00133     {
00134     case ATTACH_PARACHUTE: // Partly handled in Kart::updatePhysics
00135                            // Otherwise: disable if a certain percantage of
00136                            // initial speed was lost
00137                            if(m_kart->getSpeed()<=
00138                                m_initial_speed*stk_config->m_parachute_done_fraction)
00139                            {
00140                                    m_time_left = -1;
00141                            }
00142                            break;
00143     case ATTACH_ANVIL:     // handled in Kart::updatePhysics
00144     case ATTACH_NOTHING:   // Nothing to do, but complete all cases for switch
00145     case ATTACH_MAX:       break;
00146     case ATTACH_BOMB:      if(m_time_left<=0.0) 
00147                            {
00148                                projectile_manager->newExplosion(m_kart->getCoord());
00149                                m_kart->handleExplosion(m_kart->getPos(), 
00150                                                        /*direct_hit*/ true);
00151                            }
00152                            break;
00153     case ATTACH_TINYTUX:   if(m_time_left<=0.0) m_kart->endRescue();
00154                            break;
00155     }   // switch
00156 
00157     // Detach attachment if its time is up.
00158     if ( m_time_left <= 0.0f)
00159     {
00160         if(m_type==ATTACH_ANVIL) 
00161         {
00162             // Resets the weight, and multiplies the velocity by 1.0, 
00163             // i.e. no change of velocity.
00164             m_kart->getAttachment()->clear();
00165             m_kart->adjustSpeedWeight(1.0f);
00166         }
00167         clear();
00168     }   // if m_time_left<0
00169 }   // update
00170 //-----------------------------------------------------------------------------