3 // SuperTux - "Will-O-Wisp" Badguy
4 // Copyright (C) 2007 Matthias Braun
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.
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.
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
22 #include "treewillowisp.hpp"
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"
35 static const std::string SOUNDFILE = "sounds/willowisp.wav";
36 static const float SUCKSPEED = 25;
38 TreeWillOWisp::TreeWillOWisp(GhostTree* tree, const Vector& pos,
39 float radius, float speed)
40 : BadGuy(Vector(0, 0), "images/creatures/willowisp/willowisp.sprite",
41 LAYER_OBJECTS - 20), was_sucked(false), mystate(STATE_DEFAULT), tree(tree)
44 sound_manager->preload(SOUNDFILE);
46 this->radius = radius;
49 start_position = tree->get_pos() + treepos_delta;
51 set_colgroup_active(COLGROUP_MOVING);
54 TreeWillOWisp::~TreeWillOWisp()
59 TreeWillOWisp::activate()
61 sound_source.reset(sound_manager->create_sound_source(SOUNDFILE));
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);
70 TreeWillOWisp::vanish()
72 mystate = STATE_VANISHING;
73 sprite->set_action("vanishing", 1);
74 set_colgroup_active(COLGROUP_DISABLED);
78 TreeWillOWisp::start_sucking(Vector suck_target)
80 mystate = STATE_SUCKED;
81 this->suck_target = suck_target;
86 TreeWillOWisp::collision_player(Player& player, const CollisionHit& hit)
88 //TODO: basically a no-op. Remove if this doesn't change.
89 return BadGuy::collision_player(player, hit);
93 TreeWillOWisp::collides(GameObject& other, const CollisionHit& ) {
94 Lantern* lantern = dynamic_cast<Lantern*>(&other);
95 if (lantern && lantern->is_open())
97 if (dynamic_cast<Player*>(&other))
104 TreeWillOWisp::draw(DrawingContext& context)
106 sprite->draw(context, get_pos(), layer);
108 context.push_target();
109 context.set_target(DrawingContext::LIGHTMAP);
111 sprite->draw(context, get_pos(), layer);
113 context.pop_target();
117 TreeWillOWisp::active_update(float elapsed_time)
119 // remove TreeWillOWisp if it has completely vanished
120 if (mystate == STATE_VANISHING) {
121 if(sprite->animation_done()) {
123 tree->willowisp_died(this);
128 if (mystate == STATE_SUCKED) {
129 Vector dir = suck_target - get_pos();
134 Vector newpos = get_pos() + dir * elapsed_time;
135 movement = newpos - get_pos();
139 angle = fmodf(angle + elapsed_time * speed, (float) (2*M_PI));
140 Vector newpos(tree->get_pos() + treepos_delta + Vector(sin(angle) * radius, 0));
141 movement = newpos - get_pos();
142 float sizemod = cos(angle) * 0.8f;
143 /* TODO: modify sprite size */
145 sound_source->set_position(get_pos());
148 layer = LAYER_OBJECTS + 5;
150 layer = LAYER_OBJECTS - 20;
155 TreeWillOWisp::set_color(const Color& color)
158 sprite->set_color(color);
162 TreeWillOWisp::get_color() const