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