From fe71ab92b02e688f1ceff6159567247acc092205 Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sun, 31 Oct 2021 23:01:06 +0700 Subject: [PATCH] Visual Studio refactoring --- CMakeLists.txt | 4 ---- Player.cpp | 44 +++++++++++++++++++++++++++----------- Player.h | 16 ++++++-------- PlayerController.cpp | 6 +++--- Shooter.cpp | 5 ++--- ShooterClient.cpp | 2 +- engine/Engine.cpp | 1 - engine/Mesh.h | 2 -- engine/Mouse.cpp | 2 +- engine/Mouse.h | 1 + engine/Screen.cpp | 6 ++---- engine/Screen.h | 3 --- engine/gui/Window.cpp | 3 +-- engine/physics/RigidBody.h | 20 ++++++++--------- weapon/Ak47.cpp | 15 ------------- weapon/Ak47.h | 7 +++++- weapon/Gun.cpp | 15 ------------- weapon/Gun.h | 7 +++++- weapon/Rifle.cpp | 13 ----------- weapon/Rifle.h | 7 +++++- weapon/Shotgun.cpp | 29 ------------------------- weapon/Shotgun.h | 20 +++++++++++++++-- 22 files changed, 94 insertions(+), 134 deletions(-) delete mode 100644 weapon/Ak47.cpp delete mode 100644 weapon/Gun.cpp delete mode 100644 weapon/Rifle.cpp delete mode 100644 weapon/Shotgun.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 54d783a..ffbc675 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,14 +16,10 @@ add_executable(shooter ShooterServer.h weapon/Weapon.cpp weapon/Weapon.h - weapon/Ak47.cpp weapon/Ak47.h - weapon/Shotgun.cpp weapon/Shotgun.h - weapon/Gun.cpp weapon/Gun.h weapon/Gold_Ak47.h - weapon/Rifle.cpp weapon/Rifle.h PlayerController.cpp PlayerController.h diff --git a/Player.cpp b/Player.cpp index bb6b3fc..3666a0d 100644 --- a/Player.cpp +++ b/Player.cpp @@ -56,7 +56,9 @@ void Player::collisionWithObject(const ObjectNameTag &tag, 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()); - _weapons.back()->setAddTraceCallBack(_addTraceCallBack); + if (_addTraceCallBack != nullptr) { + _weapons.back()->setAddTraceCallBack(_addTraceCallBack); + } } -void Player::initWeapons() { +void Player::reInitWeapons() { if (!_weapons.empty()) { for (auto weapon : _weapons) { unattach(ObjectNameTag(weapon->name())); } - _removeWeaponCallBack(_weapons[_selectedWeapon]); + if (_removeWeaponCallBack != nullptr) { + _removeWeaponCallBack(_weapons[_selectedWeapon]); + } _weapons.clear(); } _selectedWeapon = 0; addWeapon(std::make_shared()); - _addWeaponCallBack(_weapons[_selectedWeapon]); + if (_addWeaponCallBack != nullptr) { + _addWeaponCallBack(_weapons[_selectedWeapon]); + } } -void Player::nextWeapon() { +void Player::selectNextWeapon() { if (_weapons.size() > 1) { // change '_selectedWeapon' - _removeWeaponCallBack(_weapons[_selectedWeapon]); + if (_removeWeaponCallBack != nullptr) { + _removeWeaponCallBack(_weapons[_selectedWeapon]); + } + _selectedWeapon = (_selectedWeapon + 1) % _weapons.size(); - _addWeaponCallBack(_weapons[_selectedWeapon]); + + if (_addWeaponCallBack != nullptr) { + _addWeaponCallBack(_weapons[_selectedWeapon]); + } Log::log("selectedWeapon " + std::to_string(_selectedWeapon)); SoundController::playSound(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND); } } -void Player::previousWeapon() { +void Player::selectPreviousWeapon() { if (_weapons.size() > 1) { // change '_selectedWeapon' - _removeWeaponCallBack(_weapons[_selectedWeapon]); + if (_removeWeaponCallBack != nullptr) { + _removeWeaponCallBack(_weapons[_selectedWeapon]); + } if (_selectedWeapon > 0) { _selectedWeapon = (_selectedWeapon - 1) % _weapons.size(); } else { _selectedWeapon = _weapons.size() - 1; } - _addWeaponCallBack(_weapons[_selectedWeapon]); + if (_addWeaponCallBack != nullptr) { + _addWeaponCallBack(_weapons[_selectedWeapon]); + } Log::log("selectedWeapon " + std::to_string(_selectedWeapon)); SoundController::playSound(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND); } @@ -128,7 +146,9 @@ 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)); - _damagePlayerCallBack(targetId, damage); + if (_damagePlayerCallBack != nullptr) { + _damagePlayerCallBack(targetId, damage); + } } return fireInfo.shot; } diff --git a/Player.h b/Player.h index 79df28d..00823ed 100644 --- a/Player.h +++ b/Player.h @@ -40,6 +40,8 @@ private: std::function)> _removeWeaponCallBack; std::function _rayCastFunction; + + void collisionWithObject(const ObjectNameTag &tag, std::shared_ptr obj); public: explicit Player(ObjectNameTag name); @@ -56,23 +58,19 @@ public: void setFullAbility(); - void initWeapons(); + void reInitWeapons(); void addWeapon(std::shared_ptr weapon); - [[nodiscard]] std::pair balance() const { return _weapons[_selectedWeapon]->balance(); } + void selectNextWeapon(); - void nextWeapon(); - - void previousWeapon(); + void selectPreviousWeapon(); bool fire(); void reload(); - [[nodiscard]] ObjectNameTag weaponName() const { return _weapons[_selectedWeapon]->name(); } - - std::shared_ptr weapon() { return _weapons[_selectedWeapon]; } + [[nodiscard]] std::shared_ptr weapon() const { return _weapons[_selectedWeapon]; } void rotateWeaponsRelativePoint(const Vec3D &point, const Vec3D &v, double val); @@ -117,8 +115,6 @@ public: [[nodiscard]] double headAngle() const { return _headAngle; }; - void collisionWithObject(const ObjectNameTag &tag, std::shared_ptr obj); - [[nodiscard]] std::string playerNickName() const { return _nickName; } void setPlayerNickName(const std::string &name) { _nickName = name; } diff --git a/PlayerController.cpp b/PlayerController.cpp index f720757..2211c17 100644 --- a/PlayerController.cpp +++ b/PlayerController.cpp @@ -137,7 +137,7 @@ void PlayerController::update() { bool shot = _player->fire(); if (shot) { - if (_player->weaponName() == ObjectNameTag("shotgun")) { + if (_player->weapon()->name() == ObjectNameTag("shotgun")) { _player->addVelocity(-camera->lookAt() * 30 * coeff); } } @@ -186,11 +186,11 @@ void PlayerController::update() { } if (_keyboard->isKeyTapped(sf::Keyboard::Right) || _keyboard->isKeyTapped(sf::Keyboard::E)) { - _player->nextWeapon(); + _player->selectNextWeapon(); } if (_keyboard->isKeyTapped(sf::Keyboard::Left) || _keyboard->isKeyTapped(sf::Keyboard::Q)) { - _player->previousWeapon(); + _player->selectPreviousWeapon(); } if (Keyboard::isKeyPressed(sf::Keyboard::R)) { diff --git a/Shooter.cpp b/Shooter.cpp index 806aaf6..6830080 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -92,7 +92,7 @@ void Shooter::start() { player->setAddWeaponCallBack([this](std::shared_ptr weapon) { addWeapon(std::move(weapon)); }); player->setRemoveWeaponCallBack([this](std::shared_ptr weapon) { removeWeapon(std::move(weapon)); }); - player->initWeapons(); + player->reInitWeapons(); player->translate(Vec3D{0, 0, 0}); camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0}); @@ -156,7 +156,6 @@ void Shooter::update() { if (inGame) { screen->setTitle(ShooterConsts::PROJECT_NAME); playerController->update(); - mouse->setMouseInCenter(); } else { mainMenu.update(); } @@ -229,7 +228,7 @@ void Shooter::drawPlayerStats() { Vec2D{xPos, yPos - 15 + height}, {255, 168, 168, 100}); - auto balance = player->balance(); + auto balance = player->weapon()->balance(); screen->drawText(std::to_string((int) balance.first), Vec2D{150, static_cast(screen->height() - 150)}, 100, sf::Color(0, 0, 0, 100)); diff --git a/ShooterClient.cpp b/ShooterClient.cpp index 3d5535c..695af6c 100644 --- a/ShooterClient.cpp +++ b/ShooterClient.cpp @@ -113,7 +113,7 @@ void ShooterClient::processCustomPacket(sf::Packet &packet) { _player->translateToPoint( Vec3D{50.0 * (-1 + 2.0 * (double) rand() / RAND_MAX), 30.0 * (double) rand() / RAND_MAX, 50.0 * (-1 + 2.0 * (double) rand() / RAND_MAX)}); - _player->initWeapons(); + _player->reInitWeapons(); _player->setFullAbility(); SoundController::playSound(SoundTag("death"), ShooterConsts::DEATH_SOUND); _lastEvent += _player->playerNickName(); diff --git a/engine/Engine.cpp b/engine/Engine.cpp index bd4d655..86374a8 100644 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -28,7 +28,6 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, start(); camera->init(screenWidth, screenHeight); - mouse->setMouseInCenter(); while (screen->isOpen()) { screen->clear(); diff --git a/engine/Mesh.h b/engine/Mesh.h index f6d25b2..1f37bea 100644 --- a/engine/Mesh.h +++ b/engine/Mesh.h @@ -36,8 +36,6 @@ public: [[nodiscard]] std::vector const &triangles() const { return _tris; } - [[nodiscard]] std::vector &triangles() { return _tris; } - void setTriangles(const std::vector &t); [[nodiscard]] size_t size() const { return _tris.size() * 3; } diff --git a/engine/Mouse.cpp b/engine/Mouse.cpp index 23a550c..3dbb990 100644 --- a/engine/Mouse.cpp +++ b/engine/Mouse.cpp @@ -12,11 +12,11 @@ Vec2D Mouse::getMousePosition() const { } Vec2D Mouse::getMouseDisplacement() const { - // TODO: getMouseDisplacement() should return displacement from the previous position but not from the center sf::Vector2 mousePos = sf::Mouse::getPosition(*_screen->renderWindow()); sf::Vector2 center = sf::Vector2(_screen->width() / 2, _screen->height() / 2); sf::Vector2 displacement = mousePos - center; + setMouseInCenter(); return Vec2D(displacement.x, displacement.y); } diff --git a/engine/Mouse.h b/engine/Mouse.h index 5d26389..d3f83de 100644 --- a/engine/Mouse.h +++ b/engine/Mouse.h @@ -16,6 +16,7 @@ private: const std::shared_ptr _screen; std::map _tappedButtons; + public: explicit Mouse(std::shared_ptr screen) : _screen(std::move(screen)) {}; diff --git a/engine/Screen.cpp b/engine/Screen.cpp index 2f70419..9b8a308 100644 --- a/engine/Screen.cpp +++ b/engine/Screen.cpp @@ -15,15 +15,13 @@ void Screen::open(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background, sf::Uint32 style) { _title = name; - _w = screenWidth; - _h = screenHeight; _background = background; sf::ContextSettings settings; settings.depthBits = 24; settings.antialiasingLevel = 8; - _window->create(sf::VideoMode(_w, _h), name, style, settings); + _window->create(sf::VideoMode(screenWidth, screenHeight), name, style, settings); _window->setVerticalSyncEnabled(verticalSync); } @@ -179,7 +177,7 @@ void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t } GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D &cameraPosition) { - std::vector &triangles = mesh->triangles(); + std::vector const &triangles = mesh->triangles(); auto *geometry = (GLfloat *) malloc(7 * 3 * triangles.size() * sizeof(GLfloat)); diff --git a/engine/Screen.h b/engine/Screen.h index 0858728..574c5bb 100644 --- a/engine/Screen.h +++ b/engine/Screen.h @@ -19,9 +19,6 @@ class Screen final { private: - int _w{}; - int _h{}; - std::string _title; sf::Color _background; diff --git a/engine/gui/Window.cpp b/engine/gui/Window.cpp index 5a3cffb..9a09282 100644 --- a/engine/gui/Window.cpp +++ b/engine/gui/Window.cpp @@ -23,6 +23,7 @@ void Window::update() { Vec2D mousePos = _mouse->getMousePosition(); Vec2D dMousePos = mousePos - _prevMousePosition; + _prevMousePosition = mousePos; _back.setPosition(_back.getPosition() - sf::Vector2f(static_cast(dMousePos.x()) / 30.0f, static_cast(dMousePos.y()) / 30.0f)); bool isPressed = _mouse->isButtonTapped(sf::Mouse::Left); @@ -44,8 +45,6 @@ void Window::update() { _screen->drawText(button.text()); } } - - _prevMousePosition = mousePos; } void Window::setBackgroundTexture(const std::string &texture, double sx, double sy, int w, int h) { diff --git a/engine/physics/RigidBody.h b/engine/physics/RigidBody.h index 5600dee..ebe9909 100644 --- a/engine/physics/RigidBody.h +++ b/engine/physics/RigidBody.h @@ -32,6 +32,15 @@ struct NextSimplex final { class RigidBody : public Mesh { private: + Vec3D _velocity{0, 0, 0}; + Vec3D _acceleration{0, 0, 0}; + + bool _collision = false; + bool _isCollider = true; + + bool _inCollision = false; + Vec3D _collisionNormal{0, 0, 0}; + Vec3D _findFurthestPoint(const Vec3D &direction); Vec3D _support(std::shared_ptr obj, const Vec3D &direction); @@ -52,17 +61,6 @@ private: static std::vector> _addIfUniqueEdge(const std::vector> &edges, const std::vector &faces, size_t a, size_t b); - -protected: - Vec3D _velocity{0, 0, 0}; - Vec3D _acceleration{0, 0, 0}; - - bool _collision = false; - bool _isCollider = true; - - bool _inCollision = false; - Vec3D _collisionNormal{0, 0, 0}; - public: explicit RigidBody(ObjectNameTag nameTag) : Mesh(std::move(nameTag)) {}; diff --git a/weapon/Ak47.cpp b/weapon/Ak47.cpp deleted file mode 100644 index 6c5f400..0000000 --- a/weapon/Ak47.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by Иван Ильин on 02.06.2021. -// - -#include "../engine/ResourceManager.h" -#include "Ak47.h" -#include "../ShooterConsts.h" - -using namespace std; - -Ak47::Ak47() : Weapon(100, 30, 3.0, 0.1, 300, 2.0, - ShooterConsts::AK47_FIRE_SOUND, ShooterConsts::AK47_RELOAD_SOUND, - ObjectNameTag("ak47"), ShooterConsts::AK47_OBJ, - Vec3D{3, 3, 3}, Vec3D{-2.2, 1.0, 1.3},Vec3D{0, Consts::PI, 0}) { -} diff --git a/weapon/Ak47.h b/weapon/Ak47.h index 2c881bc..417e546 100644 --- a/weapon/Ak47.h +++ b/weapon/Ak47.h @@ -6,10 +6,15 @@ #define SHOOTER_AK47_H #include "Weapon.h" +#include "../engine/ResourceManager.h" +#include "../ShooterConsts.h" class Ak47 final : public Weapon { public: - explicit Ak47(); + explicit Ak47() : Weapon(100, 30, 3.0, 0.1, 300, 2.0, + ShooterConsts::AK47_FIRE_SOUND, ShooterConsts::AK47_RELOAD_SOUND, + ObjectNameTag("ak47"), ShooterConsts::AK47_OBJ, + Vec3D{3, 3, 3}, Vec3D{-2.2, 1.0, 1.3},Vec3D{0, Consts::PI, 0}) {} }; diff --git a/weapon/Gun.cpp b/weapon/Gun.cpp deleted file mode 100644 index 33b5968..0000000 --- a/weapon/Gun.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by Иван Ильин on 03.06.2021. -// - -#include "../engine/ResourceManager.h" -#include "Gun.h" -#include "../ShooterConsts.h" - -using namespace std; - -Gun::Gun() : Weapon(30, 6, 2.0, 0.3, 800, 3.0, - ShooterConsts::GUN_FIRE_SOUND, ShooterConsts::GUN_RELOAD_SOUND, ObjectNameTag("gun"), - ShooterConsts::GUN_OBJ, Vec3D{3, 3, 3}, - Vec3D{-1.8, 1.3, 1.8}, Vec3D{0, Consts::PI, 0}) { -} diff --git a/weapon/Gun.h b/weapon/Gun.h index dcaed62..9efa93b 100644 --- a/weapon/Gun.h +++ b/weapon/Gun.h @@ -6,10 +6,15 @@ #define SHOOTER_GUN_H #include "Weapon.h" +#include "../engine/ResourceManager.h" +#include "../ShooterConsts.h" class Gun final : public Weapon { public: - explicit Gun(); + explicit Gun() : Weapon(30, 6, 2.0, 0.3, 800, 3.0, + ShooterConsts::GUN_FIRE_SOUND, ShooterConsts::GUN_RELOAD_SOUND, ObjectNameTag("gun"), + ShooterConsts::GUN_OBJ, Vec3D{3, 3, 3}, + Vec3D{-1.8, 1.3, 1.8}, Vec3D{0, Consts::PI, 0}) {} }; diff --git a/weapon/Rifle.cpp b/weapon/Rifle.cpp deleted file mode 100644 index b6b89c1..0000000 --- a/weapon/Rifle.cpp +++ /dev/null @@ -1,13 +0,0 @@ -// -// Created by Иван Ильин on 06.06.2021. -// - -#include "../engine/ResourceManager.h" -#include "Rifle.h" -#include "../ShooterConsts.h" - -Rifle::Rifle() : Weapon(5, 1, 1.0, 1.0, 30000, 0.5, - ShooterConsts::RIFLE_FIRE_SOUND, ShooterConsts::RIFLE_RELOAD_SOUND, - ObjectNameTag("rifle"), ShooterConsts::RIFLE_OBJ, Vec3D{3, 3, 3}, - Vec3D{-2.3, 1, 1.3},Vec3D{0, Consts::PI, 0}) { -} diff --git a/weapon/Rifle.h b/weapon/Rifle.h index 60acc02..447fac8 100644 --- a/weapon/Rifle.h +++ b/weapon/Rifle.h @@ -6,10 +6,15 @@ #define SHOOTER_RIFLE_H #include "Weapon.h" +#include "../engine/ResourceManager.h" +#include "../ShooterConsts.h" class Rifle final : public Weapon { public: - explicit Rifle(); + explicit Rifle() : Weapon(5, 1, 1.0, 1.0, 30000, 0.5, + ShooterConsts::RIFLE_FIRE_SOUND, ShooterConsts::RIFLE_RELOAD_SOUND, + ObjectNameTag("rifle"), ShooterConsts::RIFLE_OBJ, Vec3D{3, 3, 3}, + Vec3D{-2.3, 1, 1.3},Vec3D{0, Consts::PI, 0}) {} }; diff --git a/weapon/Shotgun.cpp b/weapon/Shotgun.cpp deleted file mode 100644 index b43f76d..0000000 --- a/weapon/Shotgun.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// -// Created by Иван Ильин on 02.06.2021. -// - -#include "../engine/ResourceManager.h" -#include "Shotgun.h" -#include "../ShooterConsts.h" - -using namespace std; - -Shotgun::Shotgun() : Weapon(15, 1, 1.0, 1.0, 400, 5.0, ShooterConsts::SHOTGUN_FIRE_SOUND, - ShooterConsts::SHOTGUN_RELOAD_SOUND, ObjectNameTag("shotgun"), ShooterConsts::SHOTGUN_OBJ, - Vec3D{3, 3, 3}, Vec3D{-1.95, 0.8, 1.5}, Vec3D{0, Consts::PI, 0}) { -} - -std::map -Shotgun::processFire(std::function rayCastFunction, - const Vec3D &position, const Vec3D &direction) { - std::map damagedPlayers; - - for (int i = 0; i < 15; i++) { - std::map damaged = addTrace(rayCastFunction, position, direction); - for (auto &player : damaged) { - damagedPlayers[player.first] += player.second; - } - } - - return damagedPlayers; -} diff --git a/weapon/Shotgun.h b/weapon/Shotgun.h index 56f12b0..9f2ca4c 100644 --- a/weapon/Shotgun.h +++ b/weapon/Shotgun.h @@ -6,14 +6,30 @@ #define SHOOTER_SHOTGUN_H #include "Weapon.h" +#include "../engine/ResourceManager.h" +#include "../ShooterConsts.h" class Shotgun final : public Weapon { public: - explicit Shotgun(); + explicit Shotgun(): Weapon(15, 1, 1.0, 1.0, 400, 5.0, ShooterConsts::SHOTGUN_FIRE_SOUND, + ShooterConsts::SHOTGUN_RELOAD_SOUND, ObjectNameTag("shotgun"), ShooterConsts::SHOTGUN_OBJ, + Vec3D{3, 3, 3}, Vec3D{-1.95, 0.8, 1.5}, Vec3D{0, Consts::PI, 0}) {} std::map processFire(std::function rayCastFunction, - const Vec3D &position, const Vec3D &direction) override; + const Vec3D &position, const Vec3D &direction) override { + + std::map damagedPlayers; + + for (int i = 0; i < 15; i++) { + std::map damaged = addTrace(rayCastFunction, position, direction); + for (auto &player : damaged) { + damagedPlayers[player.first] += player.second; + } + } + + return damagedPlayers; + } };