00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "badguy/kugelblitz.hpp"
00018
00019 #include <math.h>
00020
00021 #include "math/random_generator.hpp"
00022 #include "object/camera.hpp"
00023 #include "object/player.hpp"
00024 #include "sprite/sprite.hpp"
00025 #include "supertux/object_factory.hpp"
00026 #include "supertux/sector.hpp"
00027 #include "util/reader.hpp"
00028
00029 #define LIFETIME 5
00030 #define MOVETIME 0.75
00031 #define BASE_SPEED 200
00032 #define RAND_SPEED 150
00033
00034 Kugelblitz::Kugelblitz(const Reader& reader) :
00035 BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"),
00036 pos_groundhit(),
00037 groundhit_pos_set(false),
00038 dying(),
00039 movement_timer(),
00040 lifetime(),
00041 direction(),
00042 state()
00043 {
00044 reader.get("x", start_position.x);
00045 sprite->set_action("falling");
00046 physic.enable_gravity(false);
00047 countMe = false;
00048 }
00049
00050 void
00051 Kugelblitz::initialize()
00052 {
00053 physic.set_velocity_y(300);
00054 physic.set_velocity_x(-20);
00055 direction = 1;
00056 dying = false;
00057 }
00058
00059 void
00060 Kugelblitz::collision_solid(const CollisionHit& chit)
00061 {
00062 hit(chit);
00063 }
00064
00065 HitResponse
00066 Kugelblitz::collision_player(Player& player, const CollisionHit& )
00067 {
00068 if(player.is_invincible()) {
00069 explode();
00070 return ABORT_MOVE;
00071 }
00072
00073 if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
00074 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
00075
00076 if(!collision_squished(player))
00077 player.kill(false);
00078 explode();
00079 return FORCE_MOVE;
00080 }
00081 player.kill(false);
00082 explode();
00083 return FORCE_MOVE;
00084 }
00085
00086 HitResponse
00087 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
00088 {
00089
00090
00091 other.kill_fall();
00092 return hit(chit);
00093 }
00094
00095 HitResponse
00096 Kugelblitz::hit(const CollisionHit& hit)
00097 {
00098
00099 if(hit.bottom) {
00100 if (!groundhit_pos_set)
00101 {
00102 pos_groundhit = get_pos();
00103 groundhit_pos_set = true;
00104 }
00105 sprite->set_action("flying");
00106 physic.set_velocity_y(0);
00107
00108 direction = gameRandom.rand(2)? 1: -1;
00109 int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
00110 physic.set_velocity_x(speed);
00111 movement_timer.start(MOVETIME);
00112 lifetime.start(LIFETIME);
00113
00114 } else if(hit.top) {
00115 physic.set_velocity_y(0);
00116 }
00117
00118 return CONTINUE;
00119 }
00120
00121 void
00122 Kugelblitz::active_update(float elapsed_time)
00123 {
00124 if (lifetime.check()) {
00125 explode();
00126 }
00127 else {
00128 if (groundhit_pos_set) {
00129 if (movement_timer.check()) {
00130 if (direction == 1) direction = -1; else direction = 1;
00131 int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
00132 physic.set_velocity_x(speed);
00133 movement_timer.start(MOVETIME);
00134 }
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 }
00149 BadGuy::active_update(elapsed_time);
00150 }
00151
00152 void
00153 Kugelblitz::kill_fall()
00154 {
00155 }
00156
00157 void
00158 Kugelblitz::explode()
00159 {
00160 if (!dying) {
00161 sprite->set_action("pop");
00162 lifetime.start(0.2f);
00163 dying = true;
00164 }
00165 else remove_me();
00166 }
00167
00168 void
00169 Kugelblitz::try_activate()
00170 {
00171
00172 float X_OFFSCREEN_DISTANCE = 400;
00173 float Y_OFFSCREEN_DISTANCE = 600;
00174
00175 Player* player = get_nearest_player();
00176 if (!player) return;
00177 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
00178 if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
00179 set_state(STATE_ACTIVE);
00180 if (!is_initialized) {
00181
00182
00183 if (start_dir == AUTO) {
00184 Player* player = get_nearest_player();
00185 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
00186 dir = RIGHT;
00187 } else {
00188 dir = LEFT;
00189 }
00190 }
00191
00192 initialize();
00193 is_initialized = true;
00194 }
00195 activate();
00196 }
00197 }
00198
00199