[Refactored] Chase Range System + Stop Overlapping

Because of my Enemy Chase Range system I had to keep the movement code out of the Parent Class.
Here’s an update version:

void Enemy::tick(float deltaTime)
{   
    lastPos = worldPos; 
    velocity = Vector2Subtract(target->getScreenPos(), getScreenPos());

    attackRangeRec.x = getScreenPos().x - attackRangeRec.width / 2;
    attackRangeRec.y = getScreenPos().y - attackRangeRec.width / 2;

    if(CheckCollisionRecs(attackRangeRec,target->GetCollisionRec()))
    {
        if(!CheckCollisionRecs(GetCollisionRec(), target->GetCollisionRec()))
        {
            // Movement Code
            if (Vector2Length(velocity) != 0.0) // Only move when velocity isn't = 0
            {
                currentSheet = runSheet;
                worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(velocity), moveSpeed));
                    
                //Change facing direction
                if (velocity.x < 0.f) {rightLeft = -1.f;}
                else if(velocity.x > 0.f) {rightLeft = 1.f;}
            }
        }
        else {currentSheet = idleSheet;}
    }
    else
    {
        currentSheet = idleSheet;        
    }

    BaseCharacter::tick(deltaTime);
}

I also had to move lastPos = worldPos out of the BaseCharacter::tick() function, otherwise the undoMovement() function stops working and so do the map boundaries and prop collision.

ezgif-7-3e74713aa8

1 Like

Privacy & Terms