Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / toad.cpp
1 //  Toad - A jumping toad
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "badguy/toad.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 namespace {
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";
29 }
30
31 Toad::Toad(const Reader& reader) :
32   BadGuy(reader, "images/creatures/toad/toad.sprite"),
33   recover_timer(),
34   state()
35 {
36   sound_manager->preload(HOP_SOUND);
37 }
38
39 Toad::Toad(const Vector& pos, Direction d) :
40   BadGuy(pos, d, "images/creatures/toad/toad.sprite"),
41   recover_timer(),
42   state()
43 {
44   sound_manager->preload(HOP_SOUND);
45 }
46
47 void
48 Toad::initialize()
49 {
50   // initial state is JUMPING, because we might start airborne
51   state = JUMPING;
52   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
53 }
54
55 void
56 Toad::set_state(ToadState newState)
57 {
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");
62
63     recover_timer.start(RECOVER_TIME);
64   } else
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());
70     } else
71       if (newState == FALLING) {
72         Player* player = get_nearest_player();
73         // face 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");
77       }
78
79   state = newState;
80 }
81
82 bool
83 Toad::collision_squished(GameObject& object)
84 {
85   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
86   kill_squished(object);
87   return true;
88 }
89
90 void
91 Toad::collision_solid(const CollisionHit& hit)
92 {
93   // just default behaviour (i.e. stop at floor/walls) when squished
94   if (BadGuy::get_state() == STATE_SQUISHED) {
95     BadGuy::collision_solid(hit);
96     return;
97   }
98
99   // ignore collisions while standing still
100   if(state == IDLE) {
101     return;
102   }
103
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)) {
106     /*
107       dir = dir == LEFT ? RIGHT : LEFT;
108       if (state == JUMPING) {
109       sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
110       } else {
111       sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
112       }
113     */
114     physic.set_velocity_x(-0.25*physic.get_velocity_x());
115   }
116
117   // check if we hit the floor while falling
118   if ((state == FALLING) && hit.bottom) {
119     set_state(IDLE);
120     return;
121   }
122
123   // check if we hit the roof while climbing
124   if ((state == JUMPING) && hit.top) {
125     physic.set_velocity_y(0);
126   }
127
128 }
129
130 HitResponse
131 Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
132 {
133   // behaviour for badguy collisions is the same as for collisions with solids
134   collision_solid(hit);
135
136   return CONTINUE;
137 }
138
139 void
140 Toad::active_update(float elapsed_time)
141 {
142   BadGuy::active_update(elapsed_time);
143
144   // change sprite when we are falling
145   if ((state == JUMPING) && (physic.get_velocity_y() > 0)) {
146     set_state(FALLING);
147     return;
148   }
149
150   // jump when fully recovered
151   if ((state == IDLE) && (recover_timer.check())) {
152     set_state(JUMPING);
153     return;
154   }
155
156 }
157
158 IMPLEMENT_FACTORY(Toad, "toad");
159
160 /* EOF */