Working example for animated sprites
This commit is contained in:
parent
7b56493fa9
commit
37ef4020a1
10 changed files with 23 additions and 11 deletions
BIN
assets/numbers.png
Normal file
BIN
assets/numbers.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.6 KiB |
|
@ -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);
|
auto gameObject = std::make_shared<CircleObject>(DB_ISOPLANE_CORNER_RADIUS, color);
|
||||||
addChild(gameObject, WorldCoordinates(x, y));
|
addChildWorldOffset(gameObject, WorldCoordinates(x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,10 +44,10 @@ void GameObject::lateUpdate()
|
||||||
void GameObject::addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset)
|
void GameObject::addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset)
|
||||||
{
|
{
|
||||||
auto worldOffset = CoordinateTransformer::isometricToWorld(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);
|
children.push_back(child);
|
||||||
child->coordinates->setParent(coordinates, offset);
|
child->coordinates->setParent(coordinates, offset);
|
||||||
|
|
|
@ -22,7 +22,7 @@ public:
|
||||||
bool getActive() const { return isActive; }
|
bool getActive() const { return isActive; }
|
||||||
|
|
||||||
void addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset = {0, 0});
|
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::vector<std::shared_ptr<GameObject>> getChildren() const { return children; }
|
||||||
|
|
||||||
std::shared_ptr<TranslatedCoordinates> coordinates;
|
std::shared_ptr<TranslatedCoordinates> coordinates;
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include "player.hpp"
|
#include "player.hpp"
|
||||||
#include "../../sprites/texture_manager.hpp"
|
#include "../../sprites/texture_manager.hpp"
|
||||||
#include "../../sprites/single_sprite.hpp"
|
#include "../../sprites/single_sprite.hpp"
|
||||||
|
#include "../../sprites/animated_sprite.hpp"
|
||||||
|
#include "../../sprites/sprite_sheet.hpp"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -10,9 +12,14 @@ Player::Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &
|
||||||
|
|
||||||
input = std::move(assignedInput);
|
input = std::move(assignedInput);
|
||||||
|
|
||||||
auto sprite = std::make_shared<SingleSprite>(TextureManager::getInstance()->getTexture(image),
|
// auto sprite = std::make_shared<SingleSprite>(TextureManager::getInstance()->getTexture(image),
|
||||||
sf::Vector2f{width, width});
|
// sf::Vector2f{width, width});
|
||||||
addChildScreenOffset(sprite, {-width / 2.f, -width / 2.f});
|
// 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
|
sf::Vector2f Player::getTrackablePosition() const
|
||||||
|
@ -36,6 +43,8 @@ void Player::update()
|
||||||
auto moveDirection = input->direction.asIsometricVector();
|
auto moveDirection = input->direction.asIsometricVector();
|
||||||
auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds();
|
auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds();
|
||||||
coordinates->move(moveDelta);
|
coordinates->move(moveDelta);
|
||||||
|
|
||||||
|
GameObject::update();
|
||||||
}
|
}
|
||||||
|
|
||||||
TrackableState Player::getTrackableState() const
|
TrackableState Player::getTrackableState() const
|
||||||
|
|
|
@ -24,7 +24,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<InputIdentity> input;
|
std::shared_ptr<InputIdentity> input;
|
||||||
float width = 10;
|
float width = 50;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "animated_sprite.hpp"
|
#include "animated_sprite.hpp"
|
||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
|
#include "../logging/easylogging++.h"
|
||||||
|
|
||||||
AnimatedSprite::AnimatedSprite(const std::vector<sf::Sprite> &sprites, const sf::Vector2f& size)
|
AnimatedSprite::AnimatedSprite(const std::vector<sf::Sprite> &sprites, const sf::Vector2f& size)
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,12 +15,14 @@ public:
|
||||||
void draw(sf::RenderWindow *window) const override;
|
void draw(sf::RenderWindow *window) const override;
|
||||||
|
|
||||||
sf::Time frameDuration = sf::seconds(0.1f);
|
sf::Time frameDuration = sf::seconds(0.1f);
|
||||||
|
|
||||||
|
void setSize(const sf::Vector2f &size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int currentFrame = 0;
|
int currentFrame = 0;
|
||||||
sf::Time timeSinceLastFrame = sf::Time::Zero;
|
sf::Time timeSinceLastFrame = sf::Time::Zero;
|
||||||
std::vector<sf::Sprite> sprites;
|
std::vector<sf::Sprite> sprites;
|
||||||
|
|
||||||
void setSize(const sf::Vector2f &size);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ sf::Sprite SpriteSheet::getSprite(int sequenceIndex) const
|
||||||
|
|
||||||
std::shared_ptr<AnimatedSprite> SpriteSheet::getAnimation(int startingSequenceIndex, int numberOfFrames) 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");
|
throw std::runtime_error("Invalid starting sequence index");
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#define PLAYER_TEXTURE "player"
|
#define PLAYER_TEXTURE "player"
|
||||||
|
|
||||||
std::map<std::string, std::string> const all_textures = {
|
std::map<std::string, std::string> const all_textures = {
|
||||||
{PLAYER_TEXTURE, "assets/64.png"}
|
{PLAYER_TEXTURE, "assets/numbers.png"}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //HOLESOME_TEXTURE_CONFIG_H
|
#endif //HOLESOME_TEXTURE_CONFIG_H
|
||||||
|
|
Loading…
Reference in a new issue