58 lines
1.3 KiB
C++
58 lines
1.3 KiB
C++
#include "enemy.hpp"
|
|
|
|
Enemy::Enemy(float x, float y, float leftB, float rightB)
|
|
: shape(sf::Vector2f(40.f, 50.f)),
|
|
leftBound(leftB),
|
|
rightBound(rightB),
|
|
speed(200.f),
|
|
movingRight(true),
|
|
aggro(false),
|
|
platformY(y)
|
|
{
|
|
shape.setPosition(x, y);
|
|
shape.setFillColor(sf::Color::Red); // просто колір, без текстури
|
|
}
|
|
|
|
void Enemy::update(float dt, const sf::Vector2f& playerPos) {
|
|
float x = shape.getPosition().x;
|
|
|
|
if (aggro) {
|
|
if (playerPos.x < x)
|
|
x -= speed * dt;
|
|
else
|
|
x += speed * dt;
|
|
} else {
|
|
if (movingRight) {
|
|
x += speed * dt;
|
|
if (x + shape.getSize().x >= rightBound) movingRight = false;
|
|
} else {
|
|
x -= speed * dt;
|
|
if (x <= leftBound) movingRight = true;
|
|
}
|
|
}
|
|
|
|
if (x < leftBound) x = leftBound;
|
|
if (x + shape.getSize().x > rightBound) x = rightBound - shape.getSize().x;
|
|
|
|
shape.setPosition(x, platformY);
|
|
}
|
|
|
|
void Enemy::draw(sf::RenderWindow& window) {
|
|
window.draw(shape);
|
|
}
|
|
|
|
void Enemy::setAggro(bool value) {
|
|
aggro = value;
|
|
}
|
|
|
|
float Enemy::getPlatformY() const {
|
|
return platformY;
|
|
}
|
|
|
|
sf::FloatRect Enemy::getBounds() const {
|
|
return shape.getGlobalBounds();
|
|
}
|
|
|
|
sf::RectangleShape& Enemy::getShape() {
|
|
return shape;
|
|
}
|