1 //  DartTrap - Shoots a Dart at regular intervals
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 #include "badguy/darttrap.hpp"
18 
19 #include "audio/sound_manager.hpp"
20 #include "audio/sound_source.hpp"
21 #include "badguy/dart.hpp"
22 #include "editor/editor.hpp"
23 #include "sprite/sprite.hpp"
24 #include "supertux/sector.hpp"
25 #include "util/log.hpp"
26 #include "util/reader_mapping.hpp"
27 
28 namespace {
29 const float MUZZLE_Y = 25; /**< [px] muzzle y-offset from top */
30 }
31 
DartTrap(const ReaderMapping & reader)32 DartTrap::DartTrap(const ReaderMapping& reader) :
33   BadGuy(reader, "images/creatures/darttrap/darttrap.sprite", LAYER_TILES-1),
34   enabled(true),
35   initial_delay(),
36   fire_delay(),
37   ammo(),
38   state(IDLE),
39   fire_timer()
40 {
41   reader.get("enabled", enabled, true);
42   reader.get("initial-delay", initial_delay, 0.0f);
43   reader.get("fire-delay", fire_delay, 2.0f);
44   reader.get("ammo", ammo, -1);
45   m_countMe = false;
46   SoundManager::current()->preload("sounds/dartfire.wav");
47   if (m_start_dir == Direction::AUTO) { log_warning << "Setting a DartTrap's direction to AUTO is no good idea" << std::endl; }
48   state = IDLE;
49   set_colgroup_active(COLGROUP_DISABLED);
50 
51   if (!Editor::is_active()) {
52     if (initial_delay == 0) initial_delay = 0.1f;
53   }
54 }
55 
56 void
initialize()57 DartTrap::initialize()
58 {
59   m_sprite->set_action(m_dir == Direction::LEFT ? "idle-left" : "idle-right");
60 }
61 
62 void
activate()63 DartTrap::activate()
64 {
65   fire_timer.start(initial_delay);
66 }
67 
68 HitResponse
collision_player(Player &,const CollisionHit &)69 DartTrap::collision_player(Player& , const CollisionHit& )
70 {
71   return ABORT_MOVE;
72 }
73 
74 void
active_update(float)75 DartTrap::active_update(float )
76 {
77   if (!enabled) {
78     return;
79   }
80   switch (state) {
81     case IDLE:
82       if ((ammo != 0) && (fire_timer.check())) {
83         if (ammo > 0) ammo--;
84         load();
85         fire_timer.start(fire_delay);
86       }
87       break;
88 
89     case LOADING:
90       if (m_sprite->animation_done()) {
91         fire();
92       }
93       break;
94 
95     default:
96       break;
97   }
98 }
99 
100 void
load()101 DartTrap::load()
102 {
103   state = LOADING;
104   m_sprite->set_action(m_dir == Direction::LEFT ? "loading-left" : "loading-right", 1);
105 }
106 
107 void
fire()108 DartTrap::fire()
109 {
110   float px = get_pos().x;
111   if (m_dir == Direction::RIGHT) px += 5;
112   float py = get_pos().y;
113   py += MUZZLE_Y;
114 
115   SoundManager::current()->play("sounds/dartfire.wav", get_pos());
116   Sector::get().add<Dart>(Vector(px, py), m_dir, this);
117   state = IDLE;
118   m_sprite->set_action(m_dir == Direction::LEFT ? "idle-left" : "idle-right");
119 }
120 
121 ObjectSettings
get_settings()122 DartTrap::get_settings()
123 {
124   ObjectSettings result = BadGuy::get_settings();
125 
126   result.add_float(_("Initial delay"), &initial_delay, "initial-delay");
127   result.add_bool(_("Enabled"), &enabled, "enabled", true);
128   result.add_float(_("Fire delay"), &fire_delay, "fire-delay");
129   result.add_int(_("Ammo"), &ammo, "ammo");
130 
131   result.reorder({"initial-delay", "fire-delay", "ammo", "direction", "x", "y"});
132 
133   return result;
134 }
135 
136 /* EOF */
137