A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Item.h 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include <string>
  3. #include "Resource.h"
  4. namespace Swan {
  5. class Item {
  6. public:
  7. struct Builder {
  8. std::string name;
  9. std::string image;
  10. int maxStack = 64;
  11. };
  12. Item(const ResourceManager &resources, const Builder &builder):
  13. name(builder.name), image(resources.getImage(builder.image)),
  14. maxStack(builder.maxStack) {}
  15. const std::string name;
  16. const ImageResource &image;
  17. const int maxStack;
  18. static std::unique_ptr<Item> createInvalid(Context &ctx);
  19. // For testing, we want to be able to create Items without an actual ImageResource.
  20. // Tests can create a MockItem class which inherits from Item and uses this ctor,
  21. // as long as the test never does anything which tries to follow the image_ member.
  22. // Eventually, this should become unnecessary, because we don't need to require
  23. // a complete ImageResource for a headless server, but for now, this will suffice.
  24. protected:
  25. Item(const Builder &builder):
  26. name(builder.name), image(*(ImageResource *)this),
  27. maxStack(builder.maxStack) {}
  28. };
  29. }