mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-15 20:03:15 +02:00
Mostly final interface for object type handler
Partial implementation of object with reward constructor
This commit is contained in:
@@ -182,8 +182,8 @@ void ObjectTemplate::readMap(CBinaryReader & reader)
|
||||
|
||||
void ObjectTemplate::readJson(const JsonNode &node)
|
||||
{
|
||||
id = Obj(node["basebase"].Float()); // temporary, should be removed and determined indirectly via object type parent (e.g. base->base)
|
||||
subid = node["base"].Float();
|
||||
//id = Obj(node["basebase"].Float()); // temporary, should be removed and determined indirectly via object type parent (e.g. base->base)
|
||||
//subid = node["base"].Float();
|
||||
animationFile = node["animation"].String();
|
||||
|
||||
const JsonVector & visitDirs = node["visitableFrom"].Vector();
|
||||
@@ -347,7 +347,7 @@ CDefObjInfoHandler::CDefObjInfoHandler()
|
||||
{
|
||||
readTextFile("Data/Objects.txt");
|
||||
readTextFile("Data/Heroes.txt");
|
||||
|
||||
/*
|
||||
// TODO: merge into modding system
|
||||
JsonNode node = JsonUtils::assembleFromFiles("config/objectTemplates.json");
|
||||
node.setMeta("core");
|
||||
@@ -371,6 +371,7 @@ CDefObjInfoHandler::CDefObjInfoHandler()
|
||||
|
||||
// merge new templates into storage
|
||||
objects.insert(objects.end(), newTemplates.begin(), newTemplates.end());
|
||||
*/
|
||||
}
|
||||
|
||||
void CDefObjInfoHandler::eraseAll(Obj type, si32 subtype)
|
||||
|
@@ -130,38 +130,40 @@ public:
|
||||
|
||||
class CGObjectInstance;
|
||||
|
||||
class IObjectTypeHandler
|
||||
class IObjectTypesHandler
|
||||
{
|
||||
public:
|
||||
virtual CGObjectInstance * create(ui32 id, ui32 subID) const = 0;
|
||||
virtual std::vector<ObjectTemplate> getTemplates(si32 type, si32 subType) const = 0;
|
||||
|
||||
virtual bool handlesID(ui32 id) const = 0;
|
||||
virtual CGObjectInstance * create(ObjectTemplate tmpl) const = 0;
|
||||
|
||||
virtual bool handlesID(ObjectTemplate tmpl) const = 0;
|
||||
|
||||
virtual void configureObject(CGObjectInstance * object) const = 0;
|
||||
|
||||
virtual IObjectInfo * getObjectInfo(ui32 id, ui32 subID) const = 0;
|
||||
virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<IObjectTypeHandler> TObjectTypeHandler;
|
||||
typedef std::shared_ptr<IObjectTypesHandler> TObjectTypeHandler;
|
||||
|
||||
class CObjectTypesHandler
|
||||
class CObjectGroupsHandler
|
||||
{
|
||||
/// list of object handlers, each of them handles 1 or more object type
|
||||
std::vector<TObjectTypeHandler> objectTypes;
|
||||
|
||||
public:
|
||||
/// returns handler for specified object (ID-based). ObjectHandler keeps ownership
|
||||
IObjectTypeHandler * getHandlerFor(CObjectTemplate tmpl) const;
|
||||
IObjectTypesHandler * getHandlerFor(ObjectTemplate tmpl) const;
|
||||
|
||||
/// creates object based on specified template
|
||||
CGObjectInstance * createObject(CObjectTemplate tmpl);
|
||||
CGObjectInstance * createObject(ObjectTemplate tmpl);
|
||||
|
||||
template<typename CObjectClass>
|
||||
CObjectClass * createObjectTyped(CObjectTemplate tmpl)
|
||||
CObjectClass * createObjectTyped(ObjectTemplate tmpl)
|
||||
{
|
||||
auto objInst = createObject(tmpl);
|
||||
auto objClass = dynamic_cast<CObjectClass*>(objInst);
|
||||
assert(objClass);
|
||||
return objClass;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@@ -10,3 +10,65 @@
|
||||
* Full text of license available in license.txt file, in main folder
|
||||
*
|
||||
*/
|
||||
|
||||
CObjectWithRewardConstructor::CObjectWithRewardConstructor()
|
||||
{
|
||||
}
|
||||
|
||||
void CObjectWithRewardConstructor::init(const JsonNode & config)
|
||||
{
|
||||
int id = config["id"].Float();
|
||||
std::string name = config["name"].String();
|
||||
for (auto & entry : config["types"].Struct()) // for each object type
|
||||
{
|
||||
JsonNode typeConf = entry.second;
|
||||
|
||||
int subID = typeConf["id"].Float();
|
||||
|
||||
objectInfos[id][subID].info.init(typeConf["properties"]);
|
||||
for (auto entry : typeConf["templates"].Struct())
|
||||
{
|
||||
ObjectTemplate tmpl;
|
||||
tmpl.id = Obj(id);
|
||||
tmpl.subid = subID;
|
||||
tmpl.readJson(entry.second);
|
||||
objectInfos[id][subID].templates.push_back(tmpl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<ObjectTemplate> CObjectWithRewardConstructor::getTemplates(si32 type, si32 subType) const
|
||||
{
|
||||
assert(handlesID(type, subtype));
|
||||
return objectInfos.at(type).at(subType).templates;
|
||||
}
|
||||
|
||||
CGObjectInstance * CObjectWithRewardConstructor::create(ObjectTemplate tmpl) const
|
||||
{
|
||||
assert(handlesID(tmpl));
|
||||
auto ret = new CObjectWithReward();
|
||||
ret->appearance = tmpl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool CObjectWithRewardConstructor::handlesID(si32 id, si32 subID) const
|
||||
{
|
||||
return objectInfos.count(id) && objectInfos.at(id).count(subID);
|
||||
}
|
||||
|
||||
bool CObjectWithRewardConstructor::handlesID(ObjectTemplate tmpl) const
|
||||
{
|
||||
return handlesID(tmpl.id, tmpl.subid);
|
||||
}
|
||||
|
||||
void CObjectWithRewardConstructor::configureObject(CGObjectInstance * object) const
|
||||
{
|
||||
assert(handlesID(object->appearance));
|
||||
objectInfos.at(object->ID).at(object->subID).info.configureObject(dynamic_cast<CObjectWithReward*>(object));
|
||||
}
|
||||
|
||||
const IObjectInfo * CObjectWithRewardConstructor::getObjectInfo(ObjectTemplate tmpl) const
|
||||
{
|
||||
assert(handlesID(tmpl));
|
||||
return &objectInfos.at(tmpl.id).at(tmpl.subid).info;
|
||||
}
|
||||
|
@@ -16,8 +16,7 @@
|
||||
|
||||
class CRandomRewardObjectInfo : public IObjectInfo
|
||||
{
|
||||
JsonNode objectConfig;
|
||||
|
||||
JsonNode parameters;
|
||||
public:
|
||||
bool givesResources() const override;
|
||||
|
||||
@@ -39,19 +38,30 @@ public:
|
||||
CRandomRewardObjectInfo()
|
||||
{}
|
||||
|
||||
void init(JsonNode objectConfig);
|
||||
void init(const JsonNode & objectConfig);
|
||||
};
|
||||
|
||||
class CObjectWithRewardConstructor : public IObjectTypeHandler
|
||||
class CObjectWithRewardConstructor : public IObjectTypesHandler
|
||||
{
|
||||
std::map<ui32, std::map<ui32, CRewardObjectInfo> > objectConfigs;
|
||||
struct ObjectInfo
|
||||
{
|
||||
CRandomRewardObjectInfo info;
|
||||
std::vector<ObjectTemplate> templates;
|
||||
};
|
||||
std::map<ui32, std::map<ui32, ObjectInfo> > objectInfos;
|
||||
|
||||
public:
|
||||
CGObjectInstance * create(ui32 id, ui32 subID) const override;
|
||||
CObjectWithRewardConstructor();
|
||||
void init(const JsonNode & config);
|
||||
|
||||
bool handlesID(ui32 id) const override;
|
||||
std::vector<ObjectTemplate> getTemplates(si32 type, si32 subType) const override;
|
||||
|
||||
CGObjectInstance * create(ObjectTemplate tmpl) const override;
|
||||
|
||||
bool handlesID(si32 id, si32 subID) const;
|
||||
bool handlesID(ObjectTemplate tmpl) const override;
|
||||
|
||||
void configureObject(CGObjectInstance * object) const override;
|
||||
|
||||
IObjectInfo * getObjectInfo(ui32 id, ui32 subID) const override;
|
||||
const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const override;
|
||||
};
|
||||
|
Reference in New Issue
Block a user