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"
21 #include "math/random_generator.hpp"
22 #include "object/camera.hpp"
23 #include "object/player.hpp"
24 #include "sprite/sprite.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
27 #include "util/reader.hpp"
31 #define BASE_SPEED 200
32 #define RAND_SPEED 150
34 Kugelblitz::Kugelblitz(const Reader& reader) :
35 BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"),
37 groundhit_pos_set(false),
44 reader.get("x", start_position.x);
45 sprite->set_action("falling");
46 physic.enable_gravity(false);
51 Kugelblitz::initialize()
53 physic.set_velocity_y(300);
54 physic.set_velocity_x(-20); //fall a little to the left
60 Kugelblitz::collision_solid(const CollisionHit& chit)
66 Kugelblitz::collision_player(Player& player, const CollisionHit& )
68 if(player.is_invincible()) {
73 if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
74 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
75 // if it's not is it possible to squish us, then this will hurt
76 if(!collision_squished(player))
87 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
89 //Let the Kugelblitz explode, too? The problem with that is that
90 //two Kugelblitzes would cancel each other out on contact...
96 Kugelblitz::hit(const CollisionHit& hit)
100 if (!groundhit_pos_set)
102 pos_groundhit = get_pos();
103 groundhit_pos_set = true;
105 sprite->set_action("flying");
106 physic.set_velocity_y(0);
107 //Set random initial speed and direction
108 direction = gameRandom.rand(2)? 1: -1;
109 int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
110 physic.set_velocity_x(speed);
111 movement_timer.start(MOVETIME);
112 lifetime.start(LIFETIME);
114 } else if(hit.top) { // bumped on roof
115 physic.set_velocity_y(0);
122 Kugelblitz::active_update(float elapsed_time)
124 if (lifetime.check()) {
128 if (groundhit_pos_set) {
129 if (movement_timer.check()) {
130 if (direction == 1) direction = -1; else direction = 1;
131 int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
132 physic.set_velocity_x(speed);
133 movement_timer.start(MOVETIME);
137 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
139 Sector::current()->add_object(new Electrifier(75,1421,1.5));
140 Sector::current()->add_object(new Electrifier(76,1422,1.5));
143 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
144 //HIT ELECTRIFIED WATER
149 BadGuy::active_update(elapsed_time);
153 Kugelblitz::kill_fall()
158 Kugelblitz::explode()
161 sprite->set_action("pop");
162 lifetime.start(0.2f);
169 Kugelblitz::try_activate()
171 // Much smaller offscreen distances to pop out of nowhere and surprise Tux
172 float X_OFFSCREEN_DISTANCE = 400;
173 float Y_OFFSCREEN_DISTANCE = 600;
175 Player* player = get_nearest_player();
177 Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
178 if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
179 set_state(STATE_ACTIVE);
180 if (!is_initialized) {
182 // if starting direction was set to AUTO, this is our chance to re-orient the badguy
183 if (start_dir == AUTO) {
184 Player* player = get_nearest_player();
185 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
193 is_initialized = true;