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