-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
126 lines (108 loc) · 2.81 KB
/
player.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
#include "Player.h"
#include "Engine.h"
Player::Player(SDL_Renderer* renderer)
{
blaster = new Weapon(renderer);
playerTexture.LoadFromFile(renderer, "resources/player_ship_retina.png");
width = playerTexture.GetWidth();
height = playerTexture.GetHeight();
SpriteAngle = 0.0;
position.X = (Engine::GetScreenWidth() / 2) - (width / 2);
position.Y = (Engine::GetScreenHeight() /2) - (height / 2);
moveSpeed = 5;
SDLTexture = playerTexture.GetTexture();
lastShotTime = 0;
}
Player::~Player()
{
}
//handles key presses and mouse clicks
void Player::HandleInput(Input* input)
{
if (input->KeyPressed(MOVE_UP))
{
movePlayer(Y, -moveSpeed);
}
if (input->KeyPressed(MOVE_DOWN))
{
movePlayer(Y, moveSpeed);
}
if (input->KeyPressed(MOVE_LEFT))
{
movePlayer(X, -moveSpeed);
}
if (input->KeyPressed(MOVE_RIGHT))
{
movePlayer(X, moveSpeed);
}
if (input->KeyPressed(SHOOT))
{
currentTime = SDL_GetTicks();
if ((currentTime - lastShotTime) > shotInterval)
{
shoot();
lastShotTime = SDL_GetTicks();
}
}
//Calculate the angle to the mouse cursor and set the player rotation accordingly
mousePosition = input->GetMousePosition();
playerCenter = { position.X + width / 2, position.Y + height / 2};
Vector2 delta = { mousePosition->X - playerCenter.X, mousePosition->Y - playerCenter.Y};
angle = atan2(delta.Y, delta.X) * 180 / M_PI;
angle += 90.0; //Add 90 to rotate the texture correctly
}
void Player::movePlayer(Axis axis, int moveAmount)
{
if (axis == X) {
position.X += moveAmount;
}
else {
position.Y += moveAmount;
}
if ((position.X + (width*0.5))> Engine::GetScreenWidth())
{
position.X = 0 - (width*0.5);
}
else if ((position.X + (width*0.5)) < 0)
{
position.X = Engine::GetScreenWidth() - (width*0.5);
}
else if ((position.Y + (height*0.5)) > Engine::GetScreenHeight())
{
position.Y = 0 - (height*0.5);
}
else if ((position.Y + (height*0.5)) < 0)
{
position.Y = Engine::GetScreenHeight() - (height*0.5);
}
}
int Player::getMoveSpeed()
{
return moveSpeed;
}
Vector2 Player::GetPosition() const
{
return position;
}
SDL_Texture *Player::GetPlayerTexture()
{
return SDLTexture;
}
void Player::Render(SDL_Renderer *renderer)
{
blaster->Render(renderer);
SDL_Rect renderQuad = { position.X, position.Y, width, height };
SDL_RenderCopyEx(renderer, SDLTexture, NULL, &renderQuad, angle, NULL, SDL_FLIP_NONE);
}
void Player::shoot()
{
Vector2 directionVector = { mousePosition->X - playerCenter.X, mousePosition->Y - playerCenter.Y};
Vector2 normalizedDirection = directionVector.Normalize();
Vector2 direction = { normalizedDirection.X * width / 2 , normalizedDirection.Y * width / 2 };
Vector2 bulletPos = { playerCenter.X + direction.X, playerCenter.Y + direction.Y };
blaster->Shoot(bulletPos, direction, angle);
}
void Player::Update()
{
blaster->Update();
}