From 71be55ee58c8a1ffcbc61dd53ad6b440ef88a91d Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Mon, 1 Nov 2021 21:08:36 +0700 Subject: [PATCH] add reloading animation --- CMakeLists.txt | 2 +- Player.cpp | 14 +++++++++++++- engine/animation/ARotateLeft.h | 33 +++++++++++++++++++++++++++++++++ shooter.vcxproj | 1 + shooter.vcxproj.filters | 3 +++ weapon/Weapon.cpp | 5 +++++ weapon/Weapon.h | 4 ++++ 7 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 engine/animation/ARotateLeft.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 747f985..81123c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,7 +91,7 @@ add_executable(shooter engine/network/UDPConnection.h engine/network/UDPSocket.cpp engine/network/UDPSocket.h - engine/SoundController.cpp engine/SoundController.h ShooterMsgType.h ShooterMsgType.cpp engine/animation/AAttractToPoint.h engine/animation/ARotateRelativePoint.h) + engine/SoundController.cpp engine/SoundController.h ShooterMsgType.h ShooterMsgType.cpp engine/animation/AAttractToPoint.h engine/animation/ARotateRelativePoint.h engine/animation/ARotateLeft.h) if(APPLE OR UNIX) include_directories(/usr/local/include) diff --git a/Player.cpp b/Player.cpp index 3666a0d..9c8ca55 100644 --- a/Player.cpp +++ b/Player.cpp @@ -6,6 +6,9 @@ #include "engine/Screen.h" #include "engine/utils/Log.h" +#include "engine/animation/Timeline.h" +#include "engine/animation/ARotateLeft.h" + Player::Player(ObjectNameTag name) : RigidBody(name) { loadObj(ShooterConsts::CUBE_OBJ, Vec3D{0.5, 1.9, 0.5}); setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0}); @@ -79,6 +82,15 @@ void Player::addWeapon(std::shared_ptr weapon) { _weapons.back()->rotateRelativePoint(position() + Vec3D{0, 1.8, 0}, Vec3D{0, 1, 0}, angle().y()); _weapons.back()->rotateRelativePoint(position() + Vec3D{0, 1.8, 0}, left(), headAngle()); + // add animation of reloading + _weapons.back()->setReloadCallBack([this]() { + Timeline::animate(AnimationListTag("reload_weapon"), + std::make_shared(_weapons[_selectedWeapon], + 2 * Consts::PI, + _weapons[_selectedWeapon]->reloadTime() / 2)); + }); + + // add call back function to create fire traces if (_addTraceCallBack != nullptr) { _weapons.back()->setAddTraceCallBack(_addTraceCallBack); } @@ -110,7 +122,7 @@ void Player::selectNextWeapon() { if (_removeWeaponCallBack != nullptr) { _removeWeaponCallBack(_weapons[_selectedWeapon]); } - + _selectedWeapon = (_selectedWeapon + 1) % _weapons.size(); if (_addWeaponCallBack != nullptr) { diff --git a/engine/animation/ARotateLeft.h b/engine/animation/ARotateLeft.h new file mode 100644 index 0000000..a0a6297 --- /dev/null +++ b/engine/animation/ARotateLeft.h @@ -0,0 +1,33 @@ +// +// Created by Иван Ильин on 01.11.2021. +// + +#ifndef SHOOTER_AROTATELEFT_H +#define SHOOTER_AROTATELEFT_H + +#include + +#include "Animation.h" +#include "../Object.h" + +class ARotateLeft final : public Animation { +private: + const std::weak_ptr _object; + const double _rotationValue; + + void update() override { + if (_object.expired()) { + stop(); + return; + } + + _object.lock()->rotateLeft(_rotationValue * dprogress()); + } + +public: + ARotateLeft(std::weak_ptr object, double r, double duration = 1, LoopOut looped = LoopOut::None, + InterpolationType interpolationType = InterpolationType::Bezier) + : Animation(duration, looped, interpolationType), _object(object), _rotationValue(r) {} +}; + +#endif //SHOOTER_AROTATELEFT_H diff --git a/shooter.vcxproj b/shooter.vcxproj index d3317a7..bb79fef 100644 --- a/shooter.vcxproj +++ b/shooter.vcxproj @@ -208,6 +208,7 @@ + diff --git a/shooter.vcxproj.filters b/shooter.vcxproj.filters index e05e70c..1a17ff8 100644 --- a/shooter.vcxproj.filters +++ b/shooter.vcxproj.filters @@ -221,6 +221,9 @@ Файлы заголовков\engine\animation + + Файлы заголовков\engine\animation + Файлы заголовков\weapon diff --git a/weapon/Weapon.cpp b/weapon/Weapon.cpp index c78ca35..3ac5998 100644 --- a/weapon/Weapon.cpp +++ b/weapon/Weapon.cpp @@ -3,6 +3,7 @@ // #include + #include "Weapon.h" #include "../engine/ResourceManager.h" #include "../engine/utils/Log.h" @@ -70,6 +71,10 @@ void Weapon::reload() { SoundController::playSound(SoundTag("reloadSound_" + name().str()), _reloadSound); Log::log("Weapon::reload (" + std::to_string(_stockAmmo) + " : " + std::to_string(_clipAmmo) + ")"); _lastReloadTime = Time::time(); + + if(_reloadCallBack != nullptr) { + _reloadCallBack(); + } } std::map diff --git a/weapon/Weapon.h b/weapon/Weapon.h index 07ffafe..c7537ba 100644 --- a/weapon/Weapon.h +++ b/weapon/Weapon.h @@ -38,6 +38,7 @@ private: double _lastReloadTime = std::numeric_limits::min(); std::function _addTraceCallBack; + std::function _reloadCallBack; protected: std::map @@ -58,10 +59,13 @@ public: const Vec3D &direction); void reload(); + [[nodiscard]] double reloadTime() const { return _reloadTime; } [[nodiscard]] std::pair balance() const { return std::make_pair(_clipAmmo, _stockAmmo); } void setAddTraceCallBack(std::function add) { _addTraceCallBack = std::move(add); } + void setReloadCallBack(std::function reload) { _reloadCallBack = std::move(reload); } + void addAmmo(int ammoAdd) { _stockAmmo += ammoAdd; }