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.
24 #include "flyingsnowball.hpp"
25 #include "random_generator.hpp"
26 #include "object/sprite_particle.hpp"
29 const float PUFF_INTERVAL_MIN = 4.0f; /**< spawn new puff of smoke at most that often */
30 const float PUFF_INTERVAL_MAX = 8.0f; /**< spawn new puff of smoke at least that often */
33 FlyingSnowBall::FlyingSnowBall(const lisp::Lisp& reader)
34 : BadGuy(reader, "images/creatures/flying_snowball/flying_snowball.sprite")
36 physic.enable_gravity(true);
39 FlyingSnowBall::FlyingSnowBall(const Vector& pos)
40 : BadGuy(pos, "images/creatures/flying_snowball/flying_snowball.sprite")
42 physic.enable_gravity(true);
46 FlyingSnowBall::write(lisp::Writer& writer)
48 writer.start_list("flyingsnowball");
50 writer.write_float("x", start_position.x);
51 writer.write_float("y", start_position.y);
53 writer.end_list("flyingsnowball");
57 FlyingSnowBall::initialize()
59 sprite->set_action(dir == LEFT ? "left" : "right");
63 FlyingSnowBall::activate()
65 puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
66 normal_propeller_speed = systemRandom.randf(0.95, 1.05);
70 FlyingSnowBall::collision_squished(GameObject& object)
72 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
73 physic.set_acceleration_y(0);
74 physic.set_velocity_y(0);
75 kill_squished(object);
80 FlyingSnowBall::collision_solid(const CollisionHit& hit)
82 if(hit.top || hit.bottom) {
83 physic.set_velocity_y(0);
88 FlyingSnowBall::active_update(float elapsed_time)
91 const float grav = physic.get_gravity()*100;
92 if (get_pos().y > start_position.y + 2*32) {
94 // Flying too low - increased propeller speed
95 physic.set_acceleration_y(-grav*1.2);
97 physic.set_velocity_y(physic.get_velocity_y() * 0.99);
99 } else if (get_pos().y < start_position.y - 2*32) {
101 // Flying too high - decreased propeller speed
102 physic.set_acceleration_y(-grav*0.8);
104 physic.set_velocity_y(physic.get_velocity_y() * 0.99);
108 // Flying at acceptable altitude - normal propeller speed
109 physic.set_acceleration_y(-grav*normal_propeller_speed);
113 movement=physic.get_movement(elapsed_time);
115 Player* player = this->get_nearest_player();
117 dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
118 sprite->set_action(dir == LEFT ? "left" : "right");
122 if (puff_timer.check()) {
123 Vector ppos = bbox.get_middle();
124 Vector pspeed = Vector(systemRandom.randf(-10, 10), 150);
125 Vector paccel = Vector(0,0);
126 Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
127 puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
129 normal_propeller_speed = systemRandom.randf(0.95, 1.05);
130 physic.set_velocity_y(physic.get_velocity_y() - 50);
134 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball")