Badguys now inherit from MovingSprite
[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")
30 {
31   set_direction = false;
32   state = FLYING;
33 }
34
35 Zeekling::Zeekling(const Vector& pos, Direction d)
36         : BadGuy(pos, "images/creatures/zeekling/zeekling.sprite")
37 {
38   set_direction = true;
39   initial_direction = d;
40   state = FLYING;
41 }
42
43 void
44 Zeekling::write(lisp::Writer& writer)
45 {
46   writer.start_list("zeekling");
47
48   writer.write_float("x", start_position.x);
49   writer.write_float("y", start_position.y);
50
51   writer.end_list("zeekling");
52 }
53
54 void
55 Zeekling::activate()
56 {
57   speed = systemRandom.rand(130, 171);
58   if (set_direction) {dir = initial_direction;}
59   physic.set_velocity_x(dir == LEFT ? -speed : speed);
60   physic.enable_gravity(false);
61   sprite->set_action(dir == LEFT ? "left" : "right");
62 }
63
64 bool
65 Zeekling::collision_squished(Player& player)
66 {
67   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
68   kill_squished(player);
69   kill_fall();
70   return true;
71 }
72
73 void 
74 Zeekling::onBumpHorizontal() {
75   if (state == FLYING) {
76     dir = (dir == LEFT ? RIGHT : LEFT);
77     sprite->set_action(dir == LEFT ? "left" : "right");
78     physic.set_velocity_x(dir == LEFT ? -speed : speed);
79   } else
80   if (state == DIVING) {
81     dir = (dir == LEFT ? RIGHT : LEFT);
82     state = FLYING;
83     sprite->set_action(dir == LEFT ? "left" : "right");
84     physic.set_velocity_x(dir == LEFT ? -speed : speed);
85     physic.set_velocity_y(0);
86   } else
87   if (state == CLIMBING) {
88     dir = (dir == LEFT ? RIGHT : LEFT);
89     sprite->set_action(dir == LEFT ? "left" : "right");
90     physic.set_velocity_x(dir == LEFT ? -speed : speed);
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 HitResponse
111 Zeekling::collision_solid(GameObject& , const CollisionHit& hit)
112 {
113   if(fabsf(hit.normal.y) > .5) {
114     onBumpVertical(); 
115   } else {
116     onBumpHorizontal();
117   }
118
119   return CONTINUE;
120 }
121
122 /**
123  * linear prediction of player and badguy positions to decide if we should enter the DIVING state
124  */
125 bool 
126 Zeekling::should_we_dive() {
127   const MovingObject* player = this->get_nearest_player();
128   if (!player) return false;
129
130   const MovingObject* badguy = this;
131
132   const Vector playerPos = player->get_pos();
133   const Vector playerMov = player->get_movement();
134
135   const Vector badguyPos = badguy->get_pos();
136   const Vector badguyMov = badguy->get_movement();
137
138   // new vertical speed to test with
139   float vy = -2*fabsf(badguyMov.x);
140
141   // do not dive if we are not above the player
142   float height = playerPos.y - badguyPos.y;
143   if (height <= 0) return false;
144
145   // do not dive if we would not descend faster than the player
146   float relSpeed = -vy + playerMov.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 = (playerPos.x + (estFrames * playerMov.x));
154
155   // guess where we would be at this time
156   float estBx = (badguyPos.x + (estFrames * badguyMov.x));
157
158   // near misses are OK, too
159   if (fabsf(estPx - estBx) < 32) return true;
160
161   return false;
162 }
163
164 void 
165 Zeekling::active_update(float elapsed_time) {
166   BadGuy::active_update(elapsed_time);
167
168   if (state == FLYING) {
169     if (should_we_dive()) {
170       state = DIVING;
171       physic.set_velocity_y(-2*fabsf(physic.get_velocity_x()));
172       sprite->set_action(dir == LEFT ? "diving-left" : "diving-right");
173     }
174     return;
175   }
176
177   if (state == DIVING) {
178     return;
179   }
180
181   if (state == CLIMBING) {
182     // stop climbing when we're back at initial height
183     if (get_pos().y <= start_position.y) {
184       state = FLYING;
185       physic.set_velocity_y(0);
186     }
187     return;
188   }
189
190 }
191
192 IMPLEMENT_FACTORY(Zeekling, "zeekling")