Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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/random_generator.hpp"
20 #include "object/player.hpp"
21 #include "object/sprite_particle.hpp"
22 #include "supertux/game_session.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25
26 #include <math.h>
27
28 Firefly::Firefly(const Reader& lisp)
29   : MovingSprite(lisp, "images/objects/resetpoints/default-resetpoint.sprite", LAYER_TILES, COLGROUP_TOUCHABLE), activated(false)
30 {
31   initial_position = get_pos();
32   if( !lisp.get( "sprite", sprite_name ) ){
33     reactivate();
34     return;
35   }
36   if( sprite_name == "" ){
37     sprite_name = "images/objects/resetpoints/default-resetpoint.sprite";
38     reactivate();
39     return;
40   }
41   //Replace sprite
42   sprite = sprite_manager->create( sprite_name );
43   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
44   reactivate();
45 }
46
47 void
48 Firefly::reactivate()
49 {
50   if(GameSession::current()->get_reset_point_pos() == initial_position){
51     // TODO: && GameSession::current()->get_reset_point_sectorname() ==  <sector this firefly is in>
52     // GameSession::current()->get_current_sector()->get_name() is not yet initialized.
53     // Worst case a resetpoint in a different sector at the same position as the real
54     // resetpoint the player is spawning is set to ringing, too. Until we can check the sector, too, dont set
55     // activated = true; here.
56     sprite->set_action("ringing");
57   }
58 }
59
60 HitResponse
61 Firefly::collision(GameObject& other, const CollisionHit& )
62 {
63   if(activated)
64     return ABORT_MOVE;
65
66   Player* player = dynamic_cast<Player*> (&other);
67   if(player) {
68     activated = true;
69     // spawn some particles
70     // TODO: provide convenience function in MovingSprite or MovingObject?
71     for (int i = 0; i < 5; i++) {
72       Vector ppos = bbox.get_middle();
73       float angle = systemRandom.randf(-M_PI_2, M_PI_2);
74       float velocity = systemRandom.randf(450, 900);
75       float vx = sin(angle)*velocity;
76       float vy = -cos(angle)*velocity;
77       Vector pspeed = Vector(vx, vy);
78       Vector paccel = Vector(0, 1000);
79       Sector::current()->add_object(new SpriteParticle("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
80     }
81     // TODO play sound
82     sprite->set_action("ringing");
83     GameSession::current()->set_reset_point(Sector::current()->get_name(),
84                                             initial_position);
85   }
86
87   return ABORT_MOVE;
88 }
89
90 IMPLEMENT_FACTORY(Firefly, "firefly");
91
92 /* EOF */