8d78c97dddbb1683c38b7dc8ea6f842c758e4800
[supertux.git] / src / badguy / darttrap.cpp
1 //  $Id$
2 //
3 //  DartTrap - Shoots a Dart at regular intervals
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "darttrap.hpp"
23 #include "dart.hpp"
24
25 #include "object_factory.hpp"
26 #include "audio/sound_manager.hpp"
27 #include "lisp/writer.hpp"
28 #include "sector.hpp"
29 #include "lisp/lisp.hpp"
30 #include "sprite/sprite.hpp"
31
32 namespace {
33   const float MUZZLE_Y = 25; /**< [px] muzzle y-offset from top */
34 }
35
36 DartTrap::DartTrap(const lisp::Lisp& reader)
37   : BadGuy(reader, "images/creatures/darttrap/darttrap.sprite", LAYER_TILES-1), initial_delay(0), fire_delay(2), ammo(-1), state(IDLE)
38 {
39   reader.get("initial-delay", initial_delay);
40   reader.get("fire-delay", fire_delay);
41   reader.get("ammo", ammo);
42   countMe = false;
43   sound_manager->preload("sounds/dartfire.wav");
44   if (start_dir == AUTO) log_warning << "Setting a DartTrap's direction to AUTO is no good idea" << std::endl;
45   state = IDLE;
46   set_colgroup_active(COLGROUP_DISABLED);
47   if (initial_delay == 0) initial_delay = 0.1f;
48 }
49
50 void
51 DartTrap::write(lisp::Writer& writer)
52 {
53   writer.start_list("darttrap");
54   writer.write("x", start_position.x);
55   writer.write("y", start_position.y);
56   writer.write("initial-delay", initial_delay);
57   writer.write("fire-delay", fire_delay);
58   writer.write("ammo", ammo);
59   writer.end_list("darttrap");
60 }
61
62 void
63 DartTrap::initialize()
64 {
65   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
66 }
67
68 void
69 DartTrap::activate()
70 {
71   fire_timer.start(initial_delay);
72 }
73
74 HitResponse
75 DartTrap::collision_player(Player& , const CollisionHit& )
76 {
77   return ABORT_MOVE;
78 }
79
80 void
81 DartTrap::active_update(float )
82 {
83   if (state == IDLE) {
84     if ((ammo != 0) && (fire_timer.check())) {
85       if (ammo > 0) ammo--;
86       load();
87       fire_timer.start(fire_delay);
88     }
89   }
90   if (state == LOADING) {
91     if (sprite->animation_done()) {
92       fire();
93     }
94   }
95 }
96
97 void
98 DartTrap::load()
99 {
100   state = LOADING;
101   sprite->set_action(dir == LEFT ? "loading-left" : "loading-right", 1);
102 }
103
104 void
105 DartTrap::fire()
106 {
107   float px = get_pos().x;
108   if (dir == RIGHT) px += 5;
109   float py = get_pos().y;
110   py += MUZZLE_Y;
111
112   sound_manager->play("sounds/dartfire.wav", get_pos());
113   Sector::current()->add_object(new Dart(Vector(px, py), dir, this));
114   state = IDLE;
115   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
116 }
117
118 IMPLEMENT_FACTORY(DartTrap, "darttrap")