#pragma once
#include "Entity.h"
#include <iostream>
class Projectile : public Entity
{
public:
Projectile() {}
Projectile(float x, float y) {
load("Graphics/Projectile.png");
m_sprite.setPosition(x, y);
m_speed = 400;
}
};
#pragma once
#include "Entity.h"
#include <iostream>
#include "Projectile.h"
enum class Color {
red,
yellow,
brown,
blue
};
class Enemy : public Entity
{
public:
Enemy(const Color& c, const sf::Vector2f& pos) {
switch (c) {
case Color::blue:
{
load("Graphics/blueEnemy.png");
}
break;
case Color::red:
{
load("Graphics/redEnemy.png");
}
break;
case Color::yellow:
{
load("Graphics/yellowEnemy.png");
}
break;
case Color::brown:
{
load("Graphics/brownEnemy.png");
}
break;
}
setPos(pos);
m_speed = 100;
}
};
#pragma once
#include "Entity.h"
#include "Projectile.h"
#include <vector>
class Spaceship : public Entity
{
public:
Spaceship();
void move(float);
void shoot(float);
void update(float);
void draw(sf::RenderWindow*) override;
private:
bool wasSpacePressed;
std::vector<std::unique_ptr<Projectile>> m_projectiles;
sf::Clock m_clock;
};
class EnemyFleet : public Entity
{
public:
EnemyFleet();
~EnemyFleet();
void move(float);
bool isEnemyBottom() const;
bool isLeftMost() const;
bool isRightMost() const;
void moveX(float);
void moveDown();
void update(float);
void draw(sf::RenderWindow*) override;
private:
std::vector<std::unique_ptr<Enemy>> m_enemyFleet;
bool m_leftToRight; //motion of the enemyfleet
float m_speedModifier;
};
我希望能够在它们相互碰撞时删除一个射弹和敌人,但我不知道如何做到这一点,因为unique_ptr无法复制到某些碰撞管理器类中的任何参数bc它具有独占性所有权。 unique_ptr仍然是我应该使用的东西,我不必调用删除(如果它是一个原始的ptr)?
答案 0 :(得分:0)
是的,您无法复制AddOutputFilter
,但您可以复制或获取unique_ptr
指向(unique_ptr
和Projectile
)的对象的引用。
Enemy
//Example
bool collision_manager::is_collision(Projectile & pr, Enemy & en) {
if (pr.position() == en.position()) {
return true;
}
return false
}
// similar for other objects that contain vector of unique_ptr
Projectile & Spaceship::get_Projectile(size_t num) {
if(num < m_projectiles.length()) {
return *projectiles.at(num);
}
throw std::invalid_argument("some alert info");
}
。