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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. Inventory &get(InventoryTrait::Tag) 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. enum class State {
  22. IDLE,
  23. RUNNING_L,
  24. RUNNING_R,
  25. COUNT,
  26. };
  27. PlayerEntity(const Swan::Context &ctx):
  28. PhysicsEntity(SIZE),
  29. anims_{
  30. Swan::Animation(ctx.resources.getImage("core/entity/player-still"), 0.8),
  31. Swan::Animation(
  32. ctx.resources.getImage("core/entity/player-running"),
  33. 1, SDL_FLIP_HORIZONTAL),
  34. Swan::Animation(ctx.resources.getImage("core/entity/player-running"), 1)
  35. } {}
  36. State state_ = State::IDLE;
  37. std::array<Swan::Animation, (int)State::COUNT> anims_;
  38. Swan::Clock jump_timer_;
  39. Swan::TilePos mouse_tile_;
  40. BasicInventory inventory_{INVENTORY_SIZE};
  41. };