Animation refactoring: now we have 1 common timeline with huge list of animation.

master
Vectozavr 2021-10-03 01:17:03 +07:00
parent 8a0221ed77
commit 73279d1d23
21 changed files with 113 additions and 175 deletions

View File

@ -27,6 +27,10 @@ add_executable(shooter
weapon/Gold_Ak47.h weapon/Gold_Ak47.h
weapon/Rifle.cpp weapon/Rifle.cpp
weapon/Rifle.h weapon/Rifle.h
PlayerController.cpp
PlayerController.h
Shooter.cpp
Shooter.h
# 3d engine: # 3d engine:
engine/utils/Time.h engine/utils/Time.h
engine/utils/Time.cpp engine/utils/Time.cpp
@ -52,10 +56,14 @@ add_executable(shooter
engine/Engine.cpp engine/Engine.cpp
engine/Plane.h engine/Plane.h
engine/Plane.cpp engine/Plane.cpp
engine/animation/Animatable.h engine/Keyboard.cpp
engine/Keyboard.h
engine/Mouse.cpp
engine/Mouse.h
engine/animation/Animation.h engine/animation/Animation.h
engine/animation/Timeline.cpp
engine/animation/Timeline.h
engine/animation/Interpolation.h engine/animation/Interpolation.h
engine/animation/Animatable.cpp
engine/animation/Animation.cpp engine/animation/Animation.cpp
engine/animation/ATranslate.h engine/animation/ATranslate.h
engine/animation/AScale.h engine/animation/AScale.h
@ -86,7 +94,7 @@ add_executable(shooter
engine/network/UDPSocket.h engine/network/UDPSocket.h
engine/network/config.h engine/network/config.h
engine/animation/AFunction.h engine/animation/AFunction.h
PlayerController.cpp PlayerController.h engine/Keyboard.cpp engine/Keyboard.h engine/Mouse.cpp engine/Mouse.h Shooter.cpp Shooter.h) )
if(APPLE OR UNIX) if(APPLE OR UNIX)
include_directories(/usr/local/include) include_directories(/usr/local/include)

View File

@ -9,6 +9,7 @@
#include "animation/AWait.h" #include "animation/AWait.h"
#include "animation/ATranslate.h" #include "animation/ATranslate.h"
#include "animation/ATranslateToPoint.h" #include "animation/ATranslateToPoint.h"
#include "animation/Timeline.h"
PlayerController::PlayerController(std::shared_ptr<Player> player, PlayerController::PlayerController(std::shared_ptr<Player> player,
std::shared_ptr<Keyboard> keyboard, std::shared_ptr<Keyboard> keyboard,
@ -45,24 +46,26 @@ void PlayerController::update() {
std::shared_ptr<Object> camera = _player->attached("camera"); std::shared_ptr<Object> camera = _player->attached("camera");
if(_inRunning) { if(_inRunning) {
if (!camera->isInAnim()) { if (!Timeline::isInAnimList("camera_hor_oscil")) {
camera->animate("hor_oscil", new ATranslate(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::cos)); Timeline::animate("camera_hor_oscil", new ATranslate(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::cos));
camera->animate("hor_oscil", new AWait(0)); Timeline::animate("camera_hor_oscil", new AWait(0));
camera->animate("hor_oscil", new ATranslate(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, Animation::cos)); Timeline::animate("camera_hor_oscil", new ATranslate(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, Animation::cos));
camera->animate("vert_oscil", new ATranslate(camera, -Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::cos)); Timeline::animate("camera_vert_oscil", new ATranslate(camera, -Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::cos));
camera->animate("vert_oscil", new AWait(0)); Timeline::animate("camera_vert_oscil", new AWait(0));
camera->animate("vert_oscil", new ATranslate(camera, Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None,Animation::cos)); Timeline::animate("camera_vert_oscil", new ATranslate(camera, Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None,Animation::cos));
camera->animate("vert_oscil", new AWait(0)); Timeline::animate("camera_vert_oscil", new AWait(0));
camera->animate("vert_oscil", new ATranslate(camera, -Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::cos)); Timeline::animate("camera_vert_oscil", new ATranslate(camera, -Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::cos));
camera->animate("vert_oscil", new AWait(0)); Timeline::animate("camera_vert_oscil", new AWait(0));
camera->animate("vert_oscil", new ATranslate(camera, Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::cos)); Timeline::animate("camera_vert_oscil", new ATranslate(camera, Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::cos));
camera->animate("init", new ATranslateToPoint( camera, _player->position() + Point4D{0, 1.8, 0}, 0.3, Animation::None, Animation::cos)); Timeline::animate("camera_init", new ATranslateToPoint( camera, _player->position() + Point4D{0, 1.8, 0}, 0.3, Animation::None, Animation::cos));
} }
} else if(inRunning_old && !_inRunning) { } else if(inRunning_old && !_inRunning) {
camera->stopAllAnimations(); Timeline::deleteAnimationList("camera_hor_oscil");
camera->animate("init", new ATranslateToPoint( camera, _player->position() + Point4D{0, 1.8, 0}, 0.15, Animation::None, Animation::cos)); Timeline::deleteAnimationList("camera_vert_oscil");
Timeline::deleteAnimationList("camera_init");
Timeline::animate("camera_init", new ATranslateToPoint( camera, _player->position() + Point4D{0, 1.8, 0}, 0.15, Animation::None, Animation::cos));
} }
// Left and right // Left and right

View File

@ -8,6 +8,7 @@
#include "animation/AFunction.h" #include "animation/AFunction.h"
#include "animation/ATranslate.h" #include "animation/ATranslate.h"
#include "animation/ARotate.h" #include "animation/ARotate.h"
#include "animation/Timeline.h"
using namespace std; using namespace std;
@ -241,8 +242,8 @@ void Shooter::addFireTrace(const Point4D &from, const Point4D &to) {
world->addBody(std::make_shared<RigidBody>(Mesh::LineTo(from, to, 0.05)), traceName); world->addBody(std::make_shared<RigidBody>(Mesh::LineTo(from, to, 0.05)), traceName);
world->body(traceName)->setCollider(false); world->body(traceName)->setCollider(false);
world->body(traceName)->animate(traceName + "_fadeOut", new AColor(world->body(traceName), {255, 255, 255, 0})); Timeline::animate(traceName + "_fadeOut", new AColor(world->body(traceName), {255, 255, 255, 0}));
world->body("Player_im")->animate(traceName + "delete", new AFunction([this, traceName](){ deleteFireTrace(traceName); }, 1, 2)); Timeline::animate(traceName + "_delete", new AFunction([this, traceName](){ deleteFireTrace(traceName); }, 1, 2));
} }
void Shooter::deleteFireTrace(const std::string& traceName) { void Shooter::deleteFireTrace(const std::string& traceName) {
@ -253,7 +254,7 @@ void Shooter::addBonus(const string &bonusName, const Point4D &position) {
std::string name = bonusName.substr(6, bonusName.size()-3-5); std::string name = bonusName.substr(6, bonusName.size()-3-5);
world->addBody(std::make_shared<Bonus>(bonusName, "../obj/" + name + ".obj", "../obj/" + name + "_mat.txt", Point4D{3, 3, 3}), bonusName); world->addBody(std::make_shared<Bonus>(bonusName, "../obj/" + name + ".obj", "../obj/" + name + "_mat.txt", Point4D{3, 3, 3}), bonusName);
world->body(bonusName)->translateToPoint(position); world->body(bonusName)->translateToPoint(position);
world->body(bonusName)->animate("a_rotation", new ARotate(world->body(bonusName), Point4D{0, 2*M_PI, 0}, 4, Animation::Continue, Animation::linear)); Timeline::animate(bonusName + "_rotation", new ARotate(world->body(bonusName), Point4D{0, 2*M_PI, 0}, 4, Animation::Continue, Animation::linear));
} }
void Shooter::removeBonus(const string &bonusName) { void Shooter::removeBonus(const string &bonusName) {

View File

@ -4,6 +4,7 @@
#include "Camera.h" #include "Camera.h"
#include "utils/Log.h" #include "utils/Log.h"
#include <cmath>
std::vector<Triangle> &Camera::project(std::shared_ptr<Mesh> mesh) { std::vector<Triangle> &Camera::project(std::shared_ptr<Mesh> mesh) {

View File

@ -7,6 +7,7 @@
#include <iostream> #include <iostream>
#include "ResourceManager.h" #include "ResourceManager.h"
#include "physics/Solver.h" #include "physics/Solver.h"
#include "animation/Timeline.h"
Engine::Engine() { Engine::Engine() {
screen = std::make_shared<Screen>(); screen = std::make_shared<Screen>();
@ -47,9 +48,9 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name,
// hence we can set '_updateWorld' equal to false in setUpdateWorld(bool): // hence we can set '_updateWorld' equal to false in setUpdateWorld(bool):
if(_updateWorld) { if(_updateWorld) {
camera->update_animations(); Timeline::update();
camera->clear();
camera->clear();
world->update(); world->update();
world->projectObjectsInCamera(camera); world->projectObjectsInCamera(camera);

View File

@ -7,7 +7,6 @@
#include <vector> #include <vector>
#include "Triangle.h" #include "Triangle.h"
#include "animation/Animatable.h"
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "Object.h" #include "Object.h"

View File

@ -8,9 +8,8 @@
#include <map> #include <map>
#include "utils/Point4D.h" #include "utils/Point4D.h"
#include <memory> #include <memory>
#include "animation/Animatable.h"
class Object : public Animatable { class Object {
protected: protected:
Point4D _left = Point4D{1, 0, 0, 0}; // internal X Point4D _left = Point4D{1, 0, 0, 0}; // internal X
Point4D _up = Point4D{0, 1, 0, 0}; // internal Y Point4D _up = Point4D{0, 1, 0, 0}; // internal Y

View File

@ -95,7 +95,6 @@ void World::checkCollision(const std::string& body) {
void World::update() { void World::update() {
for (auto &m : _objects) { for (auto &m : _objects) {
m.second->update_animations();
m.second->updatePhysicsState(); m.second->updatePhysicsState();
checkCollision(m.first); checkCollision(m.first);
} }

View File

@ -5,7 +5,6 @@
#ifndef ENGINE_ACOLOR_H #ifndef ENGINE_ACOLOR_H
#define ENGINE_ACOLOR_H #define ENGINE_ACOLOR_H
#include "Animatable.h"
#include "Animation.h" #include "Animation.h"
#include "Mesh.h" #include "Mesh.h"

View File

@ -5,7 +5,6 @@
#ifndef ENGINE_AROTATE_H #ifndef ENGINE_AROTATE_H
#define ENGINE_AROTATE_H #define ENGINE_AROTATE_H
#include "Animatable.h"
#include "Animation.h" #include "Animation.h"
#include "Object.h" #include "Object.h"

View File

@ -5,7 +5,6 @@
#ifndef ENGINE_ATRANSLATE_H #ifndef ENGINE_ATRANSLATE_H
#define ENGINE_ATRANSLATE_H #define ENGINE_ATRANSLATE_H
#include "Animatable.h"
#include "Animation.h" #include "Animation.h"
#include "Object.h" #include "Object.h"

View File

@ -5,7 +5,6 @@
#ifndef ENGINE_ATRANSLATETOPOINT_H #ifndef ENGINE_ATRANSLATETOPOINT_H
#define ENGINE_ATRANSLATETOPOINT_H #define ENGINE_ATRANSLATETOPOINT_H
#include "Animatable.h"
#include "Animation.h" #include "Animation.h"
#include "Object.h" #include "Object.h"

View File

@ -5,7 +5,6 @@
#ifndef ENGINE_AWAIT_H #ifndef ENGINE_AWAIT_H
#define ENGINE_AWAIT_H #define ENGINE_AWAIT_H
#include "Animatable.h"
#include "Animation.h" #include "Animation.h"
class AWait : public Animation { class AWait : public Animation {

View File

@ -1,96 +0,0 @@
//
// Created by Иван Ильин on 26.01.2021.
//
#include "Animatable.h"
#include <iostream>
#include <utility>
#include "ATranslate.h"
#include "ATranslateToPoint.h"
#include "ARotate.h"
#include "AScale.h"
#include "AWait.h"
#include "AFunction.h"
#include "AColor.h"
/*
void Animatable::a_translate(const std::string& listName,
const Point4D &t,
double duration,
Animation::LoopOut looped,
Animation::InterpolationType interpolationType) {
animations[listName].emplace_back(new ATranslate(t, duration, looped, interpolationType));
}
void Animatable::a_translateToPoint(const std::string& listName,
const Point4D &point,
double duration,
Animation::LoopOut looped,
Animation::InterpolationType interpolationType) {
animations[listName].emplace_back(new ATranslateToPoint(point, duration, looped, interpolationType));
}
void Animatable::a_rotate(c
const Point4D &r,
double duration,
Animation::LoopOut looped,
Animation::InterpolationType interpolationType) {
animations[listName].emplace_back(new ARotate(r, duration, looped, interpolationType));
}
void Animatable::a_scale(const std::string& listName,
const Point4D &s,
double duration,
Animation::LoopOut looped,
Animation::InterpolationType interpolationType) {
animations[listName].emplace_back(new AScale(s, duration, looped, interpolationType));
}
void Animatable::a_color(const std::string &listName, const sf::Color &color, double duration, Animation::LoopOut looped,
Animation::InterpolationType interpolationType) {
animations[listName].emplace_back(new AColor(color, duration, looped, interpolationType));
}
void Animatable::a_wait(const std::string& listName, double duration) {
animations[listName].emplace_back(new AWait(duration));
}
void Animatable::a_function(const std::string &listName,
std::function<void()> function,
int calls,
double duration,
Animation::LoopOut looped,
Animation::InterpolationType interpolationType) {
animations[listName].emplace_back(new AFunction(std::move(function), calls, duration, looped, interpolationType));
}
*/
void Animatable::update_animations() {
for (auto& [listName, animationList] : animations) {
if (animationList.empty())
continue;
auto it = animationList.begin();
// If it the front animation is 'a_wait()' we should wait until waiting time is over
if (it.operator*()->waitFor()) {
if (!it.operator*()->update())
animationList.erase(it);
continue;
}
// Otherwise we iterate over all animation until we meet animations.end() or wait animation
while (!animationList.empty() && (it != animationList.end()) && (!it.operator*()->waitFor())) {
if (!it.operator*()->update())
animationList.erase(it++);
else
it++;
}
}
}
void Animatable::animate(const std::string &listName, Animation* anim) {
animations[listName].emplace_back(anim);
}

View File

@ -1,41 +0,0 @@
//
// Created by Иван Ильин on 26.01.2021.
//
#ifndef ENGINE_ANIMATABLE_H
#define ENGINE_ANIMATABLE_H
#include <list>
#include "../Triangle.h"
#include "Animation.h"
#include <iostream>
#include <functional>
// All _objects in 3dzavr that should be animated must inherit class Animatable:
class Animatable {
protected:
std::map<std::string, std::list<Animation*>> animations;
public:
Animatable() = default;
virtual ~Animatable() = default;
void animate(const std::string& listName, Animation* anim);
void update_animations();
void stopAllAnimations() { animations.clear(); }
void stopAnimationList(const std::string& name) { animations[name].clear(); }
[[nodiscard]] bool isInAnim() const {
for(auto& animList : animations)
if (!animList.second.empty())
return true;
return false;
}
[[nodiscard]] bool isInAnimList(const std::string& name) { return !animations[name].empty(); }
};
#endif //INC_3DZAVR_ANIMATABLE_H

View File

@ -4,9 +4,6 @@
#include "Animation.h" #include "Animation.h"
#include <utility>
#include "../utils/Log.h"
bool Animation::updateState() { bool Animation::updateState() {
if(!_started) { if(!_started) {
_startAnimationPoint = Time::time(); _startAnimationPoint = Time::time();

View File

@ -9,8 +9,6 @@
#include "../Triangle.h" #include "../Triangle.h"
#include "Interpolation.h" #include "Interpolation.h"
class Animatable;
class Animation { class Animation {
public: public:
enum InterpolationType { enum InterpolationType {

View File

@ -0,0 +1,53 @@
//
// Created by Иван Ильин on 03.10.2021.
//
#include <list>
#include "Animation.h"
namespace Timeline {
namespace {
std::map<std::string, std::list<Animation*>> _animations;
}
void animate(const std::string& listName, Animation* anim) {
_animations[listName].emplace_back(anim);
}
void deleteAllAnimations() {
_animations.clear();
}
void deleteAnimationList(const std::string& listName) {
_animations[listName].clear();
_animations.erase(listName);
}
[[nodiscard]] bool isInAnimList(const std::string& name) {
return !_animations[name].empty();
}
void update() {
for (auto& [listName, animationList] : _animations) {
if (animationList.empty())
continue;
auto it = animationList.begin();
// If it the front animation is 'a_wait()' we should wait until waiting time is over
if (it.operator*()->waitFor()) {
if (!it.operator*()->update())
animationList.erase(it);
continue;
}
// Otherwise we iterate over all animation until we meet animations.end() or wait animation
while (!animationList.empty() && (it != animationList.end()) && (!it.operator*()->waitFor())) {
if (!it.operator*()->update())
animationList.erase(it++);
else
it++;
}
}
}
}

View File

@ -0,0 +1,21 @@
//
// Created by Иван Ильин on 03.10.2021.
//
#ifndef SHOOTER_TIMELINE_H
#define SHOOTER_TIMELINE_H
#include "Animation.h"
namespace Timeline {
// TODO: replace Animation* anim on shared ptr or Animation with std::move()
void update();
void animate(const std::string& listName, Animation* anim);
void deleteAllAnimations();
void deleteAnimationList(const std::string& listName);
[[nodiscard]] bool isInAnimList(const std::string& name);
}
#endif //SHOOTER_TIMELINE_H

View File

@ -2,13 +2,12 @@
// Created by Neirokan on 30.04.2020 // Created by Neirokan on 30.04.2020
// //
#include <fstream>
#include <sstream>
#include "ServerUDP.h" #include "ServerUDP.h"
#include "utils/Time.h" #include "utils/Time.h"
#include "MsgType.h" #include "MsgType.h"
#include "config.h" #include "config.h"
#include "../utils/Log.h" #include "../utils/Log.h"
#include <cmath>
ServerUDP::ServerUDP() : _lastBroadcast(-INFINITY), _working(false) ServerUDP::ServerUDP() : _lastBroadcast(-INFINITY), _working(false)
{ {

View File

@ -10,6 +10,7 @@
#include <World.h> #include <World.h>
#include <Camera.h> #include <Camera.h>
#include <SFML/Audio/Sound.hpp> #include <SFML/Audio/Sound.hpp>
#include <cmath>
#include "Mesh.h" #include "Mesh.h"
#include "utils/Time.h" #include "utils/Time.h"