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