00001
#pragma warning(disable: 4786)
00002
00003
00004
#include "unit.h"
00005
#include "HV_bsp.h"
00006
#include "game_manager.h"
00007
#include "draw_line.h"
00008
00009
00010
00011
00012
00013
00014
00015 cUnit::cUnit(
int x,
int y,
cSprite_Display_Def * display_def ,
int s,
int r)
00016 {
00017
Px = x;
00018
Py = y;
00019
pDisplay_Def = display_def;
00020
Speed = s;
00021
Rayon = r;
00022
bOn_The_Road =
false;
00023
Current_Path.clear();
00024
00025
Time_Stamp_Last_Move =
DXUtil_Timer(
TIMER_GETAPPTIME )-
FREQUENCY_MOVE;
00026 };
00027
00028
00029
00030 void cUnit::React_To_Bonus(
cInteractive_Item * bonus)
00031 {
00032
switch( bonus->
Type)
00033 {
00034
case TELEPORTEUR:
00035
React_To_Teleporteur( (
cTeleporteur*) bonus );
00036
return;
00037
case BOMB:
00038
React_To_Bomb( (
cBomb*) bonus );
00039
return;
00040
case HOLOGRAMME:
00041
React_To_Hologramme( (
cHologramme*) bonus );
00042
return;
00043
case CADEAU:
00044
React_To_Cadeau( (
cCadeau*) bonus );
00045
return;
00046
case RED_TRAP:
00047
React_To_Red_Trap( (
cRed_Trap*) bonus );
00048
return;
00049
case BLUE_TRAP:
00050
React_To_Blue_Trap( (
cBlue_Trap*) bonus );
00051
return;
00052
case GREEN_TRAP:
00053
React_To_Green_Trap( (
cGreen_Trap*) bonus );
00054
return;
00055 }
00056 }
00057
00058
00059 void cUnit::Add_Node_To_Path(
cPoint2D& p)
00060 {
00061
Current_Path.push_back(p);
00062 }
00063
00064
00065 bool cUnit::Follow_Path()
00066 {
00067
cPoint2D node;
00068
cPoint2D new_pos;
00069
bool arrived;
00070
00071
if(
Current_Path.empty() )
00072 {
00073
return true;
00074 }
00075
else if( !
bOn_The_Road )
00076 {
00077
bOn_The_Road =
true;
00078 node =
Current_Path.front();
00079
Bresenham_Algo.
Reset(
cPoint2D(
Px,
Py) , node );
00080
Time_Stamp_Last_Move =
DXUtil_Timer(
TIMER_GETAPPTIME ) -
FREQUENCY_MOVE ;
00081 }
00082
00083
00084
if(
DXUtil_Timer(
TIMER_GETAPPTIME ) -
Time_Stamp_Last_Move >=
FREQUENCY_MOVE )
00085 {
00086 arrived =
Bresenham_Algo.
Step(
Speed, new_pos) ;
00087
Px=new_pos.
X;
00088
Py=new_pos.
Y;
00089
00090
00091
if(
Px-
Rayon-1 < 0)
00092 {
00093
Px =
Rayon+1;
00094 arrived =
true;
00095
00096 }
00097
if(
Px+
Rayon+1 >=
TX_SCREEN)
00098 {
00099
Px =
TX_SCREEN - Rayon-1;
00100 arrived =
true;
00101 }
00102
00103
if(
Py-Rayon-1 < 0)
00104 {
00105
Py = Rayon+1;
00106 arrived =
true;
00107 }
00108
if(
Py+Rayon+1 >=
TY_SCREEN)
00109 {
00110
Py =
TY_SCREEN - Rayon-1;
00111 arrived =
true;
00112 }
00113
00114
00115 assert(
Px-Rayon >= 0);
00116 assert(
Py-Rayon >= 0);
00117 assert(
Px+Rayon <
TX_SCREEN);
00118 assert(
Py+Rayon <
TY_SCREEN);
00119
00120
00121
00122
00123
if( arrived )
00124 {
00125
bOn_The_Road =
false;
00126
Current_Path.pop_front();
00127 }
00128
Time_Stamp_Last_Move +=
FREQUENCY_MOVE;
00129 }
00130
00131
return false;
00132 }
00133
00134
00135
00136
00137 void cUnit::Display()
00138 {
00139 assert(
Px-
Rayon >= 0);
00140 assert(
Py-Rayon >= 0);
00141 assert(
Px+Rayon <
TX_SCREEN );
00142 assert(
Py+Rayon <
TY_SCREEN);
00143
00144
00145
pDisplay_Def->
Display(
Px - Rayon ,
Py - Rayon);
00146 }
00147
00148
00149 void cUnit::Move(
int x_move,
int y_move)
00150 {
00151
int real_delta_x=0;
00152
int real_delta_y=0;
00153
00154
int possible_delta_x=0;
00155
int possible_delta_y=0;
00156
00157
int final_move_x = 0;
00158
int final_move_y = 0;
00159
00160
if(
DXUtil_Timer(
TIMER_GETAPPTIME ) -
Time_Stamp_Last_Move >=
FREQUENCY_MOVE )
00161 {
00162
if( x_move!=0 && y_move!=0)
00163 {
00164 real_delta_x = ((
float) x_move)/1.414;
00165 real_delta_y = ((
float) y_move)/1.414;
00166 }
00167
else
00168 {
00169 real_delta_x = x_move;
00170 real_delta_y = y_move;
00171 }
00172
00173
00174
00175
00176
if(
Px-
Rayon+real_delta_x < cGame_World::Ecran.
p0.
Get_X())
00177 possible_delta_x = cGame_World::Ecran.
p0.
Get_X() +
Rayon -
Px ;
00178
else if( Px+
Rayon+real_delta_x >= cGame_World::Ecran.
p1.
Get_X())
00179 possible_delta_x = cGame_World::Ecran.
p1.
Get_X() -
Rayon - Px ;
00180
else
00181 possible_delta_x = real_delta_x;
00182
00183
if(
Py-
Rayon+real_delta_y < cGame_World::Ecran.
p0.
Get_Y())
00184 possible_delta_y = cGame_World::Ecran.
p0.
Get_Y() +
Rayon -
Py;
00185
else if( Py+
Rayon+real_delta_y >= cGame_World::Ecran.
p1.
Get_Y())
00186 possible_delta_y = cGame_World::Ecran.
p1.
Get_Y() -
Rayon - Py ;
00187
else
00188 possible_delta_y = real_delta_y;
00189
00190
00191
if( possible_delta_x == 0 && possible_delta_y ==0)
00192
return;
00193
else if( possible_delta_x == 0)
00194 {
00195 final_move_x = 0;
00196 final_move_y = y_move;
00197 }
00198
else if( possible_delta_y == 0)
00199 {
00200 final_move_x = x_move;
00201 final_move_y = 0;
00202 }
00203
else
00204 {
00205 final_move_x = x_move;
00206 final_move_y = y_move;
00207 }
00208
00209
00210
00211 Px+= final_move_x;
00212 Py+= final_move_y;
00213
00214
00215
if( Px-
Rayon-1 < cGame_World::Ecran.
p0.
Get_X())
00216 Px =
Rayon+1;
00217
else if( Px+Rayon >= cGame_World::Ecran.
p1.
Get_X())
00218 Px = cGame_World::Ecran.
p1.
Get_X() - Rayon -1;
00219
00220
if( Py-Rayon-1 < cGame_World::Ecran.
p0.
Get_Y())
00221 Py = Rayon+1;
00222
else if( Py+Rayon >= cGame_World::Ecran.
p1.
Get_Y())
00223 Py = cGame_World::Ecran.
p1.
Get_Y() - Rayon -1;
00224
00225
00226
if(
Game_Manager.
Game_World->
Do_Ball_Intersect_Walls(Px , Py , Rayon ))
00227 {
00228
00229 Px -= final_move_x;
00230
if( !
Game_Manager.
Game_World->
Do_Ball_Intersect_Walls(Px , Py , Rayon ))
00231 {
00232
Time_Stamp_Last_Move +=
FREQUENCY_MOVE;
00233
return;
00234 }
00235
00236
00237 Px += final_move_x;
00238 Py -= final_move_y;
00239
if( !
Game_Manager.
Game_World->
Do_Ball_Intersect_Walls(Px , Py , Rayon ))
00240 {
00241
Time_Stamp_Last_Move +=
FREQUENCY_MOVE;
00242
return;
00243 }
00244
00245
00246 Px -= final_move_x;
00247
Time_Stamp_Last_Move +=
FREQUENCY_MOVE;
00248
return ;
00249 }
00250
00251
Time_Stamp_Last_Move +=
FREQUENCY_MOVE;
00252 }
00253
00254 }
00255
00256
00257
00258 void cUnit::Display_Path()
00259 {
00260
cPoint2D last_p(this->Px,this->Py);
00261
00262
00263 std::list<cPoint2D>::iterator chemin_itr;
00264
for(chemin_itr =
Current_Path.begin() ; chemin_itr !=
Current_Path.end() ;chemin_itr++)
00265 {
00266
Draw_Line(
g_pDisplay,last_p,(*chemin_itr),0x15);
00267 last_p = (*chemin_itr);
00268 }
00269 }