Enemy Run Animation

I can’t find out what I did wrong here, basically the enemy is running perfectly fine( only goes to the player if ASWD is being held down) but the player is in the wrong place and wont move (but will do run animation and turn):
ClasyClash.cpp:
#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.f};

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/knight_idle_spritesheet.png"),

    LoadTexture("characters/knight_run_spritesheet.png")

};

goblin.setTarget(&knight);

SetTargetFPS(60);

while (!WindowShouldClose())

{

    BeginDrawing();

    ClearBackground(WHITE);

    mapPos = Vector2Scale(knight.GetScreenPos(), -1.f);

    // Draw the map

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

    for (auto prop : props)

    {

        prop.Render(knight.GetScreenPos());

    }

   

    knight.tick(GetFrameTime());

    // check map bounds:

   

    if (knight.GetScreenPos().x < 0.f ||

        knight.GetScreenPos().y < 0.f ||

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

        knight.GetScreenPos().y + windowHeight > map.height * mapScale - 25)

        {

            knight.UndoMovement();

        }

   

    // check prop collision

    for (auto Prop : props)

    {

        if (CheckCollisionRecs(Prop.GetCollisionRec(knight.GetScreenPos()), knight.GetCollisionRec()))

        {

            knight.UndoMovement();

        }

    }

   

    goblin.tick(GetFrameTime());

   

    EndDrawing();

}

CloseWindow();

}

BaseCharacte.hr:
#ifndef BASE_CHARACTER_H

#define BASE_CHARACTER_H

#include “raylib.h”

class BaseCharacter

{

public:

BaseCharacter();

bool Movin {false};

void UndoMovement();

Rectangle GetCollisionRec();

virtual void tick(float DeltaTime);

virtual Vector2 GetScreenPos() = 0;

protected:

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};

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

float Speed{4.5f};

float width{};

float height{};

float Scale{4.0f};

Vector2 velocity{};

private:

};

#endif

Character.h:
#ifndef CHARACTER_H

#define CHARACTER_H

#include “raylib.h”

#include “BaseCharacter.h”

class Character : public BaseCharacter

{

public:

Character(int WinWidth, int WinHeight);

virtual void tick(float DeltaTime) override;

virtual Vector2 GetScreenPos() override;

private:

int windowWidth{};

int windowHeight{};

};

#endif

Character.cpp:
#include “Character.h”

#include “raymath.h”

Character::Character(int WinWidth, int WinHeight) :

windowWidth(WinWidth),

windowHeight(WinHeight)

{

width = texture.width / 6;

height = texture.height;

Speed = 4.f;

}

Vector2 Character::GetScreenPos()

{

return Vector2 {

    static_cast<float>(windowWidth) / 2.0f - Scale * (0.5f * width),

    static_cast<float>(windowHeight) / 2.0f - Scale * (0.5f * height)

};

}

void Character::tick(float DeltaTime)

{

if (IsKeyDown(KEY_A))

    velocity.x -= 1.0;

if (IsKeyDown(KEY_D))

    velocity.x += 1.0;

if (IsKeyDown(KEY_W))

    velocity.y -= 1.0;

if (IsKeyDown(KEY_S))

    velocity.y += 1.0;

BaseCharacter::tick(DeltaTime);

}
BaseCharacter.cpp:
#include “BaseCharacter.h”

#include “raymath.h”

BaseCharacter::BaseCharacter()

{

}

void BaseCharacter::UndoMovement()

{

worldPos = WorldPosLastFrame;

}

Rectangle BaseCharacter::GetCollisionRec()

{

return Rectangle{

    GetScreenPos().x,

    GetScreenPos().y,

    width * Scale,

    height * Scale

};

}

void BaseCharacter::tick(float DeltaTime)

{

WorldPosLastFrame = worldPos;

RunningTime += DeltaTime;

if (RunningTime >= UpdateTime)

{

    frame++;

    RunningTime = 0.f;

    if (frame > maxFrames)

        frame = 0;

}

if (Vector2Length(velocity) != 0.0)

{

    // set WorldPos = WorldPos + velocity

    worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(velocity), Speed));

    // velocity.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 (velocity.x < 0.f)

        RightLeft = -1;

    else if (velocity.x > 0.f)

        RightLeft = 1;

    texture = run;

    Movin = true;

}

else

{

    texture = idle;

    Movin = false;

}

velocity = {};

// Draw the character

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

Rectangle dest{GetScreenPos().x, GetScreenPos().y, Scale * width, Scale * height};

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

}

Hi Stew,

Can you share a screenshot of what you’re seeing?

I’m stuck and the enemy can run up to me
(the goblin is actually me! which is suppose to happen seen in I made a Morph with the enemy being the knight, Also I spawned in this position, not sure why I spawned in this position)

Should be mapPos = Vector2Scale(knight.getWorldPos(), -1.f);, note that I had to make the getWorldPos() function myself by adding this to character.h

Vector2 getWorldPos() {return worldPos;}

I redid the course from the point showed at I made a Morph
also, I did do the thing but the game gave up collision for some reason

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms