Add some getters and a way to flip an animation
This commit is contained in:
parent
dd919bf89e
commit
7ae000c4ac
|
@ -15,12 +15,17 @@ namespace simpleengine {
|
|||
Animation(sf::Sprite &sprite, sf::Texture &texture_sheet, float speed, int start_frame_x,
|
||||
int start_frame_y, int frame_ct_x, int frame_ct_y, int width, int height);
|
||||
|
||||
void Update(const float &delta_time);
|
||||
void Update(const float& delta_time);
|
||||
void Reset();
|
||||
|
||||
void FlipHorizontally();
|
||||
void FlipVertically();
|
||||
|
||||
bool IsHorizontallyFlipped() const;
|
||||
bool IsVerticallyFlipped() const;
|
||||
|
||||
/*void Play();
|
||||
void Pause();*/
|
||||
|
||||
private:
|
||||
sf::Sprite &sprite;
|
||||
sf::Texture &texture_sheet;
|
||||
|
@ -32,6 +37,8 @@ namespace simpleengine {
|
|||
sf::IntRect current_rect;
|
||||
sf::IntRect end_rect;
|
||||
|
||||
bool horizontally_flipped = false;
|
||||
bool vertically_flipped = false;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -25,12 +25,20 @@ namespace simpleengine {
|
|||
public:
|
||||
explicit AnimationComponent(Entity& owning_entity, sf::Sprite& sprite, sf::Texture& texture_sheet);
|
||||
|
||||
void Update(const float &delta_time) override;
|
||||
void Update(const float& delta_time) override;
|
||||
|
||||
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);
|
||||
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);
|
||||
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::Texture &GetTextureSheet() const;
|
||||
|
|
|
@ -18,11 +18,15 @@ namespace simpleengine {
|
|||
public:
|
||||
MovementComponent(Entity& owning_entity, float max_velocity, float acceleration = 7, float deceleration = 2.5);
|
||||
|
||||
void Move(const float &delta_time, const float& dir_x, const float& dir_y);
|
||||
void Update(const float &delta_time) override;
|
||||
void Move(const float& delta_time, const float& dir_x, const float& dir_y);
|
||||
void Update(const float& delta_time) override;
|
||||
|
||||
const sf::Vector2f& GetVelocity() const;
|
||||
const sf::Vector2f& GetLastDirection() const;
|
||||
private:
|
||||
float max_velocity;
|
||||
sf::Vector2f velocity;
|
||||
sf::Vector2f last_direction;
|
||||
float acceleration;
|
||||
float deceleration;
|
||||
};
|
||||
|
|
|
@ -17,9 +17,11 @@ simpleengine::Animation::Animation(sf::Sprite &sprite, sf::Texture &texture_shee
|
|||
this->sprite.setTexture(texture_sheet, true);
|
||||
this->sprite.setTextureRect(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) {
|
||||
// Update timer
|
||||
timer += 100 * delta_time;
|
||||
if (timer >= speed) {
|
||||
|
@ -41,3 +43,31 @@ void simpleengine::Animation::Reset() {
|
|||
timer = 0;
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ simpleengine::AnimationComponent::AnimationComponent(Entity& owning_entity, sf::
|
|||
|
||||
}
|
||||
|
||||
void simpleengine::AnimationComponent::Update(const float &delta_time) {
|
||||
void simpleengine::AnimationComponent::Update(const float& delta_time) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ void simpleengine::AnimationComponent::AddAnimation(const std::string& animation
|
|||
animations.emplace(animation_name, animation);
|
||||
}
|
||||
|
||||
void simpleengine::AnimationComponent::UpdateAnimation(const std::string &animation_name, const float &delta_time) {
|
||||
void simpleengine::AnimationComponent::UpdateAnimation(const std::string &animation_name, const float& delta_time) {
|
||||
auto anim = animations.find(animation_name);
|
||||
if (anim != animations.end()) {
|
||||
anim->second.Update(delta_time);
|
||||
|
@ -46,3 +46,54 @@ sf::Texture &simpleengine::AnimationComponent::GetTextureSheet() const {
|
|||
const std::map<std::string, simpleengine::Animation> &simpleengine::AnimationComponent::GetAnimations() const {
|
||||
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();
|
||||
}
|
|
@ -15,7 +15,7 @@ simpleengine::MovementComponent::MovementComponent(Entity& owning_entity, float
|
|||
|
||||
}
|
||||
|
||||
void simpleengine::MovementComponent::Move(const float &delta_time, const float& dir_x, const float& dir_y) {
|
||||
void simpleengine::MovementComponent::Move(const float& delta_time, const float& dir_x, const float& dir_y) {
|
||||
// Acceleration
|
||||
velocity.x += acceleration * dir_x;
|
||||
// Limit the horizontal velocity
|
||||
|
@ -46,7 +46,7 @@ void simpleengine::MovementComponent::Move(const float &delta_time, const float&
|
|||
owning_entity.Move(velocity * delta_time);
|
||||
}
|
||||
|
||||
void simpleengine::MovementComponent::Update(const float &delta_time) {
|
||||
void simpleengine::MovementComponent::Update(const float& delta_time) {
|
||||
sf::Vector2f direction;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
||||
direction.x = -1;
|
||||
|
@ -127,6 +127,7 @@ void simpleengine::MovementComponent::Update(const float &delta_time) {
|
|||
}
|
||||
|
||||
if (direction.x != 0 || direction.y != 0) {
|
||||
last_direction = direction;
|
||||
Move(delta_time, direction.x, direction.y);
|
||||
no_movement = false;
|
||||
}
|
||||
|
@ -135,3 +136,11 @@ void simpleengine::MovementComponent::Update(const float &delta_time) {
|
|||
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