库存系统中的对象切片



我正在为一个游戏开发库存系统,我正在用对象切片碰壁;我正在丢失对派生类的引用中的变量。以下是一段摘录,其中在主游戏文件中创建了一件T恤,然后将其传递到玩家库存中进行存储。然而,只有存在于基类Item中的变量被保留。

game.cpp

#include "Item.h"
#include "Clothes.h"
#include "Shirts.h"
shirt_item white_shirt = shirt_item(materialDescriptor::cotton, colourDescriptor::white);
player.getComponent<InventoryComponent>().storeItem(&whiteShirt);

InventoryComponent.cpp

bool InventoryComponent::storeItem(Item *inItem)
{
if (freeInvSpace() > 0)
{
items.push_back(inItem);
return true;
}
else if (freeInvSpace() < 0)
{
std::cout << "ERROR! Inventory over filled somehow" << std::endl;
}
return false;
}

InventoryComponent.h

#pragma once
#include "Components.h"
#include "Item.h"
#include "Clothes.h"
#include "Shirts.h"

class InventoryComponent : public Component // Entity component system
{
public:
std::vector<Item*> items;
InventoryComponent(int inSize)
{
size = inSize;
}
bool storeItem(Item *inItem);
...
}

项目.h

#pragma once
#include <string>
class Item
{
public:
std::string name,
description;
bool pronoun;
};

衣服.h

#pragma once
#include <vector>
#include <string>
#include "Item.h"
#include "materialDescriptor.h"
#include "colourDescriptor.h"
class Clothes : public Item
{
public:
materialDescriptor material;
std::vector<bodyParts> coverage;
colourDescriptor colour;
Clothes(std::string inName, std::string inDescription, materialDescriptor inMaterial, colourDescriptor colour, bool inPronoun = false)
{
name = inName;
description = inDescription;
material = inMaterial;
pronoun = inPronoun;
}
Clothes() {} 
};

衬衫.h

#pragma once
#include "Clothes.h"
#include "materialDescriptor.h"
#include "colourDescriptor.h"
class shirt_item : public Clothes
{
public:
shirt_item(materialDescriptor inMaterial, colourDescriptor inColour)
{
material = inMaterial;
colour = inColour;
description = "A basic shirt that covers the wearer from the elements";
name = "T-Shirt"
}
}

ECS.h

#pragma once
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
#include <bitset>
#include <array>
#include "Components.h"
class Component
{
public:
Entity* entity;
virtual void init() {}
virtual void update() {}
virtual void draw() {}
virtual ~Component() {}
private:
};
class Entity
{
private:
bool active = true;
std::vector<std::unique_ptr<Component>> components;
ComponentArray componentArray;
ComponentBitSet componentBitSet;
public:
template <typename T> T& getComponent() const
{
auto ptr(componentArray[getComponentTypeID<T>()]); 
return *static_cast<T*>(ptr);
}
}

使用Vs2019断点,T恤的构造函数可以工作,但一旦我尝试使用该对象,它就会归结为它的基类:ItemClothings>Shirts

如果通过指针传递和存储继承的对象,则最终必须将它们存储在堆上。相反,您在堆栈上创建它们。只需执行
auto white_shirt = std::make_unique<shirt_item>(materialDescriptor::cotton, colourDescriptor::white);

相关内容

  • 没有找到相关文章

最新更新