jeudi 1 septembre 2016

Handling collisions with objects that use vector of unique pointers

 #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)?

Aucun commentaire:

Enregistrer un commentaire