Guest User

Player.cpp

a guest
Dec 20th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. /*
  2.  * Player.cpp
  3.  *
  4.  *  Created on: Dec 15, 2017
  5.  *      Author: shinobu
  6.  */
  7.  
  8. #include "Player.h"
  9. #include "Graphics.h"
  10.  
  11. namespace player_constants {
  12.     const float WALK_SPEED = 0.2f;
  13. }
  14.  
  15. Player::Player() {
  16.     //TODO
  17. }
  18.  
  19. Player::Player(Graphics &graphics, float x, float y) : AnimatedSprite(graphics, globals::IMG_MAIN_CHAR, 0, 0, 16, 16, x, y, 100) {
  20.     graphics.loadImage(globals::IMG_MAIN_CHAR);
  21.  
  22.     this->dx = 0;
  23.     this->dy = 0;
  24.     this->facing = RIGHT;
  25.  
  26.     this->setupAnimation();
  27.     this->animate("FaceRight");
  28. }
  29.  
  30. void Player::setupAnimation() {
  31.     this->addAnimation(1, 0, 0, "FaceLeft", 16, 16, Vector2(0, 0));
  32.     this->addAnimation(1, 0, 16, "FaceRight", 16, 16, Vector2(0, 0));
  33.     this->addAnimation(3, 0, 0, "RunLeft", 16, 16, Vector2(0, 0));
  34.     this->addAnimation(3, 0, 16, "RunRight", 16, 16, Vector2(0, 0));
  35. }
  36.  
  37. void Player::animationDone(std::string currentAnimation) {
  38.     //TODO
  39. }
  40.  
  41. void Player::moveLeft() {
  42.     this->dx = -player_constants::WALK_SPEED;
  43.     this->animate("RunLeft");
  44.     this->facing = LEFT;
  45. }
  46.  
  47. void Player::moveRight() {
  48.     this->dx = player_constants::WALK_SPEED;
  49.     this->animate("RunRight");
  50.     this->facing = RIGHT;
  51. }
  52.  
  53. void Player::stopMoving() {
  54.     this->dx = 0.0f;
  55.  
  56.     //this->animate(this->facing == RIGHT ? "FaceRight" : "FaceLeft");
  57.     if (this->facing == RIGHT) {
  58.         this->animate("FaceRight");
  59.     }
  60.     else {
  61.         this->animate("FaceLeft");
  62.     }
  63. }
  64.  
  65. void Player::update(float elapsedTime) {
  66.     this->x += this->dx * elapsedTime;
  67.  
  68.     AnimatedSprite::update(elapsedTime);
  69. }
  70.  
  71. void Player::draw(Graphics &graphics) {
  72.     AnimatedSprite::draw(graphics, this->x, this->y);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment