1 // Toad - A jumping toad
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/toad.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "object/player.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 = 320; /**< x-speed when jumping */
27 const float TOAD_RECOVER_TIME = 0.5; /**< time to stand still before starting a (new) jump */
28 static const std::string HOP_SOUND = "sounds/hop.ogg";
31 Toad::Toad(const Reader& reader) :
32 BadGuy(reader, "images/creatures/toad/toad.sprite"),
36 sound_manager->preload(HOP_SOUND);
39 Toad::Toad(const Vector& pos, Direction d) :
40 BadGuy(pos, d, "images/creatures/toad/toad.sprite"),
44 sound_manager->preload(HOP_SOUND);
50 // initial state is JUMPING, because we might start airborne
52 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
56 Toad::set_state(ToadState newState)
59 if (newState == IDLE) {
60 physic.set_velocity_x(0);
61 physic.set_velocity_y(0);
63 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
65 recover_timer.start(TOAD_RECOVER_TIME);
67 if (newState == JUMPING) {
68 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
69 physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
70 physic.set_velocity_y(VERTICAL_SPEED);
71 sound_manager->play( HOP_SOUND, get_pos());
73 if (newState == FALLING) {
74 Player* player = get_nearest_player();
76 if (player && (player->get_bbox().p2.x < get_bbox().p1.x) && (dir == RIGHT)) dir = LEFT;
77 if (player && (player->get_bbox().p1.x > get_bbox().p2.x) && (dir == LEFT)) dir = RIGHT;
78 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
85 Toad::collision_squished(GameObject& object)
87 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
88 kill_squished(object);
93 Toad::collision_solid(const CollisionHit& hit)
95 // default behavior when frozen
98 BadGuy::collision_solid(hit);
102 // just default behaviour (i.e. stop at floor/walls) when squished
103 if (BadGuy::get_state() == STATE_SQUISHED) {
104 BadGuy::collision_solid(hit);
108 // ignore collisions while standing still
113 // check if we hit left or right while moving in either direction
114 if(((physic.get_velocity_x() < 0) && hit.left) || ((physic.get_velocity_x() > 0) && hit.right)) {
116 dir = dir == LEFT ? RIGHT : LEFT;
117 if (state == JUMPING) {
118 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
120 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
123 physic.set_velocity_x(-0.25*physic.get_velocity_x());
126 // check if we hit the floor while falling
127 if ((state == FALLING) && hit.bottom) {
132 // check if we hit the roof while climbing
133 if ((state == JUMPING) && hit.top) {
134 physic.set_velocity_y(0);
140 Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
142 // behaviour for badguy collisions is the same as for collisions with solids
143 collision_solid(hit);
149 Toad::active_update(float elapsed_time)
151 BadGuy::active_update(elapsed_time);
154 // change sprite when we are falling and not frozen
155 if ((state == JUMPING) && (physic.get_velocity_y() > 0) && !frozen) {
160 // jump when fully recovered and if not frozen
161 if ((state == IDLE) && (recover_timer.check() && !frozen)) {
176 Toad::is_freezable() const