Back to index

supertuxkart  0.5+dfsg1
herring.cpp
Go to the documentation of this file.
00001 //  $Id: herring.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004 Steve Baker <sjbaker1@airmail.net>
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 "world.hpp"
00021 #include "herring.hpp"
00022 #include "kart.hpp"
00023 #include "scene.hpp"
00024 
00025 Herring::Herring(herringType _type, sgVec3* xyz, ssgEntity* model)
00026 {
00027     sgSetVec3(m_coord.hpr, 0.0f, 0.0f, 0.0f);
00028 
00029     sgCopyVec3(m_coord.xyz, *xyz);
00030     m_root   = new ssgTransform();
00031     m_root->ref();
00032     m_root->setTransform(&m_coord);
00033 
00034     m_rotate = new ssgTransform();
00035     m_rotate->ref();
00036     m_rotate->addKid(model);
00037     m_root->addKid(m_rotate);
00038     scene->add(m_root);
00039 
00040     m_type           = _type;
00041     m_eaten         = false;
00042     m_rotation       = 0.0f;
00043     m_time_to_return = 0.0f;  // not strictly necessary, see isEaten()
00044 }   // Herring
00045 
00046 //-----------------------------------------------------------------------------
00047 Herring::~Herring()
00048 {
00049     ssgDeRefDelete(m_root);
00050     ssgDeRefDelete(m_rotate);
00051 }   // ~Herring
00052 
00053 //-----------------------------------------------------------------------------
00054 void Herring::reset()
00055 {
00056     m_eaten         = false;
00057     m_time_to_return = 0.0f;
00058     m_root->setTransform(&m_coord);
00059 }   // reset
00060 
00061 //-----------------------------------------------------------------------------
00062 int Herring::hitKart(Kart* kart)
00063 {
00064     return sgDistanceSquaredVec3 ( kart->getCoord()->xyz, m_coord.xyz ) < 0.8f;
00065 }   // hitKart
00066 
00067 //-----------------------------------------------------------------------------
00068 void Herring::update(float delta)
00069 {
00070     if(m_eaten)
00071     {
00072         const float T = m_time_to_return - world->getTime();
00073         if ( T > 0 )
00074         {
00075             sgVec3 hell;
00076             sgCopyVec3(hell, m_coord.xyz);
00077 
00078             hell[2] = ( T > 1.0f ) ? -1000000.0f : m_coord.xyz[2] - T / 2.0f;
00079             m_root -> setTransform(hell);
00080         }
00081         else
00082         {
00083             m_eaten   = false;
00084             m_rotation = 0.0f;
00085             m_root -> setTransform(&m_coord);
00086         }   // T>0
00087 
00088     }
00089     else
00090     {   // not m_eaten
00091         sgCoord c = { { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } } ;
00092         c.hpr[0] = m_rotation;
00093         m_rotation += 180.0f*delta;
00094         m_rotate -> setTransform ( &c ) ;
00095     }
00096 }   // update
00097 
00098 //-----------------------------------------------------------------------------
00099 void Herring::isEaten()
00100 {
00101     m_eaten=true;
00102     m_time_to_return=world->getTime()+2.0f;
00103 }
00104