-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
80 lines (67 loc) · 1.39 KB
/
Player.cpp
File metadata and controls
80 lines (67 loc) · 1.39 KB
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
76
77
78
79
80
#include "Player.h"
Player::Player(){
frameHeight = FRAME_HEIGHT;
frameOffset = 0;
frameWidth = FRAME_WIDTH;
distBetweenFrames = DIST_BETWEEN_FRAMES;
frameStartX = FRAME_START_X;
yVel = 0;
xVel = 0;
xLoc = 0;
yLoc = 0;
midX = 0;
xAcc = 0;
yAcc = GRAVITY;
}
void Player::setup(){
SDL_Texture* ishmael = graphics->getTextureFromPath("Assets/Sprites/ish.png");
setState(STANDING);
setFlipped(NO);
setGrounded(YES);
setTexture(ishmael);
//Location will be set based on the area the player is in.
setLocation(30,30);
setWidth(PLAYER_WIDTH);
setHeight(PLAYER_HEIGHT);
}
void Player::update(){
xVel += xAcc;
yVel += yAcc;
if(yVel >= TERMINAL_VEL)
yVel = TERMINAL_VEL;
if(isGrounded()){
yVel = 0;
}
xLoc += xVel;
yLoc += yVel;
midX = xLoc + width/2;
}
void Player::jump(){
state = JUMPING;
yVel = JUMP_VEL;
}
void Player::setState(int s){
Sprite::setState(s);
switch(s){
case STANDING:
numFrames = STAND_NUM_FRAMES;
frameStartY = STAND_START_Y;
break;
case RUNNING:
numFrames = RUN_NUM_FRAMES;
frameStartY = RUN_START_Y;
break;
}
}
void Player::updateFrame(){
//Offset used to delay animation
frameOffset++;
if(frameOffset %FRAME_OFFSET == 0){
currentFrame++;
if(currentFrame >= numFrames)
currentFrame = 0;
setSource(frameStartX + currentFrame * distBetweenFrames,frameStartY, frameWidth,frameHeight);
}
if(frameOffset>10)
frameOffset = 0;
}