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.

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(const Context &ctx, float dt) {}
  25. virtual void move(const Vec2 &pos) {}
  26. virtual void moveTo(const Vec2 &pos) {}
  27. virtual void despawn() {}
  28. virtual void readSRF(const Swan::Context &ctx, const SRF &srf) {}
  29. virtual SRF *writeSRF(const Swan::Context &ctx) { return new SRFNone(); }
  30. };
  31. class PhysicsEntity: public Entity {
  32. public:
  33. PhysicsEntity(Vec2 size, float mass):
  34. body_(size, mass) {}
  35. virtual std::optional<BoundingBox> getBounds() { return body_.getBounds(); }
  36. virtual void update(const Context &ctx, float dt) override {
  37. body_.friction();
  38. body_.gravity();
  39. body_.update(ctx.plane, dt);
  40. }
  41. virtual void move(const Vec2 &rel) override { body_.pos_ += rel; }
  42. virtual void moveTo(const Vec2 &pos) override { body_.pos_ = pos; }
  43. protected:
  44. Body body_;
  45. };
  46. }