diff --git a/CMakeLists.txt b/CMakeLists.txt index 9911f04..daa7878 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/PlayerController.cpp b/PlayerController.cpp index 10cfa08..d8c5453 100644 --- a/PlayerController.cpp +++ b/PlayerController.cpp @@ -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, std::shared_ptr keyboard, @@ -45,24 +46,26 @@ void PlayerController::update() { std::shared_ptr 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 diff --git a/Shooter.cpp b/Shooter.cpp index 3767d77..6bb8f64 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -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(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(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) { diff --git a/engine/Camera.cpp b/engine/Camera.cpp index af1e542..b26bdf1 100755 --- a/engine/Camera.cpp +++ b/engine/Camera.cpp @@ -4,6 +4,7 @@ #include "Camera.h" #include "utils/Log.h" +#include std::vector &Camera::project(std::shared_ptr mesh) { diff --git a/engine/Engine.cpp b/engine/Engine.cpp index 6330063..86f1064 100755 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -7,6 +7,7 @@ #include #include "ResourceManager.h" #include "physics/Solver.h" +#include "animation/Timeline.h" Engine::Engine() { screen = std::make_shared(); @@ -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); diff --git a/engine/Mesh.h b/engine/Mesh.h index e284cdb..006f121 100755 --- a/engine/Mesh.h +++ b/engine/Mesh.h @@ -7,7 +7,6 @@ #include #include "Triangle.h" -#include "animation/Animatable.h" #include #include "Object.h" diff --git a/engine/Object.h b/engine/Object.h index 49c8aa7..60d338d 100755 --- a/engine/Object.h +++ b/engine/Object.h @@ -8,9 +8,8 @@ #include #include "utils/Point4D.h" #include -#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 diff --git a/engine/World.cpp b/engine/World.cpp index 3a2eef0..c8996a2 100755 --- a/engine/World.cpp +++ b/engine/World.cpp @@ -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); } diff --git a/engine/animation/AColor.h b/engine/animation/AColor.h index 53353ca..fcaaf03 100755 --- a/engine/animation/AColor.h +++ b/engine/animation/AColor.h @@ -5,7 +5,6 @@ #ifndef ENGINE_ACOLOR_H #define ENGINE_ACOLOR_H -#include "Animatable.h" #include "Animation.h" #include "Mesh.h" diff --git a/engine/animation/ARotate.h b/engine/animation/ARotate.h index d5aacff..79ad0de 100755 --- a/engine/animation/ARotate.h +++ b/engine/animation/ARotate.h @@ -5,7 +5,6 @@ #ifndef ENGINE_AROTATE_H #define ENGINE_AROTATE_H -#include "Animatable.h" #include "Animation.h" #include "Object.h" diff --git a/engine/animation/ATranslate.h b/engine/animation/ATranslate.h index 92af632..7f30f49 100755 --- a/engine/animation/ATranslate.h +++ b/engine/animation/ATranslate.h @@ -5,7 +5,6 @@ #ifndef ENGINE_ATRANSLATE_H #define ENGINE_ATRANSLATE_H -#include "Animatable.h" #include "Animation.h" #include "Object.h" diff --git a/engine/animation/ATranslateToPoint.h b/engine/animation/ATranslateToPoint.h index 85ba5bb..7a9b765 100755 --- a/engine/animation/ATranslateToPoint.h +++ b/engine/animation/ATranslateToPoint.h @@ -5,7 +5,6 @@ #ifndef ENGINE_ATRANSLATETOPOINT_H #define ENGINE_ATRANSLATETOPOINT_H -#include "Animatable.h" #include "Animation.h" #include "Object.h" diff --git a/engine/animation/AWait.h b/engine/animation/AWait.h index 6366477..3324c76 100755 --- a/engine/animation/AWait.h +++ b/engine/animation/AWait.h @@ -5,7 +5,6 @@ #ifndef ENGINE_AWAIT_H #define ENGINE_AWAIT_H -#include "Animatable.h" #include "Animation.h" class AWait : public Animation { diff --git a/engine/animation/Animatable.cpp b/engine/animation/Animatable.cpp deleted file mode 100755 index 98a3a5d..0000000 --- a/engine/animation/Animatable.cpp +++ /dev/null @@ -1,96 +0,0 @@ -// -// Created by Иван Ильин on 26.01.2021. -// - -#include "Animatable.h" -#include -#include - -#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 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); -} diff --git a/engine/animation/Animatable.h b/engine/animation/Animatable.h deleted file mode 100755 index 1a0bebd..0000000 --- a/engine/animation/Animatable.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// Created by Иван Ильин on 26.01.2021. -// - -#ifndef ENGINE_ANIMATABLE_H -#define ENGINE_ANIMATABLE_H - -#include -#include "../Triangle.h" - -#include "Animation.h" -#include -#include - -// All _objects in 3dzavr that should be animated must inherit class Animatable: -class Animatable { -protected: - std::map> 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 diff --git a/engine/animation/Animation.cpp b/engine/animation/Animation.cpp index 1adac2b..363eb47 100755 --- a/engine/animation/Animation.cpp +++ b/engine/animation/Animation.cpp @@ -4,9 +4,6 @@ #include "Animation.h" -#include -#include "../utils/Log.h" - bool Animation::updateState() { if(!_started) { _startAnimationPoint = Time::time(); diff --git a/engine/animation/Animation.h b/engine/animation/Animation.h index ac92052..b574018 100755 --- a/engine/animation/Animation.h +++ b/engine/animation/Animation.h @@ -9,8 +9,6 @@ #include "../Triangle.h" #include "Interpolation.h" -class Animatable; - class Animation { public: enum InterpolationType { diff --git a/engine/animation/Timeline.cpp b/engine/animation/Timeline.cpp new file mode 100644 index 0000000..74645d1 --- /dev/null +++ b/engine/animation/Timeline.cpp @@ -0,0 +1,53 @@ +// +// Created by Иван Ильин on 03.10.2021. +// + +#include +#include "Animation.h" + +namespace Timeline { + namespace { + std::map> _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++; + } + } + } +} diff --git a/engine/animation/Timeline.h b/engine/animation/Timeline.h new file mode 100644 index 0000000..bcf86cf --- /dev/null +++ b/engine/animation/Timeline.h @@ -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 diff --git a/engine/network/ServerUDP.cpp b/engine/network/ServerUDP.cpp index 79cc3f3..18990d2 100755 --- a/engine/network/ServerUDP.cpp +++ b/engine/network/ServerUDP.cpp @@ -2,13 +2,12 @@ // Created by Neirokan on 30.04.2020 // -#include -#include #include "ServerUDP.h" #include "utils/Time.h" #include "MsgType.h" #include "config.h" #include "../utils/Log.h" +#include ServerUDP::ServerUDP() : _lastBroadcast(-INFINITY), _working(false) { diff --git a/weapon/Weapon.h b/weapon/Weapon.h index e4efd2b..5510667 100755 --- a/weapon/Weapon.h +++ b/weapon/Weapon.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "Mesh.h" #include "utils/Time.h"