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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "PlayerEntity.h"
  2. #include <cmath>
  3. #include "ItemStackEntity.h"
  4. PlayerEntity::PlayerEntity(const Swan::Context &ctx, const Swan::SRF &params):
  5. PhysicsEntity(SIZE, MASS), inventory_(INVENTORY_SIZE),
  6. anims_{
  7. Swan::Animation(ctx.resources.getImage("core::player-still"), 0.8),
  8. Swan::Animation(ctx.resources.getImage("core::player-running"), 1, SDL_FLIP_HORIZONTAL),
  9. Swan::Animation(ctx.resources.getImage("core::player-running"), 1) } {
  10. readSRF(ctx, params);
  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.breakBlock(mouse_tile_);
  25. // Move left
  26. if (ctx.game.isKeyPressed(SDL_SCANCODE_A) || ctx.game.isKeyPressed(SDL_SCANCODE_LEFT)) {
  27. body_.force_ += Swan::Vec2(-MOVE_FORCE, 0);
  28. state_ = State::RUNNING_L;
  29. }
  30. // Move right
  31. if (ctx.game.isKeyPressed(SDL_SCANCODE_D) || ctx.game.isKeyPressed(SDL_SCANCODE_RIGHT)) {
  32. body_.force_ += Swan::Vec2(MOVE_FORCE, 0);
  33. if (state_ == State::RUNNING_L)
  34. state_ = State::IDLE;
  35. else
  36. state_ = State::RUNNING_R;
  37. }
  38. bool jump_pressed = ctx.game.isKeyPressed(SDL_SCANCODE_SPACE);
  39. // Jump
  40. if (body_.on_ground_ && jump_pressed && jump_timer_.periodic(0.5)) {
  41. body_.vel_.y = -JUMP_VEL;
  42. }
  43. // Fall down faster than we went up
  44. if (!body_.on_ground_ && (!jump_pressed || body_.vel_.y > 0))
  45. body_.force_ += Swan::Vec2(0, DOWN_FORCE);
  46. if (state_ != oldState)
  47. anims_[(int)state_].reset();
  48. anims_[(int)state_].tick(dt);
  49. PhysicsEntity::update(ctx, dt);
  50. }
  51. void PlayerEntity::tick(const Swan::Context &ctx, float dt) {
  52. for (ItemStackEntity *ent: ctx.plane.getEntsOfType<ItemStackEntity>()) {
  53. float squared_dist =
  54. (getBody().getBounds().bottomMid() - ent->getBody().getBounds().center())
  55. .squareLength();
  56. if (squared_dist < 0.5 * 0.5) {
  57. // TODO: Pick up
  58. }
  59. }
  60. }
  61. void PlayerEntity::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) {
  62. auto pos = dynamic_cast<const Swan::SRFFloatArray &>(srf);
  63. body_.pos_.set(pos.val[0], pos.val[1]);
  64. }
  65. Swan::SRF *PlayerEntity::writeSRF(const Swan::Context &ctx) {
  66. return new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y };
  67. }