#include <zeekling.hpp>
Inherits BadGuy.
Public Member Functions | |
Zeekling (const Reader &reader) | |
Zeekling (const Vector &pos, Direction d) | |
void | initialize () |
called immediately before the first call to initialize | |
void | collision_solid (const CollisionHit &hit) |
Called when the badguy collided with solid ground. | |
void | active_update (float elapsed_time) |
called each frame when the badguy is activated. | |
Private Types | |
enum | ZeeklingState { FLYING, DIVING, CLIMBING } |
Private Member Functions | |
bool | collision_squished (GameObject &object) |
Called when the player hit the badguy from above. | |
bool | should_we_dive () |
linear prediction of player and badguy positions to decide if we should enter the DIVING state | |
void | onBumpHorizontal () |
void | onBumpVertical () |
Zeekling (const Zeekling &) | |
Zeekling & | operator= (const Zeekling &) |
Private Attributes | |
float | speed |
Timer | diveRecoverTimer |
ZeeklingState | state |
const MovingObject * | last_player |
last player we tracked | |
Vector | last_player_pos |
position we last spotted the player at | |
Vector | last_self_pos |
position we last were at |
Definition at line 23 of file zeekling.hpp.
enum Zeekling::ZeeklingState [private] |
Zeekling::Zeekling | ( | const Reader & | reader | ) |
Definition at line 27 of file zeekling.cpp.
References Physic::enable_gravity(), FLYING, gameRandom, BadGuy::physic, RandomGenerator::rand(), speed, and state.
00027 : 00028 BadGuy(reader, "images/creatures/zeekling/zeekling.sprite"), 00029 speed(), 00030 diveRecoverTimer(), 00031 state(), 00032 last_player(0), 00033 last_player_pos(), 00034 last_self_pos() 00035 { 00036 state = FLYING; 00037 speed = gameRandom.rand(130, 171); 00038 physic.enable_gravity(false); 00039 }
Definition at line 41 of file zeekling.cpp.
References Physic::enable_gravity(), FLYING, gameRandom, BadGuy::physic, RandomGenerator::rand(), speed, and state.
00041 : 00042 BadGuy(pos, d, "images/creatures/zeekling/zeekling.sprite"), 00043 speed(), 00044 diveRecoverTimer(), 00045 state(), 00046 last_player(0), 00047 last_player_pos(), 00048 last_self_pos() 00049 { 00050 state = FLYING; 00051 speed = gameRandom.rand(130, 171); 00052 physic.enable_gravity(false); 00053 }
Zeekling::Zeekling | ( | const Zeekling & | ) | [private] |
void Zeekling::initialize | ( | ) | [virtual] |
called immediately before the first call to initialize
Reimplemented from BadGuy.
Definition at line 56 of file zeekling.cpp.
References BadGuy::dir, LEFT, BadGuy::physic, Physic::set_velocity_x(), speed, and MovingSprite::sprite.
00057 { 00058 physic.set_velocity_x(dir == LEFT ? -speed : speed); 00059 sprite->set_action(dir == LEFT ? "left" : "right"); 00060 }
void Zeekling::collision_solid | ( | const CollisionHit & | hit | ) | [virtual] |
Called when the badguy collided with solid ground.
Reimplemented from BadGuy.
Definition at line 110 of file zeekling.cpp.
References CollisionHit::bottom, CollisionHit::left, onBumpHorizontal(), onBumpVertical(), CollisionHit::right, and CollisionHit::top.
00111 { 00112 if(hit.top || hit.bottom) { 00113 onBumpVertical(); 00114 } else if(hit.left || hit.right) { 00115 onBumpHorizontal(); 00116 } 00117 }
void Zeekling::active_update | ( | float | elapsed_time | ) | [virtual] |
called each frame when the badguy is activated.
Reimplemented from BadGuy.
Definition at line 172 of file zeekling.cpp.
References BadGuy::active_update(), CLIMBING, BadGuy::dir, DIVING, FLYING, MovingObject::get_pos(), Physic::get_velocity_x(), LEFT, BadGuy::physic, Physic::set_velocity_y(), should_we_dive(), MovingSprite::sprite, BadGuy::start_position, state, and Vector::y.
00172 { 00173 if (state == FLYING) { 00174 if (should_we_dive()) { 00175 state = DIVING; 00176 physic.set_velocity_y(2*fabsf(physic.get_velocity_x())); 00177 sprite->set_action(dir == LEFT ? "diving-left" : "diving-right"); 00178 } 00179 BadGuy::active_update(elapsed_time); 00180 return; 00181 } else if (state == DIVING) { 00182 BadGuy::active_update(elapsed_time); 00183 return; 00184 } else if (state == CLIMBING) { 00185 // stop climbing when we're back at initial height 00186 if (get_pos().y <= start_position.y) { 00187 state = FLYING; 00188 physic.set_velocity_y(0); 00189 } 00190 BadGuy::active_update(elapsed_time); 00191 return; 00192 } else { 00193 assert(false); 00194 } 00195 }
bool Zeekling::collision_squished | ( | GameObject & | object | ) | [private, virtual] |
Called when the player hit the badguy from above.
You should return true if the badguy was squished, false if squishing wasn't possible
Reimplemented from BadGuy.
Definition at line 63 of file zeekling.cpp.
References BadGuy::dir, BadGuy::kill_squished(), LEFT, and MovingSprite::sprite.
00064 { 00065 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right"); 00066 kill_squished(object); 00067 return true; 00068 }
bool Zeekling::should_we_dive | ( | ) | [private] |
linear prediction of player and badguy positions to decide if we should enter the DIVING state
Definition at line 123 of file zeekling.cpp.
References BadGuy::get_nearest_player(), MovingObject::get_pos(), last_player, last_player_pos, last_self_pos, Vector::x, and Vector::y.
Referenced by active_update().
00123 { 00124 00125 const MovingObject* player = this->get_nearest_player(); 00126 if (player && last_player && (player == last_player)) { 00127 00128 // get positions, calculate movement 00129 const Vector player_pos = player->get_pos(); 00130 const Vector player_mov = (player_pos - last_player_pos); 00131 const Vector self_pos = this->get_pos(); 00132 const Vector self_mov = (self_pos - last_self_pos); 00133 00134 // new vertical speed to test with 00135 float vy = 2*fabsf(self_mov.x); 00136 00137 // do not dive if we are not above the player 00138 float height = player_pos.y - self_pos.y; 00139 if (height <= 0) return false; 00140 00141 // do not dive if we are too far above the player 00142 if (height > 512) return false; 00143 00144 // do not dive if we would not descend faster than the player 00145 float relSpeed = vy - player_mov.y; 00146 if (relSpeed <= 0) return false; 00147 00148 // guess number of frames to descend to same height as player 00149 float estFrames = height / relSpeed; 00150 00151 // guess where the player would be at this time 00152 float estPx = (player_pos.x + (estFrames * player_mov.x)); 00153 00154 // guess where we would be at this time 00155 float estBx = (self_pos.x + (estFrames * self_mov.x)); 00156 00157 // near misses are OK, too 00158 if (fabsf(estPx - estBx) < 8) return true; 00159 } 00160 00161 // update last player tracked, as well as our positions 00162 last_player = player; 00163 if (player) { 00164 last_player_pos = player->get_pos(); 00165 last_self_pos = this->get_pos(); 00166 } 00167 00168 return false; 00169 }
void Zeekling::onBumpHorizontal | ( | ) | [private] |
Definition at line 71 of file zeekling.cpp.
References CLIMBING, BadGuy::dir, DIVING, FLYING, LEFT, BadGuy::physic, RIGHT, Physic::set_velocity_x(), Physic::set_velocity_y(), speed, MovingSprite::sprite, and state.
Referenced by collision_solid().
00071 { 00072 if (state == FLYING) { 00073 dir = (dir == LEFT ? RIGHT : LEFT); 00074 sprite->set_action(dir == LEFT ? "left" : "right"); 00075 physic.set_velocity_x(dir == LEFT ? -speed : speed); 00076 } else 00077 if (state == DIVING) { 00078 dir = (dir == LEFT ? RIGHT : LEFT); 00079 state = FLYING; 00080 sprite->set_action(dir == LEFT ? "left" : "right"); 00081 physic.set_velocity_x(dir == LEFT ? -speed : speed); 00082 physic.set_velocity_y(0); 00083 } else 00084 if (state == CLIMBING) { 00085 dir = (dir == LEFT ? RIGHT : LEFT); 00086 sprite->set_action(dir == LEFT ? "left" : "right"); 00087 physic.set_velocity_x(dir == LEFT ? -speed : speed); 00088 } else { 00089 assert(false); 00090 } 00091 }
void Zeekling::onBumpVertical | ( | ) | [private] |
Definition at line 94 of file zeekling.cpp.
References CLIMBING, BadGuy::dir, DIVING, FLYING, LEFT, BadGuy::physic, Physic::set_velocity_y(), speed, MovingSprite::sprite, and state.
Referenced by collision_solid().
00094 { 00095 if (state == FLYING) { 00096 physic.set_velocity_y(0); 00097 } else 00098 if (state == DIVING) { 00099 state = CLIMBING; 00100 physic.set_velocity_y(-speed); 00101 sprite->set_action(dir == LEFT ? "left" : "right"); 00102 } else 00103 if (state == CLIMBING) { 00104 state = FLYING; 00105 physic.set_velocity_y(0); 00106 } 00107 }
float Zeekling::speed [private] |
Definition at line 47 of file zeekling.hpp.
Referenced by initialize(), onBumpHorizontal(), onBumpVertical(), and Zeekling().
Timer Zeekling::diveRecoverTimer [private] |
Definition at line 48 of file zeekling.hpp.
ZeeklingState Zeekling::state [private] |
Reimplemented from BadGuy.
Definition at line 49 of file zeekling.hpp.
Referenced by active_update(), onBumpHorizontal(), onBumpVertical(), and Zeekling().
const MovingObject* Zeekling::last_player [private] |
Vector Zeekling::last_player_pos [private] |
position we last spotted the player at
Definition at line 51 of file zeekling.hpp.
Referenced by should_we_dive().
Vector Zeekling::last_self_pos [private] |
position we last were at
Definition at line 52 of file zeekling.hpp.
Referenced by should_we_dive().