From 66ae83793e07554e4dfaed4fb4e7e1666419c62e Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Thu, 7 Feb 2008 11:10:24 +0100 Subject: [PATCH] Smoothen player racket movement (accelerate) Add an "acceleration" effect to the player racket movement, so that keyboard control is a bit more fine-grained and easier to use (hopefully). Thanks To Enrico Zini for suggesting this change and providing the feature request. --- game.c | 18 ++++++++++-------- game.h | 5 ++++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/game.c b/game.c index 826bea5..51e5d7f 100644 --- a/game.c +++ b/game.c @@ -34,8 +34,8 @@ void game( bool singleplayer) { GameState s = { { 0, 0, 0.0, 0.0, 0.0 }, { 0, 0, 0, 0, 0 }, - { GAME_X_MIN-RACKET_X_MID*2, GAME_Y_MID, 0, 0, 1, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, false, 0, 0 }, - { GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0, 0, 0, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, false, 0, 0 }, + { GAME_X_MIN-RACKET_X_MID*2, GAME_Y_MID, 0, 0, 1, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, false, 0, 0, PLAYER_ACCEL_DEFAULT }, + { GAME_X_MAX+RACKET_X_MID*2, GAME_Y_MID, 0, 0, 0, DESIRE_NORMAL, PLAYER_TYPE_HUMAN, false, 0, 0, PLAYER_ACCEL_DEFAULT }, 0, 0, 0, @@ -483,12 +483,14 @@ void input_human( Player* player, bool up, bool down, bool hit, bool use_mouse, } } - if( up) { - player->y -= diff; - } - - if( down) { - player->y += diff; + if (up) { + player->y -= fminf(diff, diff*player->accelerate); + player->accelerate *= PLAYER_ACCEL_INCREASE; + } else if (down) { + player->y += fminf(diff, diff*player->accelerate); + player->accelerate *= PLAYER_ACCEL_INCREASE; + } else { + player->accelerate = PLAYER_ACCEL_DEFAULT; } if( hit) { diff --git a/game.h b/game.h index fcaddb8..a538400 100644 --- a/game.h +++ b/game.h @@ -73,6 +73,7 @@ typedef struct { int sets[SETS_TO_WIN*2]; /* score for each set */ int mouse_x; /* x position of mouse */ int mouse_y; /* y position of mouse */ + float accelerate; /* a value [0..1] how fast the user accelerates */ } Player; enum { @@ -158,7 +159,9 @@ typedef struct { #define IS_NEAR_X(px,bx) (fabsf(px-bx)