3 // Toad - A jumping toad
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
24 #include "random_generator.hpp"
25 #include "lisp/writer.hpp"
26 #include "object_factory.hpp"
27 #include "object/player.hpp"
28 #include "audio/sound_manager.hpp"
29 #include "sprite/sprite.hpp"
32 const float VERTICAL_SPEED = -450; /**< y-speed when jumping */
33 const float HORIZONTAL_SPEED = 320; /**< x-speed when jumping */
34 const float RECOVER_TIME = 0.5; /**< time to stand still before starting a (new) jump */
35 static const std::string HOP_SOUND = "sounds/hop.ogg";
38 Toad::Toad(const lisp::Lisp& reader)
39 : BadGuy(reader, "images/creatures/toad/toad.sprite")
41 sound_manager->preload(HOP_SOUND);
44 Toad::Toad(const Vector& pos, Direction d)
45 : BadGuy(pos, d, "images/creatures/toad/toad.sprite")
47 sound_manager->preload(HOP_SOUND);
51 Toad::write(lisp::Writer& writer)
53 writer.start_list("toad");
54 writer.write("x", start_position.x);
55 writer.write("y", start_position.y);
56 writer.end_list("toad");
62 // initial state is JUMPING, because we might start airborne
64 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
68 Toad::set_state(ToadState newState)
70 if (newState == IDLE) {
71 physic.set_velocity_x(0);
72 physic.set_velocity_y(0);
73 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
75 recover_timer.start(RECOVER_TIME);
77 if (newState == JUMPING) {
78 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
79 physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
80 physic.set_velocity_y(VERTICAL_SPEED);
81 sound_manager->play( HOP_SOUND, get_pos());
83 if (newState == FALLING) {
84 Player* player = get_nearest_player();
86 if (player && (player->get_bbox().p2.x < get_bbox().p1.x) && (dir == RIGHT)) dir = LEFT;
87 if (player && (player->get_bbox().p1.x > get_bbox().p2.x) && (dir == LEFT)) dir = RIGHT;
88 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
95 Toad::collision_squished(GameObject& object)
97 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
98 kill_squished(object);
103 Toad::collision_solid(const CollisionHit& hit)
105 // just default behaviour (i.e. stop at floor/walls) when squished
106 if (BadGuy::get_state() == STATE_SQUISHED) {
107 BadGuy::collision_solid(hit);
111 // ignore collisions while standing still
116 // check if we hit left or right while moving in either direction
117 if(((physic.get_velocity_x() < 0) && hit.left) || ((physic.get_velocity_x() > 0) && hit.right)) {
119 dir = dir == LEFT ? RIGHT : LEFT;
120 if (state == JUMPING) {
121 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
123 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
126 physic.set_velocity_x(-0.25*physic.get_velocity_x());
129 // check if we hit the floor while falling
130 if ((state == FALLING) && hit.bottom) {
135 // check if we hit the roof while climbing
136 if ((state == JUMPING) && hit.top) {
137 physic.set_velocity_y(0);
143 Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
145 // behaviour for badguy collisions is the same as for collisions with solids
146 collision_solid(hit);
152 Toad::active_update(float elapsed_time)
154 BadGuy::active_update(elapsed_time);
156 // change sprite when we are falling
157 if ((state == JUMPING) && (physic.get_velocity_y() > 0)) {
162 // jump when fully recovered
163 if ((state == IDLE) && (recover_timer.check())) {
170 IMPLEMENT_FACTORY(Toad, "toad")