Repository navigation
Expand file tree
/
Copy pathPlayer.cpp
More file actions
80 lines (66 loc) · 2.36 KB
/
Copy pathPlayer.cpp
File metadata and controls
80 lines (66 loc) · 2.36 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.hpp"
void Player::_Touch_Ball(Player& player, Ball& ball, float deltaTime)
{
float distance = pow(pow(player.x - ball.x, 2) + pow(player.y - ball.y, 2), 0.5);
if (distance < 43)
{
if (player.x > ball.x) { ball.x -= _Move(deltaTime, player); }
else { ball.x += _Move(deltaTime, player); }
if (player.y > ball.y) { ball.y -= _Move(deltaTime, player); }
else { ball.y += _Move(deltaTime, player); }
}
}
float Player::_Velocity_Calc(float deltaTime, Player& player)
{
float Volacity = player.velocity + player.acceleration * deltaTime;
return Volacity;
}
float Player::_Move(float deltaTime, Player& player)
{
return _Velocity_Calc(deltaTime, player) * deltaTime;
}
void Player::_Update_Pos(float deltaTime, Player& player)
{
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
// Walking w/s/a/d
if (currentKeyStates[SDL_SCANCODE_A] || currentKeyStates[SDL_SCANCODE_LEFT])
{
player.x -= _Move(deltaTime, player);
}
if (currentKeyStates[SDL_SCANCODE_D] || currentKeyStates[SDL_SCANCODE_RIGHT])
{
player.x += _Move(deltaTime, player);
}
if (currentKeyStates[SDL_SCANCODE_W] || currentKeyStates[SDL_SCANCODE_UP])
{
player.y -= _Move(deltaTime, player);
}
if (currentKeyStates[SDL_SCANCODE_S] || currentKeyStates[SDL_SCANCODE_DOWN])
{
player.y += _Move(deltaTime, player);
}
// Running shift + w/s/a/d
if (currentKeyStates[SDL_SCANCODE_LSHIFT] && (currentKeyStates[SDL_SCANCODE_A] || currentKeyStates[SDL_SCANCODE_LEFT]))
{
player.x += _Move(deltaTime, player) - 6;
}
if (currentKeyStates[SDL_SCANCODE_LSHIFT] && (currentKeyStates[SDL_SCANCODE_D] || currentKeyStates[SDL_SCANCODE_RIGHT]))
{
player.x -= _Move(deltaTime, player) - 6;
}
if (currentKeyStates[SDL_SCANCODE_LSHIFT] && (currentKeyStates[SDL_SCANCODE_W] || currentKeyStates[SDL_SCANCODE_UP]))
{
player.y += _Move(deltaTime, player)-6;
}
if (currentKeyStates[SDL_SCANCODE_LSHIFT] && (currentKeyStates[SDL_SCANCODE_S] || currentKeyStates[SDL_SCANCODE_DOWN]))
{
player.y -= _Move(deltaTime, player)-6;
}
}
void Player::_player_Border(Player& player, int SCREEN_WIDTH, int SCREEN_HEIGHT)
{
if (player.x < 0) { player.x = 0; }
if (player.x + player.width > SCREEN_WIDTH) { player.x = SCREEN_WIDTH - player.width; }
if (player.y < 0) { player.y = 0; }
if (player.y + player.height > SCREEN_HEIGHT) { player.y = SCREEN_HEIGHT - player.height; }
}