00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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