src/team/team_energy.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  * energy bar of each team
00020  *****************************************************************************/
00021 
00022 #include "team_energy.h"
00023 #include <sstream>
00024 #include <math.h>
00025 #include "../map/camera.h"
00026 #include "../map/map.h"
00027 #include "../game/time.h"
00028 #include "../graphic/text.h"
00029 #include "team.h"
00030 #include "teams_list.h"
00031 #include "../include/app.h"
00032 
00033 const uint BAR_WIDTH = 13;
00034 const uint BAR_SPACING = 30;
00035 const uint BAR_HEIGHT = 50;
00036 const uint SPACING = 3;
00037 
00038 const uchar ALPHA = 127;
00039 const uchar BACK_ALPHA = 0;
00040 
00041 const float MOVE_DURATION = 750.0;
00042 
00043 TeamEnergy::TeamEnergy(Team * _team)
00044 {
00045   dx = 0;
00046   dy = 0;
00047   move_start_time = 0;
00048   max_value = 0;
00049   status = EnergyStatusOK;
00050   energy_bar.InitPos(0, 0, BAR_WIDTH, BAR_HEIGHT);
00051 
00052   energy_bar.SetBorderColor(Color(255, 255, 255, ALPHA));
00053   energy_bar.SetBackgroundColor(Color(255*6/10, 255*6/10, 255*6/10, BACK_ALPHA));
00054 
00055   team = _team;
00056   icon = NULL;
00057   t_team_energy = new Text("None", black_color, Font::GetInstance(Font::FONT_SMALL), false);
00058 }
00059 
00060 TeamEnergy::~TeamEnergy()
00061 {
00062   if(icon) delete icon;
00063   if(t_team_energy) delete t_team_energy;
00064 }
00065 
00066 void TeamEnergy::Config(uint _current_energy,
00067                         uint _max_energy)
00068 {
00069   max_value = _max_energy;
00070 
00071   value = _current_energy;
00072   new_value = _current_energy;
00073   assert(max_value != 0)
00074       energy_bar.InitVal(value, 0, max_value, ProgressBar::PROG_BAR_VERTICAL);
00075   icon = new Sprite(team->flag);
00076   icon->Scale(0.8,0.8);
00077 }
00078 
00079 void TeamEnergy::Refresh()
00080 {
00081   switch(status)
00082   {
00083     // energy value from one team have changed
00084     case EnergyStatusValueChange:
00085       if(new_value > value)
00086         value = new_value;
00087       if(value > new_value)
00088         --value;
00089       if(value == new_value)
00090         status = EnergyStatusWait;
00091       break;
00092 
00093     // ranking is changing
00094     case EnergyStatusRankChange:
00095       Move();
00096       break;
00097 
00098     // Currently no move
00099     case EnergyStatusOK:
00100       if( value != new_value && !IsMoving())
00101         status = EnergyStatusValueChange;
00102       else
00103         if( rank != new_rank )
00104           status = EnergyStatusRankChange;
00105       break;
00106 
00107     // This energy bar wait others bar before moving
00108     case EnergyStatusWait:
00109       break;
00110   }
00111 }
00112 
00113 void TeamEnergy::Draw(const Point2i& pos)
00114 {
00115   energy_bar.Actu(value);
00116   Point2i tmp = pos + Point2i(BAR_SPACING / 2 + rank * (BAR_WIDTH + BAR_SPACING) + dx, dy);
00117   energy_bar.DrawXY(tmp);
00118   icon->DrawXY(tmp + Point2i(energy_bar.GetWidth() / 2, 0));
00119 }
00120 
00121 void TeamEnergy::SetValue(uint new_energy)
00122 {
00123   new_value = new_energy;
00124 }
00125 
00126 void TeamEnergy::SetRanking(uint _rank)
00127 {
00128   rank = _rank;
00129   new_rank = _rank;
00130 }
00131 
00132 void TeamEnergy::NewRanking(uint _new_rank)
00133 {
00134   new_rank = _new_rank;
00135 }
00136 
00137 // Move energy bar (change in ranking)
00138 void TeamEnergy::Move()
00139 {
00140   if( value != new_value && !IsMoving()) {
00141     // Other energy bar are moving so waiting for others to move
00142     status = EnergyStatusWait;
00143     return;
00144   }
00145 
00146   if( rank == new_rank && !IsMoving()) {
00147     // Others energy bar are moving
00148     status = EnergyStatusWait;
00149     return;
00150   }
00151 
00152   // teams ranking have changed
00153   Time * global_time = Time::GetInstance();
00154   if( rank != new_rank )
00155   {
00156     if(move_start_time == 0)
00157       move_start_time = global_time->Read();
00158 
00159     dx = (int)(((float)new_rank - rank) * (BAR_WIDTH + BAR_SPACING) * ((global_time->Read() - move_start_time) / MOVE_DURATION));
00160 
00161     // displacement in arc of circle only when losing place ranking
00162     if( new_rank > rank ) {
00163       dy = (int)((BAR_HEIGHT * ((float)rank - new_rank)) * 0.5 *
00164            sin( M_PI * ((global_time->Read() - move_start_time) / MOVE_DURATION)));
00165     } else {
00166       dy = (int)((BAR_HEIGHT * ((float)rank - new_rank)) * 0.5 *
00167           sin( M_PI * ((global_time->Read() - move_start_time) / MOVE_DURATION)));
00168     }
00169     // End of movement ?
00170     if( (global_time->Read() - move_start_time) > MOVE_DURATION)
00171       FinalizeMove();
00172   } else {
00173     // While moving, it came back to previous place in ranking
00174     dy = (int)((float)dy - ((global_time->Read() - move_start_time) / MOVE_DURATION) * dy);
00175     dx = (int)((float)dx - ((global_time->Read() - move_start_time) / MOVE_DURATION) * dx);
00176   }
00177 }
00178 
00179 // Move energy bar immediatly to there final destination
00180 void TeamEnergy::FinalizeMove()
00181 {
00182   dy = 0;
00183   dx = 0;
00184   rank = new_rank;
00185   move_start_time = 0;
00186   status = EnergyStatusWait;
00187   return;
00188 }
00189 
00190 bool TeamEnergy::IsMoving () const
00191 {
00192   if( dx != 0 || dy != 0 )
00193     return true;
00194   return false;
00195 }

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