-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
150 lines (123 loc) · 2.89 KB
/
Player.cpp
File metadata and controls
150 lines (123 loc) · 2.89 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
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
#include "Player.h"
Player::Player(float resolutionWidth, float resolutionHeight)
{
initVariables(resolutionHeight);
initShape(resolutionWidth, resolutionHeight);
}
void Player::initShape(float resolutionWidth, float resolutionHeight)
{
spawnPositionX = resolutionWidth / 2;
spawnPositionY = resolutionHeight / 1.1;
//triangle = new sf::CircleShape(20, 3);
triangle.setRadius(30);
triangle.setPointCount(3);
triangle.setFillColor(sf::Color::Yellow);
triangle.setOrigin(triangle.getRadius(),0);
triangle.setPosition(spawnPositionX, spawnPositionY);
}
sf::Vector2f Player::getPlayerPosition()
{
return triangle.getPosition();
}
void Player::initVariables(float resolutionHeight)
{
this->resolutionHeight = resolutionHeight;
playerMovementSpeed = 7.0f;
playerHP = 3;
collidedWithEnemyBullet = false;
}
void Player::movePlayer()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
triangle.move(-playerMovementSpeed, 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
triangle.move(playerMovementSpeed, 0.0f);
}
}
void Player::shootBullets(const sf::RenderTarget& target)
{
if (currentBullets < maxBullets)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
//create bullets at current position
//add to vector
allPlayerBullets.push_back(new PlayerBullet(resolutionHeight,
triangle.getPosition()));
currentBullets++;
playerSound.laserSound.play();
}
}
if (!allPlayerBullets.empty())
{
for (PlayerBullet* i : allPlayerBullets)
{
i->update(target);
}
}
}
void Player::checkBulletBoundary()
{
int bulletCounter = 0;
for (PlayerBullet* i : allPlayerBullets)
{
if (i->bulletOnBorder)
{
allPlayerBullets.erase(allPlayerBullets.begin() + bulletCounter);
currentBullets--;
}
bulletCounter++;
}
}
void Player::updateWindowBounds(const sf::RenderTarget& target)
{
//Allows players to swap to other side of screen
//Left Bound
if (triangle.getGlobalBounds().left + triangle.getGlobalBounds().width <= 0.f)
{
triangle.setPosition(target.getSize().x, triangle.getGlobalBounds().top);
}
//Right Bound
if (triangle.getGlobalBounds().left >= target.getSize().x)
{
triangle.setPosition(0.f, triangle.getGlobalBounds().top);
}
}
void Player::checkEnemyBulletCollision(const EnemyBullet& enemyBullet)
{
//if bullet has collided with player
//remove 1 life
//pass through enemybullet
if (triangle.getGlobalBounds().intersects(
enemyBullet.line.getGlobalBounds()))
{
playerHP--;
collidedWithEnemyBullet = true;
}
}
void Player::update(const sf::RenderTarget& target)
{
movePlayer();
shootBullets(target);
checkBulletBoundary();
updateWindowBounds(target);
}
void Player::render(sf::RenderTarget& target)
{
target.draw(triangle);
for (PlayerBullet* i : allPlayerBullets)
{
i->render(target);
}
}
Player::~Player()
{
for (PlayerBullet* ptr : allPlayerBullets)
{
delete ptr;
}
allPlayerBullets.clear();
}