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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "PlayerEntity.h"
  2. #include <cmath>
  3. #include "ItemStackEntity.h"
  4. PlayerEntity::PlayerEntity(const Swan::Context &ctx, Swan::Vec2 pos):
  5. PlayerEntity(ctx) {
  6. body_.pos = pos;
  7. }
  8. PlayerEntity::PlayerEntity(const Swan::Context &ctx, const PackObject &obj):
  9. PlayerEntity(ctx) {
  10. deserialize(ctx, obj);
  11. }
  12. void PlayerEntity::draw(const Swan::Context &ctx, Cygnet::Renderer &rnd) {
  13. Cygnet::Mat3gf mat;
  14. // Currently, there is no sprite for running left.
  15. // Running left is just running right but flipped.
  16. if (state_ == State::RUNNING_L) {
  17. mat.translate({-0.5, 0}).scale({-1, 1}).translate({0.5, 0});
  18. }
  19. anims_[(int)state_].draw(rnd, mat.translate(
  20. body_.pos - Swan::Vec2{0.2, 0.1}));
  21. rnd.drawRect(mouseTile_, {1, 1});
  22. rnd.drawRect(body_.pos, body_.size);
  23. }
  24. void PlayerEntity::update(const Swan::Context &ctx, float dt) {
  25. State oldState = state_;
  26. state_ = State::IDLE;
  27. mouseTile_ = ctx.game.getMouseTile();
  28. ctx.plane.debugBox(mouseTile_);
  29. jumpTimer_.tick(dt);
  30. placeTimer_.tick(dt);
  31. // Break block
  32. if (ctx.game.isMousePressed(GLFW_MOUSE_BUTTON_LEFT))
  33. ctx.plane.breakTile(mouseTile_);
  34. // Place block
  35. if (ctx.game.isMousePressed(GLFW_MOUSE_BUTTON_RIGHT) && placeTimer_.periodic(0.50)) {
  36. if (ctx.plane.getTileID(mouseTile_) == ctx.world.getTileID("@::air")) {
  37. ctx.plane.setTile(mouseTile_, "core::torch");
  38. }
  39. }
  40. // Move left
  41. if (ctx.game.isKeyPressed(GLFW_KEY_A) || ctx.game.isKeyPressed(GLFW_KEY_LEFT)) {
  42. physics_.force += Swan::Vec2(-MOVE_FORCE, 0);
  43. state_ = State::RUNNING_L;
  44. }
  45. // Move right
  46. if (ctx.game.isKeyPressed(GLFW_KEY_D) || ctx.game.isKeyPressed(GLFW_KEY_RIGHT)) {
  47. physics_.force += Swan::Vec2(MOVE_FORCE, 0);
  48. if (state_ == State::RUNNING_L)
  49. state_ = State::IDLE;
  50. else
  51. state_ = State::RUNNING_R;
  52. }
  53. bool jumpPressed = ctx.game.isKeyPressed(GLFW_KEY_SPACE);
  54. // Jump
  55. if (physics_.onGround && jumpPressed && jumpTimer_.periodic(0.5)) {
  56. physics_.vel.y = -JUMP_VEL;
  57. }
  58. // Fall down faster than we went up
  59. if (!physics_.onGround && (!jumpPressed || physics_.vel.y > 0))
  60. physics_.force += Swan::Vec2(0, DOWN_FORCE);
  61. if (state_ != oldState)
  62. anims_[(int)state_].reset();
  63. anims_[(int)state_].tick(dt);
  64. physics(ctx, dt, { .mass = MASS });
  65. // Do this after moving so that it's not behind
  66. Swan::Vec2 headPos = body_.topMid() + Swan::Vec2(0, 0.5);
  67. Swan::TilePos tilePos = Swan::Vec2i(floor(headPos.x), floor(headPos.y));
  68. if (!placedLight_) {
  69. ctx.plane.addLight(tilePos, LIGHT_LEVEL);
  70. placedLight_ = true;
  71. lightTile_ = tilePos;
  72. } else if (tilePos != lightTile_) {
  73. ctx.plane.removeLight(lightTile_, LIGHT_LEVEL);
  74. ctx.plane.addLight(tilePos, LIGHT_LEVEL);
  75. lightTile_ = tilePos;
  76. }
  77. }
  78. void PlayerEntity::tick(const Swan::Context &ctx, float dt) {
  79. for (ItemStackEntity *ent: ctx.plane.getEntsOfType<ItemStackEntity>()) {
  80. float squared_dist =
  81. (body_.bottomMid() - ent->get(Swan::BodyTrait::Tag{}).center())
  82. .squareLength();
  83. if (squared_dist < 0.5 * 0.5) {
  84. // TODO: Pick up
  85. }
  86. }
  87. }
  88. void PlayerEntity::deserialize(const Swan::Context &ctx, const PackObject &obj) {
  89. //body_.deserialize(obj["body"]);
  90. }
  91. Swan::Entity::PackObject PlayerEntity::serialize(const Swan::Context &ctx, msgpack::zone &zone) {
  92. return {};
  93. /*
  94. return Swan::MsgPackObject{
  95. { "body", body_.serialize(w) },
  96. };
  97. */
  98. }