-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
34 lines (26 loc) · 954 Bytes
/
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
#include "Player.hpp"
#include "GameObject.hpp"
#include <iostream>
Player::Player(sf::Vector2f pos, sf::Keyboard::Key keys[]) : controls(keys), position(pos) {
velocity = sf::Vector2f(0, 0);
friction = .8f;
// temporary until I find a proper way to get sprite height before it's loaded
position.y = (Game::SCREEN_HEIGHT / 2) - 40;
}
void Player::Update(sf::Time elapsed) {
if (sf::Keyboard::isKeyPressed(controls[0])) {
velocity.y -= 100.0;
}
if (sf::Keyboard::isKeyPressed(controls[1])) {
velocity.y += 100.0;
}
position += velocity * elapsed.asSeconds();
velocity *= friction;
if (position.y < 0 || position.y > Game::SCREEN_HEIGHT - GetSprite().getGlobalBounds().height)
velocity = -velocity;
if (position.y < 0)
position.y = 0;
if (position.y > Game::SCREEN_HEIGHT - GetSprite().getLocalBounds().height)
position.y = Game::SCREEN_HEIGHT - GetSprite().getLocalBounds().height;
SetPosition(position.x, position.y);
}