Bell sound from remaxim (plays on torch/firefly too)
[supertux.git] / src / object / firefly.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/firefly.hpp"
18
19 #include <math.h>
20
21 #include "audio/sound_manager.hpp"
22 #include "math/random_generator.hpp"
23 #include "object/player.hpp"
24 #include "object/sprite_particle.hpp"
25 #include "supertux/game_session.hpp"
26 #include "supertux/object_factory.hpp"
27 #include "supertux/sector.hpp"
28 #include "util/reader.hpp"
29
30 Firefly::Firefly(const Reader& lisp) :
31    MovingSprite(lisp, "images/objects/resetpoints/default-resetpoint.sprite", LAYER_TILES, COLGROUP_TOUCHABLE), 
32    activated(false),
33    initial_position()
34 {
35   initial_position = get_pos();
36   if( !lisp.get( "sprite", sprite_name ) ){
37     reactivate();
38     return;
39   }
40   if( sprite_name == "" ){
41     sprite_name = "images/objects/resetpoints/default-resetpoint.sprite";
42     reactivate();
43     return;
44   }
45   //Replace sprite
46   sprite = sprite_manager->create( sprite_name );
47   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
48   reactivate();
49
50   //Load sound
51   sound_manager->preload("sounds/savebell2.wav");
52 }
53
54 void
55 Firefly::reactivate()
56 {
57   if(GameSession::current()->get_reset_point_pos() == initial_position){
58     // TODO: && GameSession::current()->get_reset_point_sectorname() ==  <sector this firefly is in>
59     // GameSession::current()->get_current_sector()->get_name() is not yet initialized.
60     // Worst case a resetpoint in a different sector at the same position as the real
61     // resetpoint the player is spawning is set to ringing, too. Until we can check the sector, too, dont set
62     // activated = true; here.
63     sprite->set_action("ringing");
64   }
65 }
66
67 HitResponse
68 Firefly::collision(GameObject& other, const CollisionHit& )
69 {
70   if(activated)
71     return ABORT_MOVE;
72
73   Player* player = dynamic_cast<Player*> (&other);
74   if(player) {
75     activated = true;
76     // spawn some particles
77     // TODO: provide convenience function in MovingSprite or MovingObject?
78     for (int i = 0; i < 5; i++) {
79       Vector ppos = bbox.get_middle();
80       float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
81       float velocity = graphicsRandom.randf(450, 900);
82       float vx = sin(angle)*velocity;
83       float vy = -cos(angle)*velocity;
84       Vector pspeed = Vector(vx, vy);
85       Vector paccel = Vector(0, 1000);
86       Sector::current()->add_object(new SpriteParticle("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
87     }
88
89     sound_manager->play("sounds/savebell2.wav");
90
91     sprite->set_action("ringing");
92     GameSession::current()->set_reset_point(Sector::current()->get_name(),
93                                             initial_position);
94   }
95
96   return ABORT_MOVE;
97 }
98
99 /* EOF */