src/badguy/treewillowisp.cpp

Go to the documentation of this file.
00001 //  SuperTux - "Will-O-Wisp" Badguy
00002 //  Copyright (C) 2007 Matthias Braun
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "badguy/treewillowisp.hpp"
00018 
00019 #include "audio/sound_manager.hpp"
00020 #include "audio/sound_source.hpp"
00021 #include "badguy/ghosttree.hpp"
00022 #include "object/lantern.hpp"
00023 #include "object/player.hpp"
00024 #include "sprite/sprite.hpp"
00025 
00026 #include <math.h>
00027 
00028 static const std::string TREEWILLOSOUND = "sounds/willowisp.wav";
00029 static const float       SUCKSPEED = 25;
00030 
00031 TreeWillOWisp::TreeWillOWisp(GhostTree* tree, const Vector& pos,
00032                              float radius, float speed) :
00033   BadGuy(tree->get_pos() + pos, "images/creatures/willowisp/willowisp.sprite",
00034          LAYER_OBJECTS - 20), 
00035   was_sucked(false), 
00036   mystate(STATE_DEFAULT), 
00037   color(),
00038   angle(),
00039   radius(),
00040   speed(),
00041   sound_source(),
00042   tree(tree),
00043   suck_target()
00044 {
00045   sound_manager->preload(TREEWILLOSOUND);
00046 
00047   this->radius = radius;
00048   this->angle  = 0;
00049   this->speed  = speed;
00050 
00051   set_colgroup_active(COLGROUP_MOVING);
00052 }
00053 
00054 TreeWillOWisp::~TreeWillOWisp()
00055 {
00056 }
00057 
00058 void
00059 TreeWillOWisp::activate()
00060 {
00061   sound_source.reset(sound_manager->create_sound_source(TREEWILLOSOUND));
00062   sound_source->set_position(get_pos());
00063   sound_source->set_looping(true);
00064   sound_source->set_gain(2.0);
00065   sound_source->set_reference_distance(32);
00066   sound_source->play();
00067 }
00068 
00069 void
00070 TreeWillOWisp::vanish()
00071 {
00072   mystate = STATE_VANISHING;
00073   sprite->set_action("vanishing", 1);
00074   set_colgroup_active(COLGROUP_DISABLED);
00075 }
00076 
00077 void
00078 TreeWillOWisp::start_sucking(Vector suck_target)
00079 {
00080   mystate = STATE_SUCKED;
00081   this->suck_target = suck_target;
00082   was_sucked = true;
00083 }
00084 
00085 HitResponse
00086 TreeWillOWisp::collision_player(Player& player, const CollisionHit& hit)
00087 {
00088   //TODO: basically a no-op. Remove if this doesn't change.
00089   return BadGuy::collision_player(player, hit);
00090 }
00091 
00092 bool
00093 TreeWillOWisp::collides(GameObject& other, const CollisionHit& ) {
00094   Lantern* lantern = dynamic_cast<Lantern*>(&other);
00095   if (lantern && lantern->is_open())
00096     return true;
00097   if (dynamic_cast<Player*>(&other))
00098     return true;
00099   
00100   return false;
00101 }
00102 
00103 void
00104 TreeWillOWisp::draw(DrawingContext& context)
00105 {
00106   sprite->draw(context, get_pos(), layer);
00107 
00108   context.push_target();
00109   context.set_target(DrawingContext::LIGHTMAP);
00110 
00111   sprite->draw(context, get_pos(), layer);
00112 
00113   context.pop_target();
00114 }
00115 
00116 void
00117 TreeWillOWisp::active_update(float elapsed_time)
00118 {
00119   // remove TreeWillOWisp if it has completely vanished
00120   if (mystate == STATE_VANISHING) {
00121     if(sprite->animation_done()) {
00122       remove_me();
00123       tree->willowisp_died(this);
00124     }
00125     return;
00126   }
00127 
00128   if (mystate == STATE_SUCKED) {
00129     Vector dir = suck_target - get_pos();
00130     if(dir.norm() < 5) {
00131       vanish();
00132       return;
00133     }
00134     Vector newpos = get_pos() + dir * elapsed_time;
00135     movement = newpos - get_pos();
00136     return;
00137   }
00138 
00139   angle = fmodf(angle + elapsed_time * speed, (float) (2*M_PI));
00140   Vector newpos(start_position + Vector(sin(angle) * radius, 0));
00141   movement = newpos - get_pos();
00142   float sizemod = cos(angle) * 0.8f;
00143   /* TODO: modify sprite size */
00144 
00145   sound_source->set_position(get_pos());
00146 
00147   if(sizemod < 0) {
00148     layer = LAYER_OBJECTS + 5;
00149   } else {
00150     layer = LAYER_OBJECTS - 20;
00151   }
00152 }
00153 
00154 void
00155 TreeWillOWisp::set_color(const Color& color)
00156 {
00157   this->color = color;
00158   sprite->set_color(color);
00159 }
00160 
00161 Color
00162 TreeWillOWisp::get_color() const
00163 {
00164   return color;
00165 }
00166 
00167 /* EOF */

Generated on Mon Jun 9 03:38:17 2014 for SuperTux by  doxygen 1.5.1