2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "badguy/kugelblitz.hpp"
18 #include "math/random_generator.hpp"
19 #include "object/camera.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
27 #define BASE_SPEED 200
28 #define RAND_SPEED 150
30 static const float X_OFFSCREEN_DISTANCE = 1600;
31 static const float Y_OFFSCREEN_DISTANCE = 1200;
33 Kugelblitz::Kugelblitz(const Reader& reader) :
34 BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"),
36 groundhit_pos_set(false),
43 reader.get("x", start_position.x);
44 sprite->set_action("falling");
45 physic.enable_gravity(false);
50 Kugelblitz::initialize()
52 physic.set_velocity_y(300);
53 physic.set_velocity_x(-20); //fall a little to the left
59 Kugelblitz::collision_solid(const CollisionHit& chit)
65 Kugelblitz::collision_player(Player& player, const CollisionHit& )
67 if(player.is_invincible()) {
72 if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
73 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
74 // if it's not is it possible to squish us, then this will hurt
75 if(!collision_squished(player))
86 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
88 //Let the Kugelblitz explode, too? The problem with that is that
89 //two Kugelblitzes would cancel each other out on contact...
95 Kugelblitz::hit(const CollisionHit& hit)
99 if (!groundhit_pos_set)
101 pos_groundhit = get_pos();
102 groundhit_pos_set = true;
104 sprite->set_action("flying");
105 physic.set_velocity_y(0);
106 //Set random initial speed and direction
107 direction = systemRandom.rand(2)? 1: -1;
108 int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
109 physic.set_velocity_x(speed);
110 movement_timer.start(MOVETIME);
111 lifetime.start(LIFETIME);
113 } else if(hit.top) { // bumped on roof
114 physic.set_velocity_y(0);
121 Kugelblitz::active_update(float elapsed_time)
123 if (lifetime.check()) {
127 if (groundhit_pos_set) {
128 if (movement_timer.check()) {
129 if (direction == 1) direction = -1; else direction = 1;
130 int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
131 physic.set_velocity_x(speed);
132 movement_timer.start(MOVETIME);
136 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
138 Sector::current()->add_object(new Electrifier(75,1421,1.5));
139 Sector::current()->add_object(new Electrifier(76,1422,1.5));
142 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
143 //HIT ELECTRIFIED WATER
148 BadGuy::active_update(elapsed_time);
152 Kugelblitz::kill_fall()
157 Kugelblitz::explode()
160 sprite->set_action("pop");
161 lifetime.start(0.2f);
168 Kugelblitz::try_activate()
170 //FIXME: Don't activate Kugelblitz before it's on-screen
171 float scroll_x = Sector::current()->camera->get_translation().x;
172 float scroll_y = Sector::current()->camera->get_translation().y;
174 /* Activate badguys if they're just around the screen to avoid
175 * the effect of having badguys suddenly popping up from nowhere.
177 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
178 start_position.x < scroll_x - bbox.get_width() &&
179 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
180 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
182 set_state(STATE_ACTIVE);
184 } else if (start_position.x > scroll_x &&
185 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
186 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
187 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
189 set_state(STATE_ACTIVE);
191 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
192 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
193 ((start_position.y > scroll_y &&
194 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
195 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
196 start_position.y < scroll_y))) {
197 dir = start_position.x < scroll_x ? RIGHT : LEFT;
198 set_state(STATE_ACTIVE);
200 } else if(state == STATE_INIT
201 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
202 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
203 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
204 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
206 set_state(STATE_ACTIVE);
211 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz");