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