a4d3d8dcf79a75146dc84655b8a9d1c601da402a
[supertux.git] / src / badguy / treewillowisp.cpp
1 //  SuperTux - "Will-O-Wisp" Badguy
2 //  Copyright (C) 2007 Matthias Braun
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 "badguy/treewillowisp.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "audio/sound_source.hpp"
21 #include "badguy/ghosttree.hpp"
22 #include "object/lantern.hpp"
23 #include "object/player.hpp"
24 #include "sprite/sprite.hpp"
25
26 #include <math.h>
27
28 static const std::string TREEWILLOSOUND = "sounds/willowisp.wav";
29 static const float       SUCKSPEED = 25;
30
31 TreeWillOWisp::TreeWillOWisp(GhostTree* tree, const Vector& pos,
32                              float radius, float speed) :
33   BadGuy(tree->get_pos() + pos, "images/creatures/willowisp/willowisp.sprite",
34          LAYER_OBJECTS - 20),
35   was_sucked(false),
36   mystate(STATE_DEFAULT),
37   color(),
38   angle(),
39   radius(),
40   speed(),
41   sound_source(),
42   tree(tree),
43   suck_target()
44 {
45   sound_manager->preload(TREEWILLOSOUND);
46
47   this->radius = radius;
48   this->angle  = 0;
49   this->speed  = speed;
50
51   set_colgroup_active(COLGROUP_MOVING);
52 }
53
54 TreeWillOWisp::~TreeWillOWisp()
55 {
56 }
57
58 void
59 TreeWillOWisp::activate()
60 {
61   sound_source = sound_manager->create_sound_source(TREEWILLOSOUND);
62   sound_source->set_position(get_pos());
63   sound_source->set_looping(true);
64   sound_source->set_gain(2.0);
65   sound_source->set_reference_distance(32);
66   sound_source->play();
67 }
68
69 void
70 TreeWillOWisp::vanish()
71 {
72   mystate = STATE_VANISHING;
73   sprite->set_action("vanishing", 1);
74   set_colgroup_active(COLGROUP_DISABLED);
75 }
76
77 void
78 TreeWillOWisp::start_sucking(Vector suck_target)
79 {
80   mystate = STATE_SUCKED;
81   this->suck_target = suck_target;
82   was_sucked = true;
83 }
84
85 HitResponse
86 TreeWillOWisp::collision_player(Player& player, const CollisionHit& hit)
87 {
88   //TODO: basically a no-op. Remove if this doesn't change.
89   return BadGuy::collision_player(player, hit);
90 }
91
92 bool
93 TreeWillOWisp::collides(GameObject& other, const CollisionHit& ) {
94   Lantern* lantern = dynamic_cast<Lantern*>(&other);
95   if (lantern && lantern->is_open())
96     return true;
97   if (dynamic_cast<Player*>(&other))
98     return true;
99
100   return false;
101 }
102
103 void
104 TreeWillOWisp::draw(DrawingContext& context)
105 {
106   sprite->draw(context, get_pos(), layer);
107
108   context.push_target();
109   context.set_target(DrawingContext::LIGHTMAP);
110
111   sprite->draw(context, get_pos(), layer);
112
113   context.pop_target();
114 }
115
116 void
117 TreeWillOWisp::active_update(float elapsed_time)
118 {
119   // remove TreeWillOWisp if it has completely vanished
120   if (mystate == STATE_VANISHING) {
121     if(sprite->animation_done()) {
122       remove_me();
123       tree->willowisp_died(this);
124     }
125     return;
126   }
127
128   if (mystate == STATE_SUCKED) {
129     Vector dir = suck_target - get_pos();
130     if(dir.norm() < 5) {
131       vanish();
132       return;
133     }
134     Vector newpos = get_pos() + dir * elapsed_time;
135     movement = newpos - get_pos();
136     return;
137   }
138
139   angle = fmodf(angle + elapsed_time * speed, (float) (2*M_PI));
140   Vector newpos(start_position + Vector(sin(angle) * radius, 0));
141   movement = newpos - get_pos();
142   float sizemod = cos(angle) * 0.8f;
143   /* TODO: modify sprite size */
144
145   sound_source->set_position(get_pos());
146
147   if(sizemod < 0) {
148     layer = LAYER_OBJECTS + 5;
149   } else {
150     layer = LAYER_OBJECTS - 20;
151   }
152 }
153
154 void
155 TreeWillOWisp::set_color(const Color& color)
156 {
157   this->color = color;
158   sprite->set_color(color);
159 }
160
161 Color
162 TreeWillOWisp::get_color() const
163 {
164   return color;
165 }
166
167 /* EOF */