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