Upload files to "/"
This commit is contained in:
parent
43739633b8
commit
3a493c1485
4 changed files with 246 additions and 17 deletions
58
enemy.cpp
Normal file
58
enemy.cpp
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue