Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / darttrap.cpp
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/dart.hpp"
18 #include "badguy/darttrap.hpp"
19
20 #include "audio/sound_manager.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
24
25 namespace {
26 const float MUZZLE_Y = 25; /**< [px] muzzle y-offset from top */
27 }
28
29 DartTrap::DartTrap(const Reader& reader) :
30   BadGuy(reader, "images/creatures/darttrap/darttrap.sprite", LAYER_TILES-1),
31   initial_delay(0), 
32   fire_delay(2), 
33   ammo(-1), 
34   state(IDLE),
35   fire_timer()
36 {
37   reader.get("initial-delay", initial_delay);
38   reader.get("fire-delay", fire_delay);
39   reader.get("ammo", ammo);
40   countMe = false;
41   sound_manager->preload("sounds/dartfire.wav");
42   if (start_dir == AUTO) log_warning << "Setting a DartTrap's direction to AUTO is no good idea" << std::endl;
43   state = IDLE;
44   set_colgroup_active(COLGROUP_DISABLED);
45   if (initial_delay == 0) initial_delay = 0.1f;
46 }
47
48 void
49 DartTrap::initialize()
50 {
51   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
52 }
53
54 void
55 DartTrap::activate()
56 {
57   fire_timer.start(initial_delay);
58 }
59
60 HitResponse
61 DartTrap::collision_player(Player& , const CollisionHit& )
62 {
63   return ABORT_MOVE;
64 }
65
66 void
67 DartTrap::active_update(float )
68 {
69   if (state == IDLE) {
70     if ((ammo != 0) && (fire_timer.check())) {
71       if (ammo > 0) ammo--;
72       load();
73       fire_timer.start(fire_delay);
74     }
75   }
76   if (state == LOADING) {
77     if (sprite->animation_done()) {
78       fire();
79     }
80   }
81 }
82
83 void
84 DartTrap::load()
85 {
86   state = LOADING;
87   sprite->set_action(dir == LEFT ? "loading-left" : "loading-right", 1);
88 }
89
90 void
91 DartTrap::fire()
92 {
93   float px = get_pos().x;
94   if (dir == RIGHT) px += 5;
95   float py = get_pos().y;
96   py += MUZZLE_Y;
97
98   sound_manager->play("sounds/dartfire.wav", get_pos());
99   Sector::current()->add_object(new Dart(Vector(px, py), dir, this));
100   state = IDLE;
101   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
102 }
103
104 IMPLEMENT_FACTORY(DartTrap, "darttrap");
105
106 /* EOF */