#include <SFML/Graphics.hpp>
#include "Player.h"
#include <iostream>
extern const float WINDOW_WIDTH;
extern const float WINDOW_HEIGHT;
Player::Player( )
{
if( !texture.loadFromFile( ( "whitebackground1920x1200.jpg" ), sf::IntRect( 0, 0, spriteWidth, spriteLength ) ) )
{
std::cout << "Texture error\n";
}
sprite.setTexture( texture );
sprite.setColor( sf::Color::Red );
sprite.setOrigin( 8, 18 );
sprite.setPosition( WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 );
projectile.setPrimitiveType( sf::Lines );
sf::Vertex tempVertex;
projectile.append( tempVertex );
projectile.append( tempVertex );
projectile[0].color = sf::Color::Yellow;
projectile[1].color = sf::Color::Yellow;
}
void Player::movement( )
{
/* Start player movement */
if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
{
movePlayerUp = true;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
{
movePlayerDown = true;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
{
movePlayerLeft = true;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
{
movePlayerRight = true;
}
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Space ) )
{
movePlayerJump = true;
}
/* Stop player movement */
if( !sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
{
movePlayerUp = false;
}
if( !sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
{
movePlayerDown = false;
}
if( !sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
{
movePlayerLeft = false;
}
if( !sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
{
movePlayerRight = false;
}
if( !sf::Keyboard::isKeyPressed( sf::Keyboard::Space ) )
{
movePlayerJump = false;
}
/* Define player movement */
if( movePlayerUp == true )
{
sprite.move( 0, -1.0f );
std::cout << "Move up\n";
}
if( movePlayerDown == true )
{
sprite.move( 0, 1.0f );
std::cout << "Move down\n";
}
if( movePlayerLeft == true )
{
sprite.move( -1.0f, 0 );
std::cout << "Move left\n";
}
if( movePlayerRight == true )
{
sprite.move( 1.0f, 0 );
std::cout << "Move right\n";
}
if( movePlayerJump == true )
{
std::cout << "Jump\n";
}
/* Teleport player if out of bounds */
if( sprite.getPosition( ).y < 0 )
{
sprite.setPosition( sprite.getPosition( ).x, 0 );
std::cout << "Teleport down\n";
}
if( sprite.getPosition( ).y > WINDOW_HEIGHT )
{
sprite.setPosition( sprite.getPosition( ).x, (float)WINDOW_HEIGHT );
std::cout << "Teleport up\n";
}
if( sprite.getPosition( ).x < 0 )
{
sprite.setPosition( 0, sprite.getPosition( ).y );
std::cout << "Teleport right\n";
}
if( sprite.getPosition( ).x > WINDOW_WIDTH )
{
sprite.setPosition( (float)WINDOW_WIDTH, sprite.getPosition( ).y );
std::cout << "Teleport left\n";
}
}
void Player::dash( )
{
/* Dash player in a direction based on which keys are held down (Note: boolean AND) */
sprite.move( 0, -50.0f * movePlayerUp );
sprite.move( 0, 50.0f * movePlayerDown );
sprite.move( -50.0f * movePlayerLeft, 0 );
sprite.move( 50.0f * movePlayerRight, 0 );
std::cout << "Dash\n";
}
void Player::startShooting( int mouse_x, int mouse_y )
{
projectile[0].position = sf::Vector2f( sprite.getPosition( ).x, sprite.getPosition( ).y );
projectile[1].position = sf::Vector2f( mouse_x, mouse_y );
shooting = true;
std::cout << "Shoot\n";
}
void Player::stopShooting( )
{
projectile[0].position = sf::Vector2f( 9999, 9999 );
projectile[1].position = sf::Vector2f( 9999, 9999 );
shooting = false;
}
bool Player::get_shootingState( )
{
return shooting;
}