-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.hpp
156 lines (131 loc) · 5.72 KB
/
Sprite.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef SPRITE_HEADER_HPP
#define SPRITE_HEADER_HPP
#include <SFML/Graphics.hpp>
#include <string>
#include <array>
#include <tuple>
#include <algorithm>
#include <optional>
namespace StrategyGoo
{
enum class Direction : unsigned short
{
NORTH = 0,
SOUTH = 1,
EAST = 2,
WEST = 3,
NORTH_EAST = 4,
SOUTH_EAST = 5,
NORTH_WEST = 6,
SOUTH_WEST = 7
};
const size_t AMOUNT_OF_DIRECTIONS_CONSTANT = 8;
const std::array< const Direction, AMOUNT_OF_DIRECTIONS_CONSTANT > ALL_DIRECTIONS_CONSTANT {
Direction::NORTH, Direction::SOUTH, Direction::EAST, Direction::WEST,
Direction::NORTH_EAST, Direction::SOUTH_EAST, Direction::NORTH_WEST, Direction::SOUTH_WEST
};
const std::array< const std::string,
AMOUNT_OF_DIRECTIONS_CONSTANT > STRING_DIRECTIONS_ABBREVIATIONS_CONSTANT {
"N", "S", "E", "W", "NE", "SE", "NW", "SW"
};
const std::array< sf::Vector2i, AMOUNT_OF_DIRECTIONS_CONSTANT > DIRECTION_VECTOR_FACINGS_CONSTANT{
sf::Vector2i{ 1, 0 }, sf::Vector2i{ -1, 0 }, sf::Vector2i{ 0, 1 }, sf::Vector2i{ 0, -1 },
sf::Vector2i{ 1, 1 }, sf::Vector2i{ -1, 1 }, sf::Vector2i{ 1, -1 }, sf::Vector2i{ -1, -1 }
};
const Direction DEFAULT_DIRECTION = Direction::NORTH;
using FRAMES_TYPE = std::array< std::vector< sf::IntRect >,
AMOUNT_OF_DIRECTIONS_CONSTANT >;
using ANIMATION_TYPE = std::array< std::vector< size_t >,
AMOUNT_OF_DIRECTIONS_CONSTANT >;
using LOADED_SPRITE_DATA_TYPE = std::tuple< std::string, FRAMES_TYPE, std::string, sf::IntRect >;
const size_t LOADED_SPRITE_DATA_TYPE_SPRITE_NAME_CONSTANT = 0;
const size_t LOADED_SPRITE_DATA_TYPE_SPRITE_FRAMES_CONSTANT = 1;
const size_t LOADED_SPRITE_DATA_TYPE_SPRITE_JSON_DATA_CONSTANT = 2;
const size_t LOADED_SPRITE_DATA_TYPE_SPRITE_INT_RECT_CONSTANT = 3;
namespace Detail
{
extern sf::Texture* masterTexture;
extern sf::Image* masterSpriteBuffer;
extern bool masterTextureInitialized;
extern std::vector< LOADED_SPRITE_DATA_TYPE > spritesLoaded;
}
const std::string DEFAULT_SPRITE_NAME_CONSTANT = "DEFAULT_SPRITE_NAME";
const std::string DEFAULT_JSON_DATA_CONSTANT = "DEFAULT_JSON_DATA_NAME";
template< int LAYER_CONSTANT = 0 >
struct Sprite
{
Sprite( std::string spriteName_, Direction currentDirection_ = DEFAULT_DIRECTION );
void Draw( sf::RenderWindow& toRenderTo );
void ChangeAnimation( size_t to );
std::vector< sf::IntRect > ObtainFramesForDirection( Direction spriteDirection );
std::vector< size_t > ObtainAnimationFramesForDirection( Direction spriteDirection );
std::vector< sf::IntRect > ObtainIntRectAnimationFramesForDirection( size_t animation, Direction spriteDirection );
std::vector< sf::IntRect > ObtainIntRectCurrentAnimationFramesForDirection( Direction spriteDirection );
bool IsDirectionAvailible( Direction spriteDirection );
sf::IntRect ObtainAnimationFrameBounds( size_t animation, Direction spriteDirection, size_t frame );
sf::IntRect ObtainCurrentAnimationFrameBounds( Direction spriteDirection, size_t frame );
sf::IntRect ObtainFrameBounds( Direction spriteDirection, size_t frame );
size_t ObtainFramesForAnimation( Direction spriteDirection, size_t animation );
size_t ObtainFramesForCurrentAnimation();
void ModifyTexture( const sf::Texture& texture );
void AddAnimation( ANIMATION_TYPE toAdd );
size_t ObtainAmountOfAnimations();
ANIMATION_TYPE ObtainAnimation( size_t animation );
void SetSprite( sf::Sprite sprite_ );
void SetCurrentDirection( Direction currentDirection_ );
void SetFrames( FRAMES_TYPE frames_ );
void SetAnimations( std::vector< ANIMATION_TYPE > animations_ );
void SetCurrentFrame( size_t currentFrame_ );
void SetActive( bool active_ );
void SetJSONData( std::string jsonData_ );
void SetPlaceInSpriteSheet( sf::IntRect placeInSpriteSheet_ );
void SetAnimationActive( bool animationActive_ );
void SetAnimationRate( float animationRate_ );
int GetLayerConstant();
std::string GetSpriteName();
sf::Sprite GetSprite();
FRAMES_TYPE GetFrames();
std::vector< ANIMATION_TYPE > GetAnimations();
Direction GetCurrentDirection();
size_t GetCurrentFrame();
size_t GetCurrentAnimation();
bool GetActive();
std::string GetJSONData();
sf::IntRect GetPlaceInSpriteSheet();
bool GetAnimationActive();
float GetAnimationRate();
static sf::IntRect LoadSpriteImage( std::string spriteName, sf::Texture* buffer = nullptr,
sf::Image* spriteBuffer = nullptr, std::vector< LOADED_SPRITE_DATA_TYPE >* loadedSprites = nullptr );
static LOADED_SPRITE_DATA_TYPE LoadSprite( std::string spriteName, sf::Texture* buffer = nullptr,
sf::Image* spriteBuffer = nullptr, std::vector< LOADED_SPRITE_DATA_TYPE >* loadedSprites = nullptr );
static std::optional< int > ObtainLoadedSpriteIndex( std::string spriteName,
std::vector< LOADED_SPRITE_DATA_TYPE >* loadedSprites = nullptr );
static std::string LoadJSONData( std::string spriteName );
void InitializeSprite( std::string spriteName_ );
static FRAMES_TYPE FrameDataFromJSONMetaData( std::string metaData, std::string spriteName );
operator sf::Sprite();
sf::Sprite& operator*();
sf::Sprite& RefrenceSprite();
std::vector< ANIMATION_TYPE >& RefrenceAnimations();
ANIMATION_TYPE& RefrenceAnimation( size_t animation );
protected:
int layer = 0;
void SetCurrentAnimation( size_t currentAnimation_ );
std::string spriteName = DEFAULT_SPRITE_NAME_CONSTANT;
sf::Sprite sprite;
FRAMES_TYPE frames;
Direction currentDirection;
size_t currentFrame;
size_t currentAnimation = 0;
sf::Clock animationRateClock;
float animationRate = 1.f;
sf::IntRect placeInSpriteSheet;
std::vector< ANIMATION_TYPE > animations;
bool active = true;
bool animationActive = true;
std::string jsonData = DEFAULT_JSON_DATA_CONSTANT;
void Animate();
void ConstructDefaultAnimation();
};
}
#endif