12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #pragma once
- #include <JSON.h>
- #include <Reader.h>
- #include <ReferenceCounter.h>
- #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<typename S> class ItemSkillFactoryBase
- : public SubTypeFactory<ItemSkill, S>
- {
- public:
- ItemSkillFactoryBase()
- : SubTypeFactory<ItemSkill, S>(){};
- virtual void fromJson(
- S* zResult, Framework::JSON::JSONObject* zJson) const override
- {
- ItemSkill* zSkill = dynamic_cast<ItemSkill*>(zResult);
- zSkill->setMaxXp(
- (float)zJson->zValue("maxXp")->asNumber()->getNumber());
- zSkill->setLevel(
- (float)zJson->zValue("level")->asNumber()->getNumber());
- }
- virtual void toJson(
- S* zObject, Framework::JSON::JSONObject* zResult) const override
- {
- ItemSkill* zSkill = dynamic_cast<ItemSkill*>(zObject);
- zResult->addValue(
- "maxXp", new Framework::JSON::JSONNumber(zSkill->getMaxXp()));
- zResult->addValue(
- "level", new Framework::JSON::JSONNumber(zSkill->getLevel()));
- }
- virtual JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override
- {
- return builder->withRequiredNumber("maxXp")
- ->withDefault(10.f)
- ->finishNumber()
- ->withRequiredNumber("level")
- ->withDefault(1.f)
- ->finishNumber();
- }
- };
|