Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Having trouble implementing enemy sprite homing in on hero sprite  (Read 1499 times)

0 Members and 1 Guest are viewing this topic.

Programmy

  • Newbie
  • *
  • Posts: 19
    • View Profile
I'm writing a Zelda clone for study, was able to implement a walkcycle and attack cycle in my player sprite but I'm having trouble making the enemy sprite follow and attack
my hero sprite

[main.cpp]

#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Opponent.h"
#include "OpponentAi.h"

int main()
{
       
        sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
       
        enum class state {

                PLAYING,
                GAMEOVER,
                PAUSED


        };
        Player player;
        Opponent opponent;
        Clock clock;
        OpponentAi opponentAi;
        while (window.isOpen())
        {
               
       
                sf::Event event;
                while (window.pollEvent(event))
                {
                       
                        if (event.type == sf::Event::Closed)
                                window.close();



                }

                 player.P_Movement();
                 opponent.ShowOpponent();



                player.ShowHero().setScale(3, 3);
                opponent.ShowOpponent().setScale(3, 3);

                window.clear(sf::Color::Black);

                // draw everything here...
                 window.draw(player.ShowHero());
                 window.draw(opponent.ShowOpponent());
                // end the current frame
                window.display();
        }

        return 0;
}


 


here is the opponent cpp file


#include "Opponent.h"



Opponent::Opponent()
{
        enemytexture.loadFromFile("Opponent.png");
        enemySprite.setTexture(enemytexture);
        enemySprite.setTextureRect(sf::IntRect(0, 0, 50, 50));
        enemySprite.setPosition(800 / 2, 600 / 2);
       
}


Opponent::~Opponent()
{
}
sf::Sprite& Opponent::ShowOpponent() {

        auto dt = clock.dt();

        //this affects the speed of the opponent going towards you
        float m_Speed = 100.0;



        playerLocation = sprite.getPosition();
        m_Position = enemySprite.getPosition();

        float playerX = playerLocation.x;
        float playerY = playerLocation.y;

        if (playerX < m_Position.x)
        {   //left

                enemySprite.setPosition(playerLocation);
                m_Position.x = m_Position.x - m_Speed * dt;
                enemySprite.setTextureRect(sf::IntRect(counterWalking * 0.0f, 136.0f, 27.0f, 36.0f));
        }

        if (playerY < m_Position.y)
        {   //up
               
                m_Position.y = m_Position.y - m_Speed * dt;
                enemySprite.setTextureRect(sf::IntRect(counterWalking * 0.f, 10.0f, 25.0f, 45.0f));
        }

        if (playerX > m_Position.x)
        {   //right
               
                m_Position.x = m_Position.x + m_Speed * dt;
                enemySprite.setTextureRect(sf::IntRect(counterWalking * 0.0f, 60.0f, 25.0f, 36.0f));
        }
        if (playerY > m_Position.y)
        {   //down
               
                m_Position.y = m_Position.y + m_Speed * dt;
                enemySprite.setTextureRect(sf::IntRect(counterWalking * 0.0f, 100.0f, 25.0f, 36.0f));
        }


        enemySprite.setPosition(m_Position);


       


       
        return enemySprite;
}


 

and here is the opponent .h file

#pragma once
#include "Player.h"
#include "Clock.h"
#include <memory>
#include <iostream>

class Opponent:public Player
{
public:
        Opponent();
        ~Opponent();

        sf::Sprite& ShowOpponent();



       

       



private:
       

        sf::Sprite enemySprite;
    sf::Texture enemytexture;

        Player player;
        Clock clock;
        sf::Vector2f playerLocation;
        sf::Vector2f m_Position;


       


       

};


 


here is my hero header file


#pragma once
#include "SFML/Graphics.hpp"
#include "Clock.h"


enum class Dir_Attack {

        STATIC,
        LEFT,
        RIGHT,
        DOWN,
        UP


};


class Player
{
public:
        Player();
        ~Player();
        sf::Sprite& ShowHero();
        void P_Movement();
        void SetAttack();
       

protected:
        sf::Clock clock;
        sf::Time time;
        float dt;
        Dir_Attack d_p_attack;
        float attackSlash;
        float counterWalking;
        float playerMovementSpeed;
        float multiplier;

        sf::Texture texture;
        sf::Sprite sprite;

private:

       

       

};


 

hero cpp file
#include "Player.h"



Player::Player()
{
        texture.loadFromFile("LinkAnimation.png");
        sprite.setTexture(texture);
        sprite.setTextureRect(sf::IntRect(40, 0, 40, 40));
        attackSlash = 3.0f;
        counterWalking = 1.0f;
        playerMovementSpeed = 10.0f;
        multiplier = 30.f;
}


Player::~Player()
{
}
sf::Sprite& Player::ShowHero() {



        return sprite;
}
void Player::P_Movement() {

        dt = clock.restart().asSeconds();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                ShowHero().move(0, -playerMovementSpeed * dt * multiplier);
                ShowHero().setTextureRect(sf::IntRect(counterWalking * 40.f, 40.0f * 1.0f, 40.0f, 40.0f));
               
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                ShowHero().move(0, playerMovementSpeed * dt * multiplier);
                ShowHero().setTextureRect(sf::IntRect(counterWalking * 40.0f, 0.0f, 40.0f, 40.0f));
               
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                ShowHero().move(-playerMovementSpeed * dt * multiplier , 0);
                ShowHero().setTextureRect(sf::IntRect(counterWalking * 40.0f, 80.0f, 40.0f, 40.0f));
               
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                ShowHero().move(playerMovementSpeed * dt * multiplier, 0);
                ShowHero().setTextureRect(sf::IntRect(counterWalking * 40.0f, 119.0f, 40.0f, 40.0f));
         
        }
        //
        //this is the crucial syntax to make the
        //sprite move
        counterWalking++;
        if (counterWalking == 8)
        {
                counterWalking = 0;
        }


}
void Player::SetAttack() {

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && d_p_attack == Dir_Attack::LEFT)
        {
                sprite.setTextureRect(sf::IntRect(attackSlash * 40.0f , 200.0f, 39.0f, 40.0f));
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && d_p_attack == Dir_Attack::RIGHT)
        {
                sprite.setTextureRect(sf::IntRect(attackSlash * 40.0f, 236.0f, 38.0f, 38.0f));
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && d_p_attack == Dir_Attack::DOWN)
        {
                sprite.setTextureRect(sf::IntRect(attackSlash * 40.0f, 160.0f, 40.0f, 40.0f));
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && d_p_attack == Dir_Attack::UP)
        {
                sprite.setTextureRect(sf::IntRect( attackSlash * 40.0f, 280.0f, 40.0f, 40.0f));
        }

        attackSlash++;
        if (attackSlash == 6)
        {

                attackSlash = 0;
        }

}



 

« Last Edit: February 27, 2019, 02:22:38 am by Programmy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Having trouble implementing enemy sprite homing in on hero sprite
« Reply #1 on: February 27, 2019, 07:53:09 am »
How is posting some code listing going to help us, help you? ;)

Please read the the following thread on how to ask questions: https://en.sfml-dev.org/forums/index.php?topic=5559.msg36367#msg36367
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Programmy

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Having trouble implementing enemy sprite homing in on hero sprite
« Reply #2 on: February 28, 2019, 02:00:51 am »
 
shortened it now, basically what is happening is that I was able to implement SFML in such that the opponent Sprite would follow and go towards the hero, I was able to also make the opponent even face towards the player wherever he goes , all in one Main Cpp file , when I broke the program up and separated the code into each file that's where the problem came up, the opponent sprite moves only in one direction but does not engage the player anymore, Is it because of the reference I put on the ShowOpponent function?


Opponent .H file

class Opponent:public Player
{
public:
        Opponent();
        ~Opponent();
        sf::Sprite& ShowOpponent();

private:
    sf::Sprite enemySprite;
    sf::Texture enemytexture;
    Player player;
    Clock clock;
    sf::Vector2f playerLocation;
    sf::Vector2f m_Position;
};


Opponent Cpp file

#include "Opponent.h"

Opponent::Opponent()
{
        enemytexture.loadFromFile("Opponent.png");
        enemySprite.setTexture(enemytexture);
        enemySprite.setTextureRect(sf::IntRect(0, 0, 50, 50));
        enemySprite.setPosition(800 / 2, 600 / 2);
       
}


Opponent::~Opponent()
{
}
sf::Sprite& Opponent::ShowOpponent() {

        auto dt = clock.dt();

        //this affects the speed of the opponent going towards you
        float m_Speed = 100.0;

        playerLocation = sprite.getPosition();
        m_Position = enemySprite.getPosition();

        float playerX = playerLocation.x;
        float playerY = playerLocation.y;

        if (playerX < m_Position.x){   //left
                m_Position.x = m_Position.x - m_Speed * dt;
                enemySprite.setTextureRect(sf::IntRect(counterWalking * 0.0f, 136.0f, 27.0f, 36.0f));
        }

        if (playerY < m_Position.y){   //up
                m_Position.y = m_Position.y - m_Speed * dt;
                enemySprite.setTextureRect(sf::IntRect(counterWalking * 0.f, 10.0f, 25.0f, 45.0f));
        }

        if (playerX > m_Position.x){   //right
                m_Position.x = m_Position.x + m_Speed * dt;
                enemySprite.setTextureRect(sf::IntRect(counterWalking * 0.0f, 60.0f, 25.0f, 36.0f));
        }
        if (playerY > m_Position.y){   //down
                m_Position.y = m_Position.y + m_Speed * dt;
                enemySprite.setTextureRect(sf::IntRect(counterWalking * 0.0f, 100.0f, 25.0f, 36.0f));
        }
        enemySprite.setPosition(m_Position);
 
        return enemySprite;
}


at Main.cpp here is the code to draw the Hero and Enemy Sprite
 
    window.draw(player.ShowHero());
    window.draw(opponent.ShowOpponent());

 
« Last Edit: February 28, 2019, 02:30:48 am by Programmy »

 

anything