123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #pragma once
- #include <JSON.h>
- #include <Text.h>
- #include <Trie.h>
- #include <Writer.h>
- #include "BlockType.h"
- #include "Effect.h"
- #include "Game.h"
- #include "ModelInfo.h"
- class Item;
- class ItemStack;
- class ItemSkill;
- class ItemSkillLevelUpRule;
- class ItemTypeEnum
- {
- public:
- static const int PLAYER_HAND = 0;
- };
- class ItemType : public virtual Framework::ReferenceCounter
- {
- protected:
- int id;
- Framework::Text name;
- Framework::Text tooltipUIML;
- ModelInfo* model;
- int maxStackSize;
- Framework::RCArray<Framework::Text> groups;
- public:
- ItemType();
- ~ItemType();
- protected:
- virtual void loadSuperItem(
- Item* zItem, Framework::StreamReader* zReader) const;
- virtual void saveSuperItem(
- const Item* zItem, Framework::StreamWriter* zWriter) const;
- public:
- virtual bool initialize(Game* zGame);
- virtual const ItemType* zBrokenItemType() const;
- virtual Item* createItem() const = 0;
- virtual ItemStack* createItemStack(int size) const;
- virtual ItemSkill* createDefaultItemSkill() const;
- virtual void levelUpItemSkill(ItemSkill* zSkill) const;
- virtual Item* cloneItem(const Item* zItem) const;
- virtual Item* breakItem(const Item* zItem) const;
- virtual Item* loadItem(Framework::StreamReader* zReader) const;
- virtual void saveItem(
- const Item* zItem, Framework::StreamWriter* zWriter) const;
- virtual void setItemAttribute(Item* zItem,
- Framework::Text name,
- Framework::JSON::JSONValue* zValue) const;
- virtual void addItemAttributes(
- Item* zItem, Framework::JSON::JSONObject* zItemObjet) const;
- void setTypeId(int id);
- int getId() const;
- void setName(Framework::Text name);
- const Framework::Text& getName() const;
- void setTooltipUIML(Framework::Text tooltipUIML);
- const Framework::Text& getTooltipUIML() const;
- void setModel(ModelInfo* model);
- ModelInfo* zModel() const;
- void setMaxStackSize(int maxStackSize);
- int getMaxStackSize() const;
- void addGroup(Framework::Text group);
- const Framework::RCArray<Framework::Text>& getGroups() const;
- static Item* createBasicItem(int id,
- const char* name,
- float hp,
- float maxHp,
- float durability,
- float maxDurability,
- bool eatable,
- bool placeable,
- bool equippable,
- bool solid,
- bool usable);
- static int getTypeId(const char* name);
- static const ItemType* zByName(const char* name);
- };
- template<typename S> class ItemTypeFactoryBase
- : public SubTypeFactory<ItemType, S>
- {
- public:
- ItemTypeFactoryBase()
- : SubTypeFactory<ItemType, S>()
- {}
- virtual void fromJson(
- S* zResult, Framework::JSON::JSONObject* zJson) const override
- {
- ItemType* zType = dynamic_cast<ItemType*>(zResult);
- zType->setModel(Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- zJson->zValue("model")->asObject()));
- zType->setName(zJson->zValue("name")->asString()->getString());
- if (zJson->hasValue("tooltipUIML"))
- {
- zType->setTooltipUIML(
- zJson->zValue("tooltipUIML")->asString()->getString());
- }
- zType->setMaxStackSize(
- (int)zJson->zValue("maxStack")->asNumber()->getNumber());
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("groupNames")->asArray())
- {
- zType->addGroup(value->asString()->getString());
- }
- }
- virtual void toJson(
- S* zObject, Framework::JSON::JSONObject* zResult) const override
- {
- ItemType* zType = dynamic_cast<ItemType*>(zResult);
- zResult->addValue("model",
- Game::INSTANCE->zTypeRegistry()->toJson<ModelInfo>(
- zType->zModel()));
- zResult->addValue(
- "name", new Framework::JSON::JSONString(zType->getName()));
- zResult->addValue("tooltipUIML",
- new Framework::JSON::JSONString(zType->getTooltipUIML()));
- zResult->addValue("maxStack",
- new Framework::JSON::JSONNumber(zType->getMaxStackSize()));
- Framework::JSON::JSONArray* groupNames
- = new Framework::JSON::JSONArray();
- for (Framework::Text* groupName : zType->getGroups())
- {
- groupNames->addValue(new Framework::JSON::JSONString(*groupName));
- }
- zResult->addValue("groupNames", groupNames);
- }
- virtual JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override
- {
- return builder
- ->withRequiredAttribute("model",
- Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
- ->withRequiredString("name")
- ->finishString()
- ->withRequiredString("tooltipUIML")
- ->whichIsOptional()
- ->finishString()
- ->withRequiredNumber("maxStack")
- ->withDefault(50)
- ->finishNumber()
- ->withRequiredArray("groupNames")
- ->withDefault(new Framework::JSON::JSONArray())
- ->addAcceptedStringInArray()
- ->finishString()
- ->finishArray();
- }
- };
|