Turned a lot of other global objects into Currentons
[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 TOAD_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   SoundManager::current()->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   SoundManager::current()->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
59   if (newState == IDLE) {
60     physic.set_velocity_x(0);
61     physic.set_velocity_y(0);
62     if (!frozen)
63       sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
64
65     recover_timer.start(TOAD_RECOVER_TIME);
66   } else
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       SoundManager::current()->play( HOP_SOUND, get_pos());
72     } else
73       if (newState == FALLING) {
74         Player* player = get_nearest_player();
75         // face 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");
79       }
80
81   state = newState;
82 }
83
84 bool
85 Toad::collision_squished(GameObject& object)
86 {
87   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
88   kill_squished(object);
89   return true;
90 }
91
92 void
93 Toad::collision_solid(const CollisionHit& hit)
94 {
95   // default behavior when frozen
96   if (frozen)
97   {
98     BadGuy::collision_solid(hit);
99     return;
100   }
101
102   // just default behaviour (i.e. stop at floor/walls) when squished
103   if (BadGuy::get_state() == STATE_SQUISHED) {
104     BadGuy::collision_solid(hit);
105     return;
106   }
107
108   // ignore collisions while standing still
109   if(state == IDLE) {
110     return;
111   }
112
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)) {
115     /*
116       dir = dir == LEFT ? RIGHT : LEFT;
117       if (state == JUMPING) {
118       sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
119       } else {
120       sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
121       }
122     */
123     physic.set_velocity_x(-0.25*physic.get_velocity_x());
124   }
125
126   // check if we hit the floor while falling
127   if ((state == FALLING) && hit.bottom) {
128     set_state(IDLE);
129     return;
130   }
131
132   // check if we hit the roof while climbing
133   if ((state == JUMPING) && hit.top) {
134     physic.set_velocity_y(0);
135   }
136
137 }
138
139 HitResponse
140 Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
141 {
142   // behaviour for badguy collisions is the same as for collisions with solids
143   collision_solid(hit);
144
145   return CONTINUE;
146 }
147
148 void
149 Toad::active_update(float elapsed_time)
150 {
151   BadGuy::active_update(elapsed_time);
152
153
154   // change sprite when we are falling and not frozen
155   if ((state == JUMPING) && (physic.get_velocity_y() > 0) && !frozen) {
156     set_state(FALLING);
157     return;
158   }
159
160   // jump when fully recovered and if not frozen
161   if ((state == IDLE) && (recover_timer.check() && !frozen)) {
162     set_state(JUMPING);
163     return;
164   }
165
166 }
167
168 void
169 Toad::unfreeze()
170 {
171   BadGuy::unfreeze();
172   initialize();
173 }
174
175 bool
176 Toad::is_freezable() const
177 {
178   return true;
179 }
180
181 /* EOF */