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"
28 static const float FLYTIME = 1.0f;
29 static const float FLYSPEED = -100.0f;
32 const float PUFF_PROBABILITY = 0.1f; /**< chanche of puffs being spawned in the current cycle */
33 const float PUFF_INTERVAL_MIN = 0.1f; /**< spawn new puff of smoke at most that often */
34 const float PUFF_INTERVAL_MAX = 1.1f; /**< spawn new puff of smoke at least that often */
37 FlyingSnowBall::FlyingSnowBall(const lisp::Lisp& reader)
38 : BadGuy(reader, "images/creatures/flying_snowball/flying_snowball.sprite")
40 physic.enable_gravity(false);
43 FlyingSnowBall::FlyingSnowBall(const Vector& pos)
44 : BadGuy(pos, "images/creatures/flying_snowball/flying_snowball.sprite")
46 physic.enable_gravity(false);
50 FlyingSnowBall::write(lisp::Writer& writer)
52 writer.start_list("flyingsnowball");
54 writer.write_float("x", start_position.x);
55 writer.write_float("y", start_position.y);
57 writer.end_list("flyingsnowball");
61 FlyingSnowBall::initialize()
63 sprite->set_action(dir == LEFT ? "left" : "right");
65 physic.set_velocity_y(FLYSPEED);
66 timer.start(FLYTIME/2);
70 FlyingSnowBall::activate()
72 puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
76 FlyingSnowBall::collision_squished(GameObject& object)
78 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
79 kill_squished(object);
84 FlyingSnowBall::collision_solid(const CollisionHit& hit)
86 if(hit.top || hit.bottom) {
87 physic.set_velocity_y(0);
92 FlyingSnowBall::active_update(float elapsed_time)
97 physic.set_velocity_y(-FLYSPEED);
102 } else if(mode == FLY_DOWN) {
104 physic.set_velocity_y(FLYSPEED);
106 // roll a dice whether to start puffing
107 if (systemRandom.randf(0, 1) < PUFF_PROBABILITY) {
108 puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
112 timer.start(FLYTIME);
114 movement=physic.get_movement(elapsed_time);
116 Player* player = this->get_nearest_player();
118 dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
119 sprite->set_action(dir == LEFT ? "left" : "right");
123 if (puff_timer.check()) {
124 Vector ppos = bbox.get_middle();
125 Vector pspeed = Vector(systemRandom.randf(-10, 10), 150);
126 Vector paccel = Vector(0,0);
127 Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
128 puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
132 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball")