3 // SkullyHop - A Hopping Skull
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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
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"
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";
37 SkullyHop::SkullyHop(const lisp::Lisp& reader)
38 : BadGuy(reader, "images/creatures/skullyhop/skullyhop.sprite")
40 sound_manager->preload( HOP_SOUND );
43 SkullyHop::SkullyHop(const Vector& pos, Direction d)
44 : BadGuy(pos, d, "images/creatures/skullyhop/skullyhop.sprite")
46 sound_manager->preload( HOP_SOUND );
50 SkullyHop::write(lisp::Writer& writer)
52 writer.start_list("skullyhop");
53 writer.write("x", start_position.x);
54 writer.write("y", start_position.y);
55 writer.end_list("skullyhop");
59 SkullyHop::initialize()
61 // initial state is JUMPING, because we might start airborne
63 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
67 SkullyHop::set_state(SkullyHopState newState)
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");
74 float recover_time = systemRandom.randf(MIN_RECOVER_TIME,MAX_RECOVER_TIME);
75 recover_timer.start(recover_time);
77 if (newState == CHARGING) {
78 sprite->set_action(dir == LEFT ? "charging-left" : "charging-right", 1);
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());
91 SkullyHop::collision_squished(GameObject& object)
93 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
94 kill_squished(object);
99 SkullyHop::collision_solid(const CollisionHit& hit)
101 // just default behaviour (i.e. stop at floor/walls) when squished
102 if (BadGuy::get_state() == STATE_SQUISHED) {
103 BadGuy::collision_solid(hit);
106 // ignore collisions while standing still
110 // check if we hit the floor while falling
111 if(hit.bottom && physic.get_velocity_y() > 0 ) {
114 // check if we hit the roof while climbing
116 physic.set_velocity_y(0);
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());
128 SkullyHop::collision_badguy(BadGuy& , const CollisionHit& hit)
130 // behaviour for badguy collisions is the same as for collisions with solids
131 collision_solid(hit);
137 SkullyHop::active_update(float elapsed_time)
139 BadGuy::active_update(elapsed_time);
141 // charge when fully recovered
142 if ((state == STANDING) && (recover_timer.check())) {
147 // jump as soon as charging animation completed
148 if ((state == CHARGING) && (sprite->animation_done())) {
154 IMPLEMENT_FACTORY(SkullyHop, "skullyhop")