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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(body_.pos));
  20. }
  21. void PlayerEntity::update(const Swan::Context &ctx, float dt) {
  22. State oldState = state_;
  23. state_ = State::IDLE;
  24. mouseTile_ = ctx.game.getMouseTile();
  25. ctx.plane.debugBox(mouseTile_);
  26. jumpTimer_.tick(dt);
  27. placeTimer_.tick(dt);
  28. // Break block
  29. if (ctx.game.isMousePressed(SDL_BUTTON_LEFT))
  30. ctx.plane.breakTile(mouseTile_);
  31. // Place block
  32. if (ctx.game.isMousePressed(SDL_BUTTON_RIGHT) && placeTimer_.periodic(0.50)) {
  33. if (ctx.plane.getTileID(mouseTile_) == ctx.world.getTileID("@::air")) {
  34. ctx.plane.setTile(mouseTile_, "core::torch");
  35. }
  36. }
  37. // Move left
  38. if (ctx.game.isKeyPressed(SDL_SCANCODE_A) || ctx.game.isKeyPressed(SDL_SCANCODE_LEFT)) {
  39. physics_.force += Swan::Vec2(-MOVE_FORCE, 0);
  40. state_ = State::RUNNING_L;
  41. }
  42. // Move right
  43. if (ctx.game.isKeyPressed(SDL_SCANCODE_D) || ctx.game.isKeyPressed(SDL_SCANCODE_RIGHT)) {
  44. physics_.force += Swan::Vec2(MOVE_FORCE, 0);
  45. if (state_ == State::RUNNING_L)
  46. state_ = State::IDLE;
  47. else
  48. state_ = State::RUNNING_R;
  49. }
  50. bool jumpPressed = ctx.game.isKeyPressed(SDL_SCANCODE_SPACE);
  51. // Jump
  52. if (physics_.onGround && jumpPressed && jumpTimer_.periodic(0.5)) {
  53. physics_.vel.y = -JUMP_VEL;
  54. }
  55. // Fall down faster than we went up
  56. if (!physics_.onGround && (!jumpPressed || physics_.vel.y > 0))
  57. physics_.force += Swan::Vec2(0, DOWN_FORCE);
  58. if (state_ != oldState)
  59. anims_[(int)state_].reset();
  60. anims_[(int)state_].tick(dt);
  61. physics(ctx, dt, { .mass = MASS });
  62. // Do this after moving so that it's not behind
  63. Swan::Vec2 headPos = body_.topMid() + Swan::Vec2(0, 0.5);
  64. Swan::TilePos tilePos = Swan::Vec2i(floor(headPos.x), floor(headPos.y));
  65. if (!placedLight_) {
  66. ctx.plane.addLight(tilePos, LIGHT_LEVEL);
  67. placedLight_ = true;
  68. lightTile_ = tilePos;
  69. } else if (tilePos != lightTile_) {
  70. ctx.plane.removeLight(lightTile_, LIGHT_LEVEL);
  71. ctx.plane.addLight(tilePos, LIGHT_LEVEL);
  72. lightTile_ = tilePos;
  73. }
  74. }
  75. void PlayerEntity::tick(const Swan::Context &ctx, float dt) {
  76. for (ItemStackEntity *ent: ctx.plane.getEntsOfType<ItemStackEntity>()) {
  77. float squared_dist =
  78. (body_.bottomMid() - ent->get(Swan::BodyTrait::Tag{}).center())
  79. .squareLength();
  80. if (squared_dist < 0.5 * 0.5) {
  81. // TODO: Pick up
  82. }
  83. }
  84. }
  85. void PlayerEntity::deserialize(const Swan::Context &ctx, const PackObject &obj) {
  86. //body_.deserialize(obj["body"]);
  87. }
  88. Swan::Entity::PackObject PlayerEntity::serialize(const Swan::Context &ctx, msgpack::zone &zone) {
  89. return {};
  90. /*
  91. return Swan::MsgPackObject{
  92. { "body", body_.serialize(w) },
  93. };
  94. */
  95. }