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