cc13f88c334f36461970c2475a7bef7cb702d6eb
[supertux.git] / src / badguy / zeekling.cpp
1 //  $Id$
2 //
3 //  Zeekling - flyer that swoops down when she spots the player
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21
22 #include <config.h>
23
24 #include "zeekling.hpp"
25
26 #include <math.h>
27
28 #include "random_generator.hpp"
29 #include "lisp/writer.hpp"
30 #include "object_factory.hpp"
31 #include "object/player.hpp"
32 #include "sprite/sprite.hpp"
33
34 Zeekling::Zeekling(const lisp::Lisp& reader)
35         : BadGuy(reader, "images/creatures/zeekling/zeekling.sprite"), last_player(0)
36 {
37   state = FLYING;
38   speed = systemRandom.rand(130, 171);
39   physic.enable_gravity(false);
40 }
41
42 Zeekling::Zeekling(const Vector& pos, Direction d)
43         : BadGuy(pos, d, "images/creatures/zeekling/zeekling.sprite"), last_player(0)
44 {
45   state = FLYING;
46   speed = systemRandom.rand(130, 171);
47   physic.enable_gravity(false);
48 }
49
50 void
51 Zeekling::write(lisp::Writer& writer)
52 {
53   writer.start_list("zeekling");
54
55   writer.write("x", start_position.x);
56   writer.write("y", start_position.y);
57
58   writer.end_list("zeekling");
59 }
60
61 void
62 Zeekling::initialize()
63 {
64   physic.set_velocity_x(dir == LEFT ? -speed : speed);
65   sprite->set_action(dir == LEFT ? "left" : "right");
66 }
67
68 bool
69 Zeekling::collision_squished(GameObject& object)
70 {
71   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
72   kill_squished(object);
73   kill_fall();
74   return true;
75 }
76
77 void
78 Zeekling::onBumpHorizontal() {
79   if (state == FLYING) {
80     dir = (dir == LEFT ? RIGHT : LEFT);
81     sprite->set_action(dir == LEFT ? "left" : "right");
82     physic.set_velocity_x(dir == LEFT ? -speed : speed);
83   } else
84   if (state == DIVING) {
85     dir = (dir == LEFT ? RIGHT : LEFT);
86     state = FLYING;
87     sprite->set_action(dir == LEFT ? "left" : "right");
88     physic.set_velocity_x(dir == LEFT ? -speed : speed);
89     physic.set_velocity_y(0);
90   } else
91   if (state == CLIMBING) {
92     dir = (dir == LEFT ? RIGHT : LEFT);
93     sprite->set_action(dir == LEFT ? "left" : "right");
94     physic.set_velocity_x(dir == LEFT ? -speed : speed);
95   } else {
96     assert(false);
97   }
98 }
99
100 void
101 Zeekling::onBumpVertical() {
102   if (state == FLYING) {
103     physic.set_velocity_y(0);
104   } else
105   if (state == DIVING) {
106     state = CLIMBING;
107     physic.set_velocity_y(-speed);
108     sprite->set_action(dir == LEFT ? "left" : "right");
109   } else
110   if (state == CLIMBING) {
111     state = FLYING;
112     physic.set_velocity_y(0);
113   }
114 }
115
116 void
117 Zeekling::collision_solid(const CollisionHit& hit)
118 {
119   if(hit.top || hit.bottom) {
120     onBumpVertical();
121   } else if(hit.left || hit.right) {
122     onBumpHorizontal();
123   }
124 }
125
126 /**
127  * linear prediction of player and badguy positions to decide if we should enter the DIVING state
128  */
129 bool
130 Zeekling::should_we_dive() {
131
132   const MovingObject* player = this->get_nearest_player();
133   if (player && last_player && (player == last_player)) {
134
135     // get positions, calculate movement
136     const Vector player_pos = player->get_pos();
137     const Vector player_mov = (player_pos - last_player_pos);
138     const Vector self_pos = this->get_pos();
139     const Vector self_mov = (self_pos - last_self_pos);
140
141     // new vertical speed to test with
142     float vy = 2*fabsf(self_mov.x);
143
144     // do not dive if we are not above the player
145     float height = player_pos.y - self_pos.y;
146     if (height <= 0) return false;
147
148     // do not dive if we are too far above the player
149     if (height > 512) return false;
150
151     // do not dive if we would not descend faster than the player
152     float relSpeed = vy - player_mov.y;
153     if (relSpeed <= 0) return false;
154
155     // guess number of frames to descend to same height as player
156     float estFrames = height / relSpeed;
157
158     // guess where the player would be at this time
159     float estPx = (player_pos.x + (estFrames * player_mov.x));
160
161     // guess where we would be at this time
162     float estBx = (self_pos.x + (estFrames * self_mov.x));
163
164     // near misses are OK, too
165     if (fabsf(estPx - estBx) < 8) return true;
166   }
167
168   // update last player tracked, as well as our positions
169   last_player = player;
170   if (player) {
171     last_player_pos = player->get_pos();
172     last_self_pos = this->get_pos();
173   }
174
175   return false;
176 }
177
178 void
179 Zeekling::active_update(float elapsed_time) {
180   if (state == FLYING) {
181     if (should_we_dive()) {
182       state = DIVING;
183       physic.set_velocity_y(2*fabsf(physic.get_velocity_x()));
184       sprite->set_action(dir == LEFT ? "diving-left" : "diving-right");
185     }
186     BadGuy::active_update(elapsed_time);
187     return;
188   } else if (state == DIVING) {
189     BadGuy::active_update(elapsed_time);
190     return;
191   } else if (state == CLIMBING) {
192     // stop climbing when we're back at initial height
193     if (get_pos().y <= start_position.y) {
194       state = FLYING;
195       physic.set_velocity_y(0);
196     }
197     BadGuy::active_update(elapsed_time);
198     return;
199   } else {
200     assert(false);
201   }
202 }
203
204 IMPLEMENT_FACTORY(Zeekling, "zeekling")