From 8818a73fcb885243b6ee5a36b36303f53462d1cd Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sat, 27 May 2023 18:14:17 +0300 Subject: [PATCH] Added EventHandler, bug fixes with chat and with Error messages --- 3dzavr | 2 +- CMakeLists.txt | 3 +- Shooter.cpp | 97 ++++++++++++++++++++------------ network/Chat.cpp | 1 + network/ShooterClient.cpp | 102 ++++++++++++++++----------------- network/ShooterClient.h | 40 ++++--------- player/Player.cpp | 109 +++++++++++++++++------------------- player/Player.h | 36 +++--------- player/PlayerController.cpp | 2 + weapon/Weapon.cpp | 12 ++-- weapon/Weapon.h | 9 --- 11 files changed, 190 insertions(+), 223 deletions(-) diff --git a/3dzavr b/3dzavr index 5217578..1a01de8 160000 --- a/3dzavr +++ b/3dzavr @@ -1 +1 @@ -Subproject commit 5217578e1500cbe567ddf13a44fffd60e3ad445c +Subproject commit 1a01de8d9fbb9a6f27e3662f17c24358e2bfc699 diff --git a/CMakeLists.txt b/CMakeLists.txt index d51135b..00c27d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,7 +112,8 @@ add_executable(${CMAKE_PROJECT_NAME} 3dzavr/engine/network/UDPConnection.h 3dzavr/engine/network/UDPSocket.cpp 3dzavr/engine/network/UDPSocket.h - ) + 3dzavr/engine/utils/EventHandler.cpp + 3dzavr/engine/utils/EventHandler.h) if(APPLE OR UNIX) include_directories(/usr/local/include) diff --git a/Shooter.cpp b/Shooter.cpp index b6b116e..469c432 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -9,6 +9,8 @@ #include "ShooterConsts.h" #include "3dzavr/engine/io/SoundController.h" #include "network/Chat.h" +#include "3dzavr/engine/utils/EventHandler.h" +#include using namespace std; // Read server/client settings and start both. @@ -55,17 +57,6 @@ void Shooter::initNetwork() { client->requestMap(clientIp, ¤t_map); client->connect(clientIp, clientPort); player->setPlayerNickName(playerName); - - // TODO: encapsulate call backs inside ShooterClient - client->setSpawnPlayerCallBack([this](sf::Uint16 id) { spawnPlayer(id); }); - client->setRemovePlayerCallBack([this](sf::Uint16 id) { removePlayer(id); }); - client->setAddFireTraceCallBack([this](const Vec3D &from, const Vec3D &to) { addFireTrace(from, to); }); - client->setAddBonusCallBack( - [this](const std::string &bonusName, const Vec3D &position) { addBonus(bonusName, position); }); - client->setRemoveBonusCallBack([this](const ObjectNameTag &bonusName) { removeBonus(bonusName); }); - client->setChangeEnemyWeaponCallBack( - [this](const std::string &weaponName, sf::Uint16 id) { changeEnemyWeapon(weaponName, id); }); - } void Shooter::start() { @@ -79,20 +70,54 @@ void Shooter::start() { world->loadMap(current_map, Vec3D{5, 5, 5}); - // TODO: encapsulate call backs inside Player - player->setAddTraceCallBack([this](const Vec3D &from, const Vec3D &to) { - client->addTrace(from, to); - addFireTrace(from, to); - }); - player->setDamagePlayerCallBack( - [this](sf::Uint16 targetId, double damage) { client->damagePlayer(targetId, damage); }); - player->setRayCastFunction( - [this](const Vec3D &from, const Vec3D &to) { return world->rayCast(from, to, "Player Weapon fireTrace bulletHole"); }); - player->setTakeBonusCallBack([this](const string &bonusName) { client->takeBonus(bonusName); }); - player->setAddWeaponCallBack([this](std::shared_ptr weapon) { addWeapon(std::move(weapon)); }); - player->setRemoveWeaponCallBack([this](std::shared_ptr weapon) { removeWeapon(std::move(weapon)); }); + EventHandler::listen( + Event("spawn_player"), + [this](sf::Uint16 targetId) { + spawnPlayer(targetId); + }); + EventHandler::listen( + Event("remove_player"), + [this](sf::Uint16 targetId) { + removePlayer(targetId); + }); + EventHandler::listen(Event("fire"), [this](){ player->fireWeaponAnimation(); }); + EventHandler::listen( + Event("your_bullet"), + [this](const Vec3D &from, const Vec3D &to) { + addFireTrace(from, to); + }); + EventHandler::listen( + Event("enemy_bullet"), + [this](const Vec3D &from, const Vec3D &to) { + addFireTrace(from, to); + }); + EventHandler::listen)>( + Event("add_weapon"), + [this](std::shared_ptr weapon) { + addWeapon(weapon); + }); + EventHandler::listen)>( + Event("remove_weapon"), + [this](std::shared_ptr weapon) { + removeWeapon(weapon); + }); + EventHandler::listen( + Event("change_enemy_weapon"), + [this](const std::string &weaponName, sf::Uint16 enemyId) { + changeEnemyWeapon(weaponName, enemyId); + }); + EventHandler::listen( + Event("add_bonus"), + [this](const string &bonusName, const Vec3D &position) { addBonus(bonusName, position); } + ); + EventHandler::listen( + Event("remove_bonus"), + [this](const ObjectNameTag &bonusName) { removeBonus(bonusName); } + ); - player->reInitWeapons(); + EventHandler::listen(Event("reload_weapon"), [this](){ player->reloadWeaponAnimation(); }); + + player->setRayCastFunction([this](const Vec3D &from, const Vec3D &to) { return world->rayCast(from, to, "Player Weapon fireTrace bulletHole"); }); camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0}); player->attach(camera); @@ -110,6 +135,8 @@ void Shooter::start() { server->stop(); } + player->reInitWeapons(); + // windows init: mainMenu.setTitle("Main menu"); mainMenu.setBackgroundTexture(ShooterConsts::MAIN_MENU_BACK, 1.1, 1.1, screen->width(), screen->height()); @@ -131,6 +158,7 @@ void Shooter::start() { server->stop(); this->exit(); }, "Exit", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); + client->setChatManager(chat); } @@ -179,10 +207,10 @@ void Shooter::update() { if (isTypingMessage) { string symbols = screen->getInputSymbols(); for (char s : symbols) { - if (s == (char)8) {//backspace + if (s == (char)8) { //backspace message = message.substr(0, message.size() - 1); } - else if (s == (char)27) {//escape + else if (s == (char)27) { //escape message = ""; //FIXME: не работает потому что isKeyTapped имеют задержку, isTypingMessage = false; //т. е. этот код выполняется после нажатия на ESC, } // но при следующем цикле при проверке isKeyTapped(ESC) возвращается TRUE @@ -213,15 +241,16 @@ void Shooter::drawChat() { sf::Color chatColor = isTypingMessage? sf::Color(50, 50, 50, 255) : sf::Color(50, 50, 50, chat->update(Time::deltaTime())); string chatText = isTypingMessage ? chat->getChat() : chat->getChatPreview(); - screen->drawText(chatText, Vec2D{ 0, (double)screen->height()*0.25 }, 20, chatColor); + screen->drawText(chatText, Vec2D{ 10, (double)screen->height()*0.25 }, 20, chatColor); if (isTypingMessage){ screen->drawTetragon( - Vec2D{ (double)screen->width() * 0.05, (double)screen->height() * 0.7 }, - Vec2D{ (double)screen->width() * 0.95, (double)screen->height() * 0.7 }, - Vec2D{ (double)screen->width() * 0.95, (double)screen->height() * 0.7+40 }, - Vec2D{ (double)screen->width() * 0.05, (double)screen->height() * 0.7+40 }, sf::Color(150, 150, 150, 150)); - screen->drawText(message, Vec2D{(double)screen->width() * 0.05, (double)screen->height() * 0.7}, 30, sf::Color(0, 0, 0, 255)); + Vec2D{ (double)screen->width() * 0.05, (double)screen->height() * 0.7}, + Vec2D{ (double)screen->width() * 0.95, (double)screen->height() * 0.7}, + Vec2D{ (double)screen->width() * 0.95, (double)screen->height() * 0.7+50 }, + Vec2D{ (double)screen->width() * 0.05, (double)screen->height() * 0.7+50 }, sf::Color(150, 150, 150, 150)); + + screen->drawText(message, Vec2D{(double)screen->width() * 0.05 + 10, (double)screen->height() * 0.7 - 30}, 30, sf::Color(0, 0, 0, 255)); } } @@ -407,9 +436,7 @@ void Shooter::removeBonus(const ObjectNameTag &bonusName) { void Shooter::addWeapon(std::shared_ptr weapon) { world->addBody(weapon); - if (client != nullptr) { - client->changeWeapon(weapon->name().str()); - } + EventHandler::call(Event("change_weapon"), weapon->name().str()); } void Shooter::changeEnemyWeapon(const std::string &weaponName, sf::Uint16 enemyId) { diff --git a/network/Chat.cpp b/network/Chat.cpp index e69b9e3..817f651 100644 --- a/network/Chat.cpp +++ b/network/Chat.cpp @@ -1,6 +1,7 @@ #include "Chat.h" #include #include + void ChatManager::addNewMessage(std::string author, std::string message) { hide = 7.0; messages.push_back(message); diff --git a/network/ShooterClient.cpp b/network/ShooterClient.cpp index 08d22a4..e39e666 100644 --- a/network/ShooterClient.cpp +++ b/network/ShooterClient.cpp @@ -11,6 +11,30 @@ #include "../3dzavr/engine/animation/Timeline.h" #include "ShooterMsgType.h" #include "../3dzavr/engine/animation/Animations.h" +#include "../3dzavr/engine/utils/EventHandler.h" + + +ShooterClient::ShooterClient(std::shared_ptr player) : _player(player) { + EventHandler::listen( + Event("take_bonus"), + [this](const std::string& name){ this->takeBonus(name); } + ); + + EventHandler::listen( + Event("damage_player"), + [this](sf::Uint16 targetId, double damage) { damagePlayer(targetId, damage); } ); + + EventHandler::listen( + Event("your_bullet"), + [this](const Vec3D &from, const Vec3D &to) { + sendTrace(from, to); + }); + + EventHandler::listen( + Event("change_weapon"), + [this](const std::string &name){ changeWeapon(name); } + ); +} void ShooterClient::updatePacket() { sf::Packet packet; @@ -26,9 +50,8 @@ void ShooterClient::processInit(sf::Packet &packet) { while (packet >> targetId >> x >> y >> z >> health >> kills >> deaths) { if (targetId != _socket.ownId()) { - if (_spawnPlayerCallBack != nullptr) { - _spawnPlayerCallBack(targetId); - } + EventHandler::call(Event("spawn_player"), targetId); + _players[targetId]->translateToPoint(Vec3D{x, y, z}); _players[targetId]->setHealth(health); _players[targetId]->setKills(kills); @@ -110,15 +133,13 @@ void ShooterClient::processNewClient(sf::Packet &packet) { packet >> targetId; - if (_spawnPlayerCallBack != nullptr) { - _spawnPlayerCallBack(targetId); - } + EventHandler::call(Event("spawn_player"), targetId); } void ShooterClient::processDisconnect(sf::Uint16 targetId) { if (targetId != _socket.ownId() && _players.count(targetId)) { _players.erase(targetId); - _removePlayerCallBack(targetId); + EventHandler::call(Event("remove_player"), targetId); } } @@ -131,7 +152,8 @@ void ShooterClient::sendMessage(string message){ packet << MsgType::Custom << ShooterMsgType::newMessage << message; _socket.send(packet, _socket.serverId()); } -void ShooterClient::newMessage(string message, string name) { + +void ShooterClient::sendChatMessage(string message, string name) { chatManager->addNewMessage(name, message); } @@ -204,45 +226,45 @@ void ShooterClient::processCustomPacket(sf::Packet &packet) { } break; case ShooterMsgType::FireTrace: - packet >> dbuff[0] >> dbuff[1] >> dbuff[2] >> dbuff[3] >> dbuff[4] >> dbuff[5]; - if (_addFireTraceCallBack != nullptr) { - _addFireTraceCallBack(Vec3D(dbuff[0], dbuff[1], dbuff[2]), Vec3D(dbuff[3], dbuff[4], dbuff[5])); + if (buffId[0] != _socket.ownId()) { + packet >> dbuff[0] >> dbuff[1] >> dbuff[2] >> dbuff[3] >> dbuff[4] >> dbuff[5]; + + EventHandler::call( + Event("enemy_bullet"), + Vec3D(dbuff[0], dbuff[1], dbuff[2]), Vec3D(dbuff[3], dbuff[4], dbuff[5])); } break; case ShooterMsgType::InitBonuses: while (packet >> tmp >> dbuff[0] >> dbuff[1] >> dbuff[2]) { - if (_addBonusCallBack != nullptr) { - _addBonusCallBack(tmp, Vec3D(dbuff[0], dbuff[1], dbuff[2])); - } + EventHandler::call( + Event("add_bonus"), tmp, Vec3D(dbuff[0], dbuff[1], dbuff[2])); } break; case ShooterMsgType::AddBonus: packet >> tmp >> dbuff[0] >> dbuff[1] >> dbuff[2]; - if (_addBonusCallBack != nullptr) { - _addBonusCallBack(tmp, Vec3D(dbuff[0], dbuff[1], dbuff[2])); - } + EventHandler::call( + Event("add_bonus"), tmp, Vec3D(dbuff[0], dbuff[1], dbuff[2])); break; case ShooterMsgType::RemoveBonus: packet >> tmp; - if (_removeBonusCallBack != nullptr) { - _removeBonusCallBack(ObjectNameTag(tmp)); - } + EventHandler::call( + Event("remove_bonus"), ObjectNameTag(tmp)); break; case ShooterMsgType::ChangeWeapon: packet >> buffId[0] >> tmp; - if (_changeEnemyWeaponCallBack != nullptr) { - _changeEnemyWeaponCallBack(tmp, buffId[0]); - } + EventHandler::call( + Event("change_enemy_weapon"), tmp, buffId[0]); + break; case ShooterMsgType::newMessage: packet >> name >> message; - newMessage(message, name); + sendChatMessage(message, name); break; default: Log::log("ShooterClient::processCustomPacket: unknown message type " + @@ -266,7 +288,7 @@ void ShooterClient::damagePlayer(sf::Uint16 targetId, double damage) { Log::log("ShooterClient: damagePlayer " + std::to_string(targetId) + " ( -" + std::to_string(damage) + "hp )"); } -void ShooterClient::addTrace(const Vec3D &from, const Vec3D &to) { +void ShooterClient::sendTrace(const Vec3D &from, const Vec3D &to) { sf::Packet packet; packet << MsgType::Custom << ShooterMsgType::FireTrace << from.x() << from.y() << from.z() << to.x() << to.y() @@ -280,9 +302,8 @@ void ShooterClient::takeBonus(const std::string &bonusName) { packet << MsgType::Custom << ShooterMsgType::RemoveBonus << bonusName; _socket.sendRely(packet, _socket.serverId()); - if (_removeBonusCallBack != nullptr) { - _removeBonusCallBack(ObjectNameTag(bonusName)); - } + EventHandler::call( + Event("remove_bonus"), ObjectNameTag(bonusName)); } void ShooterClient::changeWeapon(const std::string &weaponName) { @@ -296,31 +317,6 @@ void ShooterClient::addPlayer(sf::Uint16 id, std::shared_ptr player) { _players.insert({id, player}); } -void ShooterClient::setSpawnPlayerCallBack(std::function spawn) { - _spawnPlayerCallBack = std::move(spawn); -} - -void ShooterClient::setRemovePlayerCallBack(std::function remove) { - _removePlayerCallBack = std::move(remove); -} - -void ShooterClient::setAddFireTraceCallBack(std::function addTrace) { - _addFireTraceCallBack = std::move(addTrace); -} - -void ShooterClient::setAddBonusCallBack(std::function addBonus) { - _addBonusCallBack = std::move(addBonus); -} - -void ShooterClient::setRemoveBonusCallBack(std::function removeBonus) { - _removeBonusCallBack = std::move(removeBonus); -} - -void -ShooterClient::setChangeEnemyWeaponCallBack(std::function changeEnemyWeapon) { - _changeEnemyWeaponCallBack = std::move(changeEnemyWeapon); -} - void ShooterClient::requestMap(const std::string& clientIp, std::string *current_map) { Log::log("---------[FTP server]---------"); sf::Ftp ftp; diff --git a/network/ShooterClient.h b/network/ShooterClient.h index 1f9e12a..0560a02 100644 --- a/network/ShooterClient.h +++ b/network/ShooterClient.h @@ -17,35 +17,25 @@ private: std::map> _players{}; std::shared_ptr _player; - std::function _spawnPlayerCallBack; - std::function _removePlayerCallBack; - std::function _addFireTraceCallBack; - std::function _addBonusCallBack; - std::function _removeBonusCallBack; - std::function _changeEnemyWeaponCallBack; - std::shared_ptr chatManager; + + void damagePlayer(sf::Uint16 targetId, double damage); + + void takeBonus(const std::string &bonusName); + + void sendTrace(const Vec3D &from, const Vec3D &to); + + void changeWeapon(const std::string &weaponName); + public: - explicit ShooterClient(std::shared_ptr player) : _player(player) {}; + explicit ShooterClient(std::shared_ptr player); void sendMessage(std::string message); - void newMessage(std::string message, std::string name); + void sendChatMessage(std::string message, std::string name); void updatePacket() override; - void setSpawnPlayerCallBack(std::function spawn); - - void setRemovePlayerCallBack(std::function remove); - - void setAddFireTraceCallBack(std::function addTrace); - - void setAddBonusCallBack(std::function addBonus); - - void setRemoveBonusCallBack(std::function removeBonus); - - void setChangeEnemyWeaponCallBack(std::function changeEnemyWeapon); - void processInit(sf::Packet &packet) override; void processUpdate(sf::Packet &packet) override; @@ -58,14 +48,6 @@ public: void processDisconnected() override; - void damagePlayer(sf::Uint16 targetId, double damage); - - void takeBonus(const std::string &bonusName); - - void addTrace(const Vec3D &from, const Vec3D &to); - - void changeWeapon(const std::string &weaponName); - void setChatManager(std::shared_ptr chat) { chatManager = chat; }; void addPlayer(sf::Uint16 id, std::shared_ptr player); diff --git a/player/Player.cpp b/player/Player.cpp index ad4767d..30e1f98 100644 --- a/player/Player.cpp +++ b/player/Player.cpp @@ -8,6 +8,8 @@ #include "../3dzavr/engine/io/Screen.h" #include "../3dzavr/engine/utils/Log.h" #include "../3dzavr/engine/animation/Animations.h" +#include "../3dzavr/engine/utils/EventHandler.h" + Player::Player(ObjectNameTag name, const std::string &filename, const Vec3D &scale) : RigidBody(std::move(name), filename, scale) { setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0}); @@ -54,9 +56,7 @@ void Player::collisionWithObject(const ObjectNameTag &tag, std::shared_ptr(Event("take_bonus"), tag.str()); } } @@ -76,38 +76,6 @@ void Player::addWeapon(std::shared_ptr weapon) { _weapons.back()->translate(position()); _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::addAnimation(AnimationListTag("reload_weapon"), - _weapons[_selectedWeapon], - -2 * Consts::PI, - _weapons[_selectedWeapon]->reloadTime() / 2, - Animation::LoopOut::None, - Animation::InterpolationType::Cos); - }); - - // adding fire animation - _weapons.back()->setFireCallBack([this]() { - Timeline::addAnimation(AnimationListTag("fire_weapon"), - _weapons[_selectedWeapon], - -_weapons[_selectedWeapon]->fireDelay(), - _weapons[_selectedWeapon]->fireDelay()/3, - Animation::LoopOut::None, - Animation::InterpolationType::Cos); - Timeline::addAnimation(AnimationListTag("fire_weapon"), 0); - Timeline::addAnimation(AnimationListTag("fire_weapon"), - _weapons[_selectedWeapon], - _weapons[_selectedWeapon]->fireDelay(), - _weapons[_selectedWeapon]->fireDelay()/3, - Animation::LoopOut::None, - Animation::InterpolationType::Cos); - }); - - // add call back function to create fire traces - if (_addTraceCallBack != nullptr) { - _weapons.back()->setAddTraceCallBack(_addTraceCallBack); - } } void Player::reInitWeapons() { @@ -117,34 +85,33 @@ void Player::reInitWeapons() { unattach(ObjectNameTag(weapon->name())); } - if (_removeWeaponCallBack != nullptr) { - _removeWeaponCallBack(_weapons[_selectedWeapon]); - } + EventHandler::call)>(Event("remove_weapon"), + _weapons[_selectedWeapon]); + _weapons.clear(); } _selectedWeapon = 0; addWeapon(std::make_shared()); - if (_addWeaponCallBack != nullptr) { - _addWeaponCallBack(_weapons[_selectedWeapon]); - } + + EventHandler::call)>(Event("add_weapon"), + _weapons[_selectedWeapon]); } void Player::selectNextWeapon() { if (_weapons.size() > 1) { // change '_selectedWeapon' - if (_removeWeaponCallBack != nullptr) { - _removeWeaponCallBack(_weapons[_selectedWeapon]); - } + EventHandler::call)>(Event("remove_weapon"), + _weapons[_selectedWeapon]); _selectedWeapon = (_selectedWeapon + 1) % _weapons.size(); - if (_addWeaponCallBack != nullptr) { - _addWeaponCallBack(_weapons[_selectedWeapon]); - } + EventHandler::call)>(Event("add_weapon"), + _weapons[_selectedWeapon]); + Log::log("selectedWeapon " + std::to_string(_selectedWeapon)); SoundController::loadAndPlay(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND); - rotateWeapon(); + selectWeaponAnimation(); } } @@ -152,20 +119,21 @@ void Player::selectNextWeapon() { void Player::selectPreviousWeapon() { if (_weapons.size() > 1) { // change '_selectedWeapon' - if (_removeWeaponCallBack != nullptr) { - _removeWeaponCallBack(_weapons[_selectedWeapon]); - } + EventHandler::call)>(Event("remove_weapon"), + _weapons[_selectedWeapon]); + if (_selectedWeapon > 0) { _selectedWeapon = (_selectedWeapon - 1) % _weapons.size(); } else { _selectedWeapon = _weapons.size() - 1; } - if (_addWeaponCallBack != nullptr) { - _addWeaponCallBack(_weapons[_selectedWeapon]); - } + + EventHandler::call)>(Event("add_weapon"), + _weapons[_selectedWeapon]); + Log::log("selectedWeapon " + std::to_string(_selectedWeapon)); SoundController::loadAndPlay(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND); - rotateWeapon(); + selectWeaponAnimation(); } } @@ -175,9 +143,7 @@ bool Player::fire() { auto fireInfo = _weapons[_selectedWeapon]->fire(_rayCastFunction, camera->position(), camera->lookAt()); for (auto&[damagedPlayerName, damage] : fireInfo.damagedPlayers) { sf::Uint16 targetId = std::stoi(damagedPlayerName.str().substr(6)); - if (_damagePlayerCallBack != nullptr) { - _damagePlayerCallBack(targetId, damage); - } + EventHandler::call(Event("damage_player"), targetId, damage); } return fireInfo.shot; } @@ -198,7 +164,7 @@ void Player::setFullAbility() { SoundController::loadAndPlay(SoundTag("addAbility"), ShooterConsts::RESTORE_ABILITY_SOUND); } -void Player::rotateWeapon() { +void Player::selectWeaponAnimation() { Timeline::addAnimation(AnimationListTag("select_weapon"), _weapons[_selectedWeapon], -2 * Consts::PI, @@ -206,3 +172,28 @@ void Player::rotateWeapon() { Animation::LoopOut::None, Animation::InterpolationType::Cos); } + +void Player::fireWeaponAnimation() { + Timeline::addAnimation(AnimationListTag("fire_weapon"), + _weapons[_selectedWeapon], + -_weapons[_selectedWeapon]->fireDelay(), + _weapons[_selectedWeapon]->fireDelay()/3, + Animation::LoopOut::None, + Animation::InterpolationType::Cos); + Timeline::addAnimation(AnimationListTag("fire_weapon"), 0); + Timeline::addAnimation(AnimationListTag("fire_weapon"), + _weapons[_selectedWeapon], + _weapons[_selectedWeapon]->fireDelay(), + _weapons[_selectedWeapon]->fireDelay()/3, + Animation::LoopOut::None, + Animation::InterpolationType::Cos); +} + +void Player::reloadWeaponAnimation() { + Timeline::addAnimation(AnimationListTag("reload_weapon"), + _weapons[_selectedWeapon], + -2 * Consts::PI, + _weapons[_selectedWeapon]->reloadTime() / 2, + Animation::LoopOut::None, + Animation::InterpolationType::Cos); +} diff --git a/player/Player.h b/player/Player.h index c77e659..ffc74c8 100644 --- a/player/Player.h +++ b/player/Player.h @@ -33,17 +33,10 @@ private: std::string _nickName = ShooterConsts::PLAYER_NAME; - std::function _damagePlayerCallBack; - std::function _addTraceCallBack; - std::function _takeBonusCallBack; - - std::function)> _addWeaponCallBack; - std::function)> _removeWeaponCallBack; - std::function _rayCastFunction; void collisionWithObject(const ObjectNameTag &tag, std::shared_ptr obj); - void rotateWeapon(); + public: explicit Player(ObjectNameTag name, const std::string &filename = ShooterConsts::CUBE_OBJ, const Vec3D &scale = Vec3D{1, 1, 1}); @@ -55,7 +48,6 @@ public: [[nodiscard]] double ability() const { return _ability; } - void setFullHealth(); void setFullAbility(); @@ -88,26 +80,6 @@ public: void setDeaths(int deaths) { _deaths = deaths; } - void setDamagePlayerCallBack(std::function hit) { - _damagePlayerCallBack = std::move(hit); - } - - void setAddTraceCallBack(std::function add) { - _addTraceCallBack = std::move(add); - } - - void setTakeBonusCallBack(std::function take) { - _takeBonusCallBack = std::move(take); - } - - void setAddWeaponCallBack(std::function)> addWeapon) { - _addWeaponCallBack = std::move(addWeapon); - } - - void setRemoveWeaponCallBack(std::function)> removeWeapon) { - _removeWeaponCallBack = std::move(removeWeapon); - } - void setRayCastFunction(std::function rayCastFunction) { _rayCastFunction = std::move(rayCastFunction); } @@ -120,6 +92,12 @@ public: [[nodiscard]] std::string playerNickName() const { return _nickName; } void setPlayerNickName(const std::string &name) { _nickName = name; } + + void selectWeaponAnimation(); + + void fireWeaponAnimation(); + + void reloadWeaponAnimation(); }; diff --git a/player/PlayerController.cpp b/player/PlayerController.cpp index 7aa3fb9..db8daf5 100644 --- a/player/PlayerController.cpp +++ b/player/PlayerController.cpp @@ -2,6 +2,8 @@ // Created by Иван Ильин on 19.09.2021. // +#include + #include "PlayerController.h" #include "../3dzavr/engine/utils/Log.h" #include "../3dzavr/engine/animation/Animations.h" diff --git a/weapon/Weapon.cpp b/weapon/Weapon.cpp index 2bc9107..7e73fc3 100644 --- a/weapon/Weapon.cpp +++ b/weapon/Weapon.cpp @@ -6,6 +6,7 @@ #include "../3dzavr/engine/utils/ResourceManager.h" #include "../3dzavr/engine/utils/Log.h" #include "../ShooterConsts.h" +#include "../3dzavr/engine/utils/EventHandler.h" using namespace std; @@ -46,9 +47,7 @@ FireInformation Weapon::fire(std::function(Event("fire")); return FireInformation{processFire(std::move(rayCastFunction), position, direction), true}; } @@ -69,9 +68,7 @@ void Weapon::reload() { Log::log("Weapon::reload (" + std::to_string(_stockAmmo) + " : " + std::to_string(_clipAmmo) + ")"); _lastReloadTime = Time::time(); - if (_reloadCallBack != nullptr) { - _reloadCallBack(); - } + EventHandler::call(Event("reload_weapon")); } std::map @@ -116,7 +113,8 @@ Weapon::fireABullet(std::function(Event("your_bullet"), lineFrom, lineTo); return damagedPlayers; } \ No newline at end of file diff --git a/weapon/Weapon.h b/weapon/Weapon.h index 1e77958..bd21883 100644 --- a/weapon/Weapon.h +++ b/weapon/Weapon.h @@ -37,10 +37,6 @@ private: double _lastFireTime = std::numeric_limits::min(); double _lastReloadTime = std::numeric_limits::min(); - std::function _addTraceCallBack; - std::function _reloadCallBack; - std::function _fireCallBack; - protected: std::map fireABullet(std::function rayCastFunction, @@ -65,11 +61,6 @@ public: [[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 setFireCallBack(std::function fire) { _fireCallBack = std::move(fire); } - - void addAPack() { _stockAmmo += initialPack(); } [[nodiscard]] int initialPack() const { return _initialPack; }