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.

PlayerEntity.h 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include <swan/swan.h>
  3. #include <array>
  4. class PlayerEntity: public Swan::PhysicsEntity, public Swan::InventoryTrait {
  5. public:
  6. PlayerEntity(const Swan::Context &ctx, Swan::Vec2 pos);
  7. PlayerEntity(const Swan::Context &ctx, const PackObject &obj);
  8. using PhysicsEntity::get;
  9. Inventory &get(InventoryTrait::Tag) override { return inventory_; }
  10. void draw(const Swan::Context &ctx, Swan::Win &win) override;
  11. void update(const Swan::Context &ctx, float dt) override;
  12. void tick(const Swan::Context &ctx, float dt) override;
  13. void deserialize(const Swan::Context &ctx, const PackObject &obj) override;
  14. PackObject serialize(const Swan::Context &ctx, msgpack::zone &zone) override;
  15. private:
  16. static constexpr int INVENTORY_SIZE = 18;
  17. static constexpr float MASS = 80;
  18. static constexpr float MOVE_FORCE = 34 * MASS;
  19. static constexpr float JUMP_VEL = 11;
  20. static constexpr float DOWN_FORCE = 20 * MASS;
  21. static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.6, 1.9);
  22. enum class State {
  23. IDLE,
  24. RUNNING_L,
  25. RUNNING_R,
  26. COUNT,
  27. };
  28. PlayerEntity(const Swan::Context &ctx):
  29. PhysicsEntity(SIZE),
  30. anims_{
  31. Swan::Animation(ctx.resources.getImage("core/entity/player-still"), 0.8),
  32. Swan::Animation(
  33. ctx.resources.getImage("core/entity/player-running"),
  34. 1, SDL_FLIP_HORIZONTAL),
  35. Swan::Animation(ctx.resources.getImage("core/entity/player-running"), 1)
  36. } {}
  37. State state_ = State::IDLE;
  38. std::array<Swan::Animation, (int)State::COUNT> anims_;
  39. Swan::Clock jump_timer_;
  40. Swan::TilePos mouse_tile_;
  41. BasicInventory inventory_{INVENTORY_SIZE};
  42. };