00001
00002
00003
00004
00005
00006
00007
00008 #ifndef QAF_OBJ_PLATFORMEROBJ_H
00009 #define QAF_OBJ_PLATFORMEROBJ_H
00010
00011 #include "../qafCollisionStruct.h"
00012 #include "../qafGameObj.h"
00013 #include "../qafEnvironment.h"
00014 #include "../qafutil/qafVector2D.h"
00015
00016
00017 namespace qaf {
00018
00044 class PlatformerObj : public GameObj {
00045 public:
00046
00055 class PlatformerInput {
00056 public:
00059 bool left, right, down;
00060
00063 bool jumpPress;
00064
00066 bool jump;
00067
00071 PlatformerInput ();
00072 virtual ~PlatformerInput() {}
00073 };
00074
00075
00076
00082 PlatformerInput * platformerInput;
00083
00084
00087 float WIDTH;
00088
00091 float HEIGHT;
00092
00097 float CAMERA_DX;
00098
00103 float CAMERA_DY;
00104
00117 float CAMERA_STICKINESS;
00118
00124 bool CENTER_SCROLLING_ON_SELF;
00125
00129 float WALK_SPEED;
00130
00140 float CLIMB_26_SLOPE_SLOWDOWN;
00141
00151 float CLIMB_45_SLOPE_SLOWDOWN;
00152
00159 float WALK_ACCELERATION;
00160
00166 float WALK_DECELERATION;
00167
00173 float JUMP_STRENGTH;
00174
00180 float WATER_JUMP_STRENGTH;
00181
00184 float GRAVITY;
00185
00187 float MAX_FALL_SPEED;
00188
00203 float SUBMERSION_HEIGHT;
00204
00209 float BUOYANCY;
00210
00216 float MAX_FLOAT_SPEED;
00217
00223 float MAX_SINK_SPEED;
00224
00229 float SWIM_SPEED;
00230
00237 float SWIM_ACCELERATION;
00238
00245 float SWIM_DECELERATION;
00246
00250 bool FLOAT_ON_WATER_SURFACE;
00251
00294 PlatformerObj ( float x, float y, PlatformerInput * platformerInput, AttributeTable & attributes = AttributeTable() );
00295
00296
00300 void initialize ();
00301
00307 void update ( int objLayer, float dt );
00308
00313 void render ( int objLayer, float scrollX, float scrollY );
00314
00319 CollisionStruct * getCollisionStruct ();
00320
00323 bool isFacingLeft;
00324
00326
00330 bool rightContact, leftContact, topContact, bottomContact;
00332
00333
00339 inline const Vector2D & getPos() {
00340 return pos;
00341 }
00342
00346 inline void setPos ( float x, float y ) {
00347 float dx = x - pos.x;
00348 float dy = y - pos.y;
00349
00350 translate( dx, dy );
00351 }
00352
00356 inline void translate ( float dx, float dy ) {
00357 pos.x += dx;
00358 pos.y += dy;
00359
00360 platformerCollisionStruct.setX(
00361 platformerCollisionStruct.getX() + dx );
00362 platformerCollisionStruct.setY(
00363 platformerCollisionStruct.getY() + dy );
00364 }
00365
00371 inline const Vector2D & getVel() {
00372 return vel;
00373 }
00374
00378 inline void setVel ( float x, float y ) {
00379 float dx = x - vel.x;
00380 float dy = y - vel.y;
00381
00382 vel.x += dx;
00383 vel.y += dy;
00384 }
00385
00389 inline bool isUnderwater () {
00390 return (pos.y > Environment::getLoadedRoom()->waterLevel + SUBMERSION_HEIGHT);
00391 }
00392
00396 inline bool isFloatingOnWaterSurface () {
00397 if ( !FLOAT_ON_WATER_SURFACE )
00398 return false;
00399 else
00400 if ( ABS(vel.y) > QAF_EPSILON )
00401 return false;
00402 else
00403 if ( ABS(pos.y - (Environment::getLoadedRoom()->waterLevel + SUBMERSION_HEIGHT)) > QAF_EPSILON )
00404 return false;
00405 else
00406 return true;
00407 }
00408
00409 private:
00410 float accDX, accDT;
00411 Vector2D pos, vel;
00412 Vector2D prevCamPos, nextCamPos;
00413 CollisionStruct::Box platformerCollisionStruct;
00414
00415 inline float botRightX () { return pos.x + WIDTH/2; }
00416 inline float botRightY () { return pos.y; }
00417 inline float topRightX () { return pos.x + WIDTH/2; }
00418 inline float topRightY () { return pos.y - HEIGHT; }
00419 inline float topLeftX () { return pos.x - WIDTH/2; }
00420 inline float topLeftY () { return pos.y - HEIGHT; }
00421 inline float botLeftX () { return pos.x - WIDTH/2; }
00422 inline float botLeftY () { return pos.y; }
00423
00424 inline bool rightCollision () {
00425 return Environment::getLoadedRoom()->segmentCollision(
00426 topRightX(), topRightY(),
00427 botRightX(), botRightY(),
00428 false );
00429 }
00430 inline bool leftCollision () {
00431 return Environment::getLoadedRoom()->segmentCollision(
00432 topLeftX(), topLeftY(),
00433 botLeftX(), botLeftY(),
00434 false );
00435 }
00436 inline bool topCollision () {
00437 return Environment::getLoadedRoom()->segmentCollision(
00438 topLeftX(), topLeftY(),
00439 topRightX(), topRightY(),
00440 false );
00441 }
00442 inline bool botCollision () {
00443 return Environment::getLoadedRoom()->segmentCollision(
00444 botLeftX(), botLeftY(),
00445 botRightX(), botRightY(),
00446 false );
00447 }
00448
00449
00450
00451 inline bool inputLeft() {
00452 return (platformerInput != NULL && platformerInput->left);
00453 }
00454 inline bool inputRight() {
00455 return (platformerInput != NULL && platformerInput->right);
00456 }
00457 inline bool inputDown() {
00458 return (platformerInput != NULL && platformerInput->down);
00459 }
00460 inline bool inputJumpPress() {
00461 return (platformerInput != NULL && platformerInput->jumpPress);
00462 }
00463 inline bool inputJump() {
00464 return (platformerInput != NULL && platformerInput->jump);
00465 }
00466
00467 };
00468
00469 }
00470
00471
00472
00473 #endif