125614a5ec8e29b13618e0533e2a7d1383d4fc9f
[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
24 #include "sprite/sprite_manager.hpp"
25 #include "player.hpp"
26 #include "object_factory.hpp"
27 #include "game_session.hpp"
28 #include "sector.hpp"
29 #include "random_generator.hpp"
30 #include "object/sprite_particle.hpp"
31 #include "lisp/writer.hpp"
32
33 #include <math.h>
34
35 Firefly::Firefly(const lisp::Lisp& lisp)
36        : MovingSprite(lisp, "images/objects/resetpoints/default-resetpoint.sprite", LAYER_TILES, COLGROUP_TOUCHABLE), activated(false)
37 {
38   initial_position = get_pos();
39   if( !lisp.get( "sprite", sprite_name ) ){
40     reactivate();
41     return;
42   }
43   if( sprite_name == "" ){
44     sprite_name = "images/objects/resetpoints/default-resetpoint.sprite";
45     reactivate();
46     return;
47   }
48   //Replace sprite
49   sprite = sprite_manager->create( sprite_name );
50   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
51   reactivate();
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 void
68 Firefly::write(lisp::Writer& writer)
69 {
70   writer.start_list("firefly");
71   writer.write("x", bbox.p1.x);
72   writer.write("y", bbox.p1.y);
73   writer.end_list("firefly");
74 }
75
76 HitResponse
77 Firefly::collision(GameObject& other, const CollisionHit& )
78 {
79   if(activated)
80     return ABORT_MOVE;
81
82   Player* player = dynamic_cast<Player*> (&other);
83   if(player) {
84     activated = true;
85 // spawn some particles
86 // TODO: provide convenience function in MovingSprite or MovingObject?
87        for (int i = 0; i < 5; i++) {
88          Vector ppos = bbox.get_middle();
89          float angle = systemRandom.randf(-M_PI_2, M_PI_2);
90          float velocity = systemRandom.randf(450, 900);
91          float vx = sin(angle)*velocity;
92          float vy = -cos(angle)*velocity;
93          Vector pspeed = Vector(vx, vy);
94          Vector paccel = Vector(0, 1000);
95          Sector::current()->add_object(new SpriteParticle("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
96        }
97     // TODO play sound
98     sprite->set_action("ringing");
99     GameSession::current()->set_reset_point(Sector::current()->get_name(),
100         initial_position);
101   }
102
103   return ABORT_MOVE;
104 }
105
106 IMPLEMENT_FACTORY(Firefly, "firefly");