diff --git a/assets/64.png b/assets/64.png new file mode 100644 index 0000000..9be8440 Binary files /dev/null and b/assets/64.png differ diff --git a/src/config.h b/src/config.h index eb83a9f..2ac9341 100644 --- a/src/config.h +++ b/src/config.h @@ -5,7 +5,7 @@ #include #include "game/input/input_device_group.h" -#define DEVELOPER_MODE false +#define DEVELOPER_MODE true // FPS diff --git a/src/game/game_object.cpp b/src/game/game_object.cpp index 368c2c1..9165db7 100644 --- a/src/game/game_object.cpp +++ b/src/game/game_object.cpp @@ -6,12 +6,6 @@ GameObject::GameObject() children = std::vector>(); } -void GameObject::addChild(const std::shared_ptr& child, WorldCoordinates offset) -{ - children.push_back(child); - child->coordinates->setParent(coordinates, offset); -} - void GameObject::setActive(bool active) { // Careful: potential infinite loop! @@ -46,3 +40,15 @@ void GameObject::lateUpdate() child->lateUpdate(); } } + +void GameObject::addChildScreenOffset(const std::shared_ptr &child, IsometricCoordinates offset) +{ + auto worldOffset = CoordinateTransformer::isometricToWorld(offset); + addChild(child, worldOffset); +} + +void GameObject::addChild(const std::shared_ptr &child, WorldCoordinates offset) +{ + children.push_back(child); + child->coordinates->setParent(coordinates, offset); +} diff --git a/src/game/game_object.h b/src/game/game_object.h index 93bb126..a476f1b 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -21,7 +21,8 @@ public: void setActive(bool active); bool getActive() const { return isActive; } - void addChild(const std::shared_ptr &child, WorldCoordinates offset = {0, 0}); + void addChildScreenOffset(const std::shared_ptr &child, IsometricCoordinates offset = {0, 0}); + void addChild(const std::shared_ptr &child, WorldCoordinates offset); std::vector> getChildren() const { return children; } std::shared_ptr coordinates; diff --git a/src/game/player/player.cpp b/src/game/player/player.cpp index 277f53e..671dd0b 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -10,8 +10,9 @@ Player::Player(std::shared_ptr assignedInput, const std::string & input = std::move(assignedInput); - auto sprite = std::make_shared(TextureManager::getInstance()->getTexture(image)); - addChild(sprite); + auto sprite = std::make_shared(TextureManager::getInstance()->getTexture(image), + sf::Vector2f{width, width}); + addChildScreenOffset(sprite, {-width / 2.f, -width / 2.f}); } sf::Vector2f Player::getTrackablePosition() const @@ -21,8 +22,7 @@ sf::Vector2f Player::getTrackablePosition() const sf::Vector2f Player::getTrackableSize() const { - // TODO: Proper implementation - return {50, 50}; + return {width, width}; } void Player::update() diff --git a/src/game/player/player.hpp b/src/game/player/player.hpp index bedb5d0..231d0fb 100644 --- a/src/game/player/player.hpp +++ b/src/game/player/player.hpp @@ -24,6 +24,7 @@ public: private: std::shared_ptr input; + float width = 10; }; diff --git a/src/sprites/animated_sprite.cpp b/src/sprites/animated_sprite.cpp index 86adc36..b1404ef 100644 --- a/src/sprites/animated_sprite.cpp +++ b/src/sprites/animated_sprite.cpp @@ -1,9 +1,10 @@ #include "animated_sprite.hpp" #include "../config.h" -AnimatedSprite::AnimatedSprite(const std::vector &sprites) +AnimatedSprite::AnimatedSprite(const std::vector &sprites, const sf::Vector2f& size) { this->sprites = sprites; + setSize(size); } void AnimatedSprite::update() @@ -27,3 +28,11 @@ void AnimatedSprite::draw(sf::RenderWindow *window) const window->draw(currentSprite); } + +void AnimatedSprite::setSize(const sf::Vector2f &size) +{ + for (auto &sprite: sprites) + { + sprite.setScale(size.x / sprite.getTextureRect().width, size.y / sprite.getTextureRect().height); + } +} diff --git a/src/sprites/animated_sprite.hpp b/src/sprites/animated_sprite.hpp index b2dc6c1..f314f4d 100644 --- a/src/sprites/animated_sprite.hpp +++ b/src/sprites/animated_sprite.hpp @@ -8,7 +8,7 @@ class AnimatedSprite : public GameObject { public: - AnimatedSprite(const std::vector& sprites); + AnimatedSprite(const std::vector& sprites, const sf::Vector2f& size = sf::Vector2f(0, 0)); void update() override; @@ -19,6 +19,8 @@ private: int currentFrame = 0; sf::Time timeSinceLastFrame = sf::Time::Zero; std::vector sprites; + + void setSize(const sf::Vector2f &size); }; diff --git a/src/sprites/single_sprite.cpp b/src/sprites/single_sprite.cpp index f7c6f01..fcb2d52 100644 --- a/src/sprites/single_sprite.cpp +++ b/src/sprites/single_sprite.cpp @@ -1,8 +1,18 @@ #include "single_sprite.hpp" -SingleSprite::SingleSprite(const sf::Sprite &sprite) +SingleSprite::SingleSprite(const sf::Sprite &sprite, const sf::Vector2f &size) { this->sprite = sprite; + + setSize(size); +} + +SingleSprite::SingleSprite(const std::shared_ptr &texture, const sf::Vector2f &size) +{ + sprite = sf::Sprite(); + sprite.setTexture(*texture); + + setSize(size); } void SingleSprite::draw(sf::RenderWindow *window) const @@ -15,8 +25,13 @@ void SingleSprite::lateUpdate() sprite.setPosition(coordinates->isometric().toScreen()); } -SingleSprite::SingleSprite(const std::shared_ptr& texture) +void SingleSprite::setSize(const sf::Vector2f &size) { - sprite = sf::Sprite(); - sprite.setTexture(*texture); + if (size == sf::Vector2f(0, 0)) + { + return; + } + + auto textureSize = sprite.getTextureRect(); + sprite.setScale(size.x / textureSize.width, size.y / textureSize.height); } diff --git a/src/sprites/single_sprite.hpp b/src/sprites/single_sprite.hpp index 462f604..f1e93db 100644 --- a/src/sprites/single_sprite.hpp +++ b/src/sprites/single_sprite.hpp @@ -8,8 +8,8 @@ class SingleSprite : public GameObject { public: - SingleSprite(const sf::Sprite& sprite); - SingleSprite(const std::shared_ptr& texture); + SingleSprite(const sf::Sprite& sprite, const sf::Vector2f& size = sf::Vector2f(0, 0)); + SingleSprite(const std::shared_ptr& texture, const sf::Vector2f& size = sf::Vector2f(0, 0)); void draw(sf::RenderWindow *window) const override; @@ -17,6 +17,8 @@ public: private: sf::Sprite sprite; + + void setSize(const sf::Vector2f &size); }; diff --git a/src/texture_config.h b/src/texture_config.h index 596369f..7538628 100644 --- a/src/texture_config.h +++ b/src/texture_config.h @@ -7,7 +7,7 @@ #define PLAYER_TEXTURE "player" std::map const all_textures = { - {PLAYER_TEXTURE, "assets/player.png"} + {PLAYER_TEXTURE, "assets/64.png"} }; #endif //HOLESOME_TEXTURE_CONFIG_H