00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
00094 case EnergyStatusRankChange:
00095 Move();
00096 break;
00097
00098
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
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
00138 void TeamEnergy::Move()
00139 {
00140 if( value != new_value && !IsMoving()) {
00141
00142 status = EnergyStatusWait;
00143 return;
00144 }
00145
00146 if( rank == new_rank && !IsMoving()) {
00147
00148 status = EnergyStatusWait;
00149 return;
00150 }
00151
00152
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
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
00170 if( (global_time->Read() - move_start_time) > MOVE_DURATION)
00171 FinalizeMove();
00172 } else {
00173
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
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 }