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