Working example for animated sprites

This commit is contained in:
Maximilian Giller 2023-06-04 20:55:14 +02:00
parent 7b56493fa9
commit 37ef4020a1
10 changed files with 23 additions and 11 deletions

BIN
assets/numbers.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

View file

@ -23,7 +23,7 @@ GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY)
}
auto gameObject = std::make_shared<CircleObject>(DB_ISOPLANE_CORNER_RADIUS, color);
addChild(gameObject, WorldCoordinates(x, y));
addChildWorldOffset(gameObject, WorldCoordinates(x, y));
}
}
}

View file

@ -44,10 +44,10 @@ void GameObject::lateUpdate()
void GameObject::addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset)
{
auto worldOffset = CoordinateTransformer::isometricToWorld(offset);
addChild(child, worldOffset);
addChildWorldOffset(child, worldOffset);
}
void GameObject::addChild(const std::shared_ptr<GameObject> &child, WorldCoordinates offset)
void GameObject::addChildWorldOffset(const std::shared_ptr<GameObject> &child, WorldCoordinates offset)
{
children.push_back(child);
child->coordinates->setParent(coordinates, offset);

View file

@ -22,7 +22,7 @@ public:
bool getActive() const { return isActive; }
void addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset = {0, 0});
void addChild(const std::shared_ptr<GameObject> &child, WorldCoordinates offset);
void addChildWorldOffset(const std::shared_ptr<GameObject> &child, WorldCoordinates offset);
std::vector<std::shared_ptr<GameObject>> getChildren() const { return children; }
std::shared_ptr<TranslatedCoordinates> coordinates;

View file

@ -1,6 +1,8 @@
#include "player.hpp"
#include "../../sprites/texture_manager.hpp"
#include "../../sprites/single_sprite.hpp"
#include "../../sprites/animated_sprite.hpp"
#include "../../sprites/sprite_sheet.hpp"
#include <utility>
@ -10,9 +12,14 @@ Player::Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &
input = std::move(assignedInput);
auto sprite = std::make_shared<SingleSprite>(TextureManager::getInstance()->getTexture(image),
sf::Vector2f{width, width});
addChildScreenOffset(sprite, {-width / 2.f, -width / 2.f});
// auto sprite = std::make_shared<SingleSprite>(TextureManager::getInstance()->getTexture(image),
// sf::Vector2f{width, width});
// addChildScreenOffset(sprite, {-width / 2.f, -width / 2.f});
auto spriteSheet = std::make_shared<SpriteSheet>(TextureManager::getInstance()->getTexture(image), 4, 2);
auto animatedSprite = spriteSheet->getAnimation(0, 8);
animatedSprite->setSize({width, width});
addChildScreenOffset(animatedSprite, {-width / 2.f, -width / 2.f});
}
sf::Vector2f Player::getTrackablePosition() const
@ -36,6 +43,8 @@ void Player::update()
auto moveDirection = input->direction.asIsometricVector();
auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds();
coordinates->move(moveDelta);
GameObject::update();
}
TrackableState Player::getTrackableState() const

View file

@ -24,7 +24,7 @@ public:
private:
std::shared_ptr<InputIdentity> input;
float width = 10;
float width = 50;
};

View file

@ -1,5 +1,6 @@
#include "animated_sprite.hpp"
#include "../config.h"
#include "../logging/easylogging++.h"
AnimatedSprite::AnimatedSprite(const std::vector<sf::Sprite> &sprites, const sf::Vector2f& size)
{

View file

@ -15,12 +15,14 @@ public:
void draw(sf::RenderWindow *window) const override;
sf::Time frameDuration = sf::seconds(0.1f);
void setSize(const sf::Vector2f &size);
private:
int currentFrame = 0;
sf::Time timeSinceLastFrame = sf::Time::Zero;
std::vector<sf::Sprite> sprites;
void setSize(const sf::Vector2f &size);
};

View file

@ -29,7 +29,7 @@ sf::Sprite SpriteSheet::getSprite(int sequenceIndex) const
std::shared_ptr<AnimatedSprite> SpriteSheet::getAnimation(int startingSequenceIndex, int numberOfFrames) const
{
if (startingSequenceIndex < 0 || startingSequenceIndex + numberOfFrames >= sprites.size())
if (startingSequenceIndex < 0 || startingSequenceIndex + numberOfFrames > sprites.size())
{
throw std::runtime_error("Invalid starting sequence index");
}

View file

@ -7,7 +7,7 @@
#define PLAYER_TEXTURE "player"
std::map<std::string, std::string> const all_textures = {
{PLAYER_TEXTURE, "assets/64.png"}
{PLAYER_TEXTURE, "assets/numbers.png"}
};
#endif //HOLESOME_TEXTURE_CONFIG_H