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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, Cygnet::Renderer &rnd) 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. static constexpr float LIGHT_LEVEL = 0.2;
  23. enum class State {
  24. IDLE,
  25. RUNNING_L,
  26. RUNNING_R,
  27. COUNT,
  28. };
  29. PlayerEntity(const Swan::Context &ctx):
  30. PhysicsEntity(SIZE),
  31. anims_{
  32. Swan::Animation(ctx.world.getSprite("core::entity/player-still"), 0.8),
  33. Swan::Animation(ctx.world.getSprite("core::entity/player-running"), 1),
  34. Swan::Animation(ctx.world.getSprite("core::entity/player-running"), 1),
  35. } {}
  36. State state_ = State::IDLE;
  37. std::array<Swan::Animation, (int)State::COUNT> anims_;
  38. Swan::Clock jumpTimer_;
  39. Swan::Clock placeTimer_;
  40. Swan::TilePos mouseTile_;
  41. Swan::TilePos lightTile_;
  42. bool placedLight_ = false;
  43. BasicInventory inventory_{INVENTORY_SIZE};
  44. };