Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Character.h"
- Character::Character(float xPos, float yPos, float width, float height, sf::Color color)
- :movable(true)
- {
- this->rectShape.setPosition(xPos, yPos);
- this->rectShape.setSize(sf::Vector2f(width, height));
- this->rectShape.setFillColor(color);
- }
- float Character::getXUpperLeftCorner()
- {
- return this->rectShape.getPosition().x;
- }
- float Character::getYUpperLeftCorner()
- {
- return this->rectShape.getPosition().y;
- }
- float Character::getWidth()
- {
- return this->rectShape.getGlobalBounds().width;
- }
- float Character::getHeight()
- {
- return this->rectShape.getGlobalBounds().height;
- }
- void Character::setMovable(bool moveable)
- {
- this->movable = movable;
- }
- void Character::move()
- {
- if (this->movable)
- {
- float movementSpeed = 10;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && getXUpperLeftCorner() >= 0)
- {
- this->rectShape.move(movementSpeed * -1, 0.f);
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && getXUpperLeftCorner() + getWidth() <= 800)
- {
- this->rectShape.move(movementSpeed, 0.f);
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && getYUpperLeftCorner() >= 0)
- {
- this->rectShape.move(0.f, movementSpeed * -1);
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && getYUpperLeftCorner() + getHeight() <= 600)
- {
- this->rectShape.move(0.f, movementSpeed);
- }
- }
- }
- void Character::draw(sf::RenderTarget & target, sf::RenderStates states) const
- {
- target.draw(this->rectShape);
- }
Advertisement
Add Comment
Please, Sign In to add comment