(Tree)WillOWisps no longer collide with closed lamps.
[supertux.git] / src / badguy / treewillowisp.cpp
1 //  $Id$
2 //
3 //  SuperTux - "Will-O-Wisp" Badguy
4 //  Copyright (C) 2007 Matthias Braun
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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "treewillowisp.hpp"
23 #include "object/lantern.hpp"
24
25 static const std::string SOUNDFILE = "sounds/willowisp.wav";
26
27 TreeWillOWisp::TreeWillOWisp(const Vector& pos, float radius, float speed)
28   : BadGuy(pos, "images/creatures/willowisp/willowisp.sprite",
29            LAYER_OBJECTS - 20), mystate(STATE_DEFAULT)
30 {
31   sound_manager->preload(SOUNDFILE);
32
33   this->radius = radius;
34   this->angle  = 0;
35   this->speed  = speed;
36 }
37
38 TreeWillOWisp::~TreeWillOWisp()
39 {
40 }
41
42 void
43 TreeWillOWisp::activate()
44 {
45   sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
46   sound_source->set_position(get_pos());
47   sound_source->set_looping(true);
48   sound_source->set_gain(2.0);
49   sound_source->set_reference_distance(32);
50   sound_source->play();
51
52   set_group(COLGROUP_MOVING);
53 }
54
55 void
56 TreeWillOWisp::vanish()
57 {
58   mystate = STATE_VANISHING;
59   sprite->set_action("vanishing", 1);
60   set_group(COLGROUP_DISABLED);
61 }
62
63 HitResponse
64 TreeWillOWisp::collision_player(Player& player, const CollisionHit& hit)
65 {
66   //TODO: basically a no-op. Remove if this doesn't change.
67   return BadGuy::collision_player(player, hit);
68 }
69
70 bool
71 TreeWillOWisp::collides(GameObject& other, const CollisionHit& ) {
72   Lantern* lantern = dynamic_cast<Lantern*>(&other);
73   if (lantern && lantern->is_open()) return true;
74   if (dynamic_cast<Player*>(&other)) return true;
75   return false;
76 }
77
78 void
79 TreeWillOWisp::active_update(float elapsed_time)
80 {
81
82   // remove TreeWillOWisp if it has completely vanished
83   if (mystate == STATE_VANISHING) {
84     if(sprite->animation_done()) {
85       remove_me();
86       return;
87     }
88   }
89
90   angle = fmodf(angle + elapsed_time * speed, (float) (2*M_PI));
91   Vector newpos(start_position.x + sin(angle) * radius, start_position.y);
92   movement = newpos - get_pos();
93   float sizemod = cos(angle) * 0.8f;
94   /* TODO: modify sprite size */
95
96   sound_source->set_position(get_pos());
97
98   if(sizemod < 0) {
99     layer = LAYER_OBJECTS + 5;
100   } else {
101     layer = LAYER_OBJECTS - 20;
102   }
103 }
104
105 void
106 TreeWillOWisp::set_color(const Color& color)
107 {
108   this->color = color;
109   sprite->set_color(color);
110 }
111
112 Color
113 TreeWillOWisp::get_color() const
114 {
115   return color;
116 }
117