src/badguy/darttrap.cpp

Go to the documentation of this file.
00001 //  DartTrap - Shoots a Dart at regular intervals
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 "badguy/dart.hpp"
00018 #include "badguy/darttrap.hpp"
00019 
00020 #include "audio/sound_manager.hpp"
00021 #include "sprite/sprite.hpp"
00022 #include "supertux/object_factory.hpp"
00023 #include "supertux/sector.hpp"
00024 #include "util/reader.hpp"
00025 
00026 namespace {
00027 const float MUZZLE_Y = 25; 
00028 }
00029 
00030 DartTrap::DartTrap(const Reader& reader) :
00031   BadGuy(reader, "images/creatures/darttrap/darttrap.sprite", LAYER_TILES-1),
00032   initial_delay(0), 
00033   fire_delay(2), 
00034   ammo(-1), 
00035   state(IDLE),
00036   fire_timer()
00037 {
00038   reader.get("initial-delay", initial_delay);
00039   reader.get("fire-delay", fire_delay);
00040   reader.get("ammo", ammo);
00041   countMe = false;
00042   sound_manager->preload("sounds/dartfire.wav");
00043   if (start_dir == AUTO) log_warning << "Setting a DartTrap's direction to AUTO is no good idea" << std::endl;
00044   state = IDLE;
00045   set_colgroup_active(COLGROUP_DISABLED);
00046   if (initial_delay == 0) initial_delay = 0.1f;
00047 }
00048 
00049 void
00050 DartTrap::initialize()
00051 {
00052   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00053 }
00054 
00055 void
00056 DartTrap::activate()
00057 {
00058   fire_timer.start(initial_delay);
00059 }
00060 
00061 HitResponse
00062 DartTrap::collision_player(Player& , const CollisionHit& )
00063 {
00064   return ABORT_MOVE;
00065 }
00066 
00067 void
00068 DartTrap::active_update(float )
00069 {
00070   if (state == IDLE) {
00071     if ((ammo != 0) && (fire_timer.check())) {
00072       if (ammo > 0) ammo--;
00073       load();
00074       fire_timer.start(fire_delay);
00075     }
00076   }
00077   if (state == LOADING) {
00078     if (sprite->animation_done()) {
00079       fire();
00080     }
00081   }
00082 }
00083 
00084 void
00085 DartTrap::load()
00086 {
00087   state = LOADING;
00088   sprite->set_action(dir == LEFT ? "loading-left" : "loading-right", 1);
00089 }
00090 
00091 void
00092 DartTrap::fire()
00093 {
00094   float px = get_pos().x;
00095   if (dir == RIGHT) px += 5;
00096   float py = get_pos().y;
00097   py += MUZZLE_Y;
00098 
00099   sound_manager->play("sounds/dartfire.wav", get_pos());
00100   Sector::current()->add_object(new Dart(Vector(px, py), dir, this));
00101   state = IDLE;
00102   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00103 }
00104 
00105 /* EOF */

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