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 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)
58 if (newState == IDLE) {
59 physic.set_velocity_x(0);
60 physic.set_velocity_y(0);
61 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
63 recover_timer.start(RECOVER_TIME);
65 if (newState == JUMPING) {
66 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
67 physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
68 physic.set_velocity_y(VERTICAL_SPEED);
69 sound_manager->play( HOP_SOUND, get_pos());
71 if (newState == FALLING) {
72 Player* player = get_nearest_player();
74 if (player && (player->get_bbox().p2.x < get_bbox().p1.x) && (dir == RIGHT)) dir = LEFT;
75 if (player && (player->get_bbox().p1.x > get_bbox().p2.x) && (dir == LEFT)) dir = RIGHT;
76 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
83 Toad::collision_squished(GameObject& object)
85 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
86 kill_squished(object);
91 Toad::collision_solid(const CollisionHit& hit)
93 // just default behaviour (i.e. stop at floor/walls) when squished
94 if (BadGuy::get_state() == STATE_SQUISHED) {
95 BadGuy::collision_solid(hit);
99 // ignore collisions while standing still
104 // check if we hit left or right while moving in either direction
105 if(((physic.get_velocity_x() < 0) && hit.left) || ((physic.get_velocity_x() > 0) && hit.right)) {
107 dir = dir == LEFT ? RIGHT : LEFT;
108 if (state == JUMPING) {
109 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
111 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
114 physic.set_velocity_x(-0.25*physic.get_velocity_x());
117 // check if we hit the floor while falling
118 if ((state == FALLING) && hit.bottom) {
123 // check if we hit the roof while climbing
124 if ((state == JUMPING) && hit.top) {
125 physic.set_velocity_y(0);
131 Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
133 // behaviour for badguy collisions is the same as for collisions with solids
134 collision_solid(hit);
140 Toad::active_update(float elapsed_time)
142 BadGuy::active_update(elapsed_time);
144 // change sprite when we are falling
145 if ((state == JUMPING) && (physic.get_velocity_y() > 0)) {
150 // jump when fully recovered
151 if ((state == IDLE) && (recover_timer.check())) {
158 IMPLEMENT_FACTORY(Toad, "toad");