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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. virtual ~Entity() = default;
  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 despawn() {}
  26. virtual void deserialize(const Swan::Context &ctx, const PackObject &obj) {}
  27. virtual PackObject serialize(const Swan::Context &ctx, msgpack::zone &zone) { return {}; }
  28. };
  29. class PhysicsEntity: public Entity, public BodyTrait::HasBody {
  30. public:
  31. PhysicsEntity(Vec2 size, float mass):
  32. body_(size, mass) {}
  33. virtual BodyTrait::Body &getBody() override { return body_; }
  34. virtual void update(const Context &ctx, float dt) override {
  35. body_.standardForces();
  36. body_.update(ctx, dt);
  37. }
  38. protected:
  39. BodyTrait::PhysicsBody body_;
  40. };
  41. }