Visual Studio refactoring

master
Vectozavr 2021-10-31 23:01:06 +07:00
parent 9b9aac7fa4
commit fe71ab92b0
22 changed files with 94 additions and 134 deletions

View File

@ -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

View File

@ -56,7 +56,9 @@ void Player::collisionWithObject(const ObjectNameTag &tag, std::shared_ptr<Rigid
}
if (tag.str().find("Bonus") != std::string::npos) {
_takeBonusCallBack(tag.str());
if (_takeBonusCallBack != nullptr) {
_takeBonusCallBack(tag.str());
}
}
}
@ -77,46 +79,62 @@ void Player::addWeapon(std::shared_ptr<Weapon> 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<Gun>());
_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;
}

View File

@ -40,6 +40,8 @@ private:
std::function<void(std::shared_ptr<Weapon>)> _removeWeaponCallBack;
std::function<IntersectionInformation(const Vec3D &, const Vec3D &)> _rayCastFunction;
void collisionWithObject(const ObjectNameTag &tag, std::shared_ptr<RigidBody> obj);
public:
explicit Player(ObjectNameTag name);
@ -56,23 +58,19 @@ public:
void setFullAbility();
void initWeapons();
void reInitWeapons();
void addWeapon(std::shared_ptr<Weapon> weapon);
[[nodiscard]] std::pair<double, double> 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> weapon() { return _weapons[_selectedWeapon]; }
[[nodiscard]] std::shared_ptr<Weapon> 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<RigidBody> obj);
[[nodiscard]] std::string playerNickName() const { return _nickName; }
void setPlayerNickName(const std::string &name) { _nickName = name; }

View File

@ -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)) {

View File

@ -92,7 +92,7 @@ void Shooter::start() {
player->setAddWeaponCallBack([this](std::shared_ptr<Weapon> weapon) { addWeapon(std::move(weapon)); });
player->setRemoveWeaponCallBack([this](std::shared_ptr<Weapon> 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<double>(screen->height() - 150)}, 100,
sf::Color(0, 0, 0, 100));

View File

@ -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();

View File

@ -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();

View File

@ -36,8 +36,6 @@ public:
[[nodiscard]] std::vector<Triangle> const &triangles() const { return _tris; }
[[nodiscard]] std::vector<Triangle> &triangles() { return _tris; }
void setTriangles(const std::vector<Triangle> &t);
[[nodiscard]] size_t size() const { return _tris.size() * 3; }

View File

@ -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<int> mousePos = sf::Mouse::getPosition(*_screen->renderWindow());
sf::Vector2<int> center = sf::Vector2<int>(_screen->width() / 2, _screen->height() / 2);
sf::Vector2<int> displacement = mousePos - center;
setMouseInCenter();
return Vec2D(displacement.x, displacement.y);
}

View File

@ -16,6 +16,7 @@ private:
const std::shared_ptr<Screen> _screen;
std::map<sf::Mouse::Button, double> _tappedButtons;
public:
explicit Mouse(std::shared_ptr<Screen> screen) : _screen(std::move(screen)) {};

View File

@ -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> mesh, const Vec3D &cameraPosition) {
std::vector<Triangle> &triangles = mesh->triangles();
std::vector<Triangle> const &triangles = mesh->triangles();
auto *geometry = (GLfloat *) malloc(7 * 3 * triangles.size() * sizeof(GLfloat));

View File

@ -19,9 +19,6 @@
class Screen final {
private:
int _w{};
int _h{};
std::string _title;
sf::Color _background;

View File

@ -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<float>(dMousePos.x()) / 30.0f,
static_cast<float>(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) {

View File

@ -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<RigidBody> obj, const Vec3D &direction);
@ -52,17 +61,6 @@ private:
static std::vector<std::pair<size_t, size_t>>
_addIfUniqueEdge(const std::vector<std::pair<size_t, size_t>> &edges, const std::vector<size_t> &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)) {};

View File

@ -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}) {
}

View File

@ -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}) {}
};

View File

@ -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}) {
}

View File

@ -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}) {}
};

View File

@ -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}) {
}

View File

@ -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}) {}
};

View File

@ -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<ObjectNameTag, double>
Shotgun::processFire(std::function<IntersectionInformation(const Vec3D &, const Vec3D &)> rayCastFunction,
const Vec3D &position, const Vec3D &direction) {
std::map<ObjectNameTag, double> damagedPlayers;
for (int i = 0; i < 15; i++) {
std::map<ObjectNameTag, double> damaged = addTrace(rayCastFunction, position, direction);
for (auto &player : damaged) {
damagedPlayers[player.first] += player.second;
}
}
return damagedPlayers;
}

View File

@ -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<ObjectNameTag, double>
processFire(std::function<IntersectionInformation(const Vec3D &, const Vec3D &)> rayCastFunction,
const Vec3D &position, const Vec3D &direction) override;
const Vec3D &position, const Vec3D &direction) override {
std::map<ObjectNameTag, double> damagedPlayers;
for (int i = 0; i < 15; i++) {
std::map<ObjectNameTag, double> damaged = addTrace(rayCastFunction, position, direction);
for (auto &player : damaged) {
damagedPlayers[player.first] += player.second;
}
}
return damagedPlayers;
}
};