0
\$\begingroup\$
#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;
};

I want to be able to delete a projectile and enemy when they collide with each other, but I'm not sure how to do this since unique_ptr cannot be copied into any parameter in some collision manager class bc it has exclusive ownership. Is unique_ptr still something i should use bc i dont have to call delete (like if its a raw ptr)?

\$\endgroup\$
1
1
\$\begingroup\$

since unique_ptr cannot be copied into any parameter in some collision manager class bc it has exclusive ownership.

That is correct; however, you can pass references to unique pointers, or you could simply pass the underlying bare pointer instead. For example, if you want to have a function that tests if a projectile and a ship intersect, you could declare that function as:

bool intersects (const Projectile* projectile, const Enemy* enemy);

or

bool intersects (const std::unique_ptr<Projectile>& projectile, const const std::unique_ptr<Enemy>& enemy);

In the first case, you'd use unique_ptr's get() method to recover the bare pointer. In the second case you'd just pass the unique_ptr references directly.

If the collision function returns true, you simply remove the projection and enemy from the corresponding arrays; this will delete them.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.