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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. 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.resources.getImage("core/entity/player-still"), 0.8),
  33. Swan::Animation(
  34. ctx.resources.getImage("core/entity/player-running"),
  35. 1, SDL_FLIP_HORIZONTAL),
  36. Swan::Animation(ctx.resources.getImage("core/entity/player-running"), 1)
  37. } {}
  38. State state_ = State::IDLE;
  39. std::array<Swan::Animation, (int)State::COUNT> anims_;
  40. Swan::Clock jumpTimer_;
  41. Swan::Clock placeTimer_;
  42. Swan::TilePos mouseTile_;
  43. Swan::TilePos lightTile_;
  44. bool placedLight_ = false;
  45. BasicInventory inventory_{INVENTORY_SIZE};
  46. };