-
-
Save anonymous/655f2fd6f7debedfe21974ef89f97af9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Player.h" | |
Player::Player() { } | |
Player::Player(std::string filename, int rows, int columns, sf::Vector2f position) | |
{ | |
this->create(filename, rows, columns, position); | |
} | |
void Player::create(std::string filename, int rows, int columns, sf::Vector2f position) | |
{ | |
// Load the texture | |
if (!texture.loadFromFile(filename)) | |
std::cout << "Could not load " << filename << std::endl; | |
// Sheet dimensions | |
sheetWidth = texture.getSize().x; | |
sheetHeight = texture.getSize().y; | |
// Attach to player sprite | |
sprite.setTexture(texture); | |
// Set the position | |
sprite.setPosition(position.x, position.y); | |
// Get the sprite width and height | |
spriteWidth = sheetWidth / columns; | |
spriteHeight = sheetHeight / rows; | |
// Set the source frame | |
source.x = 0; | |
source.y = Right; | |
// Sprite frame | |
frameCounter = 0; | |
switchFrame = 100; | |
frameSpeed = 1000; | |
} | |
/* =========================================================== | |
* GAME LOOP | |
* ======================================================== */ | |
void Player::update(float timePerTick) | |
{ | |
// Get the previous position/direction | |
prevPosition = crntPosition; | |
previousRow = source.y; | |
// Try to reset frame if possible | |
resetFrame(); | |
// Check which key is pressed | |
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) | |
{ | |
source.y = Up; | |
moveFrame(timePerTick); | |
crntPosition.y -= 200.0f * timePerTick; | |
} | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) | |
{ | |
source.y = Down; | |
moveFrame(timePerTick); | |
crntPosition.y += 200.0f * timePerTick; | |
} | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) | |
{ | |
source.y = Left; | |
moveFrame(timePerTick); | |
crntPosition.x -= 200.0f * timePerTick; | |
} | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) | |
{ | |
source.y = Right; | |
moveFrame(timePerTick); | |
crntPosition.x += 200.0f * timePerTick; | |
} | |
} | |
void Player::draw(sf::RenderWindow &window, float alpha) | |
{ | |
// Interpolate movement | |
sprite.setPosition(crntPosition * alpha + prevPosition * (1.0f - alpha)); | |
// Create rectangle in sheet around a sprite | |
sprite.setTextureRect(sf::IntRect(source.x * spriteWidth, source.y * spriteHeight, spriteWidth, spriteHeight)); | |
// Draw the sprite | |
window.draw(sprite); | |
} | |
void Player::moveFrame(float timePerTick) | |
{ | |
if (source.y != previousRow) | |
source.x = 0; | |
frameCounter += frameSpeed * timePerTick; | |
if (frameCounter >= switchFrame) | |
{ | |
source.x++; | |
if (source.x * spriteWidth >= sheetWidth) | |
source.x = 0; | |
frameCounter = 0; | |
} | |
} | |
// TODO: Change this later | |
void Player::resetFrame() | |
{ | |
bool upKey = sf::Keyboard::isKeyPressed(sf::Keyboard::Up); | |
bool downKey = sf::Keyboard::isKeyPressed(sf::Keyboard::Down); | |
bool leftKey = sf::Keyboard::isKeyPressed(sf::Keyboard::Left); | |
bool rightKey = sf::Keyboard::isKeyPressed(sf::Keyboard::Right); | |
// If no movement keys are pressed | |
if (!upKey && !downKey && !leftKey && !rightKey) | |
source.x = 0; | |
} | |
// Not Used | |
void Player::movePlayer(float deltaTime) | |
{ | |
// Check which key is pressed | |
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) | |
crntPosition.y -= 200.0f * deltaTime; | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) | |
crntPosition.y += 200.0f * deltaTime; | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) | |
crntPosition.x -= 200.0f * deltaTime; | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) | |
crntPosition.x += 200.0f * deltaTime; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment