#pragma once #include #include #include #include #include "TypeRegistry.h" class ItemType; class ItemSkill; class Block; class Entity; class Dimension; class Item; class ItemSkillLevelUpRule : public virtual Framework::ReferenceCounter { public: virtual void applyOn(ItemSkill* zSkill) = 0; }; class ItemSkill : public virtual Framework::ReferenceCounter { private: float xp; float maxXP; float level; int itemTypeId; public: ItemSkill(); virtual void load(Framework::StreamReader* zReader); virtual void save(Framework::StreamWriter* zWriter); virtual bool use(Entity* zActor, Item* zUsedItem, Block* zTarget) = 0; virtual bool use(Entity* zActor, Item* zUsedItem, Entity* zTarget) = 0; virtual bool interact(Entity* zActor, Item* zUsedItem, Block* zTarget); virtual bool interact(Entity* zActor, Item* zUsedItem, Entity* zTarget); void setXp(float xp); float getXp() const; void setMaxXp(float maxXp); float getMaxXp() const; void setLevel(float level); float getLevel() const; void setItemTypeId(int itemTypeId); int getItemTypeId() const; }; template class ItemSkillFactoryBase : public SubTypeFactory { public: ItemSkillFactoryBase() : SubTypeFactory() {}; virtual S* fromJson(Framework::JSON::JSONObject* zJson) const override { S* result = createValue(zJson); ItemSkill* zSkill = dynamic_cast(result); zSkill->setMaxXp( (float)zJson->zValue("maxXp")->asNumber()->getNumber()); zSkill->setLevel( (float)zJson->zValue("level")->asNumber()->getNumber()); return result; } virtual Framework::JSON::JSONObject* toJsonObject(S* zObject) const override { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); ItemSkill* zSkill = dynamic_cast(zObject); result->addValue( "maxXp", new Framework::JSON::JSONNumber(zSkill->getMaxXp())); result->addValue( "level", new Framework::JSON::JSONNumber(zSkill->getLevel())); return result; } virtual JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override { return builder->withRequiredNumber("maxXp") ->withDefault(10.f) ->finishNumber() ->withRequiredNumber("level") ->withDefault(1.f) ->finishNumber(); } protected: virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0; };