1 // SkullyHop - A Hopping Skull
2 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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/>.
17 #include "badguy/skullyhop.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
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";
32 SkullyHop::SkullyHop(const Reader& reader) :
33 BadGuy(reader, "images/creatures/skullyhop/skullyhop.sprite"),
37 sound_manager->preload( HOP_SOUND );
40 SkullyHop::SkullyHop(const Vector& pos, Direction d) :
41 BadGuy(pos, d, "images/creatures/skullyhop/skullyhop.sprite"),
45 sound_manager->preload( HOP_SOUND );
49 SkullyHop::initialize()
51 // initial state is JUMPING, because we might start airborne
53 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
57 SkullyHop::set_state(SkullyHopState newState)
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");
64 float recover_time = systemRandom.randf(MIN_RECOVER_TIME,MAX_RECOVER_TIME);
65 recover_timer.start(recover_time);
67 if (newState == CHARGING) {
68 sprite->set_action(dir == LEFT ? "charging-left" : "charging-right", 1);
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());
81 SkullyHop::collision_squished(GameObject& object)
83 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
84 kill_squished(object);
89 SkullyHop::collision_solid(const CollisionHit& hit)
91 // just default behaviour (i.e. stop at floor/walls) when squished
92 if (BadGuy::get_state() == STATE_SQUISHED) {
93 BadGuy::collision_solid(hit);
96 // ignore collisions while standing still
100 // check if we hit the floor while falling
101 if(hit.bottom && physic.get_velocity_y() > 0 ) {
104 // check if we hit the roof while climbing
106 physic.set_velocity_y(0);
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());
118 SkullyHop::collision_badguy(BadGuy& , const CollisionHit& hit)
120 // behaviour for badguy collisions is the same as for collisions with solids
121 collision_solid(hit);
127 SkullyHop::active_update(float elapsed_time)
129 BadGuy::active_update(elapsed_time);
131 // charge when fully recovered
132 if ((state == STANDING) && (recover_timer.check())) {
137 // jump as soon as charging animation completed
138 if ((state == CHARGING) && (sprite->animation_done())) {
144 IMPLEMENT_FACTORY(SkullyHop, "skullyhop");