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

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