I cant figure out what I did wrong In this:
Main:
#include “raylib.h”
#include “raymath.h”
#include “Character.h”
#include “Prop.h”
#include “Enemy.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.0f};
Character knight{windowWidth, windowHeight};
Prop props[2]{
Prop{Vector2{600.f, 300.f}, LoadTexture("nature_tileset/Rock.png")},
Prop{Vector2{400.f, 500.f}, LoadTexture("nature_tileset/Log.png")}
};
Enemy goblin{
Vector2{},
LoadTexture("characters/goblin_idle_spritesheet.png"),
LoadTexture("characters/goblin_run_spritesheet.png")
};
SetTargetFPS(60);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(WHITE);
mapPos = Vector2Scale(knight.getWorldPos(), -1.f);
DrawTextureEx(map, mapPos, 0.0, mapScale, WHITE);
for (auto prop : props)
{
prop.Render(knight.getWorldPos());
}
knight.tick(GetFrameTime());
if (knight.getWorldPos().x < 0.f ||
knight.getWorldPos().y < 0.f ||
knight.getWorldPos().x + windowWidth > map.width * mapScale - 50 ||
knight.getWorldPos().y + windowHeight > map.height * mapScale - 50)
{
knight.undoMovement();
}
for (auto prop : props)
{
if (CheckCollisionRecs(prop.GetCollisionRec(knight.getWorldPos()), knight.GetCollisionRec()))
{
knight.undoMovement();
}
}
goblin.tick(GetFrameTime());
EndDrawing();
}
CloseWindow();
}
CharacterBase.h:
#ifndef CHARACTER_BASE_H
#define CHARACTER_BASE_H
class CharacterBase
{
public:
CharacterBase();
private:
};
#endif
CharacterBase.cpp:
#include “CharacterBase.h”
CharacterBase::CharacterBase()
{
}
Enemy.h:
#include “raylib.h”
#include “CharacterBase.h”
class Enemy : public CharacterBase
{
public:
Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture);
Vector2 getWorldPos() { return worldPos; }
void tick(float deltaTime);
void undoMovement();
Rectangle GetCollisionRec();
private:
Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};
Texture2D Idle{LoadTexture("characters/knight_idle_spritesheet.png")};
Texture2D Run{LoadTexture("characters/knight_run_spritesheet.png")};
Vector2 screenPos{};
Vector2 worldPos{};
Vector2 worldPosOld{};
float rightLeft{1.f};
float runningTime{};
int frame{};
int maxFrames{6};
float updateTime{1.f / 12.f};
float speed{4.f};
float width{};
float height{};
float scale{4.0f};
}
Enemy.cpp:
#include “Enemy.h”
Enemy::Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture) :
worldPos(pos),
texture(idle_texture),
Idle(idle_texture),
Run(run_texture)
{
width = texture.width / maxFrames;
height = texture.height;
}
void Enemy::tick(float deltaTime)
{
worldPosOld = worldPos;
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, scale * width, scale * height};
DrawTexturePro(texture, source, dest, Vector2(), 0.f, WHITE);
}
void Enemy::undoMovement()
{
worldPos = worldPosOld;
}
Rectangle Enemy::GetCollisionRec()
{
return Rectangle{
screenPos.x,
screenPos.y,
width * scale,
height *scale
};
}
Prop.h:
#include “raylib.h”
class Prop
{
public:
Prop(Vector2 pos, Texture2D tex);
void Render(Vector2 knightPos);
Rectangle GetCollisionRec(Vector2 knightPos);
private:
Texture2D texture{};
Vector2 worldPos{};
float scale{4.f};
};
Prop.cpp:
#include “Prop.h”
#include “raymath.h”
Prop::Prop(Vector2 pos, Texture2D tex) :
worldPos(pos),
texture(tex)
{
}
void Prop::Render(Vector2 knightPos)
{
Vector2 screenPos { Vector2Subtract(worldPos, knightPos) };
DrawTextureEx(texture, screenPos, 0.f, scale, WHITE);
}
Rectangle Prop::GetCollisionRec(Vector2 knightPos)
{
Vector2 screenPos { Vector2Subtract(worldPos, knightPos) };
return Rectangle{
screenPos.x,
screenPos.y,
texture.width * scale,
texture.height * scale
};
}
character.h:
#include “raylib.h”
#include “CharacterBase.h”
class Character : public CharacterBase
{
public:
Character(int winWidth, int winHeight);
Vector2 getWorldPos() { return worldPos; }
void tick(float deltaTime);
void undoMovement();
Rectangle GetCollisionRec();
private:
Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};
Texture2D Idle{LoadTexture("characters/knight_idle_spritesheet.png")};
Texture2D Run{LoadTexture("characters/knight_run_spritesheet.png")};
Vector2 screenPos{};
Vector2 worldPos{};
Vector2 worldPosOld{};
float rightLeft{1.f};
float runningTime{};
int frame{};
int maxFrames{6};
float updateTime{1.f / 12.f};
float speed{4.f};
float width{};
float height{};
float scale{4.0f};
};
character.cpp:
#include “Character.h”
#include “raymath.h”
Character::Character(int winWidth, int winHeight)
{
width = texture.width / maxFrames;
height = texture.height;
screenPos = {static_cast<float>(winWidth) / 2.0f - scale * (0.5f * width),
static_cast<float>(winHeight) / 2.0f - scale * (0.5f * height)
};
}
void Character::tick(float deltaTime)
{
worldPosOld = 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)
{
worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;
texture = Run;
}
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, scale * width, scale * height};
DrawTexturePro(texture, source, dest, Vector2(), 0.f, WHITE);
}
void Character::undoMovement()
{
worldPos = worldPosOld;
}
Rectangle Character::GetCollisionRec()
{
return Rectangle{
screenPos.x,
screenPos.y,
width * scale,
height *scale
};
}