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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, Swan::Win &win) {
  13. body_.outline(win);
  14. anims_[(int)state_].draw(body_.pos - Swan::Vec2(0.2, 0.1), win);
  15. }
  16. void PlayerEntity::update(const Swan::Context &ctx, float dt) {
  17. State oldState = state_;
  18. state_ = State::IDLE;
  19. mouse_tile_ = ctx.game.getMouseTile();
  20. ctx.plane.debugBox(mouse_tile_);
  21. jump_timer_.tick(dt);
  22. // Break block
  23. if (ctx.game.isMousePressed(SDL_BUTTON_LEFT))
  24. ctx.plane.breakTile(mouse_tile_);
  25. // Place block
  26. if (ctx.game.isMousePressed(SDL_BUTTON_RIGHT)) {
  27. if (ctx.plane.getTileID(mouse_tile_) == ctx.world.getTileID("@::air")) {
  28. ctx.plane.setTile(mouse_tile_, "core::torch");
  29. }
  30. }
  31. // Move left
  32. if (ctx.game.isKeyPressed(SDL_SCANCODE_A) || ctx.game.isKeyPressed(SDL_SCANCODE_LEFT)) {
  33. physics_.force += Swan::Vec2(-MOVE_FORCE, 0);
  34. state_ = State::RUNNING_L;
  35. }
  36. // Move right
  37. if (ctx.game.isKeyPressed(SDL_SCANCODE_D) || ctx.game.isKeyPressed(SDL_SCANCODE_RIGHT)) {
  38. physics_.force += Swan::Vec2(MOVE_FORCE, 0);
  39. if (state_ == State::RUNNING_L)
  40. state_ = State::IDLE;
  41. else
  42. state_ = State::RUNNING_R;
  43. }
  44. bool jump_pressed = ctx.game.isKeyPressed(SDL_SCANCODE_SPACE);
  45. // Jump
  46. if (physics_.on_ground && jump_pressed && jump_timer_.periodic(0.5)) {
  47. physics_.vel.y = -JUMP_VEL;
  48. }
  49. // Fall down faster than we went up
  50. if (!physics_.on_ground && (!jump_pressed || physics_.vel.y > 0))
  51. physics_.force += Swan::Vec2(0, DOWN_FORCE);
  52. if (state_ != oldState)
  53. anims_[(int)state_].reset();
  54. anims_[(int)state_].tick(dt);
  55. physics(ctx, dt, { .mass = MASS });
  56. }
  57. void PlayerEntity::tick(const Swan::Context &ctx, float dt) {
  58. for (ItemStackEntity *ent: ctx.plane.getEntsOfType<ItemStackEntity>()) {
  59. float squared_dist =
  60. (body_.bottomMid() - ent->get(Swan::BodyTrait::Tag{}).center())
  61. .squareLength();
  62. if (squared_dist < 0.5 * 0.5) {
  63. // TODO: Pick up
  64. }
  65. }
  66. }
  67. void PlayerEntity::deserialize(const Swan::Context &ctx, const PackObject &obj) {
  68. //body_.deserialize(obj["body"]);
  69. }
  70. Swan::Entity::PackObject PlayerEntity::serialize(const Swan::Context &ctx, msgpack::zone &zone) {
  71. return {};
  72. /*
  73. return Swan::MsgPackObject{
  74. { "body", body_.serialize(w) },
  75. };
  76. */
  77. }