src/map/wind.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  *  Refresh du vent
00020  *****************************************************************************/
00021 
00022 #include "wind.h"
00023 #include "camera.h"
00024 #include "../game/config.h"
00025 #include "../game/time.h"
00026 #include "../graphic/sprite.h"
00027 #include "../include/action_handler.h"
00028 #include "../include/app.h"
00029 #include "../map/map.h"
00030 #include "../map/maps_list.h"
00031 #include "../tool/random.h"
00032 #include "../tool/resource_manager.h"
00033 #include "../tool/xml_document.h"
00034 #include "../interface/interface.h"
00035 
00036 const uint MAX_WIND_OBJECTS = 200;
00037 const uint BARRE_LARG = 80;
00038 const uint BARRE_HAUT = 10;
00039 const double force = 5; // Max wind strength in m/(sec*sec)
00040 const uint bar_speed = 20;
00041 
00042 Wind wind;
00043 
00044 WindParticle::WindParticle(std::string &xml_file) :
00045   PhysicalObj("wind",xml_file)
00046 {
00047   SetCollisionModel(true, false, false);
00048 
00049   sprite = resource_manager.LoadSprite( ActiveMap().ResProfile(), "wind_particle");
00050 //  if(sprite->GetFrameCount()==1)
00051 //    sprite->cache.EnableLastFrameCache();
00052   sprite->SetCurrentFrame ( randomObj.GetLong(0, sprite->GetFrameCount()-1));
00053 
00054   double mass, wind_factor ;
00055 
00056   //Mass = mass_mean + or - 25%
00057   mass = GetMass();
00058   mass *= (1.0 + randomObj.GetLong(-100, 100)/400.0);
00059   SetMass (mass);
00060   SetSize( sprite->GetSize() );
00061   wind_factor = GetWindFactor() ;
00062   wind_factor *= (1.0 + randomObj.GetLong(-100, 100)/400.0);
00063   SetWindFactor(wind_factor);
00064   StartMoving();
00065   SetAirResistFactor(GetAirResistFactor() * (1.0 + randomObj.GetLong(-100, 100)/400.0));
00066 
00067   // Fixe le rectangle de test
00068   int dx = 0 ;
00069   int dy = 0 ;
00070   SetTestRect (dx, dx, dy, dy);
00071 
00072   m_allow_negative_y = true;
00073 }
00074 
00075 void WindParticle::Refresh()
00076 {
00077   sprite->Update();
00078   UpdatePosition();
00079 
00080   // Flip the sprite if needed and if the direction of wind changed
00081   if(ActiveMap().wind.need_flip)
00082   {
00083     Point2d speed;
00084     GetSpeedXY(speed);
00085     float scale_x, scale_y;
00086     sprite->GetScaleFactors( scale_x, scale_y);
00087     if((speed.x<0 && scale_x>0)
00088     || (speed.x>0 && scale_x<0))
00089     {
00090       scale_x=-scale_x;
00091       sprite->Scale( scale_x, scale_y);
00092     }
00093   }
00094   // Put particles inside of the camera view
00095   // (there is no point in computing particle out of the camera!)
00096   int x = GetX();
00097   int y = GetY();
00098 
00099   if(GetX() > camera.GetPositionX() + camera.GetSizeX())
00100     x = camera.GetPositionX() - GetWidth() + 1;
00101 
00102   if(GetX() + GetWidth() < camera.GetPositionX() )
00103     x = camera.GetPositionX() + camera.GetSizeX() - 1;
00104 
00105   if(GetY() > camera.GetPositionY() + camera.GetSizeY())
00106     y = camera.GetPositionY() - GetHeight() + 1;
00107 
00108   if(GetY() + GetHeight() < camera.GetPositionY() )
00109     y = camera.GetPositionY() + camera.GetSizeY() - 1;
00110 
00111   m_alive = ALIVE;
00112 
00113   if(x!=GetX() || y!=GetY())
00114   {
00115     StartMoving();
00116     SetXY( Point2i(x, y) );
00117   }
00118 }
00119 
00120 void WindParticle::Draw()
00121 {
00122   sprite->Draw(GetPosition());
00123 }
00124 
00125 void WindParticle::Resize(double size)
00126 {
00127   size=0.5+size/2.0;
00128   sprite->Scale( size,size);
00129   sprite->SetAlpha( size);
00130 }
00131 
00132 //---------------------------------------------------
00133 
00134 Wind::Wind(){
00135   m_val = m_nv_val = 0;
00136 }
00137 
00138 void Wind::Reset(){
00139   m_last_move = 0;
00140   m_last_part_mvt = 0;
00141   m_val = m_nv_val = 0;
00142   Interface::GetInstance()->UpdateWindIndicator(m_val);
00143 
00144   particles.clear();
00145 
00146   if (!Config::GetInstance()->GetDisplayWindParticles())
00147     return ;
00148 
00149   uint nb = ActiveMap().wind.nb_sprite;
00150 
00151   if(!nb) return;
00152 
00153   std::string config_file = ActiveMap().m_directory + PATH_SEPARATOR + "config.xml";
00154 
00155   for (uint i=0; i<nb; ++i){
00156     WindParticle tmp = WindParticle(config_file);
00157     tmp.Resize( (double)i / nb );
00158     particles.push_back( tmp );
00159   }
00160   RandomizeParticlesPos();
00161 }
00162 
00163 double Wind::GetStrength() const{
00164   return m_nv_val * force / 100.0;
00165 }
00166 
00167 void Wind::ChooseRandomVal(){
00168   int val = randomObj.GetLong(-100, 100);
00169   ActionHandler::GetInstance()->NewAction (new Action(Action::ACTION_WIND, val));
00170 }
00171 
00172 void Wind::SetVal(long val){
00173   m_nv_val = val;
00174 }
00175 
00176 void Wind::DrawParticles(){
00177   iterator it=particles.begin(), end=particles.end();
00178   for (; it != end; ++it) it -> Draw();
00179 }
00180 
00181 void Wind::Refresh(){
00182   if(m_last_move + bar_speed < Time::GetInstance()->Read()){
00183     if(m_val>m_nv_val)
00184       --m_val;
00185     else
00186     if(m_val<m_nv_val)
00187       ++m_val;
00188     m_last_move = Time::GetInstance()->Read();
00189     Interface::GetInstance()->UpdateWindIndicator(m_val);
00190   }
00191 
00192   iterator it=particles.begin(), end=particles.end();
00193   for (; it != end; ++it) it -> Refresh();
00194 }
00195 
00196 void Wind::RandomizeParticlesPos()
00197 {
00198   iterator it=particles.begin(), end=particles.end();
00199   for (; it != end; ++it)
00200   {
00201     if(!camera.IsVisible(*it))
00202       it -> SetXY( Point2i( randomObj.GetLong(camera.GetPositionX(), camera.GetPositionX()+camera.GetSizeX()),
00203                             randomObj.GetLong(camera.GetPositionY(), camera.GetPositionY()+camera.GetSizeY())));
00204   }
00205 }

Generated on Mon Jan 1 13:10:58 2007 for Wormux by  doxygen 1.4.7