Make it build with -DCOMPILE_AMALGATION=ON. Still not certain how intern_draw/next_po...
[supertux.git] / src / badguy / skullyhop.cpp
1 //  SkullyHop - A Hopping Skull
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/skullyhop.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 namespace {
25 const float MIN_RECOVER_TIME = 0.1f; /**< minimum time to stand still before starting a (new) jump */
26 const float MAX_RECOVER_TIME = 1.0f; /**< maximum time to stand still before starting a (new) jump */
27 static const std::string SKULLYHOP_SOUND = "sounds/hop.ogg";
28 }
29
30 SkullyHop::SkullyHop(const Reader& reader) :
31   BadGuy(reader, "images/creatures/skullyhop/skullyhop.sprite"),
32   recover_timer(),
33   state()
34 {
35   sound_manager->preload( SKULLYHOP_SOUND );
36 }
37
38 SkullyHop::SkullyHop(const Vector& pos, Direction d) :
39   BadGuy(pos, d, "images/creatures/skullyhop/skullyhop.sprite"),
40   recover_timer(),
41   state()
42 {
43   sound_manager->preload( SKULLYHOP_SOUND );
44 }
45
46 void
47 SkullyHop::initialize()
48 {
49   // initial state is JUMPING, because we might start airborne
50   state = JUMPING;
51   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
52 }
53
54 void
55 SkullyHop::set_state(SkullyHopState newState)
56 {
57   if (newState == STANDING) {
58     physic.set_velocity_x(0);
59     physic.set_velocity_y(0);
60     sprite->set_action(dir == LEFT ? "standing-left" : "standing-right");
61
62     float recover_time = systemRandom.randf(MIN_RECOVER_TIME,MAX_RECOVER_TIME);
63     recover_timer.start(recover_time);
64   } else
65     if (newState == CHARGING) {
66       sprite->set_action(dir == LEFT ? "charging-left" : "charging-right", 1);
67     } else
68       if (newState == JUMPING) {
69         sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
70 const float HORIZONTAL_SPEED = 220; /**< x-speed when jumping */
71         physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
72 const float VERTICAL_SPEED = -450;   /**< y-speed when jumping */
73         physic.set_velocity_y(VERTICAL_SPEED);
74         sound_manager->play( SKULLYHOP_SOUND, get_pos());
75       }
76
77   state = newState;
78 }
79
80 bool
81 SkullyHop::collision_squished(GameObject& object)
82 {
83   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
84   kill_squished(object);
85   return true;
86 }
87
88 void
89 SkullyHop::collision_solid(const CollisionHit& hit)
90 {
91   // just default behaviour (i.e. stop at floor/walls) when squished
92   if (BadGuy::get_state() == STATE_SQUISHED) {
93     BadGuy::collision_solid(hit);
94   }
95
96   // ignore collisions while standing still
97   if(state != JUMPING)
98     return;
99
100   // check if we hit the floor while falling
101   if(hit.bottom && physic.get_velocity_y() > 0 ) {
102     set_state(STANDING);
103   }
104   // check if we hit the roof while climbing
105   if(hit.top) {
106     physic.set_velocity_y(0);
107   }
108
109   // check if we hit left or right while moving in either direction
110   if(hit.left || hit.right) {
111     dir = dir == LEFT ? RIGHT : LEFT;
112     sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
113     physic.set_velocity_x(-0.25*physic.get_velocity_x());
114   }
115 }
116
117 HitResponse
118 SkullyHop::collision_badguy(BadGuy& , const CollisionHit& hit)
119 {
120   // behaviour for badguy collisions is the same as for collisions with solids
121   collision_solid(hit);
122
123   return CONTINUE;
124 }
125
126 void
127 SkullyHop::active_update(float elapsed_time)
128 {
129   BadGuy::active_update(elapsed_time);
130
131   // charge when fully recovered
132   if ((state == STANDING) && (recover_timer.check())) {
133     set_state(CHARGING);
134     return;
135   }
136
137   // jump as soon as charging animation completed
138   if ((state == CHARGING) && (sprite->animation_done())) {
139     set_state(JUMPING);
140     return;
141   }
142 }
143
144 /* EOF */