src/badguy/mole.cpp

Go to the documentation of this file.
00001 //  SuperTux - Mole Badguy
00002 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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 "audio/sound_manager.hpp"
00018 #include "badguy/mole.hpp"
00019 #include "badguy/mole_rock.hpp"
00020 #include "math/random_generator.hpp"
00021 #include "sprite/sprite.hpp"
00022 #include "supertux/object_factory.hpp"
00023 #include "supertux/sector.hpp"
00024 
00025 #include <math.h>
00026 
00027 static const float MOLE_WAIT_TIME = 0.2f; 
00028 static const float THROW_TIME = 4.6f; 
00029 static const float THROW_INTERVAL = 1; 
00030 static const float THROW_VELOCITY = 400; 
00032 Mole::Mole(const Reader& reader) :
00033   BadGuy(reader, "images/creatures/mole/mole.sprite", LAYER_TILES-1), 
00034   state(PRE_THROWING),
00035   timer(),
00036   throw_timer()
00037 {
00038   physic.enable_gravity(false);
00039   sound_manager->preload("sounds/fall.wav");
00040   sound_manager->preload("sounds/squish.wav");
00041   sound_manager->preload("sounds/dartfire.wav");
00042 }
00043 
00044 Mole::Mole(const Vector& pos) :
00045   BadGuy(pos, "images/creatures/mole/mole.sprite", LAYER_TILES-1), 
00046   state(PRE_THROWING),
00047   timer(),
00048   throw_timer()
00049 {
00050   physic.enable_gravity(false);
00051   sound_manager->preload("sounds/fall.wav");
00052   sound_manager->preload("sounds/squish.wav");
00053   sound_manager->preload("sounds/dartfire.wav");
00054 }
00055 
00056 void
00057 Mole::activate()
00058 {
00059   if (state != DEAD) set_state(PRE_THROWING);
00060 }
00061 
00062 void
00063 Mole::kill_fall()
00064 {
00065   set_state(DEAD);
00066   sound_manager->play("sounds/fall.wav", get_pos());
00067   run_dead_script();
00068 }
00069 
00070 HitResponse
00071 Mole::collision_badguy(BadGuy& , const CollisionHit& )
00072 {
00073   return FORCE_MOVE;
00074 }
00075 
00076 bool
00077 Mole::collision_squished(GameObject& )
00078 {
00079   set_state(DEAD);
00080   sound_manager->play("sounds/squish.wav", get_pos());
00081   run_dead_script();
00082   return true;
00083 }
00084 
00085 void
00086 Mole::throw_rock()
00087 {
00088   float px = get_bbox().get_middle().x;
00089   float py = get_bbox().get_middle().y;
00090 
00091   float angle = gameRandom.rand(90 - 15, 90 + 15) * (M_PI / 180);
00092   float vx = cos(angle) * THROW_VELOCITY;
00093   float vy = -sin(angle) * THROW_VELOCITY;
00094 
00095   sound_manager->play("sounds/dartfire.wav", get_pos());
00096   Sector::current()->add_object(new MoleRock(Vector(px, py), Vector(vx, vy), this));
00097 }
00098 
00099 void
00100 Mole::active_update(float elapsed_time)
00101 {
00102   BadGuy::active_update(elapsed_time);
00103 
00104   switch (state) {
00105     case PRE_THROWING:
00106       if (timer.check()) {
00107         set_state(THROWING);
00108       }
00109       break;
00110     case THROWING:
00111       if (throw_timer.check()) {
00112         throw_rock();
00113         throw_timer.start(THROW_INTERVAL);
00114       }
00115       if (timer.check()) {
00116         set_state(POST_THROWING);
00117       }
00118       break;
00119     case POST_THROWING:
00120       if (timer.check()) {
00121         set_state(PEEKING);
00122       }
00123       break;
00124     case PEEKING:
00125       if (sprite->animation_done()) {
00126         set_state(PRE_THROWING);
00127       }
00128       break;
00129     case DEAD:
00130       break;
00131   }
00132 
00133 }
00134 
00135 void
00136 Mole::set_state(MoleState new_state)
00137 {
00138   switch (new_state) {
00139     case PRE_THROWING:
00140       sprite->set_action("idle");
00141       set_colgroup_active(COLGROUP_DISABLED);
00142       timer.start(MOLE_WAIT_TIME);
00143       break;
00144     case THROWING:
00145       sprite->set_action("idle");
00146       set_colgroup_active(COLGROUP_DISABLED);
00147       timer.start(THROW_TIME);
00148       throw_timer.start(THROW_INTERVAL);
00149       break;
00150     case POST_THROWING:
00151       sprite->set_action("idle");
00152       set_colgroup_active(COLGROUP_DISABLED);
00153       timer.start(MOLE_WAIT_TIME);
00154       break;
00155     case PEEKING:
00156       sprite->set_action("peeking", 1);
00157       set_colgroup_active(COLGROUP_STATIC);
00158       break;
00159     case DEAD:
00160       sprite->set_action("idle");
00161       set_colgroup_active(COLGROUP_DISABLED);
00162       break;
00163   }
00164 
00165   state = new_state;
00166 }
00167 
00168 /* EOF */

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