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.

Entity.h 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include <memory>
  3. #include <optional>
  4. #include "common.h"
  5. #include "SRF.h"
  6. #include "BoundingBox.h"
  7. #include "Body.h"
  8. namespace Swan {
  9. class World;
  10. class WorldPlane;
  11. class Game;
  12. class Entity {
  13. public:
  14. class Factory {
  15. public:
  16. virtual ~Factory() = default;
  17. virtual Entity *create(const Context &ctx, const SRF &params) = 0;
  18. std::string name_;
  19. };
  20. virtual ~Entity() = default;
  21. virtual std::optional<BoundingBox> getBounds() { return std::nullopt; }
  22. virtual void draw(const Context &ctx, Win &win) {}
  23. virtual void update(const Context &ctx, float dt) {}
  24. virtual void tick() {}
  25. virtual void moveTo(const Vec2 &pos) {}
  26. virtual void readSRF(const Swan::Context &ctx, const SRF &srf) {}
  27. virtual SRF *writeSRF(const Swan::Context &ctx) { return new SRFNone(); }
  28. };
  29. class PhysicsEntity: public Entity {
  30. public:
  31. PhysicsEntity(Vec2 size, double mass):
  32. body_(size, mass) {}
  33. virtual std::optional<BoundingBox> getBounds() { return body_.getBounds(); }
  34. virtual void update(const Context &ctx, float dt) override {
  35. body_.friction();
  36. body_.gravity();
  37. body_.update(dt);
  38. body_.collide(ctx.plane);
  39. }
  40. virtual void moveTo(const Vec2 &pos) override {
  41. body_.pos_ = pos;
  42. }
  43. protected:
  44. Body body_;
  45. };
  46. }