Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / skullyhop.cpp
1 //  SkullyHop - A Hopping Skull
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/skullyhop.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 namespace {
25 const float VERTICAL_SPEED = -450;   /**< y-speed when jumping */
26 const float HORIZONTAL_SPEED = 220; /**< x-speed when jumping */
27 const float MIN_RECOVER_TIME = 0.1f; /**< minimum time to stand still before starting a (new) jump */
28 const float MAX_RECOVER_TIME = 1.0f; /**< maximum time to stand still before starting a (new) jump */
29 static const std::string HOP_SOUND = "sounds/hop.ogg";
30 }
31
32 SkullyHop::SkullyHop(const Reader& reader) :
33   BadGuy(reader, "images/creatures/skullyhop/skullyhop.sprite"),
34   recover_timer(),
35   state()
36 {
37   sound_manager->preload( HOP_SOUND );
38 }
39
40 SkullyHop::SkullyHop(const Vector& pos, Direction d) :
41   BadGuy(pos, d, "images/creatures/skullyhop/skullyhop.sprite"),
42   recover_timer(),
43   state()
44 {
45   sound_manager->preload( HOP_SOUND );
46 }
47
48 void
49 SkullyHop::initialize()
50 {
51   // initial state is JUMPING, because we might start airborne
52   state = JUMPING;
53   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
54 }
55
56 void
57 SkullyHop::set_state(SkullyHopState newState)
58 {
59   if (newState == STANDING) {
60     physic.set_velocity_x(0);
61     physic.set_velocity_y(0);
62     sprite->set_action(dir == LEFT ? "standing-left" : "standing-right");
63
64     float recover_time = systemRandom.randf(MIN_RECOVER_TIME,MAX_RECOVER_TIME);
65     recover_timer.start(recover_time);
66   } else
67     if (newState == CHARGING) {
68       sprite->set_action(dir == LEFT ? "charging-left" : "charging-right", 1);
69     } else
70       if (newState == JUMPING) {
71         sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
72         physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
73         physic.set_velocity_y(VERTICAL_SPEED);
74         sound_manager->play( HOP_SOUND, get_pos());
75       }
76
77   state = newState;
78 }
79
80 bool
81 SkullyHop::collision_squished(GameObject& object)
82 {
83   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
84   kill_squished(object);
85   return true;
86 }
87
88 void
89 SkullyHop::collision_solid(const CollisionHit& hit)
90 {
91   // just default behaviour (i.e. stop at floor/walls) when squished
92   if (BadGuy::get_state() == STATE_SQUISHED) {
93     BadGuy::collision_solid(hit);
94   }
95
96   // ignore collisions while standing still
97   if(state != JUMPING)
98     return;
99
100   // check if we hit the floor while falling
101   if(hit.bottom && physic.get_velocity_y() > 0 ) {
102     set_state(STANDING);
103   }
104   // check if we hit the roof while climbing
105   if(hit.top) {
106     physic.set_velocity_y(0);
107   }
108
109   // check if we hit left or right while moving in either direction
110   if(hit.left || hit.right) {
111     dir = dir == LEFT ? RIGHT : LEFT;
112     sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
113     physic.set_velocity_x(-0.25*physic.get_velocity_x());
114   }
115 }
116
117 HitResponse
118 SkullyHop::collision_badguy(BadGuy& , const CollisionHit& hit)
119 {
120   // behaviour for badguy collisions is the same as for collisions with solids
121   collision_solid(hit);
122
123   return CONTINUE;
124 }
125
126 void
127 SkullyHop::active_update(float elapsed_time)
128 {
129   BadGuy::active_update(elapsed_time);
130
131   // charge when fully recovered
132   if ((state == STANDING) && (recover_timer.check())) {
133     set_state(CHARGING);
134     return;
135   }
136
137   // jump as soon as charging animation completed
138   if ((state == CHARGING) && (sprite->animation_done())) {
139     set_state(JUMPING);
140     return;
141   }
142 }
143
144 IMPLEMENT_FACTORY(SkullyHop, "skullyhop");
145
146 /* EOF */