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>
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.
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.
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
25 #include "zeekling.hpp"
26 #include "random_generator.hpp"
28 Zeekling::Zeekling(const lisp::Lisp& reader)
29 : BadGuy(reader, "images/creatures/zeekling/zeekling.sprite")
34 Zeekling::Zeekling(const Vector& pos, Direction d)
35 : BadGuy(pos, d, "images/creatures/zeekling/zeekling.sprite")
41 Zeekling::write(lisp::Writer& writer)
43 writer.start_list("zeekling");
45 writer.write_float("x", start_position.x);
46 writer.write_float("y", start_position.y);
48 writer.end_list("zeekling");
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");
61 Zeekling::collision_squished(Player& player)
63 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
64 kill_squished(player);
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);
76 if (state == DIVING) {
77 dir = (dir == LEFT ? RIGHT : LEFT);
79 sprite->set_action(dir == LEFT ? "left" : "right");
80 physic.set_velocity_x(dir == LEFT ? -speed : speed);
81 physic.set_velocity_y(0);
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);
93 Zeekling::onBumpVertical() {
94 if (state == FLYING) {
95 physic.set_velocity_y(0);
97 if (state == DIVING) {
99 physic.set_velocity_y(-speed);
100 sprite->set_action(dir == LEFT ? "left" : "right");
102 if (state == CLIMBING) {
104 physic.set_velocity_y(0);
109 Zeekling::collision_solid(const CollisionHit& hit)
111 if(hit.top || hit.bottom) {
113 } else if(hit.left || hit.right) {
119 * linear prediction of player and badguy positions to decide if we should enter the DIVING state
122 Zeekling::should_we_dive() {
123 const MovingObject* player = this->get_nearest_player();
124 if (!player) return false;
126 const MovingObject* badguy = this;
128 const Vector playerPos = player->get_pos();
129 const Vector playerMov = player->get_movement();
131 const Vector badguyPos = badguy->get_pos();
132 const Vector badguyMov = badguy->get_movement();
134 // new vertical speed to test with
135 float vy = -2*fabsf(badguyMov.x);
137 // do not dive if we are not above the player
138 float height = playerPos.y - badguyPos.y;
139 if (height <= 0) return false;
141 // do not dive if we would not descend faster than the player
142 float relSpeed = -vy + playerMov.y;
143 if (relSpeed <= 0) return false;
145 // guess number of frames to descend to same height as player
146 float estFrames = height / relSpeed;
148 // guess where the player would be at this time
149 float estPx = (playerPos.x + (estFrames * playerMov.x));
151 // guess where we would be at this time
152 float estBx = (badguyPos.x + (estFrames * badguyMov.x));
154 // near misses are OK, too
155 if (fabsf(estPx - estBx) < 32) return true;
161 Zeekling::active_update(float elapsed_time) {
162 if (state == FLYING) {
163 if (should_we_dive()) {
165 physic.set_velocity_y(2*fabsf(physic.get_velocity_x()));
166 sprite->set_action(dir == LEFT ? "diving-left" : "diving-right");
168 BadGuy::active_update(elapsed_time);
170 } else if (state == DIVING) {
171 BadGuy::active_update(elapsed_time);
173 } else if (state == CLIMBING) {
174 // stop climbing when we're back at initial height
175 if (get_pos().y <= start_position.y) {
177 physic.set_velocity_y(0);
179 BadGuy::active_update(elapsed_time);
186 IMPLEMENT_FACTORY(Zeekling, "zeekling")