-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlayer.cpp
More file actions
42 lines (31 loc) · 1.29 KB
/
Player.cpp
File metadata and controls
42 lines (31 loc) · 1.29 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
#include "Player.h"
#include "MoreInfo.h"
Player::Player(sf::Texture& texture, float speed)
: Entity(texture), speed(speed),
playerLivesText(100, SCREEN_HEIGHT - 50, 25, SPACEINVADERS_FONT, sf::Color(255, 255, 255)) {
entity.setOrigin(entity.getGlobalBounds().width / 2, entity.getGlobalBounds().height / 2);
entity.setScale(1 * 1.2, 1 * 1.2);
}
void Player::keyboardInputs(sf::Keyboard::Key key, bool isPressed) {
if (key == sf::Keyboard::A) { left = isPressed; }
if (key == sf::Keyboard::D) { right = isPressed; }
}
void Player::updatePlayer() {
//Movement/Animation
sf::Vector2<float> movement(0.f, 0.f);
if (left) { movement.x -= speed; }
else if (right) { movement.x += speed; }
entity.move(sf::Vector2<float>(movement));
}
void Player::updateLives(int lives) {
playerLivesText.updateOText("Lives: ", lives);
}
void Player::updateBorderBounds() {
//Entity cannot go through the borders of the screen
if (getX() <= SCREEN_WIDTH * 0) { setPosition(sf::Vector2<float>(getX() + PLAYER_SPEED, getY())); } //Left Side
if (getX() >= SCREEN_WIDTH) { setPosition(sf::Vector2<float>(getX() - PLAYER_SPEED, getY())); } //Right Side
}
void Player::extraRenderTo(sf::RenderWindow& window) {
window.draw(entity);
playerLivesText.renderTo(window);
}