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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include <memory>
  3. #include <optional>
  4. #include "common.h"
  5. #include "log.h"
  6. #include "traits/BodyTrait.h"
  7. #include "SRF.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 void draw(const Context &ctx, Win &win) {}
  22. virtual void update(const Context &ctx, float dt) {}
  23. virtual void tick(const Context &ctx, float dt) {}
  24. virtual void despawn() {}
  25. virtual void readSRF(const Swan::Context &ctx, const SRF &srf) {}
  26. virtual SRF *writeSRF(const Swan::Context &ctx) { return new SRFNone(); }
  27. };
  28. class PhysicsEntity: public Entity, public BodyTrait::HasBody {
  29. public:
  30. PhysicsEntity(Vec2 size, float mass):
  31. body_(size, mass) {}
  32. virtual BodyTrait::Body &getBody() override { return body_; }
  33. virtual void update(const Context &ctx, float dt) override {
  34. body_.standardForces();
  35. body_.update(ctx, dt);
  36. }
  37. protected:
  38. BodyTrait::PhysicsBody body_;
  39. };
  40. }