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