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"
30 #define BASE_SPEED 200
31 #define RAND_SPEED 150
33 static const float X_OFFSCREEN_DISTANCE = 1600;
34 static const float Y_OFFSCREEN_DISTANCE = 1200;
36 Kugelblitz::Kugelblitz(const lisp::Lisp& reader)
37 : BadGuy(Vector(0,0), "images/creatures/kugelblitz/kugelblitz.sprite"), groundhit_pos_set(false)
39 reader.get("x", start_position.x);
40 sprite->set_action("falling");
41 physic.gravity_enabled = false;
45 Kugelblitz::write(lisp::Writer& writer)
47 writer.start_list("kugelblitz");
49 writer.write_float("x", start_position.x);
51 writer.end_list("kugelblitz");
55 Kugelblitz::activate()
58 physic.vx = -20; //fall a little to the left
64 Kugelblitz::collision_solid(const CollisionHit& chit)
70 Kugelblitz::collision_player(Player& player, const CollisionHit& )
72 if(player.is_invincible()) {
77 if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
78 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
79 // if it's not is it possible to squish us, then this will hurt
80 if(!collision_squished(player))
91 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
93 //Let the Kugelblitz explode, too? The problem with that is that
94 //two Kugelblitzes would cancel each other out on contact...
100 Kugelblitz::hit(const CollisionHit& hit)
104 if (!groundhit_pos_set)
106 pos_groundhit = get_pos();
107 groundhit_pos_set = true;
109 sprite->set_action("flying");
111 //Set random initial speed and direction
112 direction = systemRandom.rand(2)? 1: -1;
113 int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
115 movement_timer.start(MOVETIME);
116 lifetime.start(LIFETIME);
118 } else if(hit.top) { // bumped on roof
126 Kugelblitz::active_update(float elapsed_time)
128 if (lifetime.check()) {
132 if (groundhit_pos_set) {
133 if (movement_timer.check()) {
134 if (direction == 1) direction = -1; else direction = 1;
135 int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
137 movement_timer.start(MOVETIME);
141 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
143 Sector::current()->add_object(new Electrifier(75,1421,1.5));
144 Sector::current()->add_object(new Electrifier(76,1422,1.5));
147 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
148 //HIT ELECTRIFIED WATER
153 BadGuy::active_update(elapsed_time);
157 Kugelblitz::kill_fall()
162 Kugelblitz::explode()
165 sprite->set_action("pop");
173 Kugelblitz::try_activate()
175 //FIXME: Don't activate Kugelblitz before it's on-screen
176 float scroll_x = Sector::current()->camera->get_translation().x;
177 float scroll_y = Sector::current()->camera->get_translation().y;
179 /* Activate badguys if they're just around the screen to avoid
180 * the effect of having badguys suddenly popping up from nowhere.
182 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
183 start_position.x < scroll_x - bbox.get_width() &&
184 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
185 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
187 set_state(STATE_ACTIVE);
189 } else if (start_position.x > scroll_x &&
190 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
191 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
192 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
194 set_state(STATE_ACTIVE);
196 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
197 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
198 ((start_position.y > scroll_y &&
199 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
200 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
201 start_position.y < scroll_y))) {
202 dir = start_position.x < scroll_x ? RIGHT : LEFT;
203 set_state(STATE_ACTIVE);
205 } else if(state == STATE_INIT
206 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
207 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
208 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
209 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
211 set_state(STATE_ACTIVE);
216 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz")