fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / badguy / darttrap.cpp
1 //  $Id$
2 //
3 //  DartTrap - Shoots a Dart at regular intervals
4 //  Copyright (C) 2006 Christoph Sommer <supertux@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 namespace {
26   const float MUZZLE_Y = 28; /**< [px] muzzle y-offset from top */
27 }
28
29 DartTrap::DartTrap(const lisp::Lisp& reader)
30         : BadGuy(reader, "images/creatures/darttrap/darttrap.sprite", LAYER_TILES-1), initial_delay(0), fire_delay(2), ammo(-1), state(IDLE)
31 {
32   reader.get("initial-delay", initial_delay);
33   reader.get("fire-delay", fire_delay);
34   reader.get("ammo", ammo);
35   countMe = false;
36   sound_manager->preload("sounds/dartfire.wav");
37   if (start_dir == AUTO) log_warning << "Setting a DartTrap's direction to AUTO is no good idea" << std::endl;
38 }
39
40 void
41 DartTrap::write(lisp::Writer& writer)
42 {
43   writer.start_list("darttrap");
44   writer.write_float("x", start_position.x);
45   writer.write_float("y", start_position.y);
46   writer.write_float("initial-delay", initial_delay);
47   writer.write_float("fire-delay", fire_delay);
48   writer.write_int("ammo", ammo);
49   writer.end_list("darttrap");
50 }
51
52 void
53 DartTrap::activate()
54 {
55   state = IDLE;
56   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
57   set_group(COLGROUP_DISABLED);
58
59   if (initial_delay == 0) initial_delay = 0.1;
60   fire_timer.start(initial_delay);
61 }
62
63 HitResponse
64 DartTrap::collision_player(Player& , const CollisionHit& )
65 {
66   return ABORT_MOVE;
67 }
68
69 void
70 DartTrap::active_update(float )
71 {
72   if (state == IDLE) {
73     if ((ammo != 0) && (fire_timer.check())) {
74       if (ammo > 0) ammo--;
75       load();
76       fire_timer.start(fire_delay);
77     }
78   }
79   if (state == LOADING) {
80     if (sprite->animation_done()) {
81       fire();
82     }
83   }
84 }
85
86 void
87 DartTrap::load()
88 {
89   state = LOADING;
90   sprite->set_action(dir == LEFT ? "loading-left" : "loading-right", 1);
91 }
92
93 void
94 DartTrap::fire()
95 {
96   float px = get_pos().x;
97   if (dir == RIGHT) px += 5;
98   float py = get_pos().y;
99   py += MUZZLE_Y;
100
101   sound_manager->play("sounds/dartfire.wav", get_pos());
102   Sector::current()->add_object(new Dart(Vector(px, py), dir, this));
103   state = IDLE;
104   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
105 }
106
107 IMPLEMENT_FACTORY(DartTrap, "darttrap")