1 // SuperTux - "Will-O-Wisp" Badguy
2 // Copyright (C) 2007 Matthias Braun
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.
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.
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/>.
17 #include "badguy/treewillowisp.hpp"
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"
28 static const std::string TREEWILLOSOUND = "sounds/willowisp.wav";
30 TreeWillOWisp::TreeWillOWisp(GhostTree* tree_, const Vector& pos,
31 float radius_, float speed_) :
32 BadGuy(tree_->get_pos() + pos, "images/creatures/willowisp/willowisp.sprite",
35 mystate(STATE_DEFAULT),
44 SoundManager::current()->preload(TREEWILLOSOUND);
46 this->radius = radius_;
50 set_colgroup_active(COLGROUP_MOVING);
53 TreeWillOWisp::~TreeWillOWisp()
58 TreeWillOWisp::activate()
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);
69 TreeWillOWisp::vanish()
71 mystate = STATE_VANISHING;
72 sprite->set_action("vanishing", 1);
73 set_colgroup_active(COLGROUP_DISABLED);
77 TreeWillOWisp::start_sucking(Vector suck_target_)
79 mystate = STATE_SUCKED;
80 this->suck_target = suck_target_;
85 TreeWillOWisp::collision_player(Player& player, const CollisionHit& hit)
87 //TODO: basically a no-op. Remove if this doesn't change.
88 return BadGuy::collision_player(player, hit);
92 TreeWillOWisp::collides(GameObject& other, const CollisionHit& ) {
93 Lantern* lantern = dynamic_cast<Lantern*>(&other);
94 if (lantern && lantern->is_open())
96 if (dynamic_cast<Player*>(&other))
103 TreeWillOWisp::draw(DrawingContext& context)
105 sprite->draw(context, get_pos(), layer);
107 context.push_target();
108 context.set_target(DrawingContext::LIGHTMAP);
110 sprite->draw(context, get_pos(), layer);
112 context.pop_target();
116 TreeWillOWisp::active_update(float elapsed_time)
118 // remove TreeWillOWisp if it has completely vanished
119 if (mystate == STATE_VANISHING) {
120 if(sprite->animation_done()) {
122 tree->willowisp_died(this);
127 if (mystate == STATE_SUCKED) {
128 Vector dir_ = suck_target - get_pos();
129 if(dir_.norm() < 5) {
133 Vector newpos = get_pos() + dir_ * elapsed_time;
134 movement = newpos - get_pos();
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 */
144 sound_source->set_position(get_pos());
147 layer = LAYER_OBJECTS + 5;
149 layer = LAYER_OBJECTS - 20;
154 TreeWillOWisp::set_color(const Color& color_)
156 this->color = color_;
157 sprite->set_color(color_);
161 TreeWillOWisp::get_color() const