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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include <memory>
  3. #include <optional>
  4. #include <msgpack.hpp>
  5. #include "common.h"
  6. #include "log.h"
  7. #include "traits/BodyTrait.h"
  8. namespace Swan {
  9. class World;
  10. class WorldPlane;
  11. class Game;
  12. class Entity: NonCopyable {
  13. public:
  14. using PackObject = std::unordered_map<std::string_view, msgpack::object>;
  15. struct Factory {
  16. const std::string name;
  17. std::unique_ptr<Entity> (*create)(const Context &ctx, const PackObject &obj);
  18. };
  19. Entity() = default;
  20. Entity(Entity &&) = default;
  21. Entity &operator=(Entity &&) = default;
  22. void despawn(const Swan::Context &ctx);
  23. virtual ~Entity() = default;
  24. virtual void draw(const Context &ctx, Win &win) {}
  25. virtual void update(const Context &ctx, float dt) {}
  26. virtual void tick(const Context &ctx, float dt) {}
  27. virtual void onDespawn(const Context &ctx) {}
  28. virtual void deserialize(const Swan::Context &ctx, const PackObject &obj) {}
  29. virtual PackObject serialize(const Swan::Context &ctx, msgpack::zone &zone) { return {}; }
  30. size_t index_;
  31. size_t generation_;
  32. };
  33. class PhysicsEntity: public Entity, public BodyTrait::HasBody {
  34. public:
  35. PhysicsEntity(Vec2 size, float mass):
  36. body_(size, mass) {}
  37. virtual BodyTrait::Body &getBody() override { return body_; }
  38. virtual void update(const Context &ctx, float dt) override {
  39. body_.standardForces();
  40. body_.update(ctx, dt);
  41. }
  42. protected:
  43. BodyTrait::PhysicsBody body_;
  44. };
  45. }