8bf61d7da4bb7e093b4b8917a69f7c4878f1ee91
[supertux.git] / src / badguy / skullyhop.cpp
1 //  $Id$
2 //
3 //  SkullyHop - A Hopping Skull
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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "skullyhop.hpp"
23 #include "random_generator.hpp"
24
25 namespace {
26   const float VERTICAL_SPEED = -450;   /**< y-speed when jumping */
27   const float HORIZONTAL_SPEED = 220; /**< x-speed when jumping */
28   const float MIN_RECOVER_TIME = 0.1; /**< minimum time to stand still before starting a (new) jump */
29   const float MAX_RECOVER_TIME = 1.0; /**< maximum time to stand still before starting a (new) jump */
30   static const std::string HOP_SOUND = "sounds/hop.ogg";
31 }
32
33 SkullyHop::SkullyHop(const lisp::Lisp& reader)
34         : BadGuy(reader, "images/creatures/skullyhop/skullyhop.sprite")
35 {
36   sound_manager->preload( HOP_SOUND );
37 }
38
39 SkullyHop::SkullyHop(const Vector& pos, Direction d)
40         : BadGuy(pos, d, "images/creatures/skullyhop/skullyhop.sprite")
41 {
42   sound_manager->preload( HOP_SOUND );
43 }
44
45 void
46 SkullyHop::write(lisp::Writer& writer)
47 {
48   writer.start_list("skullyhop");
49   writer.write_float("x", start_position.x);
50   writer.write_float("y", start_position.y);
51   writer.end_list("skullyhop");
52 }
53
54 void
55 SkullyHop::activate()
56 {
57   // initial state is JUMPING, because we might start airborne
58   state = JUMPING;
59   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
60 }
61
62 void
63 SkullyHop::set_state(SkullyHopState newState)
64 {
65   if (newState == STANDING) {
66     physic.set_velocity_x(0);
67     physic.set_velocity_y(0);
68     sprite->set_action(dir == LEFT ? "standing-left" : "standing-right");
69
70     float recover_time = systemRandom.randf(MIN_RECOVER_TIME,MAX_RECOVER_TIME);
71     recover_timer.start(recover_time);
72   } else
73   if (newState == CHARGING) {
74     sprite->set_action(dir == LEFT ? "charging-left" : "charging-right", 1);
75   } else
76   if (newState == JUMPING) {
77     sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
78     physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
79     physic.set_velocity_y(VERTICAL_SPEED);
80     sound_manager->play( HOP_SOUND, get_pos());
81   }
82
83   state = newState;
84 }
85
86 bool
87 SkullyHop::collision_squished(GameObject& object)
88 {
89   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
90   kill_squished(object);
91   return true;
92 }
93
94 void
95 SkullyHop::collision_solid(const CollisionHit& hit)
96 {
97   // just default behaviour (i.e. stop at floor/walls) when squished
98   if (BadGuy::get_state() == STATE_SQUISHED) {
99     BadGuy::collision_solid(hit);
100   }
101
102   // ignore collisions while standing still
103   if(state != JUMPING)
104     return;
105
106   // check if we hit the floor while falling
107   if(hit.bottom && physic.get_velocity_y() > 0 ) {
108     set_state(STANDING);
109   }
110   // check if we hit the roof while climbing
111   if(hit.top) {
112     physic.set_velocity_y(0);
113   }
114
115   // check if we hit left or right while moving in either direction
116   if(hit.left || hit.right) {
117     dir = dir == LEFT ? RIGHT : LEFT;
118     sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
119     physic.set_velocity_x(-0.25*physic.get_velocity_x());
120   }
121 }
122
123 HitResponse
124 SkullyHop::collision_badguy(BadGuy& , const CollisionHit& hit)
125 {
126   // behaviour for badguy collisions is the same as for collisions with solids
127   collision_solid(hit);
128
129   return CONTINUE;
130 }
131
132 void
133 SkullyHop::active_update(float elapsed_time)
134 {
135   BadGuy::active_update(elapsed_time);
136
137   // charge when fully recovered
138   if ((state == STANDING) && (recover_timer.check())) {
139     set_state(CHARGING);
140     return;
141   }
142
143   // jump as soon as charging animation completed
144   if ((state == CHARGING) && (sprite->animation_done())) {
145     set_state(JUMPING);
146     return;
147   }
148 }
149
150 IMPLEMENT_FACTORY(SkullyHop, "skullyhop")