-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacter.cpp
85 lines (66 loc) · 1.48 KB
/
Character.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
76
77
78
79
80
81
82
83
84
85
#include "Character.h"
#include "Texture2D.h"
Character::Character(SDL_Renderer* renderer, std::string imagePath, Vector2D startPosition,FACING mfacingDirection)
{
mRenderer = renderer;
startPosition = mPosition;
mMovingLeft = false;
mMovingRight = false;
mTexture = new Texture2D(renderer);
if (!mTexture->LoadFromFile(imagePath))
{
std::cout << "Character not loaded!" << std::endl;
}
setPosition(mPosition);
}
void Character::MoveLeft(float deltaTime) {
mfacingDirection = FACING_LEFT;
}
void Character::MoveRight(float deltaTime) {
mfacingDirection = FACING_RIGHT;
}
void Character::Render() {
if (mfacingDirection == FACING_RIGHT) {
mTexture->Render(Vector2D(), SDL_FLIP_NONE);
}
if (mfacingDirection == FACING_LEFT) {
mTexture->Render(Vector2D(), SDL_FLIP_HORIZONTAL);
}
}
void Character::setPosition(Vector2D newPosition)
{
newPosition = mPosition;
}
Vector2D Character::GetPosition()
{
return mPosition;
}
void Character::Update(float deltaTime, SDL_Event e) {
if (mMovingLeft) {
MoveLeft(deltaTime);
}
else if (mMovingRight) {
MoveLeft(deltaTime);
}
switch (e.type) {
case SDL_KEYUP:
switch (e.key.keysym.sym) {
case SDLK_a:
mPosition.x -= deltaTime;
break;
case SDLK_d:
mPosition.x += deltaTime;
default:
break;
}
break;
default:
break;
}
}
Character::~Character() {
delete mRenderer;
mRenderer = NULL;
delete mTexture;
mTexture = NULL;
}