Checking The Map Bounds Bounds fixed

This was solved by @Tuomo_T in one of my questions, but i hit the solved button too soon, so i have to show my work here:
ClassyClash.cpp:
#include “raylib.h”

#include “raymath.h”

#include “Character.h”

int main()

{

const int windowWidth{384};

const int windowHeight{384};

InitWindow(windowWidth, windowHeight, "Classy Clash");

Texture2D map = LoadTexture("nature_tileset/OpenWorldMap24x24.png");

Vector2 mapPos{0.0, 0.0};

const float mapScale(4.f);

Character knight;

knight.SetScreenPos(windowWidth, windowHeight);

SetTargetFPS(60);

while (!WindowShouldClose())

{

    BeginDrawing();

    ClearBackground(WHITE);

    // Draw the map

    DrawTextureEx(map, mapPos, 0.0, mapScale, WHITE);

    if (knight.GetWorldPos().x < 0.f

    || knight.GetWorldPos().x + windowWidth > map.width * mapScale - 25)

    {

        knight.undoMovementX();

    } else{

        knight.blockedX = false;

    }

    if (knight.GetWorldPos().y < 0.f

    || knight.GetWorldPos().y + windowHeight > map.height * mapScale - 30)

    {

        knight.undoMovementY();

    } else{

        knight.blockedY = false;

    }

    mapPos = Vector2Negate(knight.GetWorldPos());

    knight.tick(GetFrameTime());

    EndDrawing();

}

CloseWindow();

}
Character.h:
#include “raylib.h”

class Character

{

public:

Character();

Vector2 GetWorldPos() { return WorldPos; }

void SetScreenPos(int winWidth, int winHeight);

void tick(float DeltaTime);

void undoMovementX();

void undoMovementY();

bool blockedX{false};

bool blockedY{false};

private:

Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D idle{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D run{LoadTexture("characters/goblin_run_spritesheet.png")};

Vector2 ScreenPos{};

Vector2 WorldPos{};

Vector2 WorldPosLastFrame{};

float RightLeft{1.f};

float RunningTime{};

int frame{};

int maxFrames{5};

int sixMaxFrames{6};

float UpdateTime{1.f / 12.f};

float Speed{4.f};

float width{};

float height{};

};
Character.cpp:
#include “Character.h”

#include “raymath.h”

Character::Character()

{

width =texture.width / sixMaxFrames;

height = texture.height;

}

void Character::SetScreenPos(int winWidth, int winHeight)

{

ScreenPos = {

    (float)winWidth / 2.0f - 4.0f * (0.5f * width),

    (float)winHeight / 2.0f - 4.0f * (0.5f * height)};

}

void Character::tick(float DeltaTime)

{

WorldPosLastFrame = WorldPos;

Vector2 direction{};

if (IsKeyDown(KEY_A))

    direction.x -= 1.0;

if (IsKeyDown(KEY_D))

    direction.x += 1.0;

if (IsKeyDown(KEY_W))

    direction.y -= 1.0;

if (IsKeyDown(KEY_S))

    direction.y += 1.0;

if (Vector2Length(direction) != 0.0)

{

    // set WorldPos = WorldPos + directon

    WorldPos = Vector2Add(WorldPos, Vector2Scale(Vector2Normalize(direction), Speed));

    // direction.x < 0.f ? RightLeft = -1.f : RightLeft = 1.f; // this is commented out because this is code that will flip the character if it moves up/down

    if (direction.x < 0.f)

        RightLeft = -1;

    else if (direction.x > 0.f)

        RightLeft = 1;

    if (!blockedX ||!blockedY)

        texture = run;

    else

        texture = idle;

}

else

{

    texture = idle;

}

RunningTime += DeltaTime;

if (RunningTime >= UpdateTime)

{

    frame++;

    RunningTime = 0.f;

    if (frame > maxFrames)

        frame = 0;

}

Rectangle source{frame * width, 0.f, RightLeft * width, height};

Rectangle dest{ScreenPos.x, ScreenPos.y, 4.0f * width, 4.0f * height};

DrawTexturePro(texture, source, dest, Vector2{}, 0.f, WHITE);

}

void Character::undoMovementX()

{

WorldPos.x = WorldPosLastFrame.x;

blockedX = {true};

}

void Character::undoMovementY()

{

WorldPos.y = WorldPosLastFrame.y;

blockedY = {true};

}

Privacy & Terms