I'm currently following the "Udemy C++ Fundamentals Game Programming For Beginners" and I'm trying to challenge myself by adding additional functionality to each project you build.
As it stands, each time the player jumps they have the same velocity and I'd like to add a held key press to vary each jump's height.
I'd like the minimum amount of velocity to be -15, with the max velocity to be -22. The variable in question is "jumpVelocity".
Is the best/most efficient way to go about this a clamp? If not, I'm open to suggestions.
Here is the script for context with the highlighted areas, TiA!
#include <raylib.h>
int main()
{
//Window dimensions.
const int windowWidth{512};
const int windowHeight{380};
//Initialise window.
InitWindow(windowWidth, windowHeight, "Alex's Dapper Game");
//Gravity (pixels/frame)/frame.
const int gravity{1};
int jumpVelocity{-15}; <----------------------- I WANT TO USE A MIN AND MAX INT HERE.
//Rectangle dimensions.
const int rectWidth{50};
const int rectHeight{80};
bool isInAir{false};
//Rectangle properties.
float posX{(windowWidth * .5) - (rectWidth * .5)};
int posY{windowHeight - rectHeight};
int rectVelocity{0};
//Window FPS.
SetTargetFPS(60);
while (!WindowShouldClose())
{
//Start drawing.
BeginDrawing();
ClearBackground(WHITE);
//Ground check.
if (posY >= (windowHeight - rectHeight))
{
//Rect on ground
rectVelocity = 0;
isInAir = false;
}
else
{
//Rectangle in air, apply gravity.
jumpVelocity = -15;
rectVelocity += gravity;
isInAir = true;
}
if(IsKeyDown(KEY_SPACE))
{
jumpVelocity += 1; <--------------------------- I WANT TO INCREASE FROM -15 TO -25 DEPENDING ON THE KEY HOLD.
}
//Jump
if (IsKeyReleased(KEY_SPACE) && (!isInAir))
{
rectVelocity += jumpVelocity;
}
//Update position.
posY += rectVelocity;
DrawRectangle(posX, posY, rectWidth, rectHeight, BLUE);
//Stop drawing.
EndDrawing();
}
// Close the window.
CloseWindow();
}