简体   繁体   English

处理与使用唯一指针向量的对象的冲突

[英]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. 我希望能够在弹丸和敌人相互碰撞时删除它们,但是我不确定该怎么做,因为unique_ptr无法复制到某些具有专有所有权的碰撞管理器类bc中的任何参数中。 Is unique_ptr still something i should use bc i dont have to call delete (like if its a raw ptr)? 是unique_ptr还是我应该使用bc的东西吗,我不必调用delete(例如,如果它是原始的ptr)?

Yes, you can't copy unique_ptr , but you can copy or get reference from object that unique_ptr points to ( Projectile and Enemy ). 是的,您不能复制unique_ptr ,但是可以从unique_ptr指向的对象( ProjectileEnemy )复制或获取引用。

//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");
}
  • Instead of copying you can also use std::move(unique_ptr...) . 除了复制外,您还可以使用std::move(unique_ptr...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM