Add some getters and a way to flip an animation
This commit is contained in:
parent
dd919bf89e
commit
7ae000c4ac
|
@ -18,9 +18,14 @@ namespace simpleengine {
|
||||||
void Update(const float& delta_time);
|
void Update(const float& delta_time);
|
||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
|
void FlipHorizontally();
|
||||||
|
void FlipVertically();
|
||||||
|
|
||||||
|
bool IsHorizontallyFlipped() const;
|
||||||
|
bool IsVerticallyFlipped() const;
|
||||||
|
|
||||||
/*void Play();
|
/*void Play();
|
||||||
void Pause();*/
|
void Pause();*/
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::Sprite &sprite;
|
sf::Sprite &sprite;
|
||||||
sf::Texture &texture_sheet;
|
sf::Texture &texture_sheet;
|
||||||
|
@ -32,6 +37,8 @@ namespace simpleengine {
|
||||||
sf::IntRect current_rect;
|
sf::IntRect current_rect;
|
||||||
sf::IntRect end_rect;
|
sf::IntRect end_rect;
|
||||||
|
|
||||||
|
bool horizontally_flipped = false;
|
||||||
|
bool vertically_flipped = false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,15 @@ namespace simpleengine {
|
||||||
void AddAnimation(const std::string& animation_name, float speed, int start_frame_x, int start_frame_y, int frame_ct_x,
|
void AddAnimation(const std::string& animation_name, float speed, int start_frame_x, int start_frame_y, int frame_ct_x,
|
||||||
int frame_ct_y, int width, int height);
|
int frame_ct_y, int width, int height);
|
||||||
void AddAnimation(const std::string& animation_name, simpleengine::Animation animation);
|
void AddAnimation(const std::string& animation_name, simpleengine::Animation animation);
|
||||||
|
bool HasAnimation(const std::string& animation_name);
|
||||||
|
|
||||||
|
// Animation class specific
|
||||||
void UpdateAnimation(const std::string& animation_name, const float& delta_time);
|
void UpdateAnimation(const std::string& animation_name, const float& delta_time);
|
||||||
|
Animation& GetAnimation(const std::string& animation_name);
|
||||||
|
/*void FlipAnimationHorizontally(const std::string& animation_name);
|
||||||
|
void FlipAnimationVertically(const std::string& animation_name);
|
||||||
|
bool IsAnimationFlippedHorizontally(const std::string& animation_name);
|
||||||
|
bool IsAnimationFlippedVertically(const std::string& animation_name);*/
|
||||||
|
|
||||||
sf::Sprite &GetSprite() const;
|
sf::Sprite &GetSprite() const;
|
||||||
sf::Texture &GetTextureSheet() const;
|
sf::Texture &GetTextureSheet() const;
|
||||||
|
|
|
@ -20,9 +20,13 @@ namespace simpleengine {
|
||||||
|
|
||||||
void Move(const float& delta_time, const float& dir_x, const float& dir_y);
|
void Move(const float& delta_time, const float& dir_x, const float& dir_y);
|
||||||
void Update(const float& delta_time) override;
|
void Update(const float& delta_time) override;
|
||||||
|
|
||||||
|
const sf::Vector2f& GetVelocity() const;
|
||||||
|
const sf::Vector2f& GetLastDirection() const;
|
||||||
private:
|
private:
|
||||||
float max_velocity;
|
float max_velocity;
|
||||||
sf::Vector2f velocity;
|
sf::Vector2f velocity;
|
||||||
|
sf::Vector2f last_direction;
|
||||||
float acceleration;
|
float acceleration;
|
||||||
float deceleration;
|
float deceleration;
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,6 +17,8 @@ simpleengine::Animation::Animation(sf::Sprite &sprite, sf::Texture &texture_shee
|
||||||
this->sprite.setTexture(texture_sheet, true);
|
this->sprite.setTexture(texture_sheet, true);
|
||||||
this->sprite.setTextureRect(start_rect);
|
this->sprite.setTextureRect(start_rect);
|
||||||
this->current_rect = start_rect;
|
this->current_rect = start_rect;
|
||||||
|
|
||||||
|
sprite.setOrigin({ static_cast<float>(sprite.getLocalBounds().width * .5), 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
void simpleengine::Animation::Update(const float& delta_time) {
|
void simpleengine::Animation::Update(const float& delta_time) {
|
||||||
|
@ -41,3 +43,31 @@ void simpleengine::Animation::Reset() {
|
||||||
timer = 0;
|
timer = 0;
|
||||||
current_rect = start_rect;
|
current_rect = start_rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void simpleengine::Animation::FlipHorizontally() {
|
||||||
|
if (!horizontally_flipped) {
|
||||||
|
sprite.setScale(-1, 1);
|
||||||
|
horizontally_flipped = true;
|
||||||
|
} else {
|
||||||
|
sprite.setScale(1, 1);
|
||||||
|
horizontally_flipped = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::Animation::FlipVertically() {
|
||||||
|
if (!vertically_flipped) {
|
||||||
|
sprite.setScale(1, -1);
|
||||||
|
vertically_flipped = true;
|
||||||
|
} else {
|
||||||
|
sprite.setScale(1, 1);
|
||||||
|
vertically_flipped = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool simpleengine::Animation::IsHorizontallyFlipped() const {
|
||||||
|
return horizontally_flipped;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool simpleengine::Animation::IsVerticallyFlipped() const {
|
||||||
|
return vertically_flipped;
|
||||||
|
}
|
||||||
|
|
|
@ -46,3 +46,54 @@ sf::Texture &simpleengine::AnimationComponent::GetTextureSheet() const {
|
||||||
const std::map<std::string, simpleengine::Animation> &simpleengine::AnimationComponent::GetAnimations() const {
|
const std::map<std::string, simpleengine::Animation> &simpleengine::AnimationComponent::GetAnimations() const {
|
||||||
return animations;
|
return animations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*void simpleengine::AnimationComponent::FlipAnimationHorizontally(const std::string &animation_name) {
|
||||||
|
auto anim = animations.find(animation_name);
|
||||||
|
if (anim != animations.end()) {
|
||||||
|
anim->second.FlipHorizontally();
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::AnimationComponent::FlipAnimationVertically(const std::string &animation_name) {
|
||||||
|
auto anim = animations.find(animation_name);
|
||||||
|
if (anim != animations.end()) {
|
||||||
|
anim->second.FlipVertically();
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::AnimationComponent::IsAnimationFlippedHorizontally(const std::string &animation_name) {
|
||||||
|
auto anim = animations.find(animation_name);
|
||||||
|
if (anim != animations.end()) {
|
||||||
|
return anim->second.IsHorizontallyFlipped();
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void simpleengine::AnimationComponent::IsAnimationFlippedVertically(const std::string &animation_name) {
|
||||||
|
auto anim = animations.find(animation_name);
|
||||||
|
if (anim != animations.end()) {
|
||||||
|
return anim->second.IsHorizontallyFlipped();
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!");
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
simpleengine::Animation& simpleengine::AnimationComponent::GetAnimation(const std::string &animation_name) {
|
||||||
|
auto anim = animations.find(animation_name);
|
||||||
|
if (anim != animations.end()) {
|
||||||
|
return anim->second;
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool simpleengine::AnimationComponent::HasAnimation(const std::string &animation_name) {
|
||||||
|
auto anim = animations.find(animation_name);
|
||||||
|
return anim != animations.end();
|
||||||
|
}
|
|
@ -127,6 +127,7 @@ void simpleengine::MovementComponent::Update(const float &delta_time) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (direction.x != 0 || direction.y != 0) {
|
if (direction.x != 0 || direction.y != 0) {
|
||||||
|
last_direction = direction;
|
||||||
Move(delta_time, direction.x, direction.y);
|
Move(delta_time, direction.x, direction.y);
|
||||||
no_movement = false;
|
no_movement = false;
|
||||||
}
|
}
|
||||||
|
@ -135,3 +136,11 @@ void simpleengine::MovementComponent::Update(const float &delta_time) {
|
||||||
velocity = sf::Vector2f();
|
velocity = sf::Vector2f();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sf::Vector2f &simpleengine::MovementComponent::GetVelocity() const {
|
||||||
|
return velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sf::Vector2f &simpleengine::MovementComponent::GetLastDirection() const {
|
||||||
|
return last_direction;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue