Niby strzela, ale bez kolizji
This commit is contained in:
@@ -30,6 +30,8 @@ add_executable(LotoStatek main.cpp
|
|||||||
sources/ObjectItem.cpp
|
sources/ObjectItem.cpp
|
||||||
sources/Enemy.cpp
|
sources/Enemy.cpp
|
||||||
headers/Enemy.h
|
headers/Enemy.h
|
||||||
|
headers/EnemyBullet.h
|
||||||
|
sources/EnemyBullet.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
|
|||||||
BIN
assets/img/bullets/enemy_bullet.png
Normal file
BIN
assets/img/bullets/enemy_bullet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.3 KiB |
@@ -8,6 +8,8 @@ class Bullet : public Projectile {
|
|||||||
public:
|
public:
|
||||||
Bullet(float x, float y, sf::Texture &texture) : Projectile(x,y, texture) {};
|
Bullet(float x, float y, sf::Texture &texture) : Projectile(x,y, texture) {};
|
||||||
void update() override;
|
void update() override;
|
||||||
|
private:
|
||||||
|
float directionY;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,13 @@
|
|||||||
#include "Actor.h"
|
#include "Actor.h"
|
||||||
#include "SFML/System/Clock.hpp"
|
#include "SFML/System/Clock.hpp"
|
||||||
|
|
||||||
|
enum class Direction {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right
|
||||||
|
};
|
||||||
|
|
||||||
class Enemy : public Actor {
|
class Enemy : public Actor {
|
||||||
public:
|
public:
|
||||||
Enemy(int x, int y, std::string path);
|
Enemy(int x, int y, std::string path);
|
||||||
@@ -14,12 +21,19 @@ public:
|
|||||||
void moveRight() override;
|
void moveRight() override;
|
||||||
void moveUp() override;
|
void moveUp() override;
|
||||||
void moveDown() override;
|
void moveDown() override;
|
||||||
|
|
||||||
|
void update();
|
||||||
|
|
||||||
bool isAlive() const;
|
bool isAlive() const;
|
||||||
void takeDamage();
|
void takeDamage();
|
||||||
|
void updateDirection();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::Clock shootClock;
|
sf::Clock shootClock;
|
||||||
|
sf::Texture enemyBulletTexture;
|
||||||
|
float movementSpeed = 2.0f;
|
||||||
bool alive = true;
|
bool alive = true;
|
||||||
|
Direction direction = Direction::Down;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ENEMY_H
|
#endif // ENEMY_H
|
||||||
|
|||||||
14
headers/EnemyBullet.h
Normal file
14
headers/EnemyBullet.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef ENEMY_BULLET_H
|
||||||
|
#define ENEMY_BULLET_H
|
||||||
|
|
||||||
|
#include "Projectile.h"
|
||||||
|
|
||||||
|
class EnemyBullet : public Projectile {
|
||||||
|
public:
|
||||||
|
EnemyBullet(float x, float y, sf::Texture &texture);
|
||||||
|
void update() override;
|
||||||
|
|
||||||
|
void setOutOfBounds(bool status);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ENEMY_BULLET_H
|
||||||
@@ -25,6 +25,7 @@ public:
|
|||||||
void update_meteors();
|
void update_meteors();
|
||||||
void update();
|
void update();
|
||||||
void spawn_enemy();
|
void spawn_enemy();
|
||||||
|
void setOutOfBounds(bool status);
|
||||||
private:
|
private:
|
||||||
Size size;
|
Size size;
|
||||||
Background background;
|
Background background;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "../headers/Actor.h"
|
#include "../headers/Actor.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
Actor::Actor(int x, int y, std::string path) {
|
Actor::Actor(int x, int y, std::string path) {
|
||||||
loadTexture(path);
|
loadTexture(path);
|
||||||
position.x = x;
|
position.x = x;
|
||||||
@@ -29,13 +31,17 @@ std::vector<Bullet> &Actor::getBullets() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Actor::updateBullets() {
|
void Actor::updateBullets() {
|
||||||
for (auto& bullet : bullets) {
|
for (auto it = bullets.begin(); it != bullets.end(); ) {
|
||||||
if(bullet.isOutOfBounds()) {
|
if (it->isOutOfBounds()) {
|
||||||
bullets.erase(bullets.begin());
|
it = bullets.erase(it); // Usuwa element i zwraca iterator na następny element
|
||||||
|
} else {
|
||||||
|
++it; // Przechodzi do następnego elementu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
std::cout << "Liczba pociskow: " << bullets.size() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Actor::setMovingSpeed(float speed) {
|
void Actor::setMovingSpeed(float speed) {
|
||||||
moving_speed = speed;
|
moving_speed = speed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,28 +3,71 @@
|
|||||||
|
|
||||||
Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) {
|
Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) {
|
||||||
hp = 1; // Przeciwnik ma 1 punkt życia
|
hp = 1; // Przeciwnik ma 1 punkt życia
|
||||||
firerate = 1000; // Strzela co 1 sekundę
|
firerate = 2000; // Strzela co 1 sekundę
|
||||||
moving_speed = 2.0f; // Prędkość ruchu przeciwnika
|
moving_speed = 2.0f; // Prędkość ruchu przeciwnika
|
||||||
|
enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Enemy::shoot() {
|
void Enemy::shoot() {
|
||||||
if (shootClock.getElapsedTime().asMilliseconds() >= firerate) {
|
if (shootClock.getElapsedTime().asMilliseconds() >= firerate) {
|
||||||
bullets.emplace_back(position.x, position.y + 20, bulletTextureLeft); // Strzał w dół
|
Bullet bullet(position.x, position.y + actorSprite.getGlobalBounds().height / 2, enemyBulletTexture);
|
||||||
|
bullet.setSpeed(10.0f); // Prędkość w dół
|
||||||
|
bullets.emplace_back(std::move(bullet));
|
||||||
shootClock.restart();
|
shootClock.restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Enemy::updateDirection() {
|
||||||
|
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
||||||
|
if (position.y <= 0) { // Górna krawędź ekranu
|
||||||
|
direction = Direction::Down; // Zmieniamy na ruch w dół
|
||||||
|
} else if (position.y >= 800) { // Dolna krawędź ekranu
|
||||||
|
direction = Direction::Up; // Zmieniamy na ruch w górę
|
||||||
|
}
|
||||||
|
|
||||||
|
// Podobna logika dla kierunku lewo/prawo, jeśli przeciwnik będzie się poruszał w poziomie
|
||||||
|
if (position.x <= 0) { // Lewa krawędź
|
||||||
|
direction = Direction::Right; // Zmieniamy na ruch w prawo
|
||||||
|
} else if (position.x >= 1200) { // Prawa krawędź
|
||||||
|
direction = Direction::Left; // Zmieniamy na ruch w lewo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Enemy::move(float deltaX, float deltaY) {
|
void Enemy::move(float deltaX, float deltaY) {
|
||||||
actorSprite.move(deltaX, deltaY);
|
actorSprite.move(deltaX, deltaY);
|
||||||
position.x += static_cast<int>(deltaX);
|
position.x += static_cast<int>(deltaX);
|
||||||
position.y += static_cast<int>(deltaY);
|
position.y += static_cast<int>(deltaY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Enemy::moveLeft() { move(-moving_speed, 0.0f); }
|
void Enemy::moveLeft() { move(-moving_speed, 0.0f); }
|
||||||
void Enemy::moveRight() { move(moving_speed, 0.0f); }
|
void Enemy::moveRight() { move(moving_speed, 0.0f); }
|
||||||
void Enemy::moveUp() { move(0.0f, -moving_speed); }
|
void Enemy::moveUp() { move(0.0f, -moving_speed); }
|
||||||
void Enemy::moveDown() { move(0.0f, moving_speed); }
|
void Enemy::moveDown() { move(0.0f, moving_speed); }
|
||||||
|
|
||||||
|
void Enemy::update() {
|
||||||
|
// Sprawdzamy, czy przeciwnik dotarł do krawędzi i zmieniamy kierunek
|
||||||
|
updateDirection();
|
||||||
|
|
||||||
|
// Zgodnie z kierunkiem, poruszamy przeciwnikiem
|
||||||
|
switch (direction) {
|
||||||
|
case Direction::Up:
|
||||||
|
moveUp();
|
||||||
|
break;
|
||||||
|
case Direction::Down:
|
||||||
|
moveDown();
|
||||||
|
break;
|
||||||
|
case Direction::Left:
|
||||||
|
moveLeft();
|
||||||
|
break;
|
||||||
|
case Direction::Right:
|
||||||
|
moveRight();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Enemy::isAlive() const {
|
bool Enemy::isAlive() const {
|
||||||
return alive;
|
return alive;
|
||||||
}
|
}
|
||||||
|
|||||||
18
sources/EnemyBullet.cpp
Normal file
18
sources/EnemyBullet.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#include "../headers/EnemyBullet.h"
|
||||||
|
|
||||||
|
EnemyBullet::EnemyBullet(float x, float y, sf::Texture &texture)
|
||||||
|
: Projectile(x, y, texture) {
|
||||||
|
speed = 5.0f; // Ruch w dół (dodatnia prędkość)
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnemyBullet::update() {
|
||||||
|
sprite.move(0.0f, speed); // Przesuwanie pocisku w dół
|
||||||
|
position.y += speed;
|
||||||
|
|
||||||
|
if (position.y > 800) {
|
||||||
|
outOfBounds = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void EnemyBullet::setOutOfBounds(bool status) {
|
||||||
|
outOfBounds = status;
|
||||||
|
}
|
||||||
@@ -159,7 +159,7 @@ void Plansza::update() {
|
|||||||
}
|
}
|
||||||
// Ruch i renderowanie przeciwników
|
// Ruch i renderowanie przeciwników
|
||||||
for (auto it = enemies.begin(); it != enemies.end();) {
|
for (auto it = enemies.begin(); it != enemies.end();) {
|
||||||
it->moveDown(); // Przeciwnicy poruszają się w dół
|
it->update(); // Aktualizacja kierunku i ruchu przeciwnika
|
||||||
it->shoot();
|
it->shoot();
|
||||||
|
|
||||||
// Rysowanie przeciwników
|
// Rysowanie przeciwników
|
||||||
@@ -173,6 +173,28 @@ void Plansza::update() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto& enemy : enemies) { // Lista przeciwników w grze
|
||||||
|
enemy.shoot();
|
||||||
|
enemy.updateBullets();
|
||||||
|
for (auto& bullet : enemy.getBullets()) {
|
||||||
|
bullet.update();
|
||||||
|
window->draw(bullet.getSprite());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usuwanie pocisków, które są poza ekranem
|
||||||
|
for (auto enemyIt = enemies.begin(); enemyIt != enemies.end(); ) {
|
||||||
|
for (auto bulletIt = enemyIt->getBullets().begin(); bulletIt != enemyIt->getBullets().end();) {
|
||||||
|
if (bulletIt->isOutOfBounds()) {
|
||||||
|
bulletIt = enemyIt->getBullets().erase(bulletIt); // Usuwamy pocisk, który wyszedł poza ekran
|
||||||
|
} else {
|
||||||
|
++bulletIt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
++enemyIt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Kolizje między pociskami gracza a przeciwnikami
|
// Kolizje między pociskami gracza a przeciwnikami
|
||||||
for (auto enemyIt = enemies.begin(); enemyIt != enemies.end();) {
|
for (auto enemyIt = enemies.begin(); enemyIt != enemies.end();) {
|
||||||
bool hit = false;
|
bool hit = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user