-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
75 lines (71 loc) · 1.39 KB
/
Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "Player.h"
Player::Player(
sf::Vector2f pPosition,
sf::Vector2f pVelocity,
sf::Vector2i pSize,
sf::Texture *pTexture,
float pAngle,
float pAngularVelocity) :
AnimatedSprite(pPosition, pVelocity, pSize, pTexture, pAngle, pAngularVelocity)
{
isLeft = false;
isRight = false;
//add animations idle/walk/jump
mAnimations.insert(mAnimations.begin(), Animation(4, 0.2, true));
//start playing idle animation
AnimatedSprite::startAnimation();
}
Player::~Player(void)
{
}
//all this to just move left and right with acceleration
void Player::update()
{
//move right
if(isRight && !isLeft)
{
if(mVelocity.x >= mMaxSpeed)
{
mVelocity.x = mMaxSpeed;
}
else
{
mVelocity.x += mSpeed;
}
}
//move left
else if(isLeft && !isRight)
{
if(abs(mVelocity.x) >= abs(mMaxSpeed))
{
mVelocity.x = -mMaxSpeed;
}
else
{
mVelocity.x -= mSpeed;
}
}
//come to a stop
else
{
if (mVelocity.x > 0)
mVelocity.x -= mSpeed;
else if (mVelocity.x < 0)
mVelocity.x += mSpeed;
else
mVelocity.x = 0;
}
//check bounds with right and left side of screen
if(mPosition.x <= 0 + mSize.x/2)
{
mPosition.x = 1 + mSize.x/2;
mVelocity.x = -(mVelocity.x + mSpeed);
}
if(mPosition.x >= WindowWidth - 0 - mSize.x/2)
{
mPosition.x = WindowWidth - 1 - mSize.x/2;
mVelocity.x = -(mVelocity.x - mSpeed);
}
//call superclass update
AnimatedSprite::update();
}