Merged back changes from 0.3.x branch
[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 <supertux@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 #include <math.h>
24
25 #include "zeekling.hpp"
26 #include "random_generator.hpp"
27
28 Zeekling::Zeekling(const lisp::Lisp& reader)
29         : BadGuy(reader, "images/creatures/zeekling/zeekling.sprite"), last_player(0)
30 {
31   state = FLYING;
32 }
33
34 Zeekling::Zeekling(const Vector& pos, Direction d)
35         : BadGuy(pos, d, "images/creatures/zeekling/zeekling.sprite"), last_player(0)
36 {
37   state = FLYING;
38 }
39
40 void
41 Zeekling::write(lisp::Writer& writer)
42 {
43   writer.start_list("zeekling");
44
45   writer.write_float("x", start_position.x);
46   writer.write_float("y", start_position.y);
47
48   writer.end_list("zeekling");
49 }
50
51 void
52 Zeekling::activate()
53 {
54   speed = systemRandom.rand(130, 171);
55   physic.set_velocity_x(dir == LEFT ? -speed : speed);
56   physic.enable_gravity(false);
57   sprite->set_action(dir == LEFT ? "left" : "right");
58 }
59
60 bool
61 Zeekling::collision_squished(GameObject& object)
62 {
63   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
64   kill_squished(object);
65   kill_fall();
66   return true;
67 }
68
69 void
70 Zeekling::onBumpHorizontal() {
71   if (state == FLYING) {
72     dir = (dir == LEFT ? RIGHT : LEFT);
73     sprite->set_action(dir == LEFT ? "left" : "right");
74     physic.set_velocity_x(dir == LEFT ? -speed : speed);
75   } else
76   if (state == DIVING) {
77     dir = (dir == LEFT ? RIGHT : LEFT);
78     state = FLYING;
79     sprite->set_action(dir == LEFT ? "left" : "right");
80     physic.set_velocity_x(dir == LEFT ? -speed : speed);
81     physic.set_velocity_y(0);
82   } else
83   if (state == CLIMBING) {
84     dir = (dir == LEFT ? RIGHT : LEFT);
85     sprite->set_action(dir == LEFT ? "left" : "right");
86     physic.set_velocity_x(dir == LEFT ? -speed : speed);
87   } else {
88     assert(false);
89   }
90 }
91
92 void
93 Zeekling::onBumpVertical() {
94   if (state == FLYING) {
95     physic.set_velocity_y(0);
96   } else
97   if (state == DIVING) {
98     state = CLIMBING;
99     physic.set_velocity_y(-speed);
100     sprite->set_action(dir == LEFT ? "left" : "right");
101   } else
102   if (state == CLIMBING) {
103     state = FLYING;
104     physic.set_velocity_y(0);
105   }
106 }
107
108 void
109 Zeekling::collision_solid(const CollisionHit& hit)
110 {
111   if(hit.top || hit.bottom) {
112     onBumpVertical();
113   } else if(hit.left || hit.right) {
114     onBumpHorizontal();
115   }
116 }
117
118 /**
119  * linear prediction of player and badguy positions to decide if we should enter the DIVING state
120  */
121 bool
122 Zeekling::should_we_dive() {
123
124   const MovingObject* player = this->get_nearest_player();
125   if (player && last_player && (player == last_player)) {
126
127     // get positions, calculate movement
128     const Vector player_pos = player->get_pos();
129     const Vector player_mov = (player_pos - last_player_pos);
130     const Vector self_pos = this->get_pos();
131     const Vector self_mov = (self_pos - last_self_pos);
132
133     // new vertical speed to test with
134     float vy = 2*fabsf(self_mov.x);
135
136     // do not dive if we are not above the player
137     float height = player_pos.y - self_pos.y;
138     if (height <= 0) return false;
139
140     // do not dive if we are too far above the player
141     if (height > 512) return false;
142
143     // do not dive if we would not descend faster than the player
144     float relSpeed = vy - player_mov.y;
145     if (relSpeed <= 0) return false;
146
147     // guess number of frames to descend to same height as player
148     float estFrames = height / relSpeed;
149
150     // guess where the player would be at this time
151     float estPx = (player_pos.x + (estFrames * player_mov.x));
152
153     // guess where we would be at this time
154     float estBx = (self_pos.x + (estFrames * self_mov.x));
155
156     // near misses are OK, too
157     if (fabsf(estPx - estBx) < 8) return true;
158   }
159
160   // update last player tracked, as well as our positions
161   last_player = player;
162   if (player) {
163     last_player_pos = player->get_pos();
164     last_self_pos = this->get_pos();
165   }
166
167   return false;
168 }
169
170 void
171 Zeekling::active_update(float elapsed_time) {
172   if (state == FLYING) {
173     if (should_we_dive()) {
174       state = DIVING;
175       physic.set_velocity_y(2*fabsf(physic.get_velocity_x()));
176       sprite->set_action(dir == LEFT ? "diving-left" : "diving-right");
177     }
178     BadGuy::active_update(elapsed_time);
179     return;
180   } else if (state == DIVING) {
181     BadGuy::active_update(elapsed_time);
182     return;
183   } else if (state == CLIMBING) {
184     // stop climbing when we're back at initial height
185     if (get_pos().y <= start_position.y) {
186       state = FLYING;
187       physic.set_velocity_y(0);
188     }
189     BadGuy::active_update(elapsed_time);
190     return;
191   } else {
192     assert(false);
193   }
194 }
195
196 IMPLEMENT_FACTORY(Zeekling, "zeekling")