forked from rachit3006/CPP-Project-SDL2-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
80 lines (72 loc) · 2.33 KB
/
Copy pathPlayer.cpp
File metadata and controls
80 lines (72 loc) · 2.33 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"
#include <cstdlib>
using namespace std;
int Player::getx()
{
return des_rec.x;
}
int Player::gety()
{
return des_rec.y;
}
int Player::get_ammo()
{
return ammo;
}
void Player::set_ammo(int ammo)
{
// setting ammo
if (ammo >= 0)
this->ammo = ammo;
else
this->ammo = 0;
}
void Player::set_space_pressed(bool is_pressed)
{
this->space_pressed = is_pressed;
}
SDL_Rect Player::getPlayerRect()
{
return des_rec;
}
void Player::update()
{
// when spacebar is not pressed and the player is in air
if (des_rec.y <= window_height - src_rec.h - platform_height && !space_pressed)
{
// implementing gravity
final_velocity = initial_velocity + gravity;
// keeping the player above the platform
if (des_rec.y < platform_height)
des_rec.y = platform_height;
if (des_rec.y + (((final_velocity * final_velocity) - (initial_velocity * initial_velocity)) / (2 * gravity)) > window_height - src_rec.h - platform_height)
{
des_rec.y = window_height - src_rec.h - platform_height;
final_velocity = initial_velocity = 0;
}
else
{
des_rec.y += (((final_velocity * final_velocity) - (initial_velocity * initial_velocity)) / (2 * gravity));
}
initial_velocity = final_velocity;
}
// when spacebar is pressed and the player is not at top of the screen
if (des_rec.y >= platform_height && space_pressed)
{
// implementing the acceleration of the player in upwards direction
final_velocity = initial_velocity - acceleration;
// keeping the player below the ceiling
if (des_rec.y > window_height - src_rec.h - platform_height)
des_rec.y = window_height - src_rec.h - platform_height;
if (des_rec.y + (((final_velocity * final_velocity) - (initial_velocity * initial_velocity)) / (-2 * acceleration)) < platform_height)
{
des_rec.y = platform_height;
final_velocity = initial_velocity = 0;
}
else
{
des_rec.y += (((final_velocity * final_velocity) - (initial_velocity * initial_velocity)) / (-2 * acceleration));
}
initial_velocity = final_velocity;
}
}