4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "kugelblitz.hpp"
23 #include "object/tilemap.hpp"
24 #include "object/camera.hpp"
26 #include "random_generator.hpp"
27 #include "lisp/writer.hpp"
28 #include "object_factory.hpp"
29 #include "object/player.hpp"
31 #include "sprite/sprite.hpp"
35 #define BASE_SPEED 200
36 #define RAND_SPEED 150
38 static const float X_OFFSCREEN_DISTANCE = 1600;
39 static const float Y_OFFSCREEN_DISTANCE = 1200;
41 Kugelblitz::Kugelblitz(const lisp::Lisp& reader)
42 : BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"), groundhit_pos_set(false)
44 reader.get("x", start_position.x);
45 sprite->set_action("falling");
46 physic.enable_gravity(false);
50 Kugelblitz::write(lisp::Writer& writer)
52 writer.start_list("kugelblitz");
54 writer.write("x", start_position.x);
56 writer.end_list("kugelblitz");
60 Kugelblitz::initialize()
62 physic.set_velocity_y(300);
63 physic.set_velocity_x(-20); //fall a little to the left
69 Kugelblitz::collision_solid(const CollisionHit& chit)
75 Kugelblitz::collision_player(Player& player, const CollisionHit& )
77 if(player.is_invincible()) {
82 if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
83 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
84 // if it's not is it possible to squish us, then this will hurt
85 if(!collision_squished(player))
96 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
98 //Let the Kugelblitz explode, too? The problem with that is that
99 //two Kugelblitzes would cancel each other out on contact...
105 Kugelblitz::hit(const CollisionHit& hit)
109 if (!groundhit_pos_set)
111 pos_groundhit = get_pos();
112 groundhit_pos_set = true;
114 sprite->set_action("flying");
115 physic.set_velocity_y(0);
116 //Set random initial speed and direction
117 direction = systemRandom.rand(2)? 1: -1;
118 int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
119 physic.set_velocity_x(speed);
120 movement_timer.start(MOVETIME);
121 lifetime.start(LIFETIME);
123 } else if(hit.top) { // bumped on roof
124 physic.set_velocity_y(0);
131 Kugelblitz::active_update(float elapsed_time)
133 if (lifetime.check()) {
137 if (groundhit_pos_set) {
138 if (movement_timer.check()) {
139 if (direction == 1) direction = -1; else direction = 1;
140 int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
141 physic.set_velocity_x(speed);
142 movement_timer.start(MOVETIME);
146 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
148 Sector::current()->add_object(new Electrifier(75,1421,1.5));
149 Sector::current()->add_object(new Electrifier(76,1422,1.5));
152 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
153 //HIT ELECTRIFIED WATER
158 BadGuy::active_update(elapsed_time);
162 Kugelblitz::kill_fall()
167 Kugelblitz::explode()
170 sprite->set_action("pop");
171 lifetime.start(0.2f);
178 Kugelblitz::try_activate()
180 //FIXME: Don't activate Kugelblitz before it's on-screen
181 float scroll_x = Sector::current()->camera->get_translation().x;
182 float scroll_y = Sector::current()->camera->get_translation().y;
184 /* Activate badguys if they're just around the screen to avoid
185 * the effect of having badguys suddenly popping up from nowhere.
187 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
188 start_position.x < scroll_x - bbox.get_width() &&
189 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
190 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
192 set_state(STATE_ACTIVE);
194 } else if (start_position.x > scroll_x &&
195 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
196 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
197 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
199 set_state(STATE_ACTIVE);
201 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
202 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
203 ((start_position.y > scroll_y &&
204 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
205 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
206 start_position.y < scroll_y))) {
207 dir = start_position.x < scroll_x ? RIGHT : LEFT;
208 set_state(STATE_ACTIVE);
210 } else if(state == STATE_INIT
211 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
212 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
213 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
214 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
216 set_state(STATE_ACTIVE);
221 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz")