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
23 #include "random_generator.hpp"
26 const float VERTICAL_SPEED = -450; /**< y-speed when jumping */
27 const float HORIZONTAL_SPEED = 320; /**< x-speed when jumping */
28 const float RECOVER_TIME = 0.5; /**< time to stand still before starting a (new) jump */
29 static const std::string HOP_SOUND = "sounds/hop.ogg";
32 Toad::Toad(const lisp::Lisp& reader)
33 : BadGuy(reader, "images/creatures/toad/toad.sprite")
35 sound_manager->preload(HOP_SOUND);
38 Toad::Toad(const Vector& pos, Direction d)
39 : BadGuy(pos, d, "images/creatures/toad/toad.sprite")
41 sound_manager->preload(HOP_SOUND);
45 Toad::write(lisp::Writer& writer)
47 writer.start_list("toad");
48 writer.write("x", start_position.x);
49 writer.write("y", start_position.y);
50 writer.end_list("toad");
56 // initial state is JUMPING, because we might start airborne
58 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
62 Toad::set_state(ToadState newState)
64 if (newState == IDLE) {
65 physic.set_velocity_x(0);
66 physic.set_velocity_y(0);
67 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
69 recover_timer.start(RECOVER_TIME);
71 if (newState == JUMPING) {
72 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
73 physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
74 physic.set_velocity_y(VERTICAL_SPEED);
75 sound_manager->play( HOP_SOUND, get_pos());
77 if (newState == FALLING) {
78 Player* player = get_nearest_player();
80 if (player && (player->get_bbox().p2.x < get_bbox().p1.x) && (dir == RIGHT)) dir = LEFT;
81 if (player && (player->get_bbox().p1.x > get_bbox().p2.x) && (dir == LEFT)) dir = RIGHT;
82 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
89 Toad::collision_squished(GameObject& object)
91 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
92 kill_squished(object);
97 Toad::collision_solid(const CollisionHit& hit)
99 // just default behaviour (i.e. stop at floor/walls) when squished
100 if (BadGuy::get_state() == STATE_SQUISHED) {
101 BadGuy::collision_solid(hit);
105 // ignore collisions while standing still
110 // check if we hit left or right while moving in either direction
111 if(((physic.get_velocity_x() < 0) && hit.left) || ((physic.get_velocity_x() > 0) && hit.right)) {
113 dir = dir == LEFT ? RIGHT : LEFT;
114 if (state == JUMPING) {
115 sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
117 sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
120 physic.set_velocity_x(-0.25*physic.get_velocity_x());
123 // check if we hit the floor while falling
124 if ((state == FALLING) && hit.bottom) {
129 // check if we hit the roof while climbing
130 if ((state == JUMPING) && hit.top) {
131 physic.set_velocity_y(0);
137 Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
139 // behaviour for badguy collisions is the same as for collisions with solids
140 collision_solid(hit);
146 Toad::active_update(float elapsed_time)
148 BadGuy::active_update(elapsed_time);
150 // change sprite when we are falling
151 if ((state == JUMPING) && (physic.get_velocity_y() > 0)) {
156 // jump when fully recovered
157 if ((state == IDLE) && (recover_timer.check())) {
164 IMPLEMENT_FACTORY(Toad, "toad")