-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacter.cpp
159 lines (113 loc) · 3.42 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "Character.h"
Character::Character() {
mPosX = (400 - CHARACTER_WIDTH) / 2;
mPosY = GlobalResource::SCREEN_HEIGHT - CHARACTER_HEIGHT;
mVelX = 0;
mVelY = 0;
hp = 3;
}
void Character::loadFromFile(SDL_Renderer *renderer, string path) {
characterTexture.loadFromFile(renderer, path);
bulletTexture.loadFromFile(renderer, BULLET_PATH);
}
void Character::handleEvent(SDL_Event& e) {
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
if (currentKeyStates[SDL_SCANCODE_UP] && !currentKeyStates[SDL_SCANCODE_LSHIFT] ) {
mVelY = -MAX_CHARACTER_VEL;
}
else if (currentKeyStates[SDL_SCANCODE_DOWN] && !currentKeyStates[SDL_SCANCODE_LSHIFT]) {
mVelY = MAX_CHARACTER_VEL;
}
else if (currentKeyStates[SDL_SCANCODE_UP] && currentKeyStates[SDL_SCANCODE_LSHIFT]) {
mVelY = -MIN_CHARACTER_VEL;
}
else if (currentKeyStates[SDL_SCANCODE_DOWN] && currentKeyStates[SDL_SCANCODE_LSHIFT]) {
mVelY = MIN_CHARACTER_VEL;
}
if (currentKeyStates[SDL_SCANCODE_LEFT] && !currentKeyStates[SDL_SCANCODE_LSHIFT]) {
mVelX = -MAX_CHARACTER_VEL;
currentClip.x = 44;
}
else if (currentKeyStates[SDL_SCANCODE_RIGHT] && !currentKeyStates[SDL_SCANCODE_LSHIFT]) {
mVelX = MAX_CHARACTER_VEL;
currentClip.x = 85;
}
else if (currentKeyStates[SDL_SCANCODE_LEFT] && currentKeyStates[SDL_SCANCODE_LSHIFT]) {
mVelX = -MIN_CHARACTER_VEL;
currentClip.x = 44;
}
else if (currentKeyStates[SDL_SCANCODE_RIGHT] && currentKeyStates[SDL_SCANCODE_LSHIFT]) {
mVelX = MIN_CHARACTER_VEL;
currentClip.x = 85;
}
if (!currentKeyStates[SDL_SCANCODE_UP]&& !currentKeyStates[SDL_SCANCODE_DOWN]){
mVelY = 0;
}
if (!currentKeyStates[SDL_SCANCODE_LEFT] && !currentKeyStates[SDL_SCANCODE_RIGHT]) {
mVelX = 0;
currentClip.x = 0;
}
if (currentKeyStates[SDL_SCANCODE_UP] && currentKeyStates[SDL_SCANCODE_DOWN]) {
mVelY = 0;
currentClip.x = 0;
}
if (currentKeyStates[SDL_SCANCODE_LEFT] && currentKeyStates[SDL_SCANCODE_RIGHT]) {
mVelX = 0;
currentClip.x = 0;
}
}
void Character::move(double timeStep) {
mPosX += mVelX*timeStep;
mPosY += mVelY*timeStep;
if (mPosX < 0) {
mPosX = 0;
}
else if (mPosX > GlobalResource::MAIN_AREA_WIDTH - CHARACTER_WIDTH) {
mPosX = GlobalResource::MAIN_AREA_WIDTH - CHARACTER_WIDTH;
}
if (mPosY < 0) {
mPosY = 0;
}
else if (mPosY > GlobalResource::MAIN_AREA_HEIGHT - CHARACTER_HEIGHT) {
mPosY = GlobalResource::MAIN_AREA_HEIGHT - CHARACTER_HEIGHT;
}
}
//Shows the dot on the screen
void Character::render(SDL_Renderer *renderer) {
characterTexture.render(renderer, mPosX, mPosY, ¤tClip);
for (Bullet bullet : bullets) {
bullet.render(renderer, bullet.getAngle());
}
}
void Character::free() {
characterTexture.free();
}
void Character::shoot() {
}
void Character::moveBullets(double timeStep) {
for (int i = 0; i < bullets.size(); i++ ) {
bullets[i].setY(bullets[i].getY() - GlobalResource::BULLET_VEL* (double)timeStep);
}
}
void Character::createBullet(EnemyBullet &bullets, double angle) {
bullets.createBullet(mPosX+ 14, 0, mPosY-5, 0, GlobalResource::BULLET_VEL, angle);
}
double Character::getX() {
return mPosX;
}
double Character::getY() {
return mPosY;
}
vector<Bullet> Character::getBullets() {
return bullets;
}
void Character::setDefaultPlace() {
mPosX = (400 - CHARACTER_WIDTH) / 2;
mPosY = GlobalResource::SCREEN_HEIGHT - CHARACTER_HEIGHT;
}
int Character::getHp() {
return this->hp;
}
void Character::setHp(int hp) {
this->hp = hp;
}