Badguys read hitbox from .sprite file
[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) : set_direction(true), initial_direction(LEFT), initial_delay(0), fire_delay(2), ammo(-1), state(IDLE)
30 {
31   reader.get("x", start_position.x);
32   reader.get("y", start_position.y);
33   reader.get("initial-delay", initial_delay);
34   reader.get("fire-delay", fire_delay);
35   reader.get("ammo", ammo);
36   sprite = sprite_manager->create("images/creatures/darttrap/darttrap.sprite");
37   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
38   countMe = false;
39 }
40
41 void
42 DartTrap::write(lisp::Writer& writer)
43 {
44   writer.start_list("darttrap");
45   writer.write_float("x", start_position.x);
46   writer.write_float("y", start_position.y);
47   writer.write_float("initial-delay", initial_delay);
48   writer.write_float("fire-delay", fire_delay);
49   writer.write_int("ammo", ammo);
50   writer.end_list("darttrap");
51 }
52
53 void
54 DartTrap::activate()
55 {
56   if (set_direction) dir = initial_direction;
57   state = IDLE;
58   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
59
60   if (initial_delay == 0) initial_delay = 0.1;
61   fire_timer.start(initial_delay);
62 }
63
64 HitResponse 
65 DartTrap::collision_player(Player& , const CollisionHit& )
66 {
67   return ABORT_MOVE;
68 }
69
70 void
71 DartTrap::active_update(float )
72 {
73   if (state == IDLE) {
74     if ((ammo != 0) && (fire_timer.check())) {
75       if (ammo > 0) ammo--;
76       load();
77       fire_timer.start(fire_delay);
78     }
79   }
80   if (state == LOADING) {
81     if (sprite->animation_done()) {
82       fire();
83     }
84   }
85 }
86
87 void
88 DartTrap::load()
89 {
90   state = LOADING;
91   sprite->set_action(dir == LEFT ? "loading-left" : "loading-right", 1);
92 }
93
94 void
95 DartTrap::fire()
96 {
97   float px = get_pos().x;
98   if (dir == RIGHT) px += 5;
99   float py = get_pos().y;
100   py += MUZZLE_Y;
101
102   sound_manager->play("sounds/squish.wav", get_pos());
103   Sector::current()->add_object(new Dart(px, py, dir, this));
104   state = IDLE;
105   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
106 }
107
108 IMPLEMENT_FACTORY(DartTrap, "darttrap")
109