Gravity

Simple function that adds gravity to sprites not colliding with a platform (not on the ground). Function is calledd at each frame and adds a small amount in the Y direction (down).


void AddGravity(float deltaTime)
{
 float increment;
 increment = deltaTime * 25;
 mPosition.y += increment;
}

Jumping

If 'mJumping' is true the player moves up using the 'mJumpForce' times by 'deltaTime' for a smooth jump. The jump force slowly decreases over time to have a realsitic jumping effect.



if (mJumping)
{
 mPosition.y -= mJumpForce * deltaTime ;
 mJumpForce -= JUMP_FORCE_DECREMENT * deltaTime;
}

Platform logic

First the function calculates all the X and Y positions neccesary to know if the object is colliding with the floor / top of the brick. Then using some logic to set the variable to true if the bottom of the player is colliding with the top of the brick / floor and not exceeding the X coordinate bounds.


void CheckOnPlatform(SDL_Rect BrickRect)
{
 int CharLeft = GetCollisionBox().x;
 int BrickRight = BrickRect.x + BrickRect.w;
 int CharRight = GetCollisionBox().x + GetCollisionBox().w;
 int BrickLeft = BrickRect.x;
 int CharBottom = GetCollisionBox().y + GetCollisionBox().h;
 int BrickTop = BrickRect.y;
 int CharTop = GetCollisionBox().y;
 int BrickBottom = BrickRect.y + BrickRect.h;
 if (CharBottom >= BrickTop && CharBottom < BrickBottom && mCollidingY && CharRight >= BrickLeft && CharLeft <= BrickRight)
 {
  mOnPlatform = true;
 }
}
				

Character animation

Depending on the the current animation frame - 'mFrame' and the current state / size of the character - 'mSize' the image source rect changes accordingly. The size of the image rect also changes because the sprites becopme taller as they change form.


if (mSize == "Small")
 SetSourceRect(16 * mFrame, 0, 16, 16);
if (mSize == "Medium")
 SetSourceRect(16 * mFrame, 16, 16, 20);
if (mSize == "Large")
 SetSourceRect(16 * mFrame, 36, 16, 24);
if (mSize == "Fire")
 SetSourceRect(16 * mFrame, 60, 16, 24);

Feedback

As it is a University assigment I recieved a mark for hitting certain criteria. Unfortantley I dont have the breakdown available.

Final mark: 100%