1
Graphics / Having trouble implementing enemy sprite homing in on hero sprite
« on: February 27, 2019, 02:14:10 am »
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
here is the opponent cpp file
and here is the opponent .h file
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
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;
}
#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;
}
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;
};
#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;
}
}
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;
}
}