Animation refactoring: now we have 1 common timeline with huge list of animation.
parent
8a0221ed77
commit
73279d1d23
|
@ -27,6 +27,10 @@ add_executable(shooter
|
|||
weapon/Gold_Ak47.h
|
||||
weapon/Rifle.cpp
|
||||
weapon/Rifle.h
|
||||
PlayerController.cpp
|
||||
PlayerController.h
|
||||
Shooter.cpp
|
||||
Shooter.h
|
||||
# 3d engine:
|
||||
engine/utils/Time.h
|
||||
engine/utils/Time.cpp
|
||||
|
@ -52,10 +56,14 @@ add_executable(shooter
|
|||
engine/Engine.cpp
|
||||
engine/Plane.h
|
||||
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/Timeline.cpp
|
||||
engine/animation/Timeline.h
|
||||
engine/animation/Interpolation.h
|
||||
engine/animation/Animatable.cpp
|
||||
engine/animation/Animation.cpp
|
||||
engine/animation/ATranslate.h
|
||||
engine/animation/AScale.h
|
||||
|
@ -86,7 +94,7 @@ add_executable(shooter
|
|||
engine/network/UDPSocket.h
|
||||
engine/network/config.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)
|
||||
include_directories(/usr/local/include)
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "animation/AWait.h"
|
||||
#include "animation/ATranslate.h"
|
||||
#include "animation/ATranslateToPoint.h"
|
||||
#include "animation/Timeline.h"
|
||||
|
||||
PlayerController::PlayerController(std::shared_ptr<Player> player,
|
||||
std::shared_ptr<Keyboard> keyboard,
|
||||
|
@ -45,24 +46,26 @@ void PlayerController::update() {
|
|||
|
||||
std::shared_ptr<Object> camera = _player->attached("camera");
|
||||
if(_inRunning) {
|
||||
if (!camera->isInAnim()) {
|
||||
camera->animate("hor_oscil", new ATranslate(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::cos));
|
||||
camera->animate("hor_oscil", new AWait(0));
|
||||
camera->animate("hor_oscil", new ATranslate(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, Animation::cos));
|
||||
if (!Timeline::isInAnimList("camera_hor_oscil")) {
|
||||
Timeline::animate("camera_hor_oscil", new ATranslate(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::cos));
|
||||
Timeline::animate("camera_hor_oscil", new AWait(0));
|
||||
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));
|
||||
camera->animate("vert_oscil", new AWait(0));
|
||||
camera->animate("vert_oscil", new ATranslate(camera, Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None,Animation::cos));
|
||||
camera->animate("vert_oscil", new AWait(0));
|
||||
camera->animate("vert_oscil", new ATranslate(camera, -Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::cos));
|
||||
camera->animate("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));
|
||||
Timeline::animate("camera_vert_oscil", new AWait(0));
|
||||
Timeline::animate("camera_vert_oscil", new ATranslate(camera, Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None,Animation::cos));
|
||||
Timeline::animate("camera_vert_oscil", new AWait(0));
|
||||
Timeline::animate("camera_vert_oscil", new ATranslate(camera, -Point4D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::cos));
|
||||
Timeline::animate("camera_vert_oscil", new AWait(0));
|
||||
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) {
|
||||
camera->stopAllAnimations();
|
||||
camera->animate("init", new ATranslateToPoint( camera, _player->position() + Point4D{0, 1.8, 0}, 0.15, Animation::None, Animation::cos));
|
||||
Timeline::deleteAnimationList("camera_hor_oscil");
|
||||
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
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "animation/AFunction.h"
|
||||
#include "animation/ATranslate.h"
|
||||
#include "animation/ARotate.h"
|
||||
#include "animation/Timeline.h"
|
||||
|
||||
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->body(traceName)->setCollider(false);
|
||||
|
||||
world->body(traceName)->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 + "_fadeOut", new AColor(world->body(traceName), {255, 255, 255, 0}));
|
||||
Timeline::animate(traceName + "_delete", new AFunction([this, traceName](){ deleteFireTrace(traceName); }, 1, 2));
|
||||
}
|
||||
|
||||
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);
|
||||
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)->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) {
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "Camera.h"
|
||||
#include "utils/Log.h"
|
||||
#include <cmath>
|
||||
|
||||
std::vector<Triangle> &Camera::project(std::shared_ptr<Mesh> mesh) {
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <iostream>
|
||||
#include "ResourceManager.h"
|
||||
#include "physics/Solver.h"
|
||||
#include "animation/Timeline.h"
|
||||
|
||||
Engine::Engine() {
|
||||
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):
|
||||
if(_updateWorld) {
|
||||
|
||||
camera->update_animations();
|
||||
camera->clear();
|
||||
Timeline::update();
|
||||
|
||||
camera->clear();
|
||||
world->update();
|
||||
world->projectObjectsInCamera(camera);
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#include <vector>
|
||||
#include "Triangle.h"
|
||||
#include "animation/Animatable.h"
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "Object.h"
|
||||
|
||||
|
|
|
@ -8,9 +8,8 @@
|
|||
#include <map>
|
||||
#include "utils/Point4D.h"
|
||||
#include <memory>
|
||||
#include "animation/Animatable.h"
|
||||
|
||||
class Object : public Animatable {
|
||||
class Object {
|
||||
protected:
|
||||
Point4D _left = Point4D{1, 0, 0, 0}; // internal X
|
||||
Point4D _up = Point4D{0, 1, 0, 0}; // internal Y
|
||||
|
|
|
@ -95,7 +95,6 @@ void World::checkCollision(const std::string& body) {
|
|||
|
||||
void World::update() {
|
||||
for (auto &m : _objects) {
|
||||
m.second->update_animations();
|
||||
m.second->updatePhysicsState();
|
||||
checkCollision(m.first);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#ifndef ENGINE_ACOLOR_H
|
||||
#define ENGINE_ACOLOR_H
|
||||
|
||||
#include "Animatable.h"
|
||||
#include "Animation.h"
|
||||
#include "Mesh.h"
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#ifndef ENGINE_AROTATE_H
|
||||
#define ENGINE_AROTATE_H
|
||||
|
||||
#include "Animatable.h"
|
||||
#include "Animation.h"
|
||||
#include "Object.h"
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#ifndef ENGINE_ATRANSLATE_H
|
||||
#define ENGINE_ATRANSLATE_H
|
||||
|
||||
#include "Animatable.h"
|
||||
#include "Animation.h"
|
||||
#include "Object.h"
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#ifndef ENGINE_ATRANSLATETOPOINT_H
|
||||
#define ENGINE_ATRANSLATETOPOINT_H
|
||||
|
||||
#include "Animatable.h"
|
||||
#include "Animation.h"
|
||||
#include "Object.h"
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#ifndef ENGINE_AWAIT_H
|
||||
#define ENGINE_AWAIT_H
|
||||
|
||||
#include "Animatable.h"
|
||||
#include "Animation.h"
|
||||
|
||||
class AWait : public Animation {
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -4,9 +4,6 @@
|
|||
|
||||
#include "Animation.h"
|
||||
|
||||
#include <utility>
|
||||
#include "../utils/Log.h"
|
||||
|
||||
bool Animation::updateState() {
|
||||
if(!_started) {
|
||||
_startAnimationPoint = Time::time();
|
||||
|
|
|
@ -9,8 +9,6 @@
|
|||
#include "../Triangle.h"
|
||||
#include "Interpolation.h"
|
||||
|
||||
class Animatable;
|
||||
|
||||
class Animation {
|
||||
public:
|
||||
enum InterpolationType {
|
||||
|
|
|
@ -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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -2,13 +2,12 @@
|
|||
// Created by Neirokan on 30.04.2020
|
||||
//
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "ServerUDP.h"
|
||||
#include "utils/Time.h"
|
||||
#include "MsgType.h"
|
||||
#include "config.h"
|
||||
#include "../utils/Log.h"
|
||||
#include <cmath>
|
||||
|
||||
ServerUDP::ServerUDP() : _lastBroadcast(-INFINITY), _working(false)
|
||||
{
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <World.h>
|
||||
#include <Camera.h>
|
||||
#include <SFML/Audio/Sound.hpp>
|
||||
#include <cmath>
|
||||
#include "Mesh.h"
|
||||
#include "utils/Time.h"
|
||||
|
||||
|
|
Loading…
Reference in New Issue