Basically, the character is a knight when idling, but when running he is a goblin, this would be cool for a game where you have to hide from knights as a shapeshifter, this is my code:
#include “raylib.h”
#include “raymath.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};
float speed{4.0};
Texture2D knightIdle = LoadTexture("characters/knight_idle_spritesheet.png");
Texture2D knightRun = LoadTexture("characters/goblin_run_spritesheet.png");
Texture2D knight = LoadTexture("characters/goblin_idle_spritesheet.png");
Vector2 knightPos{
(float)windowWidth/2.0f - 4.0f * (0.5f * (float)knight.width/6.0f),
(float)windowHeight/2.0f - 4.0f * (0.5f * (float)knight.height)
};
float RightLeft{1.f};
float RunningTime{};
int frame{};
int maxFrames{5};
const float UpdateTime{1.f/12.f};
SetTargetFPS(60);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(WHITE);
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 mapPos = mapPos - directon
mapPos = Vector2Subtract(mapPos, 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;
knight = knightRun;
}else{
knight = knightIdle;
}
// Draw the map
DrawTextureEx(map, mapPos, 0.0, 4.0, WHITE);
RunningTime += GetFrameTime();
if (RunningTime >= UpdateTime)
{
frame++;
RunningTime=0.f;
if (frame > maxFrames) frame = 0;
}
// Draw the character
Rectangle source{(float)knight.width/6.f * frame, 0.f, RightLeft * (float)knight.width/6.f, (float)knight.height};
Rectangle dest{knightPos.x, knightPos.y, 4.0f * (float)knight.width/6.0f, 4.0f * (float)knight.height};
DrawTexturePro(knight, source, dest, Vector2{}, 0.f, WHITE);
EndDrawing();
}
CloseWindow();
}