Skip to content

Commit

Permalink
Добавил возможность включения и выключения зацикливания анимации
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo506 committed Jan 29, 2022
1 parent 1861427 commit 64e79f9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
Binary file modified Assets/ResultVideo/result.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 20 additions & 10 deletions animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace animation
{
AnimationClip::AnimationClip(std::string path, sf::Sprite* sprite, sf::IntRect& rect, int countOfSprites) : m_lostTime(0), m_current(0), m_animSpeed(1), m_pause(false)
AnimationClip::AnimationClip(std::string path, sf::Sprite* sprite, sf::IntRect& rect, int countOfSprites) : m_lostTime(0), m_current(0), m_animSpeed(1), m_pause(false), m_looped(true)
{
m_numberOfSprites = countOfSprites;

Expand Down Expand Up @@ -43,6 +43,7 @@ namespace animation
m_animSpeed = 1;
m_currentSprite = sprite;
m_pause = false;
m_looped = true;
}


Expand All @@ -52,6 +53,7 @@ namespace animation
m_numberOfSprites = animClip.m_numberOfSprites;
m_current = animClip.m_current;
m_lostTime = animClip.m_lostTime;
m_looped = animClip.m_looped;

// Копирование загруженных текстур
for (int i = 0; i < m_numberOfSprites; i++)
Expand All @@ -73,6 +75,7 @@ namespace animation
m_numberOfSprites = animClip.m_numberOfSprites;
m_current = animClip.m_current;
m_lostTime = animClip.m_lostTime;
m_looped = animClip.m_looped;

// Чистим уже загруженные текстуры
for (int i = 0; i < m_textures.size(); i++)
Expand Down Expand Up @@ -126,18 +129,25 @@ namespace animation
{
if (m_animTimes.size() != 0 && !m_pause)
{

m_lostTime += delta;
if (m_lostTime >= m_animTimes[m_current] / m_animSpeed)
if (m_current < m_numberOfSprites)
{
m_current++;
if (m_current >= m_numberOfSprites)
m_lostTime += delta;
if (m_lostTime >= m_animTimes[m_current] / m_animSpeed)
{
m_current = 0;
m_lostTime = 0;
m_current++;
if (m_current >= m_numberOfSprites)
{
if (m_looped)
{
m_current = 0;
m_lostTime = 0;
} else
return;

}

m_currentSprite->setTexture(*m_textures[m_current]);
}

m_currentSprite->setTexture(*m_textures[m_current]);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace animation
int m_numberOfSprites; // Количество спрайтов в анимации
float m_animSpeed; // Скорость анимации
bool m_pause; // Состояние анимации (на паузе или нет)
bool m_looped; // Зацикливать анимацию?

void UpdateAnimTimes(); // Обновляет время смены анимации при изменении скорости

Expand Down Expand Up @@ -68,6 +69,14 @@ namespace animation

// Пауза анимации
void Pause(bool pause) { m_pause = pause; }

// Получение текущего спрайта
const sf::Texture* GetTexture(int index) const { return m_textures[index]; }

void SetLoop(bool loop) { m_looped = loop; }

void Restart() { m_current = 0; m_lostTime = 0; }

};
}

Expand Down
13 changes: 10 additions & 3 deletions animator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "imgui.h"
#include "imgui-SFML.h"
#include "animator.h"
#include <cstring>

namespace animation
{
Expand All @@ -11,7 +12,6 @@ namespace animation
m_clip = clip;
m_animationIsSet = true;
m_speed = clip->AnimSpeed();
m_path = "";
m_rect = sf::IntRect(0,0,0,0);
m_countOfSprites = clip->NumberOfSprites();
ImGui::SFML::Init(*m_window);
Expand All @@ -23,7 +23,6 @@ namespace animation
m_clip = NULL;
m_animationIsSet = false;
m_speed = 0;
m_path = "";
m_rect = sf::IntRect(0,0,0,0);
m_countOfSprites = 0;
ImGui::SFML::Init(*m_window);
Expand All @@ -50,7 +49,6 @@ namespace animation
m_clip = clip;
m_animationIsSet = true;
m_speed = clip->AnimSpeed();
m_path = "";
m_rect = sf::IntRect(0,0,0,0);
m_countOfSprites = clip->NumberOfSprites();
ImGui::SFML::Init(*m_window);
Expand All @@ -68,12 +66,21 @@ namespace animation
if (ImGui::DragFloat("Speed", &m_speed, 0.1f, 0.0f, 5.0f))
m_clip->SetAnimSpeed(m_speed);

if (ImGui::Button("Start"))
m_clip->Restart();
ImGui::SameLine();

if (ImGui::Button("Pause"))
m_clip->Pause(true);

ImGui::SameLine();
if (ImGui::Button("Unpause"))
m_clip->Pause(false);

static bool loop = true;
if (ImGui::Checkbox("Loop", &loop))
m_clip->SetLoop(loop);

ImGui::End();
}

Expand Down
2 changes: 1 addition & 1 deletion animator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace animation
sf::RenderWindow *m_window; // Игровое окно, к которому будет привязан UI инструмента
AnimationClip *m_clip; // Анимация, над которой ведётся работа
float m_speed; // Скорость анимации
std::string m_path; // Путь к файлу со спрайтами
sf::IntRect m_rect; // Размеры и координаты первого спрайта
int m_countOfSprites; // Кол-во спрайтов в анимации
bool m_animationIsSet; // Установлена ли анимация?
bool m_loop;

public:
// Конструктор аниматора
Expand Down
4 changes: 2 additions & 2 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Size=405,127
Collapsed=0

[Window][Animation Controller]
Pos=311,86
Size=324,151
Pos=211,33
Size=475,156
Collapsed=0

0 comments on commit 64e79f9

Please sign in to comment.