-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spritemanager.cpp
249 lines (215 loc) · 5.81 KB
/
Spritemanager.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "SpriteManager.h"
//#include "SDL2/SDL_timer.h"
int check = 0;
int lastPosition; // ADDED BY KALEB - last position of the srcRect
int lastSecond = 0; // ADDED BY KALEB - for timing animations
Uint32 startPunchTimer; // ADDED BY KALEB
Uint32 endPunchTimer; // ADDED BY KALEB
Uint32 startKickTimer;
Uint32 endKickTimer;
bool punchAction_flag = 0 ;
bool kickAction_flag = 0 ;
SpriteManager::SpriteManager(const char* texture,int x, int y)
{
objTexture = TextureManager::loadTexture(texture);
xpos = x;
ypos = y;
startPunchTimer = 0;
endPunchTimer = 300;
startKickTimer = 0;
endKickTimer = 300;
}
SpriteManager::~SpriteManager()
{
}
void SpriteManager::updateSprite(){
// srcRect.x = 1600;
srcRect.y = 0;
srcRect.w = MC_IMG_SRC;
srcRect.h = MC_IMG_SRC;
if(start == 0){
srcRect.x = 1600;
xpos = SpriteManager::getCharacterXpos() + STEPX;
destRect.x = xpos;
start = 1 ;
lastPosition = srcRect.x; // save lastPosition (don't stay on fight anim)
}
else{
if(flag_right == true){
if(rightcount == 0){
srcRect.x = 1600;
}
else{
srcRect.x = 2000;
}
xpos = SpriteManager::getCharacterXpos() + STEPX;
destRect.x = xpos;
flag_right =false;
lastPosition = srcRect.x; // save lastPosition (don't stay on fight anim)
}
else if(flag_left == true){
if(leftcount == 0){
srcRect.x = 1200;
std::cout << "SET TO 1200" << std::endl;
}
else{
srcRect.x = 800;
}
xpos = SpriteManager::getCharacterXpos() - STEPX;
destRect.x = xpos;
flag_left =false;
lastPosition = srcRect.x; // save lastPosition (don't stay on fight anim)
}
else if(flag_kick == true){
kickAction_flag = 1;
startKickTimer = SDL_GetTicks();
srcRect.x = MC_IMG_SRC*6;
flag_kick = false;
}
// ADDED BY KALEB
else if (flag_punch == true) {
//lastPosition = srcRect.x; // save lastPosition (don't stay on fight anim)
//
// lastSecond = SDL_GetTicks() / 500;
// gSpriteClips[ 0 ].x = 2800;
// gSpriteClips[ 0 ].y = 0;
// gSpriteClips[ 0 ].w = MC_IMG_SRC;
// gSpriteClips[ 0 ].h = MC_IMG_SRC;
//
// gSpriteClips[ 1 ].x = 2800;
// gSpriteClips[ 1 ].y = 0;
// gSpriteClips[ 1 ].w = MC_IMG_SRC;
// gSpriteClips[ 1 ].h = MC_IMG_SRC;
//
// gSpriteClips[ 2 ].x = 2800;
// gSpriteClips[ 2 ].y = 0;
// gSpriteClips[ 2 ].w = MC_IMG_SRC;
// gSpriteClips[ 2 ].h = MC_IMG_SRC;
//
// gSpriteClips[ 3 ].x = 2800;
// gSpriteClips[ 3 ].y = 0;
// gSpriteClips[ 3 ].w = MC_IMG_SRC;
// gSpriteClips[ 3 ].h = MC_IMG_SRC;
punchAction_flag = 1;
startPunchTimer = SDL_GetTicks();
srcRect.x = 2800;
flag_punch = false;
//std::cout << "Flag punch set false" << std::endl;
}
else if (flag_hit == true) {
srcRect.x = MC_IMG_SRC*8;
flag_hit = false;
}
}
// else{
// srcRect.x = 1000;
// xpos = SpriteManager::getCharacterXpos();
// destRect.x = xpos;
// }
destRect.y = 320;
}
// ADDED BY KALEB
// Updates enemy sprite based on flag given and step
// step is the x-direction movement
// flag table:
// 0 - calm
// 1 - moving
// 2 - injured
// 3 - dead
// 4 - block
// 5 - punch
void SpriteManager::updateEnemySprite(int flag, int step) {
srcRect.y = 0;
srcRect.w = MC_IMG_SRC;
srcRect.h = MC_IMG_SRC;
// if calm
if (flag == 0) {
srcRect.y = 0;
srcRect.x = 1600;
xpos = SpriteManager::getCharacterXpos();
//lastPosition = srcRect.x;
}
// if moving
if (flag == 1) {
srcRect.y = 0;
// if step < 0, move left
if (step <= 0) {
srcRect.x = 1200;
}
// else moving right
else {
srcRect.x = 1600;
}
xpos = SpriteManager::getCharacterXpos() + step;
//lastPosition = srcRect.x;
}
// if hit
// show red
if (flag == 2) {
//std::cout << "HIT!" << std::endl;
//lastPosition = srcRect.x;
srcRect.y = srcRect.h*2;
srcRect.x = srcRect.w*3;
}
// if dead
if (flag == 3) {
srcRect.y = srcRect.h*2;
srcRect.x = 0;
}
// if block
if (flag == 4) {
srcRect.y = 0;
srcRect.x = srcRect.w*2;
}
// if punch
if (flag == 5) {
srcRect.y = 0;
srcRect.x = 0;
}
destRect.x = xpos;
destRect.y = 320;
}
int SpriteManager::getCharacterXpos()
{
return xpos;
}
int SpriteManager::getCharacterYpos()
{
return ypos;
}
void SpriteManager::renderSprite(SDL_Rect& camera )
{
//edge collision prevention
if(destRect.x>LEVEL_WIDTH-150){
destRect.x = LEVEL_WIDTH-150;
}
//when the punch action flag is activated the below code captures the current time and renders the punch texture
//for 2 seconds
/*if(punchAction_flag ==1){
int max = SDL_GetTicks()+2000;
for(int i = SDL_GetTicks(); i < max; i++){
std::cout << max << " " << i << std::endl;
TextureManager::renderTexture(999, destRect.x-camera.x, destRect.y ,objTexture, &srcRect);
}
punchAction_flag=0;
}
else{
TextureManager::renderTexture(999, destRect.x-camera.x, destRect.y ,objTexture, &srcRect);
}*/
/*if (punchAction_flag == 1) {
endPunchTimer = SDL_GetTicks();
if (endPunchTimer >= startPunchTimer + 300) {
TextureManager::renderTexture(999, destRect.x-camera.x, destRect.y ,objTexture, &srcRect);
srcRect.x = lastPosition;
}
} */
TextureManager::renderTexture(999, destRect.x-camera.x, destRect.y ,objTexture, &srcRect);
//std::cout << destRect.x-camera.x << ", " << destRect.y << std::endl;
// ADDED BY KALEB
// a poor workaround for timing of animations
int ticks = SDL_GetTicks() / 500;
if ((ticks) != lastSecond) {
srcRect.x = lastPosition;
lastSecond = ticks;
}
}