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.

EntPlayer.cc 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "EntPlayer.h"
  2. EntPlayer::EntPlayer(Swan::World &world, const Swan::Vec2 &pos):
  3. body_(pos, SIZE, MASS) {
  4. anims_[(int)State::IDLE].init(32, 64, 0.8,
  5. world.getAsset("core::player-still"));
  6. anims_[(int)State::RUNNING_R].init(32, 64, 1,
  7. world.getAsset("core::player-running"));
  8. anims_[(int)State::RUNNING_L].init(32, 64, 1,
  9. world.getAsset("core::player-running"), (int)Swan::Animation::Flags::HFLIP);
  10. }
  11. void EntPlayer::draw(Swan::Win &win) {
  12. body_.outline(win);
  13. win.setPos(body_.pos_ - Swan::Vec2(0.2, 0.1));
  14. anims_[(int)state_].draw(win);
  15. }
  16. void EntPlayer::update(Swan::WorldPlane &plane, float dt) {
  17. State oldState = state_;
  18. state_ = State::IDLE;
  19. if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
  20. body_.force_ += Swan::Vec2(-FORCE, 0);
  21. state_ = State::RUNNING_L;
  22. }
  23. if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
  24. body_.force_ += Swan::Vec2(FORCE, 0);
  25. if (state_ == State::RUNNING_L)
  26. state_ = State::IDLE;
  27. else
  28. state_ = State::RUNNING_R;
  29. }
  30. if (body_.on_ground_ && sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
  31. body_.vel_.y_ = -JUMP_FORCE;
  32. }
  33. if (state_ != oldState)
  34. anims_[(int)state_].reset();
  35. anims_[(int)state_].tick(dt);
  36. body_.friction(FRICTION);
  37. body_.gravity();
  38. body_.update(dt);
  39. body_.collide(plane);
  40. }