New GameObject SpriteParticle
[supertux.git] / src / badguy / flyingsnowball.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include <stdio.h>
23
24 #include "flyingsnowball.hpp"
25 #include "random_generator.hpp"
26 #include "object/sprite_particle.hpp"
27
28 static const float FLYTIME = 1.0;
29 static const float FLYSPEED = 100.0;
30
31 namespace {
32   const float PUFF_PROBABILITY = 0.1; /**< chanche of puffs being spawned in the current cycle */
33   const float PUFF_INTERVAL_MIN = 0.1; /**< spawn new puff of smoke at most that often */
34   const float PUFF_INTERVAL_MAX = 1.1; /**< spawn new puff of smoke at least that often */
35 }
36
37 FlyingSnowBall::FlyingSnowBall(const lisp::Lisp& reader)
38 {
39   reader.get("x", start_position.x);
40   reader.get("y", start_position.y);
41   sprite = sprite_manager->create("images/creatures/flying_snowball/flying_snowball.sprite");
42   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
43   physic.enable_gravity(false);
44 }
45
46 FlyingSnowBall::FlyingSnowBall(float pos_x, float pos_y)
47 {
48   start_position.x = pos_x;
49   start_position.y = pos_y;
50   sprite = sprite_manager->create("images/creatures/flying_snowball/flying_snowball.sprite");
51   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
52   physic.enable_gravity(false);
53 }
54
55 void
56 FlyingSnowBall::write(lisp::Writer& writer)
57 {
58   writer.start_list("flyingsnowball");
59
60   writer.write_float("x", start_position.x);
61   writer.write_float("y", start_position.y);
62
63   writer.end_list("flyingsnowball");
64 }
65
66 void 
67 FlyingSnowBall::activate()
68 {
69   sprite->set_action(dir == LEFT ? "left" : "right");
70   mode = FLY_UP;
71   physic.set_velocity_y(FLYSPEED);
72   timer.start(FLYTIME/2);
73   puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
74 }
75
76 bool
77 FlyingSnowBall::collision_squished(Player& player)
78 {
79   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
80   kill_squished(player);
81   return true;
82 }
83
84 HitResponse
85 FlyingSnowBall::collision_solid(GameObject& , const CollisionHit& hit)
86 {
87   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
88     physic.set_velocity_y(0);
89   }
90
91   return CONTINUE;
92 }
93
94 void
95 FlyingSnowBall::active_update(float elapsed_time) 
96 {
97   if(timer.check()) {
98     if(mode == FLY_UP) {
99       mode = FLY_DOWN;
100       physic.set_velocity_y(-FLYSPEED);
101
102       // stop puffing
103       puff_timer.stop();
104       
105     } else if(mode == FLY_DOWN) {
106       mode = FLY_UP;
107       physic.set_velocity_y(FLYSPEED);
108
109       // roll a dice whether to start puffing
110       if (systemRandom.randf(0, 1) < PUFF_PROBABILITY) {
111         puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
112       }
113
114     }
115     timer.start(FLYTIME);
116   }
117   movement=physic.get_movement(elapsed_time);
118
119   Player* player = this->get_nearest_player();
120   if (player) {
121     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
122     sprite->set_action(dir == LEFT ? "left" : "right");
123   }
124
125   // spawn smoke puffs
126   if (puff_timer.check()) {
127     Vector ppos = bbox.get_middle();
128     Vector pspeed = Vector(systemRandom.randf(-10, 10), 150);
129     Vector paccel = Vector(0,0);
130     Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", ppos, pspeed, paccel, LAYER_OBJECTS-1));
131     puff_timer.start(systemRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
132   }
133 }
134
135 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball")