I am trying to make this work but whenever I use class outside the file it crashes…
Here is my class:
#include"raylib.h"
#include"raymath.h"
int winWidth{382};
int winHeight{382};
class Character
{
public:
Vector2 GetWorldPos() { return worldPos; }
void SetScreenPos(int winwidth, int winheight);
void Tick(float deltaTime);
private:
Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};
Texture2D idle{LoadTexture("characters/knight_idle_spritesheet.png")};
Texture run{LoadTexture("characters/knight_run_spritesheet.png")};
Vector2 screenPos{};
Vector2 worldPos{};
// 1.f facing right, -1.f facing left
float rightLeft{1.f};
// animation variables
float runningTime{};
int frame{};
int maxFrames{6};
float updateTime{1.f / 12.f};
float speed{4.f};
};
#include"Character.h"
void Character::SetScreenPos(int winwidth, int winheight)
{
screenPos = {
(float)winwidth / 2.0f - 4.0f * (0.5f * (float)texture.width),
(float)winheight / 2.0f - 4.0f * (0.5f * (float)texture.height)};
};
void Character::Tick(float deltaTime)
{
Vector2 direction{};
if (IsKeyDown(KEY_A))
direction.x -= 1;
if (IsKeyDown(KEY_D))
direction.x += 1;
if (IsKeyDown(KEY_W))
direction.y -= 1;
if (IsKeyDown(KEY_S))
direction.y += 1;
if (Vector2Length(direction) != 0.0)
{
// set worldPos = worldPos + direction
worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;
texture = run;
}
else
{
texture = idle;
}
// update animation frame
runningTime += deltaTime; // update with frame time (delta time)
if (runningTime >= updateTime)
{
runningTime = 0.f;
frame++;
if (frame > maxFrames)
frame = 0;
}
//DrawTextureEx(texture, worldPos, 0.0, 4.0, WHITE); //all sprites rendered from the sheet..
// Draw the character
Rectangle source{frame * (float)texture.width / 6.f, 0.f,
rightLeft * (float)texture.width / 6.f, (float)texture.height};
Rectangle dest{screenPos.x + screenPos.y,
screenPos.y, 4.0f * (float)texture.width / 6.0f,
4.0f * (float)texture.height};
DrawTexturePro(texture, source, dest, Vector2Zero() , 0, WHITE);
};