Getting rid of nasty tabs
[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 namespace {
26   const float MUZZLE_Y = 25; /**< [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   state = IDLE;
39   set_colgroup_active(COLGROUP_DISABLED);
40   if (initial_delay == 0) initial_delay = 0.1f;
41 }
42
43 void
44 DartTrap::write(lisp::Writer& writer)
45 {
46   writer.start_list("darttrap");
47   writer.write_float("x", start_position.x);
48   writer.write_float("y", start_position.y);
49   writer.write_float("initial-delay", initial_delay);
50   writer.write_float("fire-delay", fire_delay);
51   writer.write_int("ammo", ammo);
52   writer.end_list("darttrap");
53 }
54
55 void
56 DartTrap::initialize()
57 {
58   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
59 }
60
61 void
62 DartTrap::activate()
63 {
64   fire_timer.start(initial_delay);
65 }
66
67 HitResponse
68 DartTrap::collision_player(Player& , const CollisionHit& )
69 {
70   return ABORT_MOVE;
71 }
72
73 void
74 DartTrap::active_update(float )
75 {
76   if (state == IDLE) {
77     if ((ammo != 0) && (fire_timer.check())) {
78       if (ammo > 0) ammo--;
79       load();
80       fire_timer.start(fire_delay);
81     }
82   }
83   if (state == LOADING) {
84     if (sprite->animation_done()) {
85       fire();
86     }
87   }
88 }
89
90 void
91 DartTrap::load()
92 {
93   state = LOADING;
94   sprite->set_action(dir == LEFT ? "loading-left" : "loading-right", 1);
95 }
96
97 void
98 DartTrap::fire()
99 {
100   float px = get_pos().x;
101   if (dir == RIGHT) px += 5;
102   float py = get_pos().y;
103   py += MUZZLE_Y;
104
105   sound_manager->play("sounds/dartfire.wav", get_pos());
106   Sector::current()->add_object(new Dart(Vector(px, py), dir, this));
107   state = IDLE;
108   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
109 }
110
111 IMPLEMENT_FACTORY(DartTrap, "darttrap")