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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
#include "Player.h"
int Player::Init(SDL_Surface* graphic, int x, int y, int vel, int health, int maxhealth)
{
int _x = 0;
int _y = 0;
X = x;
Y = y;
Health = health;
MaxHealth = maxhealth;
Box.x = X - 5;
Box.y = Y + 29;
Box.w = 23;
Box.h = 21;
DownAnim = Res.player_dwn;
LeftAnim = Res.player_lft;
RightAnim = Res.player_rgt;
UpAnim = Res.player_up;
// Start to load the clips from the images
for(int c = 0; c < 4; c++)
{
DClips[c].x = _x;
DClips[c].y = _y;
DClips[c].w = PLYRG_W;
DClips[c].h = PLYRG_H;
_x += PLYRG_W;
}
_x = 0;
_y = PLYRG_H;
for(int c = 0; c < 4; c++)
{
LClips[c].x = _x;
LClips[c].y = _y;
LClips[c].w = PLYRG_W;
LClips[c].h = PLYRG_H;
_x += PLYRG_W;
}
_x = 0;
_y += PLYRG_H;
for(int c = 0; c < 4; c++)
{
RClips[c].x = _x;
RClips[c].y = _y;
RClips[c].w = PLYRG_W;
RClips[c].h = PLYRG_H;
_x += PLYRG_W;
}
_x = 0;
_y += PLYRG_H;
for(int c = 0; c < 4; c++)
{
UClips[c].x = _x;
UClips[c].y = _y;
UClips[c].w = PLYRG_W;
UClips[c].h = PLYRG_H;
_x += PLYRG_W;
}
// End clip loading
// Attempt to load the graphic
if(graphic != NULL) {Graphic = graphic;}
else {std::cout << "Player graphic is equal to NULL\n"; return 1;}
// Now set postions to avoid
// initialization errors.
frame = 0; // - Set the frame to the first
Position = DOWN; // - Set the player position to
// face down
delay = 0; // - Set the frame delay to zero
delaychange = 6; // - Set the amount of cycles
// until the frame changes to 6
moving = false; // - Tell the program the player isn't moving
action = false; // - Tell the program the player isn't doin an action
EnergyRegenT = 0;
HealthRegenT = 0;
return 0;
}
void Player::SetCamera(Map &map)
{
// Set the camera position then check if it goes past map boundries
monitor.setCameraPos(((X + Box.w/2) - monitor.getCamera().w / 2), ((Y + Box.h/2) - monitor.getCamera().w/2));
// If the camera is in the end of the left side of the map or
// the camera is in the end of the right side of the map prevent it from moving
if(monitor.getCamera().x < 0) {monitor.setCameraPos(0, monitor.getCamera().y);}
if(monitor.getCamera().x + monitor.getCamera().w > map.getMapW()) {monitor.setCameraPos(map.getMapW() - monitor.getCamera().w, monitor.getCamera().y);}
// If the camera is at the top or bottom of the map prevent it from moving
if(monitor.getCamera().y < 0) {monitor.setCameraPos(monitor.getCamera().x, 0);}
if(monitor.getCamera().y + monitor.getCamera().h > map.getMapH()) {monitor.setCameraPos(monitor.getCamera().x, map.getMapH() - monitor.getCamera().h);}
}
void Player::Input(SDL_Event event)
{
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
Xvel -= 5;
if(Health != 0){Position = LEFT;}
moving = true;
break;
case SDLK_RIGHT:
Xvel = 5;
if(Health != 0){Position = RIGHT;}
moving = true;
break;
case SDLK_UP:
Yvel -= 5;
if(Health != 0){Position = UP;}
moving = true;
break;
case SDLK_DOWN:
Yvel = 5;
if(Health != 0){Position = DOWN;}
moving = true;
break;
case SDLK_0:
Health --;
break;
case SDLK_MINUS:
Energy --;
break;
case SDLK_1:
Health = MaxHealth;
Energy = MaxEnergy;
break;
case SDLK_s:
Health = 0;
Energy = 0;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
if(Xvel < 0) {Xvel = 0;}
frame = 0;
if(Yvel == 0){moving = false;}
break;
case SDLK_RIGHT:
if(Xvel > 0) {Xvel = 0;}
frame = 0;
if(Yvel == 0){moving = false;}
break;
case SDLK_UP:
if(Yvel < 0) {Yvel = 0;}
frame = 0;
if(Xvel == 0){moving = false;}
break;
case SDLK_DOWN:
if(Yvel > 0) {Yvel = 0;}
frame = 0;
if(Xvel == 0){moving = false;}
break;
}
break;
}
}
void Player::Update(Map &map)
{
SDL_Color Black = {0, 0, 0, 0};
std::string FRAME;
if(Health < 0) {Health = 0;}
if(Energy < 0) {Energy = 0;}
DownAnim.UpdateAnim();
RightAnim.UpdateAnim();
LeftAnim.UpdateAnim();
UpAnim.UpdateAnim();
FRAME = Int2String::int2string(RightAnim.getFrame());
showframe = TTF_RenderText_Solid(Res.stdFONT, FRAME.c_str(), Black);
if(Health > 0)
{
// Handle Energy and Health regeneration
EnergyRegenT++;
if(EnergyRegenT > 175)
{
Energy += 1;
EnergyRegenT = 0;
}
if(Energy > MaxEnergy) {Energy = MaxEnergy;}
HealthRegenT++;
if(HealthRegenT > 250)
{
Health += 1;
HealthRegenT = 0;
}
if(Health > MaxHealth) {Health = MaxHealth;}
// End of Energy and Health regeneration
// If the player is moving, add one value to the delay
if(moving == true)
{
delay++;
}
else
{
// If he/she isn't moving, make sure the player
// doesn't get animated
delay = 0;
}
// If the delay reaches the point where it
// needs to add a frame, go to the next frame
// and reset the delay
if(delay == delaychange)
{
frame++;
delay = 0;
}
// If the frame is larger than the frames in the
// image, reset the frame to 0
if(frame > 3)
{
frame = 0;
}
// Make copies of the X and Y values
// incase we collide with something, so
// we can revert the position
int prevx = X;
int prevy = Y;
// Add the velocity to the
// proper coordinates
X += Xvel;
Y += Yvel;
// Update the collision box position
Box.x = X + 5;
Box.y = Y + 24;
// If there is a X collision, revert the X coordinate
if((Box.x < 0) || (Box.x + Box.w > map.getMapW()) || map.TouchSolid(Box) == true)
{
//std::cout << "Collision Detected.\n";
X = prevx;
}
// If there is a Y collision, revert the Y coordinate
if((Box.y < 0) || (Box.y + Box.h > map.getMapH()) || map.TouchSolid(Box) == true)
{
//std::cout << "Collision Detected.\n";
Y = prevy;
}
SetCamera(map);
}
}
void Player::Render(SDL_Surface* Dest)
{
// If the player isn't dead
if(Health != 0)
{
switch(Position)
{
case LEFT:
//Draw::DrawGFX(Graphic, LClips[frame], Dest, X - monitor.getCamera().x, Y - monitor.getCamera().y);
LeftAnim.RenderObj(Dest);
break;
case RIGHT:
//Draw::DrawGFX(Graphic, RClips[frame], Dest, X - monitor.getCamera().x, Y - monitor.getCamera().y);
RightAnim.RenderObj(Dest);
break;
case UP:
Draw::DrawGFX(Graphic, UClips[frame], Dest, X - monitor.getCamera().x, Y - monitor.getCamera().y);
break;
case DOWN:
Draw::DrawGFX(Graphic, DClips[frame], Dest, X - monitor.getCamera().x, Y - monitor.getCamera().y);
break;
}
}
// Display a red rectangle if the player is dead
// (For debug purposes)
else
{
SDL_Rect offset = {X - monitor.getCamera().x, Y - monitor.getCamera().y, 32, 48};
SDL_FillRect(Dest, &offset, SDL_MapRGB(Dest->format, 255, 0, 0));
}
Draw::DrawGFX(showframe, Dest, 50, 32);
}
void Player::Cleanup()
{
// Free the memory allocated when
// the sprite sheet was loaded.
SDL_FreeSurface(Graphic);
SDL_FreeSurface(showframe);
}
|