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);
51 Kugelblitz::write(lisp::Writer& writer)
53 writer.start_list("kugelblitz");
55 writer.write("x", start_position.x);
57 writer.end_list("kugelblitz");
61 Kugelblitz::initialize()
63 physic.set_velocity_y(300);
64 physic.set_velocity_x(-20); //fall a little to the left
70 Kugelblitz::collision_solid(const CollisionHit& chit)
76 Kugelblitz::collision_player(Player& player, const CollisionHit& )
78 if(player.is_invincible()) {
83 if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
84 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
85 // if it's not is it possible to squish us, then this will hurt
86 if(!collision_squished(player))
97 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
99 //Let the Kugelblitz explode, too? The problem with that is that
100 //two Kugelblitzes would cancel each other out on contact...
106 Kugelblitz::hit(const CollisionHit& hit)
110 if (!groundhit_pos_set)
112 pos_groundhit = get_pos();
113 groundhit_pos_set = true;
115 sprite->set_action("flying");
116 physic.set_velocity_y(0);
117 //Set random initial speed and direction
118 direction = systemRandom.rand(2)? 1: -1;
119 int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
120 physic.set_velocity_x(speed);
121 movement_timer.start(MOVETIME);
122 lifetime.start(LIFETIME);
124 } else if(hit.top) { // bumped on roof
125 physic.set_velocity_y(0);
132 Kugelblitz::active_update(float elapsed_time)
134 if (lifetime.check()) {
138 if (groundhit_pos_set) {
139 if (movement_timer.check()) {
140 if (direction == 1) direction = -1; else direction = 1;
141 int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
142 physic.set_velocity_x(speed);
143 movement_timer.start(MOVETIME);
147 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
149 Sector::current()->add_object(new Electrifier(75,1421,1.5));
150 Sector::current()->add_object(new Electrifier(76,1422,1.5));
153 if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
154 //HIT ELECTRIFIED WATER
159 BadGuy::active_update(elapsed_time);
163 Kugelblitz::kill_fall()
168 Kugelblitz::explode()
171 sprite->set_action("pop");
172 lifetime.start(0.2f);
179 Kugelblitz::try_activate()
181 //FIXME: Don't activate Kugelblitz before it's on-screen
182 float scroll_x = Sector::current()->camera->get_translation().x;
183 float scroll_y = Sector::current()->camera->get_translation().y;
185 /* Activate badguys if they're just around the screen to avoid
186 * the effect of having badguys suddenly popping up from nowhere.
188 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
189 start_position.x < scroll_x - bbox.get_width() &&
190 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
191 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
193 set_state(STATE_ACTIVE);
195 } else if (start_position.x > scroll_x &&
196 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
197 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
198 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
200 set_state(STATE_ACTIVE);
202 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
203 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
204 ((start_position.y > scroll_y &&
205 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
206 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
207 start_position.y < scroll_y))) {
208 dir = start_position.x < scroll_x ? RIGHT : LEFT;
209 set_state(STATE_ACTIVE);
211 } else if(state == STATE_INIT
212 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
213 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
214 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
215 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
217 set_state(STATE_ACTIVE);
222 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz")