00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "air_attack.h"
00023 #include <sstream>
00024 #include "../game/game_loop.h"
00025 #include "../graphic/sprite.h"
00026 #include "../include/action_handler.h"
00027 #include "../interface/mouse.h"
00028 #include "../map/map.h"
00029 #include "../map/camera.h"
00030 #include "../network/randomsync.h"
00031 #include "../object/objects_list.h"
00032 #include "../team/teams_list.h"
00033 #include "../tool/i18n.h"
00034 #include "../weapon/explosion.h"
00035
00036 const int FORCE_X_MIN = -50;
00037 const uint FORCE_X_MAX = 0;
00038 const uint FORCE_Y_MIN = 1;
00039 const uint FORCE_Y_MAX = 40;
00040
00041 const double OBUS_SPEED = 7 ;
00042
00043 Obus::Obus(AirAttackConfig& cfg) :
00044 WeaponProjectile("air_attack_projectile", cfg, NULL)
00045 {
00046 explode_colliding_character = true;
00047 }
00048
00049
00050
00051 Plane::Plane(AirAttackConfig &p_cfg) :
00052 PhysicalObj("air_attack_plane"),
00053 cfg(p_cfg)
00054 {
00055 SetCollisionModel(true, false, false);
00056
00057 image = resource_manager.LoadSprite(weapons_res_profile, "air_attack_plane");
00058 SetSize(image->GetSize());
00059 obus_dx = 100;
00060 obus_dy = GetY() + GetHeight();
00061 }
00062
00063 void Plane::Shoot(double speed, Point2i& target)
00064 {
00065 nb_dropped_bombs = 0;
00066 last_dropped_bomb = NULL;
00067
00068 Point2d speed_vector ;
00069 int dir = ActiveCharacter().GetDirection();
00070 cible_x = target.x;
00071 SetY(0);
00072 distance_to_release =(int)(speed * sqrt(2 * (GetY() + target.y)));
00073
00074 image->Scale(dir, 1);
00075
00076 if (dir == 1) {
00077 speed_vector.SetValues(speed, 0);
00078 SetX(-image->GetWidth() + 1);
00079
00080 if(distance_to_release > cible_x) distance_to_release=0;
00081 } else {
00082 speed_vector.SetValues(-speed, 0) ;
00083 SetX(world.GetWidth() - 1);
00084
00085 if(distance_to_release > (world.GetWidth()-cible_x - obus_dx)) distance_to_release=0;
00086 }
00087
00088 SetSpeedXY (speed_vector);
00089
00090 camera.FollowObject(this, true, true);
00091
00092 lst_objects.AddObject(this);
00093 }
00094
00095 void Plane::DropBomb()
00096 {
00097 Obus * instance = new Obus(cfg);
00098 instance->SetXY(Point2i(GetX(), obus_dy) );
00099
00100 Point2d speed_vector;
00101 GetSpeedXY(speed_vector);
00102
00103 int fx = randomSync.GetLong(FORCE_X_MIN, FORCE_X_MAX);
00104 fx *= GetDirection();
00105 int fy = randomSync.GetLong(FORCE_Y_MIN, FORCE_Y_MAX);
00106
00107 speed_vector.SetValues(speed_vector.x + fx/30.0, speed_vector.y + fy/30.0);
00108 instance->SetSpeedXY(speed_vector);
00109
00110 lst_objects.AddObject(instance);
00111
00112 camera.FollowObject(instance, true, true);
00113
00114 last_dropped_bomb = instance;
00115 nb_dropped_bombs++;
00116 }
00117
00118 void Plane::Refresh()
00119 {
00120 UpdatePosition();
00121 image->Update();
00122
00123 if ( OnTopOfTarget() && nb_dropped_bombs == 0) {
00124 camera.StopFollowingObj(this);
00125 DropBomb();
00126 } else if (nb_dropped_bombs > 0 && nb_dropped_bombs < cfg.nbr_obus) {
00127
00128 if ( last_dropped_bomb->GetY() > GetY()+GetHeight()+10 )
00129 DropBomb();
00130 }
00131 }
00132
00133 int Plane::GetDirection() const
00134 {
00135 float x,y;
00136 image->GetScaleFactors(x,y);
00137 return (x<0)?-1:1;
00138 }
00139
00140 void Plane::Draw()
00141 {
00142 if (IsGhost()) return;
00143 image->Draw(GetPosition());
00144 }
00145
00146 bool Plane::OnTopOfTarget() const
00147 {
00148 if (GetDirection() == 1)
00149 return (cible_x <= GetX() + distance_to_release);
00150 else
00151 return (GetX() - (int)image->GetWidth() + obus_dx - distance_to_release <= cible_x);
00152 }
00153
00154
00155
00156 AirAttack::AirAttack() :
00157 Weapon(WEAPON_AIR_ATTACK, "air_attack",new AirAttackConfig(), ALWAYS_VISIBLE)
00158 {
00159 m_name = _("Air Attack");
00160 mouse_character_selection = false;
00161 can_be_used_on_closed_map = false;
00162 target_chosen = false;
00163 }
00164
00165 void AirAttack::Refresh()
00166 {
00167 m_is_active = false;
00168 }
00169
00170 void AirAttack::ChooseTarget(Point2i mouse_pos)
00171 {
00172 target = mouse_pos;
00173 target_chosen = true;
00174 Shoot();
00175 }
00176
00177 bool AirAttack::p_Shoot ()
00178 {
00179 if(!target_chosen)
00180 return false;
00181
00182 Plane* plane = new Plane(cfg());
00183 plane->Shoot(cfg().speed, target);
00184 return true;
00185 }
00186
00187 AirAttackConfig& AirAttack::cfg()
00188 {
00189 return static_cast<AirAttackConfig&>(*extra_params);
00190 }
00191
00192
00193
00194 AirAttackConfig::AirAttackConfig()
00195 {
00196 nbr_obus = 3;
00197 speed = 7;
00198 }
00199
00200 void AirAttackConfig::LoadXml(xmlpp::Element *elem)
00201 {
00202 ExplosiveWeaponConfig::LoadXml(elem);
00203 XmlReader::ReadUint(elem, "nbr_obus", nbr_obus);
00204 XmlReader::ReadDouble(elem, "speed", speed);
00205 }