diff --git a/Player.cpp b/Player.cpp index db90776..bb6b3fc 100644 --- a/Player.cpp +++ b/Player.cpp @@ -13,47 +13,49 @@ Player::Player(ObjectNameTag name) : RigidBody(name) { setVisible(false); Vec3D randColor = Vec3D::Random(); - setColor({static_cast(randColor.x()*255), static_cast(randColor.y()*255), static_cast(randColor.z()*255)}); + setColor({static_cast(randColor.x() * 255), static_cast(randColor.y() * 255), + static_cast(randColor.z() * 255)}); - setCollisionCallBack([this](const ObjectNameTag& tag, std::shared_ptr obj) {collisionWithObject(tag, obj);}); + setCollisionCallBack( + [this](const ObjectNameTag &tag, std::shared_ptr obj) { collisionWithObject(tag, obj); }); } -void Player::rotateWeaponsRelativePoint(const Vec3D& point4D, const Vec3D& v, double val) { - for(auto& weapon : _weapons) { +void Player::rotateWeaponsRelativePoint(const Vec3D &point4D, const Vec3D &v, double val) { + for (auto &weapon : _weapons) { weapon->rotateRelativePoint(point4D, v, val); } } -void Player::collisionWithObject(const ObjectNameTag& tag, std::shared_ptr obj) { - if(tag.str().find("Bonus_gun") != std::string::npos) { +void Player::collisionWithObject(const ObjectNameTag &tag, std::shared_ptr obj) { + if (tag.str().find("Bonus_gun") != std::string::npos) { addWeapon(std::make_shared()); } - if(tag.str().find("Bonus_shotgun") != std::string::npos) { + if (tag.str().find("Bonus_shotgun") != std::string::npos) { addWeapon(std::make_shared()); } - if(tag.str().find("Bonus_ak47") != std::string::npos) { + if (tag.str().find("Bonus_ak47") != std::string::npos) { addWeapon(std::make_shared()); } - if(tag.str().find("Bonus_gold_ak47") != std::string::npos) { + if (tag.str().find("Bonus_gold_ak47") != std::string::npos) { addWeapon(std::make_shared()); } - if(tag.str().find("Bonus_rifle") != std::string::npos) { + if (tag.str().find("Bonus_rifle") != std::string::npos) { addWeapon(std::make_shared()); } - if(tag.str().find("Bonus_hill") != std::string::npos) { + if (tag.str().find("Bonus_hill") != std::string::npos) { setFullHealth(); } - if(tag.str().find("Bonus_ability") != std::string::npos) { + if (tag.str().find("Bonus_ability") != std::string::npos) { setFullAbility(); } - if(tag.str().find("Bonus") != std::string::npos) { + if (tag.str().find("Bonus") != std::string::npos) { _takeBonusCallBack(tag.str()); } } @@ -61,7 +63,7 @@ void Player::collisionWithObject(const ObjectNameTag& tag, std::shared_ptr weapon) { SoundController::playSound(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND); - for(auto& w : _weapons) { + for (auto &w : _weapons) { if (w->name() == weapon->name()) { w->addAmmo(w->initialPack()); return; @@ -80,8 +82,8 @@ void Player::addWeapon(std::shared_ptr weapon) { void Player::initWeapons() { - if(!_weapons.empty()) { - for(auto weapon : _weapons) { + if (!_weapons.empty()) { + for (auto weapon : _weapons) { unattach(ObjectNameTag(weapon->name())); } @@ -95,7 +97,7 @@ void Player::initWeapons() { } void Player::nextWeapon() { - if(_weapons.size() > 1) { + if (_weapons.size() > 1) { // change '_selectedWeapon' _removeWeaponCallBack(_weapons[_selectedWeapon]); _selectedWeapon = (_selectedWeapon + 1) % _weapons.size(); @@ -106,7 +108,7 @@ void Player::nextWeapon() { } void Player::previousWeapon() { - if(_weapons.size() > 1) { + if (_weapons.size() > 1) { // change '_selectedWeapon' _removeWeaponCallBack(_weapons[_selectedWeapon]); if (_selectedWeapon > 0) { @@ -122,9 +124,9 @@ void Player::previousWeapon() { bool Player::fire() { auto camera = attached(ObjectNameTag("Camera")); - if(camera != nullptr) { + if (camera != nullptr) { auto fireInfo = _weapons[_selectedWeapon]->fire(_rayCastFunction, camera->position(), camera->lookAt()); - for(auto& [damagedPlayerName, damage] : fireInfo.damagedPlayers) { + for (auto&[damagedPlayerName, damage] : fireInfo.damagedPlayers) { sf::Uint16 targetId = std::stoi(damagedPlayerName.str().substr(6)); _damagePlayerCallBack(targetId, damage); } diff --git a/Player.h b/Player.h index 5564726..79df28d 100644 --- a/Player.h +++ b/Player.h @@ -17,7 +17,7 @@ #include "weapon/Rifle.h" #include "ShooterConsts.h" -class Player final : public RigidBody{ +class Player final : public RigidBody { private: double _health = ShooterConsts::HEALTH_MAX; double _ability = ShooterConsts::ABILITY_MAX; @@ -33,75 +33,95 @@ private: std::string _nickName = ShooterConsts::PLAYER_NAME; std::function _damagePlayerCallBack; - std::function _addTraceCallBack; - std::function _takeBonusCallBack; + std::function _addTraceCallBack; + std::function _takeBonusCallBack; std::function)> _addWeaponCallBack; std::function)> _removeWeaponCallBack; - std::function _rayCastFunction; + std::function _rayCastFunction; public: explicit Player(ObjectNameTag name); void setHealth(double h) { _health = h; } + void setAbility(double a) { _ability = a; } [[nodiscard]] double health() const { return _health; } + [[nodiscard]] double ability() const { return _ability; } void setFullHealth(); + void setFullAbility(); void initWeapons(); + void addWeapon(std::shared_ptr weapon); - [[nodiscard]] std::pair balance() const{ return _weapons[_selectedWeapon]->balance();} + + [[nodiscard]] std::pair balance() const { return _weapons[_selectedWeapon]->balance(); } void nextWeapon(); + void previousWeapon(); + bool fire(); + void reload(); + [[nodiscard]] ObjectNameTag weaponName() const { return _weapons[_selectedWeapon]->name(); } + std::shared_ptr weapon() { return _weapons[_selectedWeapon]; } - void rotateWeaponsRelativePoint(const Vec3D& point, const Vec3D& v, double val); + void rotateWeaponsRelativePoint(const Vec3D &point, const Vec3D &v, double val); [[nodiscard]] int kills() const { return _kills; } + [[nodiscard]] int deaths() const { return _deaths; } void addKill() { _kills++; } + void addDeath() { _deaths++; } void setKills(int kills) { _kills = kills; } + void setDeaths(int deaths) { _deaths = deaths; } void setDamagePlayerCallBack(std::function hit) { _damagePlayerCallBack = std::move(hit); } - void setAddTraceCallBack(std::function add) { + + void setAddTraceCallBack(std::function add) { _addTraceCallBack = std::move(add); } - void setTakeBonusCallBack(std::function take) { + + 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) { + + void setRayCastFunction(std::function rayCastFunction) { _rayCastFunction = std::move(rayCastFunction); } // This is for situation when you want to store the position of the head but you dont have attached camera void setHeadAngle(double a) { _headAngle = a; } + [[nodiscard]] double headAngle() const { return _headAngle; }; - void collisionWithObject(const ObjectNameTag& tag, std::shared_ptr obj); + void collisionWithObject(const ObjectNameTag &tag, std::shared_ptr obj); [[nodiscard]] std::string playerNickName() const { return _nickName; } - void setPlayerNickName(const std::string& name) { _nickName = name; } + + void setPlayerNickName(const std::string &name) { _nickName = name; } }; diff --git a/PlayerController.cpp b/PlayerController.cpp index c86943d..f720757 100644 --- a/PlayerController.cpp +++ b/PlayerController.cpp @@ -12,22 +12,24 @@ PlayerController::PlayerController(std::shared_ptr player, std::shared_ptr keyboard, - std::shared_ptr mouse) : _player(player), _keyboard(keyboard), _mouse(mouse) {} + std::shared_ptr mouse) : _player(player), _keyboard(keyboard), + _mouse(mouse) {} void PlayerController::update() { // friction - if(_player->inCollision()) { + if (_player->inCollision()) { _player->setVelocity(_player->velocity() * (1.0 - Time::deltaTime() * 2)); } - if(_isInSlowMo) { - if(_player->ability() > 0) { + if (_isInSlowMo) { + if (_player->ability() > 0) { _player->setAbility(_player->ability() - Time::deltaTime()); } else { _player->setAbility(0); _isInSlowMo = false; _player->setVelocity(_player->velocity() * ShooterConsts::SLOW_MO_COEFFICIENT); - _player->setAcceleration(_player->acceleration() * ShooterConsts::SLOW_MO_COEFFICIENT * ShooterConsts::SLOW_MO_COEFFICIENT); + _player->setAcceleration( + _player->acceleration() * ShooterConsts::SLOW_MO_COEFFICIENT * ShooterConsts::SLOW_MO_COEFFICIENT); SoundController::stopSound(SoundTag("slowMo")); SoundController::playSound(SoundTag("unSlowMo"), ShooterConsts::UN_SLOW_MO_SOUND); } @@ -36,46 +38,64 @@ void PlayerController::update() { double coeff = _isInSlowMo ? 1.0 / ShooterConsts::SLOW_MO_COEFFICIENT : 1.0; bool inRunning_old = _inRunning; - _inRunning = ( Keyboard::isKeyPressed(sf::Keyboard::A) || - Keyboard::isKeyPressed(sf::Keyboard::D) || - Keyboard::isKeyPressed(sf::Keyboard::W) || - Keyboard::isKeyPressed(sf::Keyboard::S)); + _inRunning = (Keyboard::isKeyPressed(sf::Keyboard::A) || + Keyboard::isKeyPressed(sf::Keyboard::D) || + Keyboard::isKeyPressed(sf::Keyboard::W) || + Keyboard::isKeyPressed(sf::Keyboard::S)); std::shared_ptr camera = _player->attached(ObjectNameTag("Camera")); - if(camera != nullptr && _inRunning && _player->inCollision()) { + if (camera != nullptr && _inRunning && _player->inCollision()) { if (!Timeline::isInAnimList(AnimationListTag("camera_hor_oscil"))) { - Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_hor_oscil"), + std::make_shared(camera, -camera->left() / 6, 0.3, Animation::LoopOut::None, + Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared(0)); - Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_hor_oscil"), + std::make_shared(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, + Animation::InterpolationType::Cos)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), + std::make_shared(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, + Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(0)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), + std::make_shared(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, + Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(0)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), + std::make_shared(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, + Animation::InterpolationType::Cos)); Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(0)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), + std::make_shared(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, + Animation::InterpolationType::Cos)); - Timeline::animate(AnimationListTag("camera_init"), std::make_shared( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_init"), + std::make_shared(camera, _player->position() + Vec3D{0, 1.8, 0}, 0.3, + Animation::LoopOut::None, + Animation::InterpolationType::Cos)); } - } else if(camera != nullptr && inRunning_old && !_inRunning) { + } else if (camera != nullptr && inRunning_old && !_inRunning) { Timeline::deleteAnimationList(AnimationListTag("camera_hor_oscil")); Timeline::deleteAnimationList(AnimationListTag("camera_vert_oscil")); Timeline::deleteAnimationList(AnimationListTag("camera_init")); - Timeline::animate(AnimationListTag("camera_init"), std::make_shared( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_init"), + std::make_shared(camera, _player->position() + Vec3D{0, 1.8, 0}, 0.15, + Animation::LoopOut::None, + Animation::InterpolationType::Cos)); } // Left and right if (Keyboard::isKeyPressed(sf::Keyboard::A)) { _player->translate(_player->left() * Time::deltaTime() * ShooterConsts::WALK_SPEED * coeff); - if(_player->inCollision()) { + if (_player->inCollision()) { _player->setVelocity(Vec3D{0, 0, 0}); } } if (Keyboard::isKeyPressed(sf::Keyboard::D)) { _player->translate(-_player->left() * Time::deltaTime() * ShooterConsts::WALK_SPEED * coeff); - if(_player->inCollision()) { + if (_player->inCollision()) { _player->setVelocity(Vec3D{0, 0, 0}); } } @@ -83,7 +103,7 @@ void PlayerController::update() { // Forward and backward if (Keyboard::isKeyPressed(sf::Keyboard::W)) { _player->translate(_player->lookAt() * Time::deltaTime() * ShooterConsts::WALK_SPEED * coeff); - if(_player->inCollision()) { + if (_player->inCollision()) { _player->setVelocity(Vec3D{0, 0, 0}); } } @@ -91,7 +111,7 @@ void PlayerController::update() { if (Keyboard::isKeyPressed(sf::Keyboard::S)) { _player->translate(-_player->lookAt() * Time::deltaTime() * ShooterConsts::WALK_SPEED * coeff); - if(_player->inCollision()) { + if (_player->inCollision()) { _player->setVelocity(Vec3D{0, 0, 0}); } } @@ -100,7 +120,9 @@ void PlayerController::update() { // slow mo _isInSlowMo = true; _player->setVelocity(_player->velocity() / ShooterConsts::SLOW_MO_COEFFICIENT); - _player->setAcceleration(Vec3D(0, -ShooterConsts::GRAVITY / (ShooterConsts::SLOW_MO_COEFFICIENT * ShooterConsts::SLOW_MO_COEFFICIENT), 0)); + _player->setAcceleration(Vec3D(0, -ShooterConsts::GRAVITY / + (ShooterConsts::SLOW_MO_COEFFICIENT * ShooterConsts::SLOW_MO_COEFFICIENT), + 0)); SoundController::stopSound(SoundTag("unSlowMo")); SoundController::playSound(SoundTag("slowMo"), ShooterConsts::SLOW_MO_SOUND); } else if (_isInSlowMo && !Keyboard::isKeyPressed(sf::Keyboard::LShift)) { @@ -114,8 +136,8 @@ void PlayerController::update() { if (Mouse::isButtonPressed(sf::Mouse::Button::Left)) { bool shot = _player->fire(); - if(shot) { - if(_player->weaponName() == ObjectNameTag("shotgun")) { + if (shot) { + if (_player->weaponName() == ObjectNameTag("shotgun")) { _player->addVelocity(-camera->lookAt() * 30 * coeff); } } @@ -133,7 +155,7 @@ void PlayerController::update() { sqrt(2 * -_player->acceleration().y() * ShooterConsts::JUMP_HEIGHT) * coeff * Time::deltaTime() * 60, 0}); } - _player->translate(Vec3D{ 0, Time::deltaTime() * ShooterConsts::WALK_SPEED * 2 * coeff, 0 }); + _player->translate(Vec3D{0, Time::deltaTime() * ShooterConsts::WALK_SPEED * 2 * coeff, 0}); _isSliding = true; } else { _isSliding = false; @@ -143,7 +165,8 @@ void PlayerController::update() { Vec2D displacement = _mouse->getMouseDisplacement(); _player->rotate(Vec3D{0, -displacement.x() * ShooterConsts::MOUSE_SENSITIVITY, 0}); - _player->setVelocity(Matrix4x4::RotationY(-displacement.x() * ShooterConsts::MOUSE_SENSITIVITY) * _player->velocity()); + _player->setVelocity( + Matrix4x4::RotationY(-displacement.x() * ShooterConsts::MOUSE_SENSITIVITY) * _player->velocity()); double rotationLeft = displacement.y() * ShooterConsts::MOUSE_SENSITIVITY; @@ -158,7 +181,7 @@ void PlayerController::update() { _player->setHeadAngle(_player->headAngle() + rotationLeft); _player->rotateWeaponsRelativePoint(_player->position() + Vec3D{0, 1.8, 0}, _player->left(), rotationLeft); - if(camera != nullptr) { + if (camera != nullptr) { camera->rotateLeft(_player->headAngle() - camera->angleLeftUpLookAt().x()); } @@ -170,18 +193,19 @@ void PlayerController::update() { _player->previousWeapon(); } - if(Keyboard::isKeyPressed(sf::Keyboard::R)) { + if (Keyboard::isKeyPressed(sf::Keyboard::R)) { _player->reload(); } bool walkSoundPlayed = false; - for(int k = 1; k < 7; k++) { - if(SoundController::getStatus(SoundTag("walkSound_" + std::to_string(k))) == sf::Sound::Status::Playing) { + for (int k = 1; k < 7; k++) { + if (SoundController::getStatus(SoundTag("walkSound_" + std::to_string(k))) == sf::Sound::Status::Playing) { walkSoundPlayed = true; } } if ((_inRunning || _player->velocity().sqrAbs() > 3) && _player->inCollision() && !walkSoundPlayed) { - int soundNum = (int)((double) rand() / RAND_MAX * 6) + 1; - SoundController::playSound(SoundTag("walkSound_" + std::to_string(soundNum)), "sound/stonestep" + std::to_string(soundNum) + ".ogg"); + int soundNum = (int) ((double) rand() / RAND_MAX * 6) + 1; + SoundController::playSound(SoundTag("walkSound_" + std::to_string(soundNum)), + "sound/stonestep" + std::to_string(soundNum) + ".ogg"); } } diff --git a/PlayerController.h b/PlayerController.h index 0130a60..1d81e55 100644 --- a/PlayerController.h +++ b/PlayerController.h @@ -21,6 +21,7 @@ private: public: PlayerController(std::shared_ptr player, std::shared_ptr keyboard, std::shared_ptr mouse); + void update(); }; diff --git a/Shooter.cpp b/Shooter.cpp index ff8c8b6..806aaf6 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -24,8 +24,8 @@ void Shooter::InitNetwork() { std::ifstream connectFile("connect.txt", std::ifstream::in); // If failed to read client settings - if (!connectFile.is_open() || !(connectFile >> clientIp >> clientPort >> playerName) || sf::IpAddress(clientIp) == sf::IpAddress::None) - { + if (!connectFile.is_open() || !(connectFile >> clientIp >> clientPort >> playerName) || + sf::IpAddress(clientIp) == sf::IpAddress::None) { connectFile.close(); // Create file and write default settings clientIp = "127.0.0.1"; @@ -39,8 +39,7 @@ void Shooter::InitNetwork() { // If failed to read server settings connectFile.open("server.txt", std::ifstream::in); - if (!connectFile.is_open() || !(connectFile >> serverPort)) - { + if (!connectFile.is_open() || !(connectFile >> serverPort)) { connectFile.close(); // Create file and write default settings serverPort = 54000; @@ -52,7 +51,7 @@ void Shooter::InitNetwork() { if (clientIp == sf::IpAddress::LocalHost) { server->start(serverPort); - if(server->isWorking()) + if (server->isWorking()) server->generateBonuses(); } @@ -60,12 +59,14 @@ void Shooter::InitNetwork() { 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); }); + 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,12 +80,17 @@ void Shooter::start() { player->scale(Vec3D(3, 1, 3)); // 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"); }); - 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)); }); + 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"); }); + 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)); }); player->initWeapons(); @@ -112,10 +118,23 @@ void Shooter::start() { mainMenu.setTitle("Main menu"); mainMenu.setBackgroundTexture(ShooterConsts::MAIN_MENU_BACK, 1.1, 1.1, screen->width(), screen->height()); - mainMenu.addButton(screen->width()/2, 200, 200, 20, [this] () { this->play(); SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND);}, "Server: " + client->serverIp().toString(), 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); - mainMenu.addButton(screen->width()/2, 350, 200, 20, [this] () { this->player->translateToPoint(Vec3D{0, 0, 0}); this->player->setVelocity({}); this->play(); SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND);}, "Respawn", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); + mainMenu.addButton(screen->width() / 2, 200, 200, 20, [this]() { + this->play(); + SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND); + }, "Server: " + client->serverIp().toString(), 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, + Consts::MEDIUM_FONT, {255, 255, 255}); + mainMenu.addButton(screen->width() / 2, 350, 200, 20, [this]() { + this->player->translateToPoint(Vec3D{0, 0, 0}); + this->player->setVelocity({}); + this->play(); + SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND); + }, "Respawn", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); - mainMenu.addButton(screen->width()/2, 500, 200, 20, [this] () { client->disconnect(); server->stop(); this->exit();}, "Exit", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); + mainMenu.addButton(screen->width() / 2, 500, 200, 20, [this]() { + client->disconnect(); + server->stop(); + this->exit(); + }, "Exit", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); } void Shooter::update() { @@ -129,12 +148,12 @@ void Shooter::update() { return; } - if(keyboard->isKeyTapped(sf::Keyboard::Escape)) { + if (keyboard->isKeyTapped(sf::Keyboard::Escape)) { inGame = !inGame; screen->setMouseCursorVisible(!inGame); } - if(inGame) { + if (inGame) { screen->setTitle(ShooterConsts::PROJECT_NAME); playerController->update(); mouse->setMouseInCenter(); @@ -145,7 +164,7 @@ void Shooter::update() { setUpdateWorld(inGame); // background sounds and music control - if(SoundController::getStatus(SoundTag("background")) != sf::Sound::Status::Playing) { + if (SoundController::getStatus(SoundTag("background")) != sf::Sound::Status::Playing) { SoundController::playSound(SoundTag("background"), ShooterConsts::BACK_NOISE); } } @@ -156,7 +175,8 @@ void Shooter::gui() { sprite.setTexture(*ResourceManager::loadTexture(ShooterConsts::MAIN_MENU_GUI)); sprite.setTextureRect(sf::IntRect(243, 3, 9, 9)); sprite.scale(3, 3); - sprite.setPosition(static_cast(screen->width()) / 2.0f - 27.0f/2.0f, static_cast(screen->height()) / 2.0f - 27.0f/2.0f); + sprite.setPosition(static_cast(screen->width()) / 2.0f - 27.0f / 2.0f, + static_cast(screen->height()) / 2.0f - 27.0f / 2.0f); sprite.setColor(sf::Color(0, 0, 0, 250)); screen->drawSprite(sprite); @@ -168,20 +188,21 @@ void Shooter::gui() { void Shooter::drawStatsTable() { int i = 1; - screen->drawText(client->lastEvent(),Vec2D{10, 10},25, sf::Color(0, 0, 0, 100)); + screen->drawText(client->lastEvent(), Vec2D{10, 10}, 25, sf::Color(0, 0, 0, 100)); vector> allPlayers; allPlayers.push_back(player); - for(auto& [playerId, player] : client->players()) + for (auto&[playerId, player] : client->players()) allPlayers.push_back(player); - std::sort(allPlayers.begin(), allPlayers.end(), [](std::shared_ptr p1, std::shared_ptr p2){ + std::sort(allPlayers.begin(), allPlayers.end(), [](std::shared_ptr p1, std::shared_ptr p2) { return p1->kills() - p1->deaths() > p2->kills() - p2->deaths(); - } ); + }); - for(auto& p : allPlayers) { - screen->drawText(std::to_string(i) + "\t" + p->playerNickName() + "\t" + std::to_string(p->kills()) + " / " + std::to_string(p->deaths()), - Vec2D{10, 15 + 35.0*i}, 25, sf::Color(0, 0, 0, 150)); + for (auto &p : allPlayers) { + screen->drawText(std::to_string(i) + "\t" + p->playerNickName() + "\t" + std::to_string(p->kills()) + " / " + + std::to_string(p->deaths()), + Vec2D{10, 15 + 35.0 * i}, 25, sf::Color(0, 0, 0, 150)); i++; } } @@ -191,25 +212,29 @@ void Shooter::drawPlayerStats() { double xPos = 10; double yPos = screen->height() - 20; - int width = screen->width()/2 - 20; + int width = screen->width() / 2 - 20; int height = 10; screen->drawTetragon(Vec2D{xPos, yPos}, Vec2D{xPos + width * player->health() / ShooterConsts::HEALTH_MAX, yPos}, Vec2D{xPos + width * player->health() / ShooterConsts::HEALTH_MAX, yPos + height}, Vec2D{xPos, yPos + height}, - { static_cast((ShooterConsts::HEALTH_MAX - player->health())/ShooterConsts::HEALTH_MAX * 255), static_cast(player->health() * 255 / ShooterConsts::HEALTH_MAX), 0, 100 }); + {static_cast((ShooterConsts::HEALTH_MAX - player->health()) / + ShooterConsts::HEALTH_MAX * 255), + static_cast(player->health() * 255 / ShooterConsts::HEALTH_MAX), 0, 100}); screen->drawTetragon(Vec2D{xPos, yPos - 15}, Vec2D{xPos + width * player->ability() / ShooterConsts::ABILITY_MAX, yPos - 15}, Vec2D{xPos + width * player->ability() / ShooterConsts::ABILITY_MAX, yPos - 15 + height}, Vec2D{xPos, yPos - 15 + height}, - { 255, 168, 168, 100 }); + {255, 168, 168, 100}); auto balance = player->balance(); - screen->drawText(std::to_string((int)balance.first), Vec2D{150, static_cast(screen->height() - 150)}, 100, sf::Color(0, 0, 0, 100)); - screen->drawText(std::to_string((int)balance.second), Vec2D{50, static_cast(screen->height() - 100)}, 50, sf::Color(0, 0, 0, 70)); + screen->drawText(std::to_string((int) balance.first), Vec2D{150, static_cast(screen->height() - 150)}, 100, + sf::Color(0, 0, 0, 100)); + screen->drawText(std::to_string((int) balance.second), Vec2D{50, static_cast(screen->height() - 100)}, 50, + sf::Color(0, 0, 0, 70)); } void Shooter::play() { @@ -263,23 +288,28 @@ void Shooter::addFireTrace(const Vec3D &from, const Vec3D &to) { world->addBody(std::make_shared(Mesh::LineTo(ObjectNameTag(traceName), from, to, 0.05))); world->body(ObjectNameTag(traceName))->setCollider(false); - Timeline::animate(AnimationListTag(traceName + "_fadeOut"), std::make_shared(world->body(ObjectNameTag(traceName)), sf::Color{150, 150, 150, 0})); - Timeline::animate(AnimationListTag(traceName + "_delete"), std::make_shared([this, traceName](){ removeFireTrace(ObjectNameTag(traceName)); }, 1, 2)); + Timeline::animate(AnimationListTag(traceName + "_fadeOut"), + std::make_shared(world->body(ObjectNameTag(traceName)), sf::Color{150, 150, 150, 0})); + Timeline::animate(AnimationListTag(traceName + "_delete"), + std::make_shared([this, traceName]() { removeFireTrace(ObjectNameTag(traceName)); }, 1, + 2)); } -void Shooter::removeFireTrace(const ObjectNameTag& traceName) { +void Shooter::removeFireTrace(const ObjectNameTag &traceName) { world->removeBody(traceName); } void Shooter::addBonus(const string &bonusName, const Vec3D &position) { - std::string name = bonusName.substr(6, bonusName.size()-3-5); + std::string name = bonusName.substr(6, bonusName.size() - 3 - 5); ObjectNameTag nameTag(bonusName); world->addBody(std::make_shared(ObjectNameTag(bonusName), "obj/" + name + ".obj", Vec3D{3, 3, 3})); world->body(ObjectNameTag(bonusName))->translateToPoint(position); world->body(ObjectNameTag(bonusName))->setCollider(false); - Timeline::animate(AnimationListTag(bonusName + "_rotation"), std::make_shared(world->body(ObjectNameTag(bonusName)), Vec3D{0, 2*Consts::PI, 0}, 4, Animation::LoopOut::Continue, Animation::InterpolationType::Linear)); + Timeline::animate(AnimationListTag(bonusName + "_rotation"), + std::make_shared(world->body(ObjectNameTag(bonusName)), Vec3D{0, 2 * Consts::PI, 0}, 4, + Animation::LoopOut::Continue, Animation::InterpolationType::Linear)); } void Shooter::removeBonus(const ObjectNameTag &bonusName) { @@ -290,12 +320,12 @@ void Shooter::removeBonus(const ObjectNameTag &bonusName) { void Shooter::addWeapon(std::shared_ptr weapon) { world->addBody(weapon); - if(client != nullptr) { + if (client != nullptr) { client->changeWeapon(weapon->name().str()); } } -void Shooter::changeEnemyWeapon(const std::string& weaponName, sf::Uint16 enemyId) { +void Shooter::changeEnemyWeapon(const std::string &weaponName, sf::Uint16 enemyId) { ObjectNameTag weaponTag("Enemy_" + std::to_string(enemyId) + "_weapon"); auto head = world->body(ObjectNameTag("Enemy_" + std::to_string(enemyId) + "_head")); auto enemy = world->body(ObjectNameTag("Enemy_" + std::to_string(enemyId))); @@ -308,9 +338,9 @@ void Shooter::changeEnemyWeapon(const std::string& weaponName, sf::Uint16 enemyI world->body(weaponTag)->setCollider(false); world->body(weaponTag)->scale(Vec3D(3, 3, 3)); - world->body(weaponTag)->translateToPoint(head->position() - enemy->left() - enemy->up()); + world->body(weaponTag)->translateToPoint(head->position() - enemy->left()*2 - enemy->up()*0.5); - world->body(weaponTag)->rotate(Vec3D(0, Consts::PI + head->angle().y(), 0)); + world->body(weaponTag)->rotate(Vec3D(0, Consts::PI + enemy->angle().y(), 0)); world->body(weaponTag)->rotateLeft(-head->angleLeftUpLookAt().x()); enemy->attach(world->body(weaponTag)); } diff --git a/Shooter.h b/Shooter.h index 6c37f25..452e4bb 100644 --- a/Shooter.h +++ b/Shooter.h @@ -30,6 +30,7 @@ private: int fireTraces = 0; void start() override; + void update() override; void gui() override; @@ -37,18 +38,29 @@ private: void play(); void drawPlayerStats(); + void drawStatsTable(); + void InitNetwork(); void spawnPlayer(sf::Uint16 id); + void removePlayer(sf::Uint16 id); - void addFireTrace(const Vec3D& from, const Vec3D& to); - void removeFireTrace(const ObjectNameTag& traceName); - void addBonus(const std::string& bonusName, const Vec3D& position); - void removeBonus(const ObjectNameTag& bonusName); + + void addFireTrace(const Vec3D &from, const Vec3D &to); + + void removeFireTrace(const ObjectNameTag &traceName); + + void addBonus(const std::string &bonusName, const Vec3D &position); + + void removeBonus(const ObjectNameTag &bonusName); + void addWeapon(std::shared_ptr weapon); + void removeWeapon(std::shared_ptr weapon); - void changeEnemyWeapon(const std::string& weaponName, sf::Uint16 enemyId); + + void changeEnemyWeapon(const std::string &weaponName, sf::Uint16 enemyId); + public: Shooter() : mainMenu(screen, mouse) {}; }; diff --git a/ShooterClient.cpp b/ShooterClient.cpp index 7d9d6eb..3d5535c 100644 --- a/ShooterClient.cpp +++ b/ShooterClient.cpp @@ -7,28 +7,27 @@ #include #include "engine/utils/Log.h" #include "engine/animation/Timeline.h" -#include "engine/animation/ATranslateToPoint.h" #include "ShooterMsgType.h" void ShooterClient::updatePacket() { sf::Packet packet; - packet << MsgType::ClientUpdate << _player->position().x() << _player->position().y() << _player->position().z() << _player->angle().y() << _player->headAngle() << _player->playerNickName(); + packet << MsgType::ClientUpdate << _player->position().x() << _player->position().y() << _player->position().z() + << _player->angle().y() << _player->headAngle() << _player->playerNickName(); _socket.send(packet, _socket.serverId()); } -void ShooterClient::processInit(sf::Packet& packet) { +void ShooterClient::processInit(sf::Packet &packet) { sf::Uint16 targetId; double buf[4]; int kills, deaths; - while (packet >> targetId >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> kills >> deaths) - { - if(targetId != _socket.ownId()) { - if(_spawnPlayerCallBack != nullptr) { + while (packet >> targetId >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> kills >> deaths) { + if (targetId != _socket.ownId()) { + if (_spawnPlayerCallBack != nullptr) { _spawnPlayerCallBack(targetId); } - _players[targetId]->translateToPoint(Vec3D{ buf[0], buf[1], buf[2]}); + _players[targetId]->translateToPoint(Vec3D{buf[0], buf[1], buf[2]}); _players[targetId]->setHealth(buf[3]); _players[targetId]->setKills(kills); _players[targetId]->setDeaths(deaths); @@ -36,7 +35,7 @@ void ShooterClient::processInit(sf::Packet& packet) { } } -void ShooterClient::processUpdate(sf::Packet& packet) { +void ShooterClient::processUpdate(sf::Packet &packet) { sf::Uint16 targetId; double buf[6]; std::string playerName; @@ -51,13 +50,13 @@ void ShooterClient::processUpdate(sf::Packet& packet) { _players[targetId]->rotateToAngle(Vec3D{0, buf[4], 0}); _players[targetId]->setPlayerNickName(playerName); - auto head = _players[targetId]->attached(ObjectNameTag(name + "_head")); + auto head = _players[targetId]->attached(ObjectNameTag(name + "_head")); auto weapon = _players[targetId]->attached(ObjectNameTag("Enemy_" + std::to_string(targetId) + "_weapon")); - if(head != nullptr) { + if (head != nullptr) { head->rotateLeft(buf[5] - _players[targetId]->headAngle()); } - if(weapon != nullptr) { + if (weapon != nullptr) { weapon->rotateLeft(-(buf[5] - _players[targetId]->headAngle())); } @@ -68,12 +67,12 @@ void ShooterClient::processUpdate(sf::Packet& packet) { } } -void ShooterClient::processNewClient(sf::Packet& packet) { +void ShooterClient::processNewClient(sf::Packet &packet) { sf::Uint16 targetId; packet >> targetId; - if(_spawnPlayerCallBack != nullptr) { + if (_spawnPlayerCallBack != nullptr) { _spawnPlayerCallBack(targetId); } } @@ -86,7 +85,7 @@ void ShooterClient::processDisconnect(sf::Uint16 targetId) { } -void ShooterClient::processCustomPacket(sf::Packet& packet) { +void ShooterClient::processCustomPacket(sf::Packet &packet) { sf::Uint16 buffId[2]; double dbuff[10]; std::string tmp, tmp2; @@ -98,27 +97,27 @@ void ShooterClient::processCustomPacket(sf::Packet& packet) { case ShooterMsgType::Kill: packet >> buffId[0] >> buffId[1]; _lastEvent = ""; - if(buffId[1] == _socket.ownId()) { + if (buffId[1] == _socket.ownId()) { _player->addKill(); SoundController::playSound(SoundTag("kill"), ShooterConsts::KILL_SOUND); _lastEvent += _player->playerNickName(); - } - else { + } else { _players[buffId[1]]->addKill(); _lastEvent += _players[buffId[1]]->playerNickName(); } _lastEvent += " ~> "; - if(buffId[0] == _socket.ownId()) { + if (buffId[0] == _socket.ownId()) { _player->addDeath(); // respawn - _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->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->setFullAbility(); SoundController::playSound(SoundTag("death"), ShooterConsts::DEATH_SOUND); _lastEvent += _player->playerNickName(); - } - else { + } else { _players[buffId[0]]->addDeath(); _lastEvent += _players[buffId[0]]->playerNickName(); } @@ -126,14 +125,14 @@ void ShooterClient::processCustomPacket(sf::Packet& packet) { case ShooterMsgType::FireTrace: packet >> dbuff[0] >> dbuff[1] >> dbuff[2] >> dbuff[3] >> dbuff[4] >> dbuff[5]; - if(_addFireTraceCallBack != nullptr) { + if (_addFireTraceCallBack != nullptr) { _addFireTraceCallBack(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) { + if (_addBonusCallBack != nullptr) { _addBonusCallBack(tmp, Vec3D(dbuff[0], dbuff[1], dbuff[2])); } } @@ -141,26 +140,27 @@ void ShooterClient::processCustomPacket(sf::Packet& packet) { case ShooterMsgType::AddBonus: packet >> tmp >> dbuff[0] >> dbuff[1] >> dbuff[2]; - if(_addBonusCallBack != nullptr) { + if (_addBonusCallBack != nullptr) { _addBonusCallBack(tmp, Vec3D(dbuff[0], dbuff[1], dbuff[2])); } break; case ShooterMsgType::RemoveBonus: packet >> tmp; - if(_removeBonusCallBack != nullptr) { + if (_removeBonusCallBack != nullptr) { _removeBonusCallBack(ObjectNameTag(tmp)); } break; case ShooterMsgType::ChangeWeapon: packet >> buffId[0] >> tmp; - if(_changeEnemyWeaponCallBack != nullptr) { + if (_changeEnemyWeaponCallBack != nullptr) { _changeEnemyWeaponCallBack(tmp, buffId[0]); } break; default: - Log::log("ShooterClient::processCustomPacket: unknown message type " + std::to_string(static_cast(type))); + Log::log("ShooterClient::processCustomPacket: unknown message type " + + std::to_string(static_cast(type))); return; } } @@ -180,20 +180,21 @@ 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::addTrace(const Vec3D &from, const Vec3D &to) { sf::Packet packet; - packet << MsgType::Custom << ShooterMsgType::FireTrace << from.x() << from.y() << from.z() << to.x() << to.y() << to.z(); + packet << MsgType::Custom << ShooterMsgType::FireTrace << from.x() << from.y() << from.z() << to.x() << to.y() + << to.z(); _socket.send(packet, _socket.serverId()); } -void ShooterClient::takeBonus(const std::string& bonusName) { +void ShooterClient::takeBonus(const std::string &bonusName) { sf::Packet packet; packet << MsgType::Custom << ShooterMsgType::RemoveBonus << bonusName; _socket.sendRely(packet, _socket.serverId()); - if(_removeBonusCallBack != nullptr) { + if (_removeBonusCallBack != nullptr) { _removeBonusCallBack(ObjectNameTag(bonusName)); } } @@ -206,7 +207,7 @@ void ShooterClient::changeWeapon(const std::string &weaponName) { } void ShooterClient::addPlayer(sf::Uint16 id, std::shared_ptr player) { - _players.insert({ id, player }); + _players.insert({id, player}); } void ShooterClient::setSpawnPlayerCallBack(std::function spawn) { @@ -229,6 +230,7 @@ void ShooterClient::setRemoveBonusCallBack(std::function changeEnemyWeapon) { +void +ShooterClient::setChangeEnemyWeaponCallBack(std::function changeEnemyWeapon) { _changeEnemyWeaponCallBack = std::move(changeEnemyWeapon); } diff --git a/ShooterClient.h b/ShooterClient.h index 2a29247..9d40cb1 100644 --- a/ShooterClient.h +++ b/ShooterClient.h @@ -17,42 +17,50 @@ private: std::function _spawnPlayerCallBack; std::function _removePlayerCallBack; - std::function _addFireTraceCallBack; - std::function _addBonusCallBack; - std::function _removeBonusCallBack; - std::function _changeEnemyWeaponCallBack; + std::function _addFireTraceCallBack; + std::function _addBonusCallBack; + std::function _removeBonusCallBack; + std::function _changeEnemyWeaponCallBack; public: - explicit ShooterClient(std::shared_ptr player) : _player(player){}; + explicit ShooterClient(std::shared_ptr player) : _player(player) {}; 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 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; + + void processNewClient(sf::Packet &packet) override; - void processInit(sf::Packet& packet) override; - void processUpdate(sf::Packet& packet) override; - void processNewClient(sf::Packet& packet) override; void processDisconnect(sf::Uint16 targetId) override; - void processCustomPacket(sf::Packet& packet) override; + void processCustomPacket(sf::Packet &packet) override; void processDisconnected() override; void damagePlayer(sf::Uint16 targetId, double damage); - void takeBonus(const std::string& bonusName); + void takeBonus(const std::string &bonusName); - void addTrace(const Vec3D& from, const Vec3D& to); + void addTrace(const Vec3D &from, const Vec3D &to); - void changeWeapon(const std::string& weaponName); + void changeWeapon(const std::string &weaponName); void addPlayer(sf::Uint16 id, std::shared_ptr player); - [[nodiscard]] std::map>const & players() const { return _players; } + + [[nodiscard]] std::map> const &players() const { return _players; } [[nodiscard]] std::string lastEvent() const { return _lastEvent; } }; diff --git a/ShooterMsgType.cpp b/ShooterMsgType.cpp index 9bbc0b6..b0891ea 100644 --- a/ShooterMsgType.cpp +++ b/ShooterMsgType.cpp @@ -4,15 +4,13 @@ #include "ShooterMsgType.h" -sf::Packet& operator<<(sf::Packet& packet, ShooterMsgType type) -{ - return packet << (sf::Uint16)type; +sf::Packet &operator<<(sf::Packet &packet, ShooterMsgType type) { + return packet << (sf::Uint16) type; } -sf::Packet& operator>>(sf::Packet& packet, ShooterMsgType& type) -{ +sf::Packet &operator>>(sf::Packet &packet, ShooterMsgType &type) { sf::Uint16 temp; packet >> temp; - type = (ShooterMsgType)temp; + type = (ShooterMsgType) temp; return packet; } \ No newline at end of file diff --git a/ShooterMsgType.h b/ShooterMsgType.h index f4b6dfe..91290e8 100644 --- a/ShooterMsgType.h +++ b/ShooterMsgType.h @@ -7,8 +7,7 @@ #include -enum class ShooterMsgType -{ +enum class ShooterMsgType { Damage, Kill, FireTrace, @@ -18,7 +17,8 @@ enum class ShooterMsgType ChangeWeapon }; -sf::Packet& operator<<(sf::Packet& packet, ShooterMsgType type); -sf::Packet& operator>>(sf::Packet& packet, ShooterMsgType& type); +sf::Packet &operator<<(sf::Packet &packet, ShooterMsgType type); + +sf::Packet &operator>>(sf::Packet &packet, ShooterMsgType &type); #endif //SHOOTER_SHOOTERMSGTYPE_H diff --git a/ShooterServer.cpp b/ShooterServer.cpp index 69481d3..f66cae7 100644 --- a/ShooterServer.cpp +++ b/ShooterServer.cpp @@ -10,11 +10,12 @@ void ShooterServer::broadcast() { sf::Packet updatePacket; updatePacket << MsgType::ServerUpdate; - for (auto& [playerId, player] : _players) { - updatePacket << playerId << player->position().x() << player->position().y() << player->position().z() << player->health() << player->angle().y() << player->headAngle() << player->playerNickName(); + for (auto&[playerId, player] : _players) { + updatePacket << playerId << player->position().x() << player->position().y() << player->position().z() + << player->health() << player->angle().y() << player->headAngle() << player->playerNickName(); } - for (auto& player : _players) { + for (auto &player : _players) { _socket.send(updatePacket, player.first); } } @@ -27,9 +28,10 @@ void ShooterServer::processConnect(sf::Uint16 targetId) { // players init extraPacket << MsgType::NewClient << targetId; sendPacket1 << MsgType::Init << targetId; - _players.insert({ targetId, std::make_shared(ObjectNameTag("Player_" + std::to_string(targetId))) }); - for (const auto& [playerId, player] : _players) { - sendPacket1 << playerId << player->position().x() << player->position().y() << player->position().z() << player->health() << player->kills() << player->deaths(); + _players.insert({targetId, std::make_shared(ObjectNameTag("Player_" + std::to_string(targetId)))}); + for (const auto&[playerId, player] : _players) { + sendPacket1 << playerId << player->position().x() << player->position().y() << player->position().z() + << player->health() << player->kills() << player->deaths(); if (playerId != targetId) _socket.sendRely(extraPacket, playerId); } @@ -37,8 +39,8 @@ void ShooterServer::processConnect(sf::Uint16 targetId) { // bonuses init sendPacket2 << MsgType::Custom << ShooterMsgType::InitBonuses; - for(auto& [bonusName, bonusInfo] : _bonuses) { - if(bonusInfo->onTheMap) { + for (auto&[bonusName, bonusInfo] : _bonuses) { + if (bonusInfo->onTheMap) { sendPacket2 << bonusName << bonusInfo->position.x() << bonusInfo->position.y() << bonusInfo->position.z(); } } @@ -46,12 +48,12 @@ void ShooterServer::processConnect(sf::Uint16 targetId) { } -void ShooterServer::processClientUpdate(sf::Uint16 senderId, sf::Packet& packet) { +void ShooterServer::processClientUpdate(sf::Uint16 senderId, sf::Packet &packet) { double buf[5]; std::string playerName; packet >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> buf[4] >> playerName; - _players.at(senderId)->translateToPoint(Vec3D{ buf[0], buf[1], buf[2] }); + _players.at(senderId)->translateToPoint(Vec3D{buf[0], buf[1], buf[2]}); _players.at(senderId)->rotateToAngle(Vec3D{0, buf[3], 0}); _players.at(senderId)->setHeadAngle(buf[4]); _players.at(senderId)->setPlayerNickName(playerName); @@ -62,13 +64,13 @@ void ShooterServer::processDisconnect(sf::Uint16 senderId) { sendPacket << MsgType::Disconnect << senderId; _players.erase(senderId); - for (const auto& player : _players) { + for (const auto &player : _players) { _socket.sendRely(sendPacket, player.first); } } -void ShooterServer::processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) { +void ShooterServer::processCustomPacket(sf::Packet &packet, sf::Uint16 senderId) { sf::Packet sendPacket; double dbuff[10]; sf::Uint16 targetId; @@ -83,7 +85,7 @@ void ShooterServer::processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) case ShooterMsgType::Damage: packet >> targetId >> damage; newHealth = _players[targetId]->health() - damage; - if(newHealth > 0) { + if (newHealth > 0) { _players[targetId]->setHealth(newHealth); } else { _players[targetId]->setFullHealth(); @@ -92,15 +94,16 @@ void ShooterServer::processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) _players[senderId]->addKill(); sendPacket << MsgType::Custom << ShooterMsgType::Kill << targetId << senderId; - for (auto& player : _players) + for (auto &player : _players) _socket.sendRely(sendPacket, player.first); } break; case ShooterMsgType::FireTrace: packet >> dbuff[0] >> dbuff[1] >> dbuff[2] >> dbuff[3] >> dbuff[4] >> dbuff[5]; - sendPacket << MsgType::Custom << ShooterMsgType::FireTrace << dbuff[0] << dbuff[1] << dbuff[2] << dbuff[3] << dbuff[4] << dbuff[5]; - for (auto& player : _players) { - if(player.first != senderId) { + sendPacket << MsgType::Custom << ShooterMsgType::FireTrace << dbuff[0] << dbuff[1] << dbuff[2] << dbuff[3] + << dbuff[4] << dbuff[5]; + for (auto &player : _players) { + if (player.first != senderId) { _socket.send(sendPacket, player.first); } } @@ -109,18 +112,18 @@ void ShooterServer::processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) case ShooterMsgType::RemoveBonus: packet >> tmp; - if(tmp.find("Bonus_hill") != std::string::npos) { + if (tmp.find("Bonus_hill") != std::string::npos) { _players[senderId]->setFullHealth(); } - if(tmp.find("Bonus_ability") != std::string::npos) { + if (tmp.find("Bonus_ability") != std::string::npos) { _players[senderId]->setFullAbility(); } _bonuses[tmp] = std::make_shared(BonusInfo{_bonuses[tmp]->position, Time::time(), false}); sendPacket << MsgType::Custom << ShooterMsgType::RemoveBonus << tmp; - for (auto& player : _players) { - if(player.first != senderId) { + for (auto &player : _players) { + if (player.first != senderId) { _socket.sendRely(sendPacket, player.first); } } @@ -129,15 +132,16 @@ void ShooterServer::processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) packet >> tmp; sendPacket << MsgType::Custom << ShooterMsgType::ChangeWeapon << senderId << tmp; - for (auto& player : _players) { - if(player.first != senderId) { + for (auto &player : _players) { + if (player.first != senderId) { _socket.sendRely(sendPacket, player.first); } } break; default: - Log::log("ShooterServer::processCustomPacket: unknown message type " + std::to_string(static_cast(type))); + Log::log("ShooterServer::processCustomPacket: unknown message type " + + std::to_string(static_cast(type))); return; } } @@ -148,34 +152,49 @@ void ShooterServer::processStop() { } void ShooterServer::generateBonuses() { - _bonuses.insert({"Bonus_gun_1", std::make_shared(BonusInfo{Vec3D(-10, -2, -15), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_gun_2", std::make_shared(BonusInfo{Vec3D(10, -2, 15), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_gun_1", std::make_shared( + BonusInfo{Vec3D(-10, -2, -15), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_gun_2", std::make_shared( + BonusInfo{Vec3D(10, -2, 15), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_shotgun_1", std::make_shared(BonusInfo{Vec3D(-10, 13, -24), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_shotgun_2", std::make_shared(BonusInfo{Vec3D(10, 13, 24), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_shotgun_1", std::make_shared( + BonusInfo{Vec3D(-10, 13, -24), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_shotgun_2", std::make_shared( + BonusInfo{Vec3D(10, 13, 24), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_ak47_1", std::make_shared(BonusInfo{Vec3D(-25, 30, 50), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_ak47_2", std::make_shared(BonusInfo{Vec3D(25, 30, -50), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_ak47_1", std::make_shared( + BonusInfo{Vec3D(-25, 30, 50), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_ak47_2", std::make_shared( + BonusInfo{Vec3D(25, 30, -50), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_gold_ak47_1", std::make_shared(BonusInfo{Vec3D(-35, 80, 25), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_gold_ak47_2", std::make_shared(BonusInfo{Vec3D(35, 80, -25), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_gold_ak47_1", std::make_shared( + BonusInfo{Vec3D(-35, 80, 25), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_gold_ak47_2", std::make_shared( + BonusInfo{Vec3D(35, 80, -25), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_rifle_1", std::make_shared(BonusInfo{Vec3D(40, -2, 45), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_rifle_2", std::make_shared(BonusInfo{Vec3D(-40, -2, -45), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_rifle_1", std::make_shared( + BonusInfo{Vec3D(40, -2, 45), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_rifle_2", std::make_shared( + BonusInfo{Vec3D(-40, -2, -45), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_hill_1", std::make_shared(BonusInfo{Vec3D(-40, -2, 45), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_hill_2", std::make_shared(BonusInfo{Vec3D(40, -2, -45), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_hill_1", std::make_shared( + BonusInfo{Vec3D(-40, -2, 45), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_hill_2", std::make_shared( + BonusInfo{Vec3D(40, -2, -45), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_ability_1", std::make_shared(BonusInfo{Vec3D(25, 18, -33), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); - _bonuses.insert({"Bonus_ability_2", std::make_shared(BonusInfo{Vec3D(-25, 18, 33), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_ability_1", std::make_shared( + BonusInfo{Vec3D(25, 18, -33), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); + _bonuses.insert({"Bonus_ability_2", std::make_shared( + BonusInfo{Vec3D(-25, 18, 33), -2 * ShooterConsts::BONUS_RECHARGE_TIME, true})}); } void ShooterServer::updateInfo() { - for(auto& [bonusName, bonusInfo] : _bonuses) { - if(!bonusInfo->onTheMap && std::abs(Time::time() - bonusInfo->lastTake) > ShooterConsts::BONUS_RECHARGE_TIME) { + for (auto&[bonusName, bonusInfo] : _bonuses) { + if (!bonusInfo->onTheMap && std::abs(Time::time() - bonusInfo->lastTake) > ShooterConsts::BONUS_RECHARGE_TIME) { sf::Packet sendPacket; - sendPacket << MsgType::Custom << ShooterMsgType::AddBonus << bonusName << bonusInfo->position.x() << bonusInfo->position.y() << bonusInfo->position.z(); - for (const auto& player : _players) { + sendPacket << MsgType::Custom << ShooterMsgType::AddBonus << bonusName << bonusInfo->position.x() + << bonusInfo->position.y() << bonusInfo->position.z(); + for (const auto &player : _players) { _socket.sendRely(sendPacket, player.first); } bonusInfo = std::make_shared(BonusInfo{bonusInfo->position, bonusInfo->lastTake, true}); diff --git a/ShooterServer.h b/ShooterServer.h index fd88fc2..99e51c4 100644 --- a/ShooterServer.h +++ b/ShooterServer.h @@ -24,10 +24,12 @@ public: void broadcast() override; void processConnect(sf::Uint16 senderId) override; - void processClientUpdate(sf::Uint16 senderId, sf::Packet& packet) override; + + void processClientUpdate(sf::Uint16 senderId, sf::Packet &packet) override; + void processDisconnect(sf::Uint16 senderId) override; - void processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) override; + void processCustomPacket(sf::Packet &packet, sf::Uint16 senderId) override; void processStop() override; diff --git a/engine/Camera.cpp b/engine/Camera.cpp index de00794..31bcbd4 100644 --- a/engine/Camera.cpp +++ b/engine/Camera.cpp @@ -2,19 +2,20 @@ // Created by Иван Ильин on 14.01.2021. // +#include + #include "Camera.h" #include "utils/Log.h" #include "Consts.h" -#include std::vector> Camera::project(std::shared_ptr mesh) { - if(!_ready) { + if (!_ready) { Log::log("Camera::project(): cannot project _tris without camera initialization ( Camera::init() ) "); return _triangles; } - if(!mesh->isVisible()) { + if (!mesh->isVisible()) { return this->_triangles; } @@ -25,13 +26,13 @@ std::vector> Camera::project(std::shared_ptr mes // We don't want to waste time re-allocating memory every time std::vector clippedTriangles, tempBuffer; - for(auto& t : mesh->triangles()) { + for (auto &t : mesh->triangles()) { Triangle MTriangle = t * M; double dot = MTriangle.norm().dot((Vec3D(MTriangle[0]) - position()).normalized()); - if(dot > 0) { + if (dot > 0) { continue; } @@ -44,23 +45,23 @@ std::vector> Camera::project(std::shared_ptr mes // In the beginning we need to to translate triangle from world coordinate to our camera system: // After that we apply clipping for all planes from _clipPlanes clippedTriangles.emplace_back(VMTriangle); - for(auto& plane : _clipPlanes) { - while(!clippedTriangles.empty()) { + for (auto &plane : _clipPlanes) { + while (!clippedTriangles.empty()) { std::vector clipResult = plane.clip(clippedTriangles.back()); clippedTriangles.pop_back(); - for(auto & i : clipResult) { + for (auto &i : clipResult) { tempBuffer.emplace_back(i); } } clippedTriangles.swap(tempBuffer); } - for(auto& clipped : clippedTriangles) { + for (auto &clipped : clippedTriangles) { sf::Color color = clipped.color(); - sf::Color ambientColor = sf::Color((sf::Uint8)(color.r * (0.3 * std::abs(dot) + 0.7)), - (sf::Uint8)(color.g * (0.3 * std::abs(dot) + 0.7)), - (sf::Uint8)(color.b * (0.3 * std::abs(dot) + 0.7)), - (sf::Uint8)color.a); + sf::Color ambientColor = sf::Color((sf::Uint8) (color.r * (0.3 * std::abs(dot) + 0.7)), + (sf::Uint8) (color.g * (0.3 * std::abs(dot) + 0.7)), + (sf::Uint8) (color.b * (0.3 * std::abs(dot) + 0.7)), + (sf::Uint8) color.a); // Finally its time to project our clipped colored drawTriangle from 3D -> 2D // and transform it's coordinate to screen space (in pixels): @@ -81,7 +82,7 @@ std::vector> Camera::project(std::shared_ptr mes void Camera::init(int width, int height, double fov, double ZNear, double ZFar) { // We need to init camera only after creation or changing width, height, fov, ZNear or ZFar. // Because here we calculate matrix that does not change during the motion of _objects or camera - _aspect = (double)width / (double)height; + _aspect = (double) width / (double) height; Matrix4x4 P = Matrix4x4::Projection(fov, _aspect, ZNear, ZFar); Matrix4x4 S = Matrix4x4::ScreenSpace(width, height); @@ -92,7 +93,7 @@ void Camera::init(int width, int height, double fov, double ZNear, double ZFar) _clipPlanes.emplace_back(Plane(Vec3D{0, 0, 1}, Vec3D{0, 0, ZNear})); // near plane _clipPlanes.emplace_back(Plane(Vec3D{0, 0, -1}, Vec3D{0, 0, ZFar})); // far plane - double thetta1 = Consts::PI*fov*0.5/180.0; + double thetta1 = Consts::PI * fov * 0.5 / 180.0; double thetta2 = atan(_aspect * tan(thetta1)); _clipPlanes.emplace_back(Plane(Vec3D{-cos(thetta2), 0, sin(thetta2)}, Vec3D{0, 0, 0})); // left plane _clipPlanes.emplace_back(Plane(Vec3D{cos(thetta2), 0, sin(thetta2)}, Vec3D{0, 0, 0})); // right plane diff --git a/engine/Camera.h b/engine/Camera.h index db164b6..4064792 100644 --- a/engine/Camera.h +++ b/engine/Camera.h @@ -6,11 +6,13 @@ #define ENGINE_CAMERA_H #include -#include "Plane.h" -#include "Mesh.h" + #include -class Camera final : public Object{ +#include "Plane.h" +#include "Mesh.h" + +class Camera final : public Object { private: std::vector> _triangles{}; std::vector _clipPlanes{}; @@ -18,10 +20,10 @@ private: double _aspect = 0; Matrix4x4 _SP; - public: Camera() : Object(ObjectNameTag("Camera")) {}; - Camera(const Camera& camera) = delete; + + Camera(const Camera &camera) = delete; void init(int width, int height, double fov = 110.0, double ZNear = 0.1, double ZFar = 5000.0); @@ -30,6 +32,7 @@ public: void clear(); [[nodiscard]] int buffSize() const { return _triangles.size(); } + std::vector> sorted(); }; diff --git a/engine/Consts.h b/engine/Consts.h index ccaeb98..e46237e 100644 --- a/engine/Consts.h +++ b/engine/Consts.h @@ -4,7 +4,9 @@ #ifndef SHOOTER_CONSTS_H #define SHOOTER_CONSTS_H + #include + #include "Vec2D.h" namespace Consts { @@ -27,7 +29,7 @@ namespace Consts { const std::string THIN_FONT = "engine/fonts/Roboto-Thin.ttf"; const std::string MEDIUM_FONT = "engine/fonts/Roboto-Medium.ttf"; - const double LARGEST_TIME_STEP = 1.0/15.0; + const double LARGEST_TIME_STEP = 1.0 / 15.0; const double TAP_DELAY = 0.2; const Vec2D BEZIER[2] = {Vec2D{0.8, 0}, Vec2D{0.2, 1}}; @@ -35,10 +37,8 @@ namespace Consts { const unsigned NETWORK_VERSION = 3U; const int NETWORK_TIMEOUT = 5U; const int NETWORK_WORLD_UPDATE_RATE = 30; - const double NETWORK_RELIABLE_RETRY_TIME = 1.0/20; + const double NETWORK_RELIABLE_RETRY_TIME = 1.0 / 20; const uint16_t NETWORK_MAX_CLIENTS = 64; - - } #endif //SHOOTER_CONSTS_H diff --git a/engine/Engine.cpp b/engine/Engine.cpp index 667310c..84cd606 100644 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -2,9 +2,10 @@ // Created by Иван Ильин on 14.01.2021. // +#include + #include "Engine.h" #include "utils/Time.h" -#include #include "ResourceManager.h" #include "animation/Timeline.h" #include "SoundController.h" @@ -16,11 +17,13 @@ Engine::Engine() { SoundController::init(); } -void Engine::create(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background, sf::Uint32 style) { +void Engine::create(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background, + sf::Uint32 style) { _name = name; screen->open(screenWidth, screenHeight, name, verticalSync, background, style); - Log::log("Engine::create(): started engine (" + std::to_string(screenWidth) + "x" + std::to_string(screenHeight) + ") with title '" + name + "'."); + Log::log("Engine::create(): started engine (" + std::to_string(screenWidth) + "x" + std::to_string(screenHeight) + + ") with title '" + name + "'."); Time::update(); start(); @@ -36,18 +39,18 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, // sometimes we dont need to update physics world // (for example in menu or while pause) // hence we can set '_updateWorld' equal to false in setUpdateWorld(bool): - if(_updateWorld) { + if (_updateWorld) { Timeline::update(); world->update(); - if(_useOpenGL) { - GLfloat* view = camera->glView(); - for(auto & it : *world) { + if (_useOpenGL) { + GLfloat *view = camera->glView(); + for (auto &it : *world) { if (it.second->isVisible()) { - GLfloat* model = it.second->glModel(); - GLfloat* geometry = Screen::glMeshToGLfloatArray(it.second, camera->position()); + GLfloat *model = it.second->glModel(); + GLfloat *geometry = Screen::glMeshToGLfloatArray(it.second, camera->position()); screen->glDrawMesh(geometry, view, model, 3 * it.second->triangles().size()); free(geometry); free(model); @@ -58,7 +61,7 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, // clear triangles from previous frame camera->clear(); // project triangles to the camera plane - for(auto & it : *world) { + for (auto &it : *world) { camera->project(it.second); } // draw triangles on the screen @@ -70,7 +73,8 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, } if (Consts::SHOW_FPS_COUNTER) { - screen->drawText(std::to_string(Time::fps()) + " fps", Vec2D(screen->width() - 100, 10), 25, sf::Color(100,100,100)); + screen->drawText(std::to_string(Time::fps()) + " fps", Vec2D(screen->width() - 100, 10), 25, + sf::Color(100, 100, 100)); } printDebugText(); @@ -82,7 +86,7 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, } void Engine::exit() { - if(screen->isOpen()) { + if (screen->isOpen()) { screen->close(); } SoundController::free(); @@ -90,21 +94,21 @@ void Engine::exit() { Timeline::free(); Time::free(); - Log::log("Engine::exit(): exit engine (" + std::to_string(screen->width()) + "x" + std::to_string(screen->height()) + ") with title '" + - screen->title() + "'."); + Log::log("Engine::exit(): exit engine (" + std::to_string(screen->width()) + "x" + + std::to_string(screen->height()) + ") with title '" + screen->title() + "'."); } void Engine::printDebugText() const { if (_debugText) { std::string text = _name + "\n\n X: " + - std::to_string((camera->position().x())) + "\n Y: " + - std::to_string((camera->position().y())) + "\n Z: " + - std::to_string((camera->position().z())) + "\n\n" + - std::to_string(screen->width()) + "x" + - std::to_string(screen->height()) + "\t" + - std::to_string(Time::fps()) + " fps"; - if(_useOpenGL) { + std::to_string((camera->position().x())) + "\n Y: " + + std::to_string((camera->position().y())) + "\n Z: " + + std::to_string((camera->position().z())) + "\n\n" + + std::to_string(screen->width()) + "x" + + std::to_string(screen->height()) + "\t" + + std::to_string(Time::fps()) + " fps"; + if (_useOpenGL) { text += "\n Using OpenGL acceleration"; } else { text += "\n" + std::to_string((int) _triPerSec) + " tris/s"; diff --git a/engine/Engine.h b/engine/Engine.h index 46363c1..eb20a84 100644 --- a/engine/Engine.h +++ b/engine/Engine.h @@ -8,7 +8,6 @@ #include "Screen.h" #include "Keyboard.h" #include "Mouse.h" - #include "World.h" #include "Camera.h" #include "utils/Log.h" @@ -23,6 +22,7 @@ private: bool _useOpenGL = Consts::USE_OPEN_GL; void printDebugText() const; + protected: const std::shared_ptr screen = std::make_shared(); const std::shared_ptr keyboard = std::make_shared(); @@ -32,19 +32,26 @@ protected: const std::shared_ptr camera = std::make_shared(); virtual void start() {}; + virtual void update() {}; void setDebugText(bool value) { _debugText = value; } + void setUpdateWorld(bool value) { _updateWorld = value; } + void setGlEnable(bool value) { _useOpenGL = value; } - virtual void gui(){} + virtual void gui() {} + public: Engine(); virtual ~Engine() = default; - void create(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT, const std::string& name = Consts::PROJECT_NAME, bool verticalSync = true, sf::Color background = Consts::BACKGROUND_COLOR, sf::Uint32 style = sf::Style::Default); + void create(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT, + const std::string &name = Consts::PROJECT_NAME, bool verticalSync = true, + sf::Color background = Consts::BACKGROUND_COLOR, sf::Uint32 style = sf::Style::Default); + void exit(); }; diff --git a/engine/Keyboard.cpp b/engine/Keyboard.cpp index 989ddf6..753bec9 100644 --- a/engine/Keyboard.cpp +++ b/engine/Keyboard.cpp @@ -15,10 +15,10 @@ bool Keyboard::isKeyTapped(sf::Keyboard::Key key) { return false; } - if(_tappedKeys.count(key) == 0) { + if (_tappedKeys.count(key) == 0) { _tappedKeys.emplace(key, Time::time()); return true; - } else if((Time::time() - _tappedKeys[key]) > Consts::TAP_DELAY) { + } else if ((Time::time() - _tappedKeys[key]) > Consts::TAP_DELAY) { _tappedKeys[key] = Time::time(); return true; } diff --git a/engine/Keyboard.h b/engine/Keyboard.h index fe9560c..215de4f 100644 --- a/engine/Keyboard.h +++ b/engine/Keyboard.h @@ -13,8 +13,11 @@ private: public: Keyboard() = default; - static bool isKeyPressed(sf::Keyboard::Key key); // returns true if this key is _pressed - bool isKeyTapped(sf::Keyboard::Key key); // returns true if this key is tapped and 1/5 sec passed (_button bouncing problem solved) + // returns true if this key is _pressed + static bool isKeyPressed(sf::Keyboard::Key key); + + // returns true if this key is tapped and 1/5 sec passed (_button bouncing problem solved) + bool isKeyTapped(sf::Keyboard::Key key); }; diff --git a/engine/Matrix4x4.cpp b/engine/Matrix4x4.cpp index c4f8d49..2f02ed9 100644 --- a/engine/Matrix4x4.cpp +++ b/engine/Matrix4x4.cpp @@ -2,18 +2,17 @@ // Created by Иван Ильин on 12.01.2021. // -#include "Matrix4x4.h" -#include - #include + +#include "Matrix4x4.h" #include "Consts.h" Matrix4x4 Matrix4x4::operator*(const Matrix4x4 &matrix4X4) const { Matrix4x4 result = Matrix4x4::Zero(); - for(int i = 0; i < 4; i++) - for(int j = 0; j < 4; j++) - for(int k = 0; k < 4; k++) + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + for (int k = 0; k < 4; k++) result._arr[i][j] += _arr[i][k] * matrix4X4._arr[k][j]; return result; } @@ -32,13 +31,13 @@ Vec3D Matrix4x4::operator*(const Vec3D &vec) const { _arr[0][0] * vec.x() + _arr[0][1] * vec.y() + _arr[0][2] * vec.z(), _arr[1][0] * vec.x() + _arr[1][1] * vec.y() + _arr[1][2] * vec.z(), _arr[2][0] * vec.x() + _arr[2][1] * vec.y() + _arr[2][2] * vec.z() - ); + ); } Matrix4x4 Matrix4x4::Identity() { Matrix4x4 result; - for(int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (i == j) { result._arr[j][i] = 1.0; @@ -54,7 +53,7 @@ Matrix4x4 Matrix4x4::Identity() { Matrix4x4 Matrix4x4::Constant(double value) { Matrix4x4 result; - for(int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { result._arr[j][i] = value; } @@ -67,7 +66,7 @@ Matrix4x4 Matrix4x4::Zero() { return Matrix4x4::Constant(0); } -Matrix4x4 Matrix4x4::Scale(const Vec3D& factor) { +Matrix4x4 Matrix4x4::Scale(const Vec3D &factor) { Matrix4x4 s{}; s._arr[0][0] = factor.x(); s._arr[1][1] = factor.y(); @@ -77,7 +76,7 @@ Matrix4x4 Matrix4x4::Scale(const Vec3D& factor) { return s; } -Matrix4x4 Matrix4x4::Translation(const Vec3D& v) { +Matrix4x4 Matrix4x4::Translation(const Vec3D &v) { Matrix4x4 t{}; t._arr[0][0] = 1.0; @@ -136,25 +135,25 @@ Matrix4x4 Matrix4x4::RotationZ(double rz) { return Rz; } -Matrix4x4 Matrix4x4::Rotation(const Vec3D& r) { +Matrix4x4 Matrix4x4::Rotation(const Vec3D &r) { return RotationX(r.x()) * RotationY(r.y()) * RotationZ(r.z()); } -Matrix4x4 Matrix4x4::Rotation(const Vec3D& v, double rv) { +Matrix4x4 Matrix4x4::Rotation(const Vec3D &v, double rv) { Matrix4x4 Rv{}; Vec3D nv(v.normalized()); - Rv._arr[0][0] = cos(rv) + (1.0 - cos(rv))*nv.x()*nv.x(); - Rv._arr[0][1] = (1.0 - cos(rv))*nv.x()*nv.y() - sin(rv)*nv.z(); - Rv._arr[0][2] = (1.0 - cos(rv))*nv.x()*nv.z() + sin(rv)*nv.y(); + Rv._arr[0][0] = cos(rv) + (1.0 - cos(rv)) * nv.x() * nv.x(); + Rv._arr[0][1] = (1.0 - cos(rv)) * nv.x() * nv.y() - sin(rv) * nv.z(); + Rv._arr[0][2] = (1.0 - cos(rv)) * nv.x() * nv.z() + sin(rv) * nv.y(); - Rv._arr[1][0] = (1.0 - cos(rv))*nv.x()*nv.y() + sin(rv)*nv.z(); - Rv._arr[1][1] = cos(rv) + (1.0 - cos(rv))*nv.y()*nv.y(); - Rv._arr[1][2] = (1.0 - cos(rv))*nv.y()*nv.z() - sin(rv)*nv.x(); + Rv._arr[1][0] = (1.0 - cos(rv)) * nv.x() * nv.y() + sin(rv) * nv.z(); + Rv._arr[1][1] = cos(rv) + (1.0 - cos(rv)) * nv.y() * nv.y(); + Rv._arr[1][2] = (1.0 - cos(rv)) * nv.y() * nv.z() - sin(rv) * nv.x(); - Rv._arr[2][0] = (1.0 - cos(rv))*nv.z()*nv.x() - sin(rv)*nv.y(); - Rv._arr[2][1] = (1.0 - cos(rv))*nv.z()*nv.y() + sin(rv)*nv.x(); - Rv._arr[2][2] = cos(rv) + (1.0 - cos(rv))*nv.z()*nv.z(); + Rv._arr[2][0] = (1.0 - cos(rv)) * nv.z() * nv.x() - sin(rv) * nv.y(); + Rv._arr[2][1] = (1.0 - cos(rv)) * nv.z() * nv.y() + sin(rv) * nv.x(); + Rv._arr[2][2] = cos(rv) + (1.0 - cos(rv)) * nv.z() * nv.z(); Rv._arr[3][3] = 1.0; @@ -164,10 +163,10 @@ Matrix4x4 Matrix4x4::Rotation(const Vec3D& v, double rv) { Matrix4x4 Matrix4x4::Projection(double fov, double aspect, double ZNear, double ZFar) { Matrix4x4 p{}; - p._arr[0][0] = 1.0/(tan(Consts::PI*fov*0.5/180.0)*aspect); - p._arr[1][1] = 1.0/tan(Consts::PI*fov*0.5/180.0); - p._arr[2][2] = ZFar/(ZFar - ZNear); - p._arr[2][3] = -ZFar*ZNear/(ZFar - ZNear); + p._arr[0][0] = 1.0 / (tan(Consts::PI * fov * 0.5 / 180.0) * aspect); + p._arr[1][1] = 1.0 / tan(Consts::PI * fov * 0.5 / 180.0); + p._arr[2][2] = ZFar / (ZFar - ZNear); + p._arr[2][3] = -ZFar * ZNear / (ZFar - ZNear); p._arr[3][2] = 1.0; return p; @@ -176,12 +175,12 @@ Matrix4x4 Matrix4x4::Projection(double fov, double aspect, double ZNear, double Matrix4x4 Matrix4x4::ScreenSpace(int width, int height) { Matrix4x4 s{}; - s._arr[0][0] = -0.5*width; - s._arr[1][1] = -0.5*height; + s._arr[0][0] = -0.5 * width; + s._arr[1][1] = -0.5 * height; s._arr[2][2] = 1.0; - s._arr[0][3] = 0.5*width; - s._arr[1][3] = 0.5*height; + s._arr[0][3] = 0.5 * width; + s._arr[1][3] = 0.5 * height; s._arr[3][3] = 1.0; @@ -212,17 +211,17 @@ Matrix4x4 Matrix4x4::View(const Vec3D &left, const Vec3D &up, const Vec3D &lookA } Vec3D Matrix4x4::x() const { - return Vec3D(_arr[0][0], _arr[1][0],_arr[2][0]); + return Vec3D(_arr[0][0], _arr[1][0], _arr[2][0]); } Vec3D Matrix4x4::y() const { - return Vec3D(_arr[0][1], _arr[1][1],_arr[2][1]); + return Vec3D(_arr[0][1], _arr[1][1], _arr[2][1]); } Vec3D Matrix4x4::z() const { - return Vec3D(_arr[0][2], _arr[1][2],_arr[2][2]); + return Vec3D(_arr[0][2], _arr[1][2], _arr[2][2]); } Vec3D Matrix4x4::w() const { - return Vec3D(_arr[0][3], _arr[1][3],_arr[2][3]); + return Vec3D(_arr[0][3], _arr[1][3], _arr[2][3]); } diff --git a/engine/Matrix4x4.h b/engine/Matrix4x4.h index 9f06ba7..c9980db 100644 --- a/engine/Matrix4x4.h +++ b/engine/Matrix4x4.h @@ -6,6 +6,7 @@ #define ENGINE_MATRIX4X4_H #include + #include "Vec4D.h" #include "Vec3D.h" @@ -14,35 +15,51 @@ private: std::array, 4> _arr{}; public: - Matrix4x4 () = default; - Matrix4x4& operator=(const Matrix4x4& matrix4X4) = default; + Matrix4x4() = default; - [[nodiscard]] Matrix4x4 operator*(const Matrix4x4& matrix4X4) const; - [[nodiscard]] Vec4D operator*(const Vec4D& point4D) const; - [[nodiscard]] Vec3D operator*(const Vec3D& vec) const; + Matrix4x4 &operator=(const Matrix4x4 &matrix4X4) = default; + + [[nodiscard]] Matrix4x4 operator*(const Matrix4x4 &matrix4X4) const; + + [[nodiscard]] Vec4D operator*(const Vec4D &point4D) const; + + [[nodiscard]] Vec3D operator*(const Vec3D &vec) const; [[nodiscard]] Vec3D x() const; + [[nodiscard]] Vec3D y() const; + [[nodiscard]] Vec3D z() const; + [[nodiscard]] Vec3D w() const; // Any useful matrix (static methods) Matrix4x4 static Identity(); - Matrix4x4 static Zero(); - Matrix4x4 static Constant (double value); - Matrix4x4 static Scale(const Vec3D& factor); - Matrix4x4 static Translation(const Vec3D& v); - Matrix4x4 static Rotation(const Vec3D& r); - Matrix4x4 static RotationX (double rx); - Matrix4x4 static RotationY (double ry); - Matrix4x4 static RotationZ (double rz); - Matrix4x4 static Rotation (const Vec3D& v, double rv); + Matrix4x4 static Zero(); + + Matrix4x4 static Constant(double value); + + Matrix4x4 static Scale(const Vec3D &factor); + + Matrix4x4 static Translation(const Vec3D &v); + + Matrix4x4 static Rotation(const Vec3D &r); + + Matrix4x4 static RotationX(double rx); + + Matrix4x4 static RotationY(double ry); + + Matrix4x4 static RotationZ(double rz); + + Matrix4x4 static Rotation(const Vec3D &v, double rv); Matrix4x4 static View(const Vec3D &left, const Vec3D &up, const Vec3D &lookAt, const Vec3D &eye); - Matrix4x4 static Projection (double fov = 90.0, double aspect = 1.0, double ZNear = 1.0, double ZFar = 10.0); - Matrix4x4 static ScreenSpace (int width, int height); + + Matrix4x4 static Projection(double fov = 90.0, double aspect = 1.0, double ZNear = 1.0, double ZFar = 10.0); + + Matrix4x4 static ScreenSpace(int width, int height); }; diff --git a/engine/Mesh.cpp b/engine/Mesh.cpp index 4b3682f..b166979 100644 --- a/engine/Mesh.cpp +++ b/engine/Mesh.cpp @@ -3,6 +3,7 @@ // #include + #include "Mesh.h" #include "ResourceManager.h" @@ -10,7 +11,7 @@ using namespace std; Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) { std::vector newTriangles; - for(auto &t : _tris) { + for (auto &t : _tris) { newTriangles.emplace_back(t * matrix4X4); } setTriangles(newTriangles); @@ -18,10 +19,10 @@ Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) { return *this; } -void Mesh::loadObj(const std::string& filename, const Vec3D& scale) { +void Mesh::loadObj(const std::string &filename, const Vec3D &scale) { auto objects = ResourceManager::loadObjects(filename); - for(auto& obj : objects) { + for (auto &obj : objects) { for (auto &tri : obj->triangles()) { _tris.push_back(tri); } @@ -29,29 +30,30 @@ void Mesh::loadObj(const std::string& filename, const Vec3D& scale) { this->scale(scale); } -Mesh::Mesh(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale) : Object(std::move(nameTag)) { +Mesh::Mesh(ObjectNameTag nameTag, const std::string &filename, const Vec3D &scale) : Object(std::move(nameTag)) { loadObj(filename, scale); } Mesh::Mesh(ObjectNameTag nameTag, const vector &tries) : Object(std::move(nameTag)), _tris(tries) { } -Mesh Mesh::Obj(ObjectNameTag nameTag, const std::string& filename) { +Mesh Mesh::Obj(ObjectNameTag nameTag, const std::string &filename) { return Mesh(std::move(nameTag), filename); } -void Mesh::setColor(const sf::Color& c) { +void Mesh::setColor(const sf::Color &c) { _color = c; // change color of all mesh triangles: std::vector newTriangles; - for(auto &t : _tris) { + for (auto &t : _tris) { newTriangles.emplace_back(Triangle(t[0], t[1], t[2], c)); } setTriangles(newTriangles); } -Mesh Mesh::LineTo(ObjectNameTag nameTag, const Vec3D& from, const Vec3D& to, double line_width, const sf::Color& color) { +Mesh +Mesh::LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width, const sf::Color &color) { Mesh line(std::move(nameTag)); @@ -72,18 +74,18 @@ Mesh Mesh::LineTo(ObjectNameTag nameTag, const Vec3D& from, const Vec3D& to, dou line._tris = std::move(std::vector{ - { p2, p4, p1 }, - { p2, p3, p4 }, - { p1, p6, p2 }, - { p1, p5, p6 }, - { p2, p6, p7 }, - { p2, p7, p3 }, - { p6, p5, p8 }, - { p6, p8, p7 }, - { p4, p3, p7 }, - { p4, p7, p8 }, - { p1, p8, p5 }, - { p1, p4, p8 } + {p2, p4, p1}, + {p2, p3, p4}, + {p1, p6, p2}, + {p1, p5, p6}, + {p2, p6, p7}, + {p2, p7, p3}, + {p6, p5, p8}, + {p6, p8, p7}, + {p4, p3, p7}, + {p4, p7, p8}, + {p1, p8, p5}, + {p1, p4, p8} }); line.setColor(color); @@ -92,7 +94,7 @@ Mesh Mesh::LineTo(ObjectNameTag nameTag, const Vec3D& from, const Vec3D& to, dou void Mesh::setTriangles(const vector &t) { _tris.clear(); - for (auto & tri : t) { + for (auto &tri : t) { _tris.push_back(tri); } } diff --git a/engine/Mesh.h b/engine/Mesh.h index 2604be4..f6d25b2 100644 --- a/engine/Mesh.h +++ b/engine/Mesh.h @@ -7,8 +7,10 @@ #include #include -#include "Triangle.h" + #include + +#include "Triangle.h" #include "Object.h" class Mesh : public Object { @@ -17,33 +19,43 @@ private: sf::Color _color = sf::Color(255, 245, 194); bool _visible = true; - Mesh& operator*=(const Matrix4x4& matrix4X4); + Mesh &operator*=(const Matrix4x4 &matrix4X4); + public: explicit Mesh(ObjectNameTag nameTag) : Object(std::move(nameTag)) {}; - Mesh& operator=(const Mesh& mesh) = delete; - Mesh(const Mesh& mesh) = default; - explicit Mesh(ObjectNameTag nameTag, const std::vector& tries); - explicit Mesh(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); + Mesh &operator=(const Mesh &mesh) = delete; - void loadObj(const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); + Mesh(const Mesh &mesh) = default; - [[nodiscard]] std::vectorconst &triangles() const { return _tris; } - [[nodiscard]] std::vector& triangles() { return _tris; } - void setTriangles(const std::vector& t); + explicit Mesh(ObjectNameTag nameTag, const std::vector &tries); - [[nodiscard]] int size() const { return _tris.size()*3; } + explicit Mesh(ObjectNameTag nameTag, const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); + + void loadObj(const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); + + [[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; } [[nodiscard]] sf::Color color() const { return _color; } - void setColor(const sf::Color& c); + + void setColor(const sf::Color &c); void setVisible(bool visibility) { _visible = visibility; } + [[nodiscard]] bool isVisible() const { return _visible; } ~Mesh() override; - Mesh static Obj(ObjectNameTag nameTag, const std::string& filename); - Mesh static LineTo(ObjectNameTag nameTag, const Vec3D& from, const Vec3D& to, double line_width = 0.1, const sf::Color& color = {150, 150, 150, 100}); + Mesh static Obj(ObjectNameTag nameTag, const std::string &filename); + + Mesh static LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width = 0.1, + const sf::Color &color = {150, 150, 150, 100}); }; #endif //INC_3DZAVR_MESH_H diff --git a/engine/Mouse.cpp b/engine/Mouse.cpp index 1967a3b..23a550c 100644 --- a/engine/Mouse.cpp +++ b/engine/Mouse.cpp @@ -12,16 +12,17 @@ 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 center = sf::Vector2(_screen->width() / 2, _screen->height() / 2); sf::Vector2 displacement = mousePos - center; - //setMouseInCenter(); return Vec2D(displacement.x, displacement.y); } void Mouse::setMouseInCenter() const { - sf::Mouse::setPosition({ static_cast(_screen->width() / 2), static_cast(_screen->height() / 2) }, *_screen->renderWindow()); + sf::Mouse::setPosition({static_cast(_screen->width() / 2), static_cast(_screen->height() / 2)}, + *_screen->renderWindow()); } bool Mouse::isButtonPressed(sf::Mouse::Button button) { @@ -33,10 +34,10 @@ bool Mouse::isButtonTapped(sf::Mouse::Button button) { return false; } - if(_tappedButtons.count(button) == 0) { + if (_tappedButtons.count(button) == 0) { _tappedButtons.emplace(button, Time::time()); return true; - } else if((Time::time() - _tappedButtons[button]) > Consts::TAP_DELAY) { + } else if ((Time::time() - _tappedButtons[button]) > Consts::TAP_DELAY) { _tappedButtons[button] = Time::time(); return true; } diff --git a/engine/Mouse.h b/engine/Mouse.h index 6381114..5d26389 100644 --- a/engine/Mouse.h +++ b/engine/Mouse.h @@ -7,6 +7,7 @@ #include #include + #include "Screen.h" #include "Vec2D.h" @@ -18,11 +19,16 @@ private: public: explicit Mouse(std::shared_ptr screen) : _screen(std::move(screen)) {}; - static bool isButtonPressed(sf::Mouse::Button button); // returns true if this _button is _pressed - bool isButtonTapped(sf::Mouse::Button button); // returns true if this _button is tapped and 1/5 sec passed (_button bouncing problem solved) + // returns true if this _button is _pressed + static bool isButtonPressed(sf::Mouse::Button button); + + // returns true if this _button is tapped and 1/5 sec passed (_button bouncing problem solved) + bool isButtonTapped(sf::Mouse::Button button); [[nodiscard]] Vec2D getMousePosition() const; + [[nodiscard]] Vec2D getMouseDisplacement() const; + void setMouseInCenter() const; }; diff --git a/engine/Object.cpp b/engine/Object.cpp index fdfdcf7..bf72bea 100644 --- a/engine/Object.cpp +++ b/engine/Object.cpp @@ -5,27 +5,28 @@ #include "Object.h" #include "Matrix4x4.h" -void Object::transform(const Matrix4x4& t) { +void Object::transform(const Matrix4x4 &t) { _transformMatrix = t * _transformMatrix; - for(auto &[attachedName, attachedObject] : _attachedObjects) { - if(!attachedObject.expired()) { + for (auto &[attachedName, attachedObject] : _attachedObjects) { + if (!attachedObject.expired()) { attachedObject.lock()->transformRelativePoint(position(), t); } } } -void Object::transformRelativePoint(const Vec3D &point, const Matrix4x4& transform) { + +void Object::transformRelativePoint(const Vec3D &point, const Matrix4x4 &transform) { // translate object in new coordinate system (connected with point) _transformMatrix = Matrix4x4::Translation(position() - point) * _transformMatrix; // transform object in the new coordinate system - _transformMatrix = transform*_transformMatrix; + _transformMatrix = transform * _transformMatrix; // translate object back in self connected coordinate system _position = _transformMatrix.w() + point; _transformMatrix = Matrix4x4::Translation(-_transformMatrix.w()) * _transformMatrix; - for(auto &[attachedName, attachedObject] : _attachedObjects) { - if(!attachedObject.expired()) { + for (auto &[attachedName, attachedObject] : _attachedObjects) { + if (!attachedObject.expired()) { attachedObject.lock()->transformRelativePoint(point, transform); } } @@ -35,8 +36,8 @@ void Object::translate(const Vec3D &dv) { _position = _position + dv; - for(auto &[attachedName, attachedObject] : _attachedObjects) { - if(!attachedObject.expired()) { + for (auto &[attachedName, attachedObject] : _attachedObjects) { + if (!attachedObject.expired()) { attachedObject.lock()->translate(dv); } } @@ -49,7 +50,8 @@ void Object::scale(const Vec3D &s) { void Object::rotate(const Vec3D &r) { _angle = _angle + r; - Matrix4x4 rotationMatrix = Matrix4x4::RotationZ(r.z())*Matrix4x4::RotationY(r.y())*Matrix4x4::RotationX(r.z()); + // TODO: when you rotate body _angle is changed only for this body but all attached objects have incorrect _angle + Matrix4x4 rotationMatrix = Matrix4x4::RotationZ(r.z()) * Matrix4x4::RotationY(r.y()) * Matrix4x4::RotationX(r.z()); transform(rotationMatrix); } @@ -96,15 +98,15 @@ void Object::rotateToAngle(const Vec3D &v) { rotate(v - _angle); } -std::shared_ptr Object::attached(const ObjectNameTag& tag) { - if(_attachedObjects.count(tag) == 0 || _attachedObjects.find(tag)->second.expired()) { +std::shared_ptr Object::attached(const ObjectNameTag &tag) { + if (_attachedObjects.count(tag) == 0 || _attachedObjects.find(tag)->second.expired()) { return nullptr; } return _attachedObjects.find(tag)->second.lock(); } bool Object::checkIfAttached(Object *obj) { - for(const auto& [nameTag, attachedObject] : _attachedObjects) { + for (const auto&[nameTag, attachedObject] : _attachedObjects) { if (obj == attachedObject.lock().get() || attachedObject.lock()->checkIfAttached(obj)) { return true; } @@ -113,8 +115,8 @@ bool Object::checkIfAttached(Object *obj) { } void Object::attach(std::shared_ptr object) { - if(this != object.get()) { - if(!object->checkIfAttached(this)) { + if (this != object.get()) { + if (!object->checkIfAttached(this)) { _attachedObjects.emplace(object->name(), object); } else { throw std::invalid_argument{"Object::attach: You tried to create infinite recursive call chains"}; @@ -124,59 +126,59 @@ void Object::attach(std::shared_ptr object) { } } -void Object::unattach(const ObjectNameTag& tag) { +void Object::unattach(const ObjectNameTag &tag) { _attachedObjects.erase(tag); } // OpenGL function -GLfloat* Object::glView() const { - auto* v = (GLfloat*)malloc(4*4*sizeof(GLfloat)); +GLfloat *Object::glView() const { + auto *v = (GLfloat *) malloc(4 * 4 * sizeof(GLfloat)); - v[0] = -(GLfloat)left().x(); - v[4] = -(GLfloat)left().y(); - v[8] = -(GLfloat)left().z(); - v[12] = (GLfloat)position().dot(left()); + v[0] = -(GLfloat) left().x(); + v[4] = -(GLfloat) left().y(); + v[8] = -(GLfloat) left().z(); + v[12] = (GLfloat) position().dot(left()); - v[1] = (GLfloat)up().x(); - v[5] = (GLfloat)up().y(); - v[9] = (GLfloat)up().z(); - v[13] = -(GLfloat)position().dot(up()); + v[1] = (GLfloat) up().x(); + v[5] = (GLfloat) up().y(); + v[9] = (GLfloat) up().z(); + v[13] = -(GLfloat) position().dot(up()); - v[2] = -(GLfloat)lookAt().x(); - v[6] = -(GLfloat)lookAt().y(); - v[10] = -(GLfloat)lookAt().z(); - v[14] = (GLfloat)position().dot(lookAt()); + v[2] = -(GLfloat) lookAt().x(); + v[6] = -(GLfloat) lookAt().y(); + v[10] = -(GLfloat) lookAt().z(); + v[14] = (GLfloat) position().dot(lookAt()); - v[3] = (GLfloat)0.0f; - v[7] = (GLfloat)0.0f; - v[11] = (GLfloat)0.0f; - v[15] = (GLfloat)1.0f; + v[3] = (GLfloat) 0.0f; + v[7] = (GLfloat) 0.0f; + v[11] = (GLfloat) 0.0f; + v[15] = (GLfloat) 1.0f; return v; } -GLfloat* Object::glModel() const { - auto* m = (GLfloat*)malloc(4*4*sizeof(GLfloat)); +GLfloat *Object::glModel() const { + auto *m = (GLfloat *) malloc(4 * 4 * sizeof(GLfloat)); - m[0] = (GLfloat)left().x(); - m[4] = (GLfloat)up().x(); - m[8] = (GLfloat)lookAt().x(); - m[12] = (GLfloat)position().x(); + m[0] = (GLfloat) left().x(); + m[4] = (GLfloat) up().x(); + m[8] = (GLfloat) lookAt().x(); + m[12] = (GLfloat) position().x(); - m[1] = (GLfloat)left().y(); - m[5] = (GLfloat)up().y(); - m[9] = (GLfloat)lookAt().y(); - m[13] = (GLfloat)position().y(); + m[1] = (GLfloat) left().y(); + m[5] = (GLfloat) up().y(); + m[9] = (GLfloat) lookAt().y(); + m[13] = (GLfloat) position().y(); - m[2] = (GLfloat)left().z(); - m[6] = (GLfloat)up().z(); - m[10] =(GLfloat)lookAt().z(); - m[14] = (GLfloat)position().z(); + m[2] = (GLfloat) left().z(); + m[6] = (GLfloat) up().z(); + m[10] = (GLfloat) lookAt().z(); + m[14] = (GLfloat) position().z(); - m[3] = (GLfloat)0.0f; - m[7] = (GLfloat)0.0f; - m[11] = (GLfloat)0.0f; - m[15] = (GLfloat)1.0f; + m[3] = (GLfloat) 0.0f; + m[7] = (GLfloat) 0.0f; + m[11] = (GLfloat) 0.0f; + m[15] = (GLfloat) 1.0f; return m; } diff --git a/engine/Object.h b/engine/Object.h index 68fb9f9..cdf4f1f 100644 --- a/engine/Object.h +++ b/engine/Object.h @@ -6,10 +6,11 @@ #define ENGINE_OBJECT_H #include -#include "Vec3D.h" #include #include #include + +#include "Vec3D.h" #include "Matrix4x4.h" #include @@ -18,63 +19,87 @@ private: const std::string _name; public: explicit ObjectNameTag(std::string name = "") : _name(std::move(name)) {} + [[nodiscard]] std::string str() const { return _name; } - bool operator==(const ObjectNameTag& tag) const { return _name == tag._name; } - bool operator!=(const ObjectNameTag& tag) const { return _name != tag._name; } - bool operator<(const ObjectNameTag& tag) const { return _name < tag._name; } + bool operator==(const ObjectNameTag &tag) const { return _name == tag._name; } + + bool operator!=(const ObjectNameTag &tag) const { return _name != tag._name; } + + bool operator<(const ObjectNameTag &tag) const { return _name < tag._name; } }; class Object { private: - bool checkIfAttached(Object* obj); + bool checkIfAttached(Object *obj); + const ObjectNameTag _nameTag; Matrix4x4 _transformMatrix = Matrix4x4::Identity(); std::map> _attachedObjects; - Vec3D _position {0, 0, 0}; - Vec3D _angle {0, 0, 0}; + Vec3D _position{0, 0, 0}; + Vec3D _angle{0, 0, 0}; Vec3D _angleLeftUpLookAt{0, 0, 0}; public: explicit Object(ObjectNameTag nameTag) : _nameTag(std::move(nameTag)) {}; - Object(const Object& object) : _nameTag(object.name()), _transformMatrix(object.model()) {}; + + Object(const Object &object) : _nameTag(object.name()), _transformMatrix(object.model()) {}; // TODO: implement rotations using quaternions (?) - void transform(const Matrix4x4& t); - void transformRelativePoint(const Vec3D &point, const Matrix4x4& transform); - void translate(const Vec3D& dv); - void translateToPoint(const Vec3D& point); - void scale(const Vec3D& s); - void rotate(const Vec3D& r); - void rotate(const Vec3D& v, double rv); - void rotateToAngle(const Vec3D& v); - void rotateRelativePoint(const Vec3D& s, const Vec3D& r); - void rotateRelativePoint(const Vec3D& s, const Vec3D& v, double r); + void transform(const Matrix4x4 &t); + + void transformRelativePoint(const Vec3D &point, const Matrix4x4 &transform); + + void translate(const Vec3D &dv); + + void translateToPoint(const Vec3D &point); + + void scale(const Vec3D &s); + + void rotate(const Vec3D &r); + + void rotate(const Vec3D &v, double rv); + + void rotateToAngle(const Vec3D &v); + + void rotateRelativePoint(const Vec3D &s, const Vec3D &r); + + void rotateRelativePoint(const Vec3D &s, const Vec3D &v, double r); + void rotateLeft(double rl); + void rotateUp(double ru); + void rotateLookAt(double rlAt); - [[nodiscard]] Vec3D left() const { return _transformMatrix.x(); } - [[nodiscard]] Vec3D up() const { return _transformMatrix.y(); } - [[nodiscard]] Vec3D lookAt() const { return _transformMatrix.z(); } - [[nodiscard]] Vec3D position() const { return _position; } + [[nodiscard]] Vec3D left() const { return _transformMatrix.x(); } + + [[nodiscard]] Vec3D up() const { return _transformMatrix.y(); } + + [[nodiscard]] Vec3D lookAt() const { return _transformMatrix.z(); } + + [[nodiscard]] Vec3D position() const { return _position; } [[nodiscard]] Vec3D angle() const { return _angle; } + [[nodiscard]] Vec3D angleLeftUpLookAt() const { return _angleLeftUpLookAt; } void attach(std::shared_ptr object); - void unattach(const ObjectNameTag& tag); - std::shared_ptr attached(const ObjectNameTag& tag); + + void unattach(const ObjectNameTag &tag); + + std::shared_ptr attached(const ObjectNameTag &tag); [[nodiscard]] ObjectNameTag name() const { return _nameTag; } [[nodiscard]] Matrix4x4 model() const { return Matrix4x4::Translation(_position) * _transformMatrix; } // OpenGL function - [[nodiscard]] GLfloat* glModel() const; - [[nodiscard]] GLfloat* glView() const; + [[nodiscard]] GLfloat *glModel() const; + + [[nodiscard]] GLfloat *glView() const; virtual ~Object(); }; diff --git a/engine/Plane.cpp b/engine/Plane.cpp index 0f85b67..06be7db 100644 --- a/engine/Plane.cpp +++ b/engine/Plane.cpp @@ -4,7 +4,7 @@ #include "Plane.h" -Plane::Plane(const Triangle& tri) : _normal(tri.norm()), _point(tri[0]) { +Plane::Plane(const Triangle &tri) : _normal(tri.norm()), _point(tri[0]) { } Plane::Plane(const Vec3D &N, const Vec3D &P) : _normal(N.normalized()), _point(P) { @@ -17,7 +17,7 @@ double Plane::distance(const Vec3D &point) const { std::pair Plane::intersection(const Vec3D &start, const Vec3D &end) const { double s_dot_n = start.dot(_normal); double k = (s_dot_n - _point.dot(_normal)) / (s_dot_n - end.dot(_normal)); - Vec3D res = start + (end - start)*k; + Vec3D res = start + (end - start) * k; return std::make_pair(res, k); } @@ -32,7 +32,7 @@ std::vector Plane::clip(const Triangle &tri) const { distance(Vec3D(tri[1])), distance(Vec3D(tri[2]))}; - for(int i = 0; i < 3; i++) { + for (int i = 0; i < 3; i++) { if (distances[i] >= 0) { insidePoints.emplace_back(tri[i]); } else { @@ -40,7 +40,7 @@ std::vector Plane::clip(const Triangle &tri) const { } } - if(insidePoints.size() == 1) { + if (insidePoints.size() == 1) { std::pair intersect1 = intersection(insidePoints[0], outsidePoints[0]); std::pair intersect2 = intersection(insidePoints[0], outsidePoints[1]); @@ -50,7 +50,7 @@ std::vector Plane::clip(const Triangle &tri) const { tri.color()); } - if(insidePoints.size() == 2) { + if (insidePoints.size() == 2) { std::pair intersect1 = intersection(insidePoints[0], outsidePoints[0]); std::pair intersect2 = intersection(insidePoints[1], outsidePoints[0]); @@ -64,7 +64,7 @@ std::vector Plane::clip(const Triangle &tri) const { tri.color()); } - if(insidePoints.size() == 3) { + if (insidePoints.size() == 3) { result.emplace_back(tri); } diff --git a/engine/Plane.h b/engine/Plane.h index 262453e..9b0fb8f 100644 --- a/engine/Plane.h +++ b/engine/Plane.h @@ -14,18 +14,24 @@ private: const Vec3D _point; public: Plane() = delete; - Plane(const Plane& plane) = default; - // You can define plane by defining the points in 3D space - explicit Plane(const Triangle& tri); - // Or by defining normal vector and one val laying on the plane - Plane(const Vec3D& N, const Vec3D& P); - [[nodiscard]] double distance(const Vec3D& point4D) const; + Plane(const Plane &plane) = default; + + // You can define plane by defining the points in 3D space + explicit Plane(const Triangle &tri); + + // Or by defining normal vector and one val laying on the plane + Plane(const Vec3D &N, const Vec3D &P); + + [[nodiscard]] double distance(const Vec3D &point4D) const; + // Vec4D in space where line ('start' to 'end') intersects plain with normal vector '_n' and val '_p' lays on the plane - [[nodiscard]] std::pair intersection(const Vec3D& start, const Vec3D& end) const; - [[nodiscard]] std::vector clip(const Triangle& tri) const; + [[nodiscard]] std::pair intersection(const Vec3D &start, const Vec3D &end) const; + + [[nodiscard]] std::vector clip(const Triangle &tri) const; [[nodiscard]] Vec3D N() const { return _normal; } + [[nodiscard]] Vec3D P() const { return _point; } }; diff --git a/engine/ResourceManager.cpp b/engine/ResourceManager.cpp index 5443b32..ffbc320 100644 --- a/engine/ResourceManager.cpp +++ b/engine/ResourceManager.cpp @@ -2,14 +2,15 @@ // Created by Neirokan on 09.05.2020 // -#include "ResourceManager.h" -#include "utils/Log.h" #include #include #include #include -ResourceManager* ResourceManager::_instance = nullptr; +#include "ResourceManager.h" +#include "utils/Log.h" + +ResourceManager *ResourceManager::_instance = nullptr; bool ResourceManager::_validInstance = false; @@ -22,7 +23,7 @@ void ResourceManager::init() { void ResourceManager::unloadTextures() { int texturesCounter = _instance->_textures.size(); - for (auto & _texture : _instance->_textures) { + for (auto &_texture : _instance->_textures) { _texture.second.reset(); } _instance->_textures.clear(); @@ -33,17 +34,18 @@ void ResourceManager::unloadTextures() { void ResourceManager::unloadSoundBuffers() { int soundBuffersCounter = _instance->_soundBuffers.size(); - for (auto & _soundBuffer : _instance->_soundBuffers) { + for (auto &_soundBuffer : _instance->_soundBuffers) { _soundBuffer.second.reset(); } _instance->_soundBuffers.clear(); - Log::log("ResourceManager::unloadSoundBuffers(): all " + std::to_string(soundBuffersCounter) + " soundBuffers was unloaded"); + Log::log("ResourceManager::unloadSoundBuffers(): all " + std::to_string(soundBuffersCounter) + + " soundBuffers was unloaded"); } void ResourceManager::unloadFonts() { int fontsCounter = _instance->_fonts.size(); - for (auto & _font : _instance->_fonts) { + for (auto &_font : _instance->_fonts) { _font.second.reset(); } _instance->_fonts.clear(); @@ -59,7 +61,7 @@ void ResourceManager::unloadObjects() { } void ResourceManager::unloadAllResources() { - if(!_validInstance) { + if (!_validInstance) { return; } @@ -79,8 +81,8 @@ void ResourceManager::free() { Log::log("ResourceManager::free(): pointer to 'ResourceManager' was freed"); } -std::shared_ptr ResourceManager::loadTexture(const std::string& filename) { - if(!_validInstance) { +std::shared_ptr ResourceManager::loadTexture(const std::string &filename) { + if (!_validInstance) { return nullptr; } @@ -106,8 +108,8 @@ std::shared_ptr ResourceManager::loadTexture(const std::string& fil return texture; } -std::shared_ptr ResourceManager::loadSoundBuffer(const std::string& filename) { - if(!_validInstance) { +std::shared_ptr ResourceManager::loadSoundBuffer(const std::string &filename) { + if (!_validInstance) { return nullptr; } @@ -132,8 +134,8 @@ std::shared_ptr ResourceManager::loadSoundBuffer(const std::str return soundBuffer; } -std::shared_ptr ResourceManager::loadFont(const std::string& filename) { - if(!_validInstance) { +std::shared_ptr ResourceManager::loadFont(const std::string &filename) { + if (!_validInstance) { return nullptr; } @@ -163,7 +165,7 @@ std::vector> ResourceManager::loadObjects(const std::strin std::vector> objects{}; std::map maters{}; - if(!_validInstance) { + if (!_validInstance) { return objects; } @@ -191,40 +193,39 @@ std::vector> ResourceManager::loadObjects(const std::strin s << line; char junk; - if(line[0] == 'o') { - if(!tris.empty()) - objects.push_back(std::make_shared(ObjectNameTag(filename + "_temp_obj_" + std::to_string(objects.size())), tris)); + if (line[0] == 'o') { + if (!tris.empty()) + objects.push_back( + std::make_shared(ObjectNameTag(filename + "_temp_obj_" + std::to_string(objects.size())), + tris)); tris.clear(); } - if (line[0] == 'v') - { + if (line[0] == 'v') { double x, y, z; s >> junk >> x >> y >> z; verts.emplace_back(x, y, z, 1.0); } - if(line[0] == 'g') { + if (line[0] == 'g') { std::string matInfo; s >> junk >> matInfo; - std::string colorName = matInfo.substr(matInfo.size()-3, 3); - currentColor = maters[matInfo.substr(matInfo.size()-3, 3)]; + std::string colorName = matInfo.substr(matInfo.size() - 3, 3); + currentColor = maters[matInfo.substr(matInfo.size() - 3, 3)]; } - if (line[0] == 'f') - { + if (line[0] == 'f') { int f[3]; s >> junk >> f[0] >> f[1] >> f[2]; tris.emplace_back(verts[f[0] - 1], verts[f[1] - 1], verts[f[2] - 1], currentColor); } - if(line[0] == 'm') - { + if (line[0] == 'm') { int color[4]; std::string matName; s >> junk >> matName >> color[0] >> color[1] >> color[2] >> color[3]; - maters.insert({matName, sf::Color(color[0],color[1],color[2], color[3])}); + maters.insert({matName, sf::Color(color[0], color[1], color[2], color[3])}); } } - if(!tris.empty()) { + if (!tris.empty()) { objects.push_back( std::make_shared(ObjectNameTag(filename + "_temp_obj_" + std::to_string(objects.size())), tris)); } diff --git a/engine/ResourceManager.h b/engine/ResourceManager.h index a3c4c1b..1e2127c 100644 --- a/engine/ResourceManager.h +++ b/engine/ResourceManager.h @@ -5,9 +5,11 @@ #ifndef ENGINE_RESOURCEMANAGER_H #define ENGINE_RESOURCEMANAGER_H +#include + #include #include -#include + #include "Mesh.h" class ResourceManager final { @@ -17,32 +19,41 @@ private: std::map> _soundBuffers; std::map>> _objects; - static ResourceManager* _instance; + static ResourceManager *_instance; static bool _validInstance; ResourceManager() = default; + // Unloads all currently loaded textures. static void unloadObjects(); + static void unloadTextures(); + static void unloadSoundBuffers(); + static void unloadFonts(); public: - ResourceManager(const ResourceManager&) = delete; - ResourceManager& operator=(ResourceManager&) = delete; + ResourceManager(const ResourceManager &) = delete; + + ResourceManager &operator=(ResourceManager &) = delete; static void unloadAllResources(); static void init(); + static void free(); // Try to load texture from file. // If success returns pointer to texture. // Otherwise returns nullptr. - static std::vector> loadObjects(const std::string& filename); - static std::shared_ptr loadTexture(const std::string& filename); - static std::shared_ptr loadFont(const std::string& filename); - static std::shared_ptr loadSoundBuffer(const std::string& filename); + static std::vector> loadObjects(const std::string &filename); + + static std::shared_ptr loadTexture(const std::string &filename); + + static std::shared_ptr loadFont(const std::string &filename); + + static std::shared_ptr loadSoundBuffer(const std::string &filename); }; #endif //PSEUDO3DENGINE_RESOURCEMANAGER_H diff --git a/engine/Screen.cpp b/engine/Screen.cpp index 69938c2..c6e52ec 100644 --- a/engine/Screen.cpp +++ b/engine/Screen.cpp @@ -2,16 +2,18 @@ // Created by Иван Ильин on 14.01.2021. // -#include "Screen.h" -#include "utils/Time.h" #include -#include "utils/Log.h" -#include "ResourceManager.h" -#include #include +#include -void Screen::open(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background, sf::Uint32 style) { +#include "Screen.h" +#include "utils/Time.h" +#include "utils/Log.h" +#include "ResourceManager.h" + +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; @@ -46,13 +48,12 @@ void Screen::clear() { _window->clear(_background); } -void Screen::drawTriangle(const Triangle& triangle) -{ +void Screen::drawTriangle(const Triangle &triangle) { sf::Vertex tris[3] = { - sf::Vertex(sf::Vector2f((float)triangle[0].x(), (float)triangle[0].y()), triangle.color()), - sf::Vertex(sf::Vector2f((float)triangle[1].x(), (float)triangle[1].y()), triangle.color()), - sf::Vertex(sf::Vector2f((float)triangle[2].x(), (float)triangle[2].y()), triangle.color()) + sf::Vertex(sf::Vector2f((float) triangle[0].x(), (float) triangle[0].y()), triangle.color()), + sf::Vertex(sf::Vector2f((float) triangle[1].x(), (float) triangle[1].y()), triangle.color()), + sf::Vertex(sf::Vector2f((float) triangle[2].x(), (float) triangle[2].y()), triangle.color()) }; _window->pushGLStates(); @@ -60,7 +61,7 @@ void Screen::drawTriangle(const Triangle& triangle) _window->popGLStates(); } -void Screen::setTitle(const std::string& title) { +void Screen::setTitle(const std::string &title) { _title = title; } @@ -79,10 +80,10 @@ void Screen::setMouseCursorVisible(bool visible) { void Screen::drawTetragon(const Vec2D &p1, const Vec2D &p2, const Vec2D &p3, const Vec2D &p4, sf::Color color) { sf::ConvexShape polygon; polygon.setPointCount(4); - polygon.setPoint(0, sf::Vector2f((float)p1.x(), (float)p1.y())); - polygon.setPoint(1, sf::Vector2f((float)p2.x(), (float)p2.y())); - polygon.setPoint(2, sf::Vector2f((float)p3.x(), (float)p3.y())); - polygon.setPoint(3, sf::Vector2f((float)p4.x(), (float)p4.y())); + polygon.setPoint(0, sf::Vector2f((float) p1.x(), (float) p1.y())); + polygon.setPoint(1, sf::Vector2f((float) p2.x(), (float) p2.y())); + polygon.setPoint(2, sf::Vector2f((float) p3.x(), (float) p3.y())); + polygon.setPoint(3, sf::Vector2f((float) p4.x(), (float) p4.y())); polygon.setFillColor(color); _window->pushGLStates(); @@ -90,7 +91,7 @@ void Screen::drawTetragon(const Vec2D &p1, const Vec2D &p2, const Vec2D &p3, con _window->popGLStates(); } -void Screen::drawText(const std::string& string, const Vec2D &position, int size, sf::Color color) { +void Screen::drawText(const std::string &string, const Vec2D &position, int size, sf::Color color) { sf::Text text; text.setFont(*ResourceManager::loadFont(Consts::MEDIUM_FONT)); @@ -98,7 +99,7 @@ void Screen::drawText(const std::string& string, const Vec2D &position, int size text.setCharacterSize(size); text.setFillColor(color); text.setStyle(sf::Text::Italic); - text.setPosition((float)position.x(), (float)position.y()); + text.setPosition((float) position.x(), (float) position.y()); text.setString(string); @@ -120,7 +121,7 @@ void Screen::drawText(const sf::Text &text) { } // OpenGL functions -void Screen::glDrawMesh(GLfloat* geometry, GLfloat* view, GLfloat* model, size_t count) { +void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count) { // OpenGL: // Make the window the active window for OpenGL calls _window->setActive(true); @@ -174,38 +175,38 @@ void Screen::glDrawMesh(GLfloat* geometry, GLfloat* view, GLfloat* model, size_t _window->setActive(false); } -GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D& cameraPosition) { - std::vector& triangles = mesh->triangles(); +GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D &cameraPosition) { + std::vector &triangles = mesh->triangles(); - auto* geometry = (GLfloat*)malloc(7*3*triangles.size()*sizeof(GLfloat)); + auto *geometry = (GLfloat *) malloc(7 * 3 * triangles.size() * sizeof(GLfloat)); - for(int i = 0; i < triangles.size(); i++) { + for (int i = 0; i < triangles.size(); i++) { - int stride = 21*i; + int stride = 21 * i; double dot[3]; sf::Color ambientColor[3]; - Triangle MTriangle = triangles[i]*mesh->model(); + Triangle MTriangle = triangles[i] * mesh->model(); - for(int k = 0; k < 3; k++) { + for (int k = 0; k < 3; k++) { dot[k] = MTriangle.norm().dot((Vec3D(MTriangle[k]) - cameraPosition).normalized()); sf::Color color = triangles[i].color(); - ambientColor[k] = sf::Color((sf::Uint8)(color.r * (0.3 * std::abs(dot[k]) + 0.7)), - (sf::Uint8)(color.g * (0.3 * std::abs(dot[k]) + 0.7)), - (sf::Uint8)(color.b * (0.3 * std::abs(dot[k]) + 0.7)), - (sf::Uint8)color.a); + ambientColor[k] = sf::Color((sf::Uint8) (color.r * (0.3 * std::abs(dot[k]) + 0.7)), + (sf::Uint8) (color.g * (0.3 * std::abs(dot[k]) + 0.7)), + (sf::Uint8) (color.b * (0.3 * std::abs(dot[k]) + 0.7)), + (sf::Uint8) color.a); - geometry[stride + 7*k + 0] = (GLfloat)MTriangle[k].x(); - geometry[stride + 7*k + 1] = (GLfloat)MTriangle[k].y(); - geometry[stride + 7*k + 2] = (GLfloat)MTriangle[k].z(); + geometry[stride + 7 * k + 0] = (GLfloat) MTriangle[k].x(); + geometry[stride + 7 * k + 1] = (GLfloat) MTriangle[k].y(); + geometry[stride + 7 * k + 2] = (GLfloat) MTriangle[k].z(); - geometry[stride + 7*k + 3] = (GLfloat)ambientColor[k].r/255.0f; - geometry[stride + 7*k + 4] = (GLfloat)ambientColor[k].g/255.0f; - geometry[stride + 7*k + 5] = (GLfloat)ambientColor[k].b/255.0f; - geometry[stride + 7*k + 6] = (GLfloat)ambientColor[k].a/255.0f; + geometry[stride + 7 * k + 3] = (GLfloat) ambientColor[k].r / 255.0f; + geometry[stride + 7 * k + 4] = (GLfloat) ambientColor[k].g / 255.0f; + geometry[stride + 7 * k + 5] = (GLfloat) ambientColor[k].b / 255.0f; + geometry[stride + 7 * k + 6] = (GLfloat) ambientColor[k].a / 255.0f; } } return geometry; diff --git a/engine/Screen.h b/engine/Screen.h index b5ab6ed..0858728 100644 --- a/engine/Screen.h +++ b/engine/Screen.h @@ -7,9 +7,11 @@ #include -#include "Triangle.h" -#include #include + +#include + +#include "Triangle.h" #include "utils/Time.h" #include "Consts.h" #include "Mesh.h" @@ -26,33 +28,44 @@ private: const std::shared_ptr _window = std::make_shared(); public: - void open(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT, const std::string& name = Consts::PROJECT_NAME, bool verticalSync = true, sf::Color background = Consts::BACKGROUND_COLOR, sf::Uint32 style = sf::Style::Default); + void open(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT, + const std::string &name = Consts::PROJECT_NAME, bool verticalSync = true, + sf::Color background = Consts::BACKGROUND_COLOR, sf::Uint32 style = sf::Style::Default); void display(); + void clear(); + [[nodiscard]] bool hasFocus() const { return _window->hasFocus(); } - void drawTriangle(const Triangle& triangle); - void drawTetragon(const Vec2D& p1, const Vec2D& p2, const Vec2D& p3, const Vec2D& p4, sf::Color color); - void drawText(const std::string& string, const Vec2D& position, int size, sf::Color color); - void drawText(const sf::Text& text); - void drawSprite(const sf::Sprite& sprite); + void drawTriangle(const Triangle &triangle); + + void drawTetragon(const Vec2D &p1, const Vec2D &p2, const Vec2D &p3, const Vec2D &p4, sf::Color color); + + void drawText(const std::string &string, const Vec2D &position, int size, sf::Color color); + + void drawText(const sf::Text &text); + + void drawSprite(const sf::Sprite &sprite); + + void setTitle(const std::string &title); - void setTitle(const std::string& title); [[nodiscard]] std::string title() const { return _title; }; bool isOpen(); - [[nodiscard]] int width() const {return _window->getSize().x;} - [[nodiscard]] int height() const {return _window->getSize().y;} + [[nodiscard]] int width() const { return _window->getSize().x; } + + [[nodiscard]] int height() const { return _window->getSize().y; } void close(); void setMouseCursorVisible(bool visible); // OpenGL functions - void glDrawMesh(GLfloat* geometry, GLfloat* view, GLfloat* model, size_t count); - static GLfloat* glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D& cameraPosition); + void glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count); + + static GLfloat *glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D &cameraPosition); [[nodiscard]] std::shared_ptr renderWindow() { return _window; } }; diff --git a/engine/SoundController.cpp b/engine/SoundController.cpp index a0e58fb..b83035c 100644 --- a/engine/SoundController.cpp +++ b/engine/SoundController.cpp @@ -6,7 +6,7 @@ #include "ResourceManager.h" #include "utils/Log.h" -SoundController* SoundController::_instance = nullptr; +SoundController *SoundController::_instance = nullptr; bool SoundController::_validInstance = false; @@ -17,12 +17,12 @@ void SoundController::init() { Log::log("SoundController::init(): sound controller was initialized"); } -void SoundController::playSound(const SoundTag& soundTag, const std::string& filename) { - if(!_validInstance) { +void SoundController::playSound(const SoundTag &soundTag, const std::string &filename) { + if (!_validInstance) { return; } - if(_instance->_sounds.count(soundTag) == 0) { + if (_instance->_sounds.count(soundTag) == 0) { _instance->_sounds.emplace(soundTag, sf::Sound(*ResourceManager::loadSoundBuffer(filename))); } _instance->_sounds[soundTag].play(); @@ -30,36 +30,36 @@ void SoundController::playSound(const SoundTag& soundTag, const std::string& fil Log::log("SoundController::playSound(): play sound '" + soundTag.str() + "'"); } -void SoundController::pauseSound(const SoundTag& soundTag) { - if(!_validInstance) { +void SoundController::pauseSound(const SoundTag &soundTag) { + if (!_validInstance) { return; } - if(_instance->_sounds.count(soundTag) > 0) { + if (_instance->_sounds.count(soundTag) > 0) { _instance->_sounds[soundTag].pause(); } Log::log("SoundController::pauseSound(): sound '" + soundTag.str() + "' was paused"); } -void SoundController::stopSound(const SoundTag& soundTag) { - if(!_validInstance) { +void SoundController::stopSound(const SoundTag &soundTag) { + if (!_validInstance) { return; } - if(_instance->_sounds.count(soundTag) > 0) { + if (_instance->_sounds.count(soundTag) > 0) { _instance->_sounds[soundTag].stop(); } Log::log("SoundController::stopSound(): sound '" + soundTag.str() + "' was stopped"); } -sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) { - if(!_validInstance) { +sf::Sound::Status SoundController::getStatus(const SoundTag &soundTag) { + if (!_validInstance) { return sf::Sound::Status::Stopped; } - if(_instance->_sounds.count(soundTag) > 0) { + if (_instance->_sounds.count(soundTag) > 0) { return _instance->_sounds[soundTag].getStatus(); } else { return sf::Sound::Status::Stopped; @@ -67,8 +67,8 @@ sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) { } void SoundController::free() { - if(_validInstance) { - for(auto& [soundTag, sound] : _instance->_sounds) { + if (_validInstance) { + for (auto&[soundTag, sound] : _instance->_sounds) { stopSound(soundTag); } _instance->_sounds.clear(); diff --git a/engine/SoundController.h b/engine/SoundController.h index b175d8d..38b17f2 100644 --- a/engine/SoundController.h +++ b/engine/SoundController.h @@ -7,6 +7,7 @@ #include #include + #include class SoundTag final { @@ -14,31 +15,40 @@ private: const std::string _name; public: explicit SoundTag(std::string name = "") : _name(std::move(name)) {} + [[nodiscard]] std::string str() const { return _name; } - bool operator==(const SoundTag& tag) const { return _name == tag._name; } - bool operator!=(const SoundTag& tag) const { return _name != tag._name; } - bool operator<(const SoundTag& tag) const { return _name < tag._name; } + bool operator==(const SoundTag &tag) const { return _name == tag._name; } + + bool operator!=(const SoundTag &tag) const { return _name != tag._name; } + + bool operator<(const SoundTag &tag) const { return _name < tag._name; } }; class SoundController final { private: std::map _sounds; - static SoundController* _instance; + static SoundController *_instance; static bool _validInstance; SoundController() = default; -public: - SoundController(const SoundController&) = delete; - SoundController& operator=(SoundController&) = delete; - static void playSound(const SoundTag& soundTag, const std::string& filename); - static void pauseSound(const SoundTag& soundTag); - static void stopSound(const SoundTag& soundTag); - static sf::Sound::Status getStatus(const SoundTag& soundTag); +public: + SoundController(const SoundController &) = delete; + + SoundController &operator=(SoundController &) = delete; + + static void playSound(const SoundTag &soundTag, const std::string &filename); + + static void pauseSound(const SoundTag &soundTag); + + static void stopSound(const SoundTag &soundTag); + + static sf::Sound::Status getStatus(const SoundTag &soundTag); static void init(); + static void free(); }; diff --git a/engine/Triangle.cpp b/engine/Triangle.cpp index 515a1a8..8d25f1a 100644 --- a/engine/Triangle.cpp +++ b/engine/Triangle.cpp @@ -5,10 +5,12 @@ #include "Triangle.h" #include "Consts.h" -Triangle::Triangle(const Vec4D& p1, const Vec4D& p2, const Vec4D& p3, sf::Color color) : _color(color), _points{p1, p2, p3} { +Triangle::Triangle(const Vec4D &p1, const Vec4D &p2, const Vec4D &p3, sf::Color color) : _color(color), + _points{p1, p2, p3} { } -Triangle::Triangle(const Triangle &triangle) : _points{triangle._points[0], triangle._points[1], triangle._points[2]}, _color(triangle._color) { +Triangle::Triangle(const Triangle &triangle) : _points{triangle._points[0], triangle._points[1], triangle._points[2]}, + _color(triangle._color) { } Triangle Triangle::operator*(const Matrix4x4 &matrix4X4) const { @@ -21,7 +23,7 @@ Vec3D Triangle::norm() const { Vec3D v2 = Vec3D(_points[2] - _points[0]); Vec3D crossProduct = v1.cross(v2); - if(crossProduct.sqrAbs() > Consts::EPS) { + if (crossProduct.sqrAbs() > Consts::EPS) { return crossProduct.normalized(); } else { return Vec3D(0); @@ -39,7 +41,7 @@ bool Triangle::isPointInside(const Vec3D &point) const { double dot2 = (point - Vec3D(_points[1])).cross(Vec3D(_points[2] - _points[1])).dot(triangleNorm); double dot3 = (point - Vec3D(_points[2])).cross(Vec3D(_points[0] - _points[2])).dot(triangleNorm); - if((dot1 >= 0 && dot2 >= 0 && dot3 >= 0) || (dot1 <= 0 && dot2 <= 0 && dot3 <= 0)) { + if ((dot1 >= 0 && dot2 >= 0 && dot3 >= 0) || (dot1 <= 0 && dot2 <= 0 && dot3 <= 0)) { return true; } return false; diff --git a/engine/Triangle.h b/engine/Triangle.h index 714d630..733ba4f 100644 --- a/engine/Triangle.h +++ b/engine/Triangle.h @@ -5,10 +5,11 @@ #ifndef ENGINE_TRIANGLE_H #define ENGINE_TRIANGLE_H +#include + #include "Vec4D.h" #include "Vec3D.h" #include "Matrix4x4.h" -#include class Triangle final { private: @@ -16,21 +17,25 @@ private: Vec4D _points[3]; public: Triangle() : _points{Vec4D{}, Vec4D{}, Vec4D{}} {}; - Triangle(const Triangle& triangle); - Triangle(const Vec4D& p1, const Vec4D& p2, const Vec4D& p3, sf::Color color = {0, 0, 0}); - Triangle& operator=(const Triangle&) = default; - [[nodiscard]] Vec4D operator[] (int i) const; + Triangle(const Triangle &triangle); + + Triangle(const Vec4D &p1, const Vec4D &p2, const Vec4D &p3, sf::Color color = {0, 0, 0}); + + Triangle &operator=(const Triangle &) = default; + + [[nodiscard]] Vec4D operator[](int i) const; + [[nodiscard]] Vec3D norm() const; // Operations with Matrix4x4 - [[nodiscard]] Triangle operator*(const Matrix4x4& matrix4X4) const; + [[nodiscard]] Triangle operator*(const Matrix4x4 &matrix4X4) const; - [[nodiscard]] bool isPointInside(const Vec3D& point) const; + [[nodiscard]] bool isPointInside(const Vec3D &point) const; [[nodiscard]] sf::Color color() const { return _color; } - [[nodiscard]] double distance(const Vec3D& vec) const { return norm().dot(Vec3D(_points[0]) - vec); } + [[nodiscard]] double distance(const Vec3D &vec) const { return norm().dot(Vec3D(_points[0]) - vec); } }; diff --git a/engine/Vec2D.cpp b/engine/Vec2D.cpp index 1f3c7e7..5db1e25 100644 --- a/engine/Vec2D.cpp +++ b/engine/Vec2D.cpp @@ -3,6 +3,7 @@ // #include + #include "Vec2D.h" #include "Consts.h" @@ -11,7 +12,7 @@ Vec2D::Vec2D(const Vec2D &vec) { _arr_point[1] = vec.y(); } -Vec2D::Vec2D (double x, double y) { +Vec2D::Vec2D(double x, double y) { _arr_point[0] = x; _arr_point[1] = y; } @@ -25,26 +26,28 @@ Vec2D Vec2D::operator-() const { return Vec2D(-x(), -y()); } -bool Vec2D::operator==(const Vec2D& vec) const { +bool Vec2D::operator==(const Vec2D &vec) const { return (*this - vec).sqrAbs() < Consts::EPS; } -bool Vec2D::operator!=(const Vec2D& vec) const { + +bool Vec2D::operator!=(const Vec2D &vec) const { return !(*this == vec); } -Vec2D Vec2D::operator+(const Vec2D& vec) const { - return Vec2D(x()+vec.x(), y()+vec.y()); +Vec2D Vec2D::operator+(const Vec2D &vec) const { + return Vec2D(x() + vec.x(), y() + vec.y()); } -Vec2D Vec2D::operator-(const Vec2D& vec) const { + +Vec2D Vec2D::operator-(const Vec2D &vec) const { return Vec2D(*this) + -vec; } Vec2D Vec2D::operator*(double number) const { - return Vec2D(x()*number, y()*number); + return Vec2D(x() * number, y() * number); } Vec2D Vec2D::operator/(double number) const { - if(std::abs(number) > Consts::EPS) { + if (std::abs(number) > Consts::EPS) { return Vec2D(*this) * (1.0 / number); } else { throw std::domain_error{"Vec2D::operator/(double number): division by zero"}; @@ -53,7 +56,7 @@ Vec2D Vec2D::operator/(double number) const { // Other useful methods double Vec2D::sqrAbs() const { - return x()*x() + y()*y(); + return x() * x() + y() * y(); } double Vec2D::abs() const { @@ -62,13 +65,13 @@ double Vec2D::abs() const { Vec2D Vec2D::normalized() const { double vecAbs = abs(); - if(vecAbs > Consts::EPS) { + if (vecAbs > Consts::EPS) { return Vec2D(*this) / abs(); } else { return Vec2D(0); } } -double Vec2D::dot(const Vec2D& vec) const { +double Vec2D::dot(const Vec2D &vec) const { return vec.x() * x() + vec.y() * y(); } diff --git a/engine/Vec2D.h b/engine/Vec2D.h index 168b348..7c2b862 100644 --- a/engine/Vec2D.h +++ b/engine/Vec2D.h @@ -6,6 +6,7 @@ #define SHOOTER_VEC2D_H #include + #include "Vec4D.h" class Vec2D final { @@ -13,11 +14,15 @@ private: std::array _arr_point{}; public: - Vec2D () = default; - Vec2D (const Vec2D& vec); - explicit Vec2D (const Vec4D& point4D); - explicit Vec2D (double x, double y = 0.0); - Vec2D& operator=(const Vec2D&) = default; + Vec2D() = default; + + Vec2D(const Vec2D &vec); + + explicit Vec2D(const Vec4D &point4D); + + explicit Vec2D(double x, double y = 0.0); + + Vec2D &operator=(const Vec2D &) = default; [[nodiscard]] double x() const { return _arr_point[0]; } [[nodiscard]] double y() const { return _arr_point[1]; } @@ -25,13 +30,13 @@ public: [[nodiscard]] Vec2D operator-() const; // Boolean operations - bool operator==(const Vec2D& vec) const; - bool operator!=(const Vec2D& vec) const; + bool operator==(const Vec2D &vec) const; + bool operator!=(const Vec2D &vec) const; - [[nodiscard]] Vec2D operator+(const Vec2D& vec) const; - [[nodiscard]] Vec2D operator-(const Vec2D& vec) const; + [[nodiscard]] Vec2D operator+(const Vec2D &vec) const; + [[nodiscard]] Vec2D operator-(const Vec2D &vec) const; - [[nodiscard]] double dot(const Vec2D& vec) const; // Returns dot product + [[nodiscard]] double dot(const Vec2D &vec) const; // Returns dot product // Operations with numbers [[nodiscard]] Vec2D operator*(double number) const; diff --git a/engine/Vec3D.cpp b/engine/Vec3D.cpp index 82cc2d6..b216a2f 100644 --- a/engine/Vec3D.cpp +++ b/engine/Vec3D.cpp @@ -2,24 +2,25 @@ // Created by Иван Ильин on 09.10.2021. // -#include "Vec3D.h" -#include "Consts.h" #include #include +#include "Vec3D.h" +#include "Consts.h" + Vec3D::Vec3D(const Vec3D &vec) { _arr_point[0] = vec.x(); _arr_point[1] = vec.y(); _arr_point[2] = vec.z(); } -Vec3D::Vec3D (const Vec4D& point4D) { +Vec3D::Vec3D(const Vec4D &point4D) { _arr_point[0] = point4D.x(); _arr_point[1] = point4D.y(); _arr_point[2] = point4D.z(); } -Vec3D::Vec3D (double x, double y, double z) { +Vec3D::Vec3D(double x, double y, double z) { _arr_point[0] = x; _arr_point[1] = y; _arr_point[2] = z; @@ -29,27 +30,29 @@ Vec3D Vec3D::operator-() const { return Vec3D(-x(), -y(), -z()); } -bool Vec3D::operator==(const Vec3D& vec) const { +bool Vec3D::operator==(const Vec3D &vec) const { return (*this - vec).sqrAbs() < Consts::EPS; } -bool Vec3D::operator!=(const Vec3D& vec) const { + +bool Vec3D::operator!=(const Vec3D &vec) const { return !(*this == vec); } // Operations with Vec3D -Vec3D Vec3D::operator+(const Vec3D& vec) const { - return Vec3D(x()+vec.x(), y()+vec.y(), z()+vec.z()); +Vec3D Vec3D::operator+(const Vec3D &vec) const { + return Vec3D(x() + vec.x(), y() + vec.y(), z() + vec.z()); } -Vec3D Vec3D::operator-(const Vec3D& vec) const { + +Vec3D Vec3D::operator-(const Vec3D &vec) const { return Vec3D(*this) + -vec; } Vec3D Vec3D::operator*(double number) const { - return Vec3D(x()*number, y()*number, z()*number); + return Vec3D(x() * number, y() * number, z() * number); } Vec3D Vec3D::operator/(double number) const { - if(std::abs(number) > Consts::EPS) { + if (std::abs(number) > Consts::EPS) { return Vec3D(*this) * (1.0 / number); } else { throw std::domain_error{"Vec3D::operator/(double number): division by zero"}; @@ -58,7 +61,7 @@ Vec3D Vec3D::operator/(double number) const { // Other useful methods double Vec3D::sqrAbs() const { - return x()*x() + y()*y() + z()*z(); + return x() * x() + y() * y() + z() * z(); } double Vec3D::abs() const { @@ -67,18 +70,18 @@ double Vec3D::abs() const { Vec3D Vec3D::normalized() const { double vecAbs = abs(); - if(vecAbs > Consts::EPS) { + if (vecAbs > Consts::EPS) { return Vec3D(*this) / abs(); } else { return Vec3D(1); } } -double Vec3D::dot(const Vec3D& vec) const { +double Vec3D::dot(const Vec3D &vec) const { return vec.x() * x() + vec.y() * y() + vec.z() * z(); } -Vec3D Vec3D::cross(const Vec3D& vec) const { +Vec3D Vec3D::cross(const Vec3D &vec) const { return Vec3D{y() * vec.z() - vec.y() * z(), z() * vec.x() - vec.z() * x(), x() * vec.y() - vec.x() * y()}; @@ -89,5 +92,5 @@ Vec4D Vec3D::makePoint4D() const { } Vec3D Vec3D::Random() { - return Vec3D((double)rand()/RAND_MAX, (double)rand()/RAND_MAX, (double)rand()/RAND_MAX); + return Vec3D((double) rand() / RAND_MAX, (double) rand() / RAND_MAX, (double) rand() / RAND_MAX); } diff --git a/engine/Vec3D.h b/engine/Vec3D.h index 43c65b1..996fdc8 100644 --- a/engine/Vec3D.h +++ b/engine/Vec3D.h @@ -6,6 +6,7 @@ #define SHOOTER_VEC3D_H #include + #include "Vec4D.h" class Vec3D final { @@ -13,11 +14,15 @@ private: std::array _arr_point{}; public: - Vec3D () = default; - Vec3D (const Vec3D& vec); - explicit Vec3D (const Vec4D& vec); - explicit Vec3D (double x, double y = 0.0, double z = 0.0); - Vec3D& operator=(const Vec3D&) = default; + Vec3D() = default; + + Vec3D(const Vec3D &vec); + + explicit Vec3D(const Vec4D &vec); + + explicit Vec3D(double x, double y = 0.0, double z = 0.0); + + Vec3D &operator=(const Vec3D &) = default; [[nodiscard]] double x() const { return _arr_point[0]; } [[nodiscard]] double y() const { return _arr_point[1]; } @@ -26,15 +31,15 @@ public: [[nodiscard]] Vec3D operator-() const; // Boolean operations - bool operator==(const Vec3D& vec) const; - bool operator!=(const Vec3D& vec) const; + bool operator==(const Vec3D &vec) const; + bool operator!=(const Vec3D &vec) const; // Operations with Vec4D - [[nodiscard]] Vec3D operator+(const Vec3D& vec) const; - [[nodiscard]] Vec3D operator-(const Vec3D& vec) const; + [[nodiscard]] Vec3D operator+(const Vec3D &vec) const; + [[nodiscard]] Vec3D operator-(const Vec3D &vec) const; - [[nodiscard]] double dot(const Vec3D& vec) const; // Returns dot product - [[nodiscard]] Vec3D cross(const Vec3D& vec) const; // Returns cross product + [[nodiscard]] double dot(const Vec3D &vec) const; // Returns dot product + [[nodiscard]] Vec3D cross(const Vec3D &vec) const; // Returns cross product // Operations with numbers [[nodiscard]] Vec3D operator*(double number) const; diff --git a/engine/Vec4D.cpp b/engine/Vec4D.cpp index c60c10e..6aa8423 100644 --- a/engine/Vec4D.cpp +++ b/engine/Vec4D.cpp @@ -2,12 +2,13 @@ // Created by Иван Ильин on 12.01.2021. // -#include "Vec4D.h" -#include "Consts.h" #include #include -Vec4D::Vec4D (double x, double y, double z, double w) { +#include "Vec4D.h" +#include "Consts.h" + +Vec4D::Vec4D(double x, double y, double z, double w) { _arr_point[0] = x; _arr_point[1] = y; _arr_point[2] = z; @@ -25,20 +26,20 @@ Vec4D::Vec4D(const Vec4D &point4D) { return Vec4D(-x(), -y(), -z(), -w()); } -bool Vec4D::operator==(const Vec4D& point4D) const { +bool Vec4D::operator==(const Vec4D &point4D) const { return (*this - point4D).sqrAbs() < Consts::EPS; } -bool Vec4D::operator!=(const Vec4D& point4D) const { +bool Vec4D::operator!=(const Vec4D &point4D) const { return !(*this == point4D); } // Operations with Vec4D -Vec4D Vec4D::operator+(const Vec4D& point4D) const { +Vec4D Vec4D::operator+(const Vec4D &point4D) const { return Vec4D(x() + point4D.x(), y() + point4D.y(), z() + point4D.z(), w() + point4D.w()); } -Vec4D Vec4D::operator-(const Vec4D& point4D) const { +Vec4D Vec4D::operator-(const Vec4D &point4D) const { return Vec4D(*this) + -point4D; } @@ -47,7 +48,7 @@ Vec4D Vec4D::operator*(double number) const { } Vec4D Vec4D::operator/(double number) const { - if(std::abs(number) > Consts::EPS) { + if (std::abs(number) > Consts::EPS) { return Vec4D(*this) * (1.0 / number); } else { throw std::domain_error{"Vec4D::operator/(double number): division by zero"}; @@ -56,7 +57,7 @@ Vec4D Vec4D::operator/(double number) const { // Other useful methods double Vec4D::sqrAbs() const { - return x()*x() + y()*y() + z()*z(); + return x() * x() + y() * y() + z() * z(); } double Vec4D::abs() const { @@ -65,7 +66,7 @@ double Vec4D::abs() const { Vec4D Vec4D::normalized() const { double vecAbs = abs(); - if(vecAbs > Consts::EPS) { + if (vecAbs > Consts::EPS) { return Vec4D(*this) / abs(); } else { return Vec4D(1); diff --git a/engine/Vec4D.h b/engine/Vec4D.h index a8bdc97..0b310db 100644 --- a/engine/Vec4D.h +++ b/engine/Vec4D.h @@ -12,11 +12,13 @@ private: std::array _arr_point{}; public: - Vec4D () = default; - Vec4D (const Vec4D& point4D); - explicit Vec4D (double x, double y = 0.0, double z = 0.0, double w = 0.0); - Vec4D& operator=(const Vec4D& point4D) = default; + Vec4D() = default; + Vec4D(const Vec4D &point4D); + + explicit Vec4D(double x, double y = 0.0, double z = 0.0, double w = 0.0); + + Vec4D &operator=(const Vec4D &point4D) = default; [[nodiscard]] double x() const { return _arr_point[0]; } [[nodiscard]] double y() const { return _arr_point[1]; } @@ -26,12 +28,13 @@ public: [[nodiscard]] Vec4D operator-() const; // Boolean operations - bool operator==(const Vec4D& point4D) const; - bool operator!=(const Vec4D& point4D) const; + bool operator==(const Vec4D &point4D) const; + + bool operator!=(const Vec4D &point4D) const; // Operations with Vec4D - [[nodiscard]] Vec4D operator+(const Vec4D& point4D) const; - [[nodiscard]] Vec4D operator-(const Vec4D& point4D) const; + [[nodiscard]] Vec4D operator+(const Vec4D &point4D) const; + [[nodiscard]] Vec4D operator-(const Vec4D &point4D) const; // Operations with numbers [[nodiscard]] Vec4D operator*(double number) const; diff --git a/engine/World.cpp b/engine/World.cpp index b04c667..88a7b55 100644 --- a/engine/World.cpp +++ b/engine/World.cpp @@ -2,29 +2,32 @@ // Created by Иван Ильин on 13.01.2021. // +#include +#include + #include "World.h" #include "utils/Log.h" #include "Plane.h" #include "ResourceManager.h" -#include -#include using namespace std; void World::addBody(std::shared_ptr body) { _objects.emplace(body->name(), body); - Log::log("World::addBody(): inserted body '" + body->name().str() + "' with " + std::to_string(_objects[body->name()]->triangles().size()) + " tris."); + Log::log("World::addBody(): inserted body '" + body->name().str() + "' with " + + std::to_string(_objects[body->name()]->triangles().size()) + " tris."); } -void World::loadBody(const ObjectNameTag& tag, const string &filename, const Vec3D& scale) { +void World::loadBody(const ObjectNameTag &tag, const string &filename, const Vec3D &scale) { _objects.emplace(tag, std::make_shared(Mesh(tag, filename, scale))); - Log::log("World::loadBody(): inserted body from " + filename + " with title '" + tag.str() + "' with " + std::to_string(_objects[tag]->triangles().size()) + " tris."); + Log::log("World::loadBody(): inserted body from " + filename + " with title '" + tag.str() + "' with " + + std::to_string(_objects[tag]->triangles().size()) + " tris."); } -IntersectionInformation World::rayCast(const Vec3D& from, const Vec3D& to, const std::string& skipTags) { +IntersectionInformation World::rayCast(const Vec3D &from, const Vec3D &to, const std::string &skipTags) { // make vector of tags, that we are going to escape - vector tagsToSkip; + vector tagsToSkip; stringstream s(skipTags); std::string t; while (s >> t) { @@ -38,27 +41,27 @@ IntersectionInformation World::rayCast(const Vec3D& from, const Vec3D& to, const double minDistance = Consts::RAY_CAST_MAX_DISTANCE; std::shared_ptr intersectedBody = nullptr; - for(auto& [name, body] : _objects) { + for (auto&[name, body] : _objects) { bool escapeThisBody = false; - for (auto& escapeTag : tagsToSkip) { + for (auto &escapeTag : tagsToSkip) { if (name.str().find(escapeTag) != std::string::npos) { escapeThisBody = true; break; } } - if(escapeThisBody) { + if (escapeThisBody) { continue; } - for(auto& tri : body->triangles()) { + for (auto &tri : body->triangles()) { Matrix4x4 model = body->model(); - Triangle tri_translated(model*tri[0], model*tri[1], model*tri[2]); + Triangle tri_translated(model * tri[0], model * tri[1], model * tri[2]); Plane plane(tri_translated); auto intersection = plane.intersection(from, to); double distance = (intersection.first - from).sqrAbs(); - if(intersection.second > 0 && distance < minDistance && tri_translated.isPointInside(intersection.first)) { + if (intersection.second > 0 && distance < minDistance && tri_translated.isPointInside(intersection.first)) { minDistance = distance; point = intersection.first; triangle = tri_translated; @@ -68,27 +71,28 @@ IntersectionInformation World::rayCast(const Vec3D& from, const Vec3D& to, const } } } - return IntersectionInformation{point, sqrt(minDistance), triangle, ObjectNameTag(bodyName), intersectedBody, intersected}; + return IntersectionInformation{point, sqrt(minDistance), triangle, ObjectNameTag(bodyName), intersectedBody, + intersected}; } -void World::loadMap(const std::string& filename, const Vec3D& scale) { +void World::loadMap(const std::string &filename, const Vec3D &scale) { auto objs = ResourceManager::loadObjects(filename); - for(auto & i : objs) { + for (auto &i : objs) { std::shared_ptr obj = std::make_shared(*i); addBody(obj); obj->scale(scale); } } -void World::removeBody(const ObjectNameTag& tag) { - if(_objects.erase(tag) > 0) { +void World::removeBody(const ObjectNameTag &tag) { + if (_objects.erase(tag) > 0) { Log::log("World::removeBody(): removed body '" + tag.str() + "'"); } else { Log::log("World::removeBody(): cannot remove body '" + tag.str() + "': body does not exist."); } } -void World::checkCollision(const ObjectNameTag& tag) { +void World::checkCollision(const ObjectNameTag &tag) { if (_objects[tag]->isCollision()) { _objects[tag]->setInCollision(false); @@ -98,7 +102,7 @@ void World::checkCollision(const ObjectNameTag& tag) { ObjectNameTag name = it->first; it++; - if(name != tag) { + if (name != tag) { std::pair gjk = _objects[tag]->checkGJKCollision(obj); if (gjk.first) { if (obj->isCollider()) { @@ -121,8 +125,8 @@ void World::update() { } } -std::shared_ptr World::body(const ObjectNameTag& tag) { - if(_objects.count(tag) == 0) { +std::shared_ptr World::body(const ObjectNameTag &tag) { + if (_objects.count(tag) == 0) { return nullptr; } return _objects.find(tag)->second; diff --git a/engine/World.h b/engine/World.h index 4d63056..cbab883 100644 --- a/engine/World.h +++ b/engine/World.h @@ -6,6 +6,7 @@ #define ENGINE_WORLD_H #include + #include "Camera.h" #include "Screen.h" #include "physics/RigidBody.h" @@ -25,20 +26,25 @@ private: public: World() = default; - void checkCollision(const ObjectNameTag& tag); + void checkCollision(const ObjectNameTag &tag); + void update(); void addBody(std::shared_ptr mesh); - std::shared_ptr body(const ObjectNameTag& tag); - void removeBody(const ObjectNameTag& tag); - void loadBody(const ObjectNameTag& tag, const std::string &filename, const Vec3D& scale = Vec3D{1, 1, 1}); + + std::shared_ptr body(const ObjectNameTag &tag); + + void removeBody(const ObjectNameTag &tag); + + void loadBody(const ObjectNameTag &tag, const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); // std::string skipTags is a string that consist of all objects we want to skip in ray casting - IntersectionInformation rayCast(const Vec3D& from, const Vec3D& to, const std::string& skipTags = ""); + IntersectionInformation rayCast(const Vec3D &from, const Vec3D &to, const std::string &skipTags = ""); - void loadMap(const std::string& filename, const Vec3D & scale = Vec3D{1, 1, 1}); + void loadMap(const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); std::map>::iterator begin() { return _objects.begin(); } + std::map>::iterator end() { return _objects.end(); } }; diff --git a/engine/animation/AColor.h b/engine/animation/AColor.h index 73031a8..ea3cfe9 100644 --- a/engine/animation/AColor.h +++ b/engine/animation/AColor.h @@ -19,12 +19,12 @@ private: bool _started = false; void update() override { - if(_mesh.expired()) { + if (_mesh.expired()) { stop(); return; } - if(!_started) { + if (!_started) { _started = true; _startColor = _mesh.lock()->color(); } @@ -33,10 +33,15 @@ private: Vec4D end(_newColor.r, _newColor.g, _newColor.b, _newColor.a); Vec4D mid = start + (end - start) * progress(); - _mesh.lock()->setColor(sf::Color(static_cast(mid.x()), static_cast(mid.y()), static_cast(mid.z()), static_cast(mid.w()))); + _mesh.lock()->setColor(sf::Color(static_cast(mid.x()), static_cast(mid.y()), + static_cast(mid.z()), static_cast(mid.w()))); } + public: - AColor(std::weak_ptr mesh, const sf::Color &color, double duration = 1, LoopOut looped = LoopOut::None, InterpolationType interpolationType = InterpolationType::Linear) : Animation(duration, looped, interpolationType), _mesh(std::move(mesh)), _newColor(color) { + AColor(std::weak_ptr mesh, const sf::Color &color, double duration = 1, LoopOut looped = LoopOut::None, + InterpolationType interpolationType = InterpolationType::Linear) : Animation(duration, looped, + interpolationType), + _mesh(std::move(mesh)), _newColor(color) { } }; diff --git a/engine/animation/AFunction.h b/engine/animation/AFunction.h index ab8b6b4..1523196 100644 --- a/engine/animation/AFunction.h +++ b/engine/animation/AFunction.h @@ -6,6 +6,7 @@ #define ENGINE_AFUNCTION_H #include + #include "Animation.h" class AFunction final : public Animation { @@ -15,14 +16,16 @@ private: const std::function _callBack; void update() override { - if(_allCalls != 0 && progress() >= (double)(_callsCounter + 1) / (_allCalls + 1)) { + if (_allCalls != 0 && progress() >= (double) (_callsCounter + 1) / (_allCalls + 1)) { _callsCounter++; _callBack(); } } public: - explicit AFunction(std::function function, int calls = 1, double duration = 1, LoopOut looped = LoopOut::None, InterpolationType interpolationType = InterpolationType::Linear) : Animation(duration, looped, interpolationType), _callBack(std::move(function)), _allCalls(calls) { + explicit AFunction(std::function function, int calls = 1, double duration = 1, + LoopOut looped = LoopOut::None, InterpolationType interpolationType = InterpolationType::Linear) + : Animation(duration, looped, interpolationType), _callBack(std::move(function)), _allCalls(calls) { } }; diff --git a/engine/animation/ARotate.h b/engine/animation/ARotate.h index ca0ea85..089a0f3 100644 --- a/engine/animation/ARotate.h +++ b/engine/animation/ARotate.h @@ -16,15 +16,18 @@ private: const Vec3D _rotationValue; void update() override { - if(_object.expired()) { + if (_object.expired()) { stop(); return; } _object.lock()->rotate(_rotationValue * dprogress()); } + public: - ARotate(std::weak_ptr object, const Vec3D& r, double duration = 1, LoopOut looped = LoopOut::None, InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, interpolationType), _object(std::move(object)), _rotationValue(r) { + ARotate(std::weak_ptr object, const Vec3D &r, double duration = 1, LoopOut looped = LoopOut::None, + InterpolationType interpolationType = InterpolationType::Bezier) + : Animation(duration, looped, interpolationType), _object(std::move(object)), _rotationValue(r) { } }; diff --git a/engine/animation/AScale.h b/engine/animation/AScale.h index 1c901ff..adba083 100644 --- a/engine/animation/AScale.h +++ b/engine/animation/AScale.h @@ -16,20 +16,25 @@ private: const Vec3D _scalingValue; void update() override { - if(_object.expired()) { + if (_object.expired()) { stop(); return; } std::vector newTriangles; - for(auto &t : _object->triangles()) { - newTriangles.emplace_back(t * Matrix4x4::Scale(Vec3D{1, 1, 1} + (_scalingValue - Vec3D{1, 1, 1}) * progress())); + for (auto &t : _object->triangles()) { + newTriangles.emplace_back( + t * Matrix4x4::Scale(Vec3D{1, 1, 1} + (_scalingValue - Vec3D{1, 1, 1}) * progress())); } _object.lock()->setTriangles(newTriangles); return updateState(); } + public: - AScale(std::weak_ptr object, const Vec3D &s, double duration = 1, LoopOut looped = LoopOut::None, InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, interpolationType), _object(object), _scalingValue(s) { + AScale(std::weak_ptr object, const Vec3D &s, double duration = 1, LoopOut looped = LoopOut::None, + InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, + interpolationType), + _object(object), _scalingValue(s) { } }; diff --git a/engine/animation/ATranslate.h b/engine/animation/ATranslate.h index f627e73..ac7a50b 100644 --- a/engine/animation/ATranslate.h +++ b/engine/animation/ATranslate.h @@ -16,15 +16,20 @@ private: const Vec3D _translationValue; void update() override { - if(_object.expired()) { + if (_object.expired()) { stop(); return; } _object.lock()->translate(_translationValue * dprogress()); } + public: - ATranslate(std::weak_ptr object, const Vec3D& t, double duration = 1, LoopOut looped = LoopOut::None, InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, interpolationType), _object(std::move(object)), _translationValue(t){ + ATranslate(std::weak_ptr object, const Vec3D &t, double duration = 1, LoopOut looped = LoopOut::None, + InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, + interpolationType), + _object(std::move(object)), + _translationValue(t) { } }; diff --git a/engine/animation/ATranslateToPoint.h b/engine/animation/ATranslateToPoint.h index 329f0d8..4ee1048 100644 --- a/engine/animation/ATranslateToPoint.h +++ b/engine/animation/ATranslateToPoint.h @@ -19,19 +19,22 @@ private: bool _started = false; void update() override { - if(_object.expired()) { + if (_object.expired()) { stop(); return; } - if(!_started) { + if (!_started) { _started = true; _translationValue = _targetPoint - _object.lock()->position(); } _object.lock()->translate(_translationValue * dprogress()); } + public: - ATranslateToPoint(std::weak_ptr object, const Vec3D& p, double duration = 1, LoopOut looped = LoopOut::None, InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, interpolationType), _targetPoint(p), _object(object) { + ATranslateToPoint(std::weak_ptr object, const Vec3D &p, double duration = 1, LoopOut looped = LoopOut::None, + InterpolationType interpolationType = InterpolationType::Bezier) + : Animation(duration, looped, interpolationType), _targetPoint(p), _object(object) { } }; diff --git a/engine/animation/AWait.h b/engine/animation/AWait.h index 5042b36..59a3a40 100644 --- a/engine/animation/AWait.h +++ b/engine/animation/AWait.h @@ -9,7 +9,8 @@ class AWait final : public Animation { private: - void update() override{} + void update() override {} + public: explicit AWait(double duration = 1) : Animation(duration, LoopOut::None, InterpolationType::Linear, true) { } diff --git a/engine/animation/Animation.cpp b/engine/animation/Animation.cpp index 359f94c..2dbe2c2 100644 --- a/engine/animation/Animation.cpp +++ b/engine/animation/Animation.cpp @@ -4,19 +4,21 @@ #include "Animation.h" -Animation::Animation(double duration, Animation::LoopOut looped, Animation::InterpolationType intType, bool waitFor) : _duration(duration), _looped(looped), _intType(intType), _waitFor(waitFor) { +Animation::Animation(double duration, Animation::LoopOut looped, Animation::InterpolationType intType, bool waitFor) + : _duration(duration), _looped(looped), _intType(intType), _waitFor(waitFor) { } bool Animation::updateState() { - if(_finished || std::abs(_duration) < Consts::EPS) { + if (_finished || std::abs(_duration) < Consts::EPS) { + _finished = true; return false; } // linear normalized time: - _dtime = Time::deltaTime()/_duration; + _dtime = Time::deltaTime() / _duration; _time += _dtime; - if(_looped == LoopOut::Continue && _time > 0.5) { + if (_looped == LoopOut::Continue && _time > 0.5) { _time = 0.5; } @@ -38,7 +40,8 @@ bool Animation::updateState() { _dprogress = Interpolation::dCos(_time, _dtime); break; default: - throw std::logic_error{"Animation::updateState: unknown interpolation type " + std::to_string(static_cast(_intType))}; + throw std::logic_error{ + "Animation::updateState: unknown interpolation type " + std::to_string(static_cast(_intType))}; } update(); diff --git a/engine/animation/Animation.h b/engine/animation/Animation.h index a7262c5..8748963 100644 --- a/engine/animation/Animation.h +++ b/engine/animation/Animation.h @@ -41,8 +41,10 @@ private: // You should override this method for your particular animation virtual void update() = 0; + public: Animation(double duration, LoopOut looped, InterpolationType intType, bool _waitFor = false); + virtual ~Animation() = default; [[nodiscard]] bool waitFor() const { return _waitFor; } @@ -50,8 +52,10 @@ public: bool updateState(); [[nodiscard]] double progress() const { return _progress; } + [[nodiscard]] double dprogress() const { return _dprogress; } - void stop() { _finished = true;} + + void stop() { _finished = true; } }; #endif //INC_3DZAVR_ANIMATION_H diff --git a/engine/animation/Interpolation.h b/engine/animation/Interpolation.h index e6a1643..06033ca 100644 --- a/engine/animation/Interpolation.h +++ b/engine/animation/Interpolation.h @@ -5,31 +5,37 @@ #ifndef ENGINE_INTERPOLATION_H #define ENGINE_INTERPOLATION_H -#include "../Vec2D.h" - #include + +#include "../Vec2D.h" #include "../Consts.h" namespace Interpolation { static double Linear(double t); + static double Cos(double t); - static double Bezier(const Vec2D& p1, const Vec2D& p2, double t); + + static double Bezier(const Vec2D &p1, const Vec2D &p2, double t); + static double Bouncing(double t); static double dLinear(double t, double dt); + static double dCos(double t, double dt); - static double dBezier(const Vec2D& p1, const Vec2D& p2, double t, double dt); + + static double dBezier(const Vec2D &p1, const Vec2D &p2, double t, double dt); + static double dBouncing(double t, double dt); }; double Interpolation::Linear(double t) { - if(t < 0) + if (t < 0) t = -t; - return ((int)trunc(t) % 2) ? 1.0 - (t-trunc(t)) : (t-trunc(t)); + return ((int) trunc(t) % 2) ? 1.0 - (t - trunc(t)) : (t - trunc(t)); } double Interpolation::Cos(double t) { - return 0.5*(1 - cos(Consts::PI*Interpolation::Linear(t))); + return 0.5 * (1 - cos(Consts::PI * Interpolation::Linear(t))); } double Interpolation::Bezier(const Vec2D &p1, const Vec2D &p2, double t) { @@ -40,23 +46,23 @@ double Interpolation::Bezier(const Vec2D &p1, const Vec2D &p2, double t) { double eps = Consts::EPS; // We are trying to find 's' when px = t - auto f = [=](double s){ - return 3.0*(1.0-s)*(1.0-s)*s*p1.x() + 3.0*(1.0-s)*s*s*p2.x() + s*s*s - t; + auto f = [=](double s) { + return 3.0 * (1.0 - s) * (1.0 - s) * s * p1.x() + 3.0 * (1.0 - s) * s * s * p2.x() + s * s * s - t; }; // Using found 's' we will calculate resulting py - auto py = [=](double s){ - return 3.0*(1.0-s)*(1.0-s)*s*p1.y() + 3.0*(1.0-s)*s*s*p2.y() + s*s*s; + auto py = [=](double s) { + return 3.0 * (1.0 - s) * (1.0 - s) * s * p1.y() + 3.0 * (1.0 - s) * s * s * p2.y() + s * s * s; }; - auto df = [=](double s){ - return (f(s+h) - f(s-h))/(2.0*h); + auto df = [=](double s) { + return (f(s + h) - f(s - h)) / (2.0 * h); }; // Newton method double s1 = 0.0, s2 = 0.5; int i = 0; - while(std::abs(s1 - s2) > eps) { + while (std::abs(s1 - s2) > eps) { s1 = s2; s2 = s1 - f(s1) / df(s1); i++; @@ -67,15 +73,16 @@ double Interpolation::Bezier(const Vec2D &p1, const Vec2D &p2, double t) { double Interpolation::Bouncing(double t) { t = Interpolation::Linear(t); - return 0.5*(1.0/(1.0 + exp(10.0*(-4.0*t+0.8))) + (1.0 + 2.5*sin(50.0*(t - 1.0/3.0))*exp(-7.0*t))/(1.0+exp(10.0*(-15.0*t + 3.1)))); + return 0.5 * (1.0 / (1.0 + exp(10.0 * (-4.0 * t + 0.8))) + + (1.0 + 2.5 * sin(50.0 * (t - 1.0 / 3.0)) * exp(-7.0 * t)) / (1.0 + exp(10.0 * (-15.0 * t + 3.1)))); } double Interpolation::dLinear(double t, double dt) { - return ((int)trunc(t) % 2) ? -dt : dt; + return ((int) trunc(t) % 2) ? -dt : dt; } double Interpolation::dCos(double t, double dt) { - return 0.5*Consts::PI*sin(Consts::PI*t)*dt; + return 0.5 * Consts::PI * sin(Consts::PI * t) * dt; } double Interpolation::dBezier(const Vec2D &p1, const Vec2D &p2, double t, double dt) { diff --git a/engine/animation/Timeline.cpp b/engine/animation/Timeline.cpp index 5ec7cee..8e01319 100644 --- a/engine/animation/Timeline.cpp +++ b/engine/animation/Timeline.cpp @@ -3,12 +3,12 @@ // #include + #include "Animation.h" #include "Timeline.h" -#include #include "../utils/Log.h" -Timeline* Timeline::_instance = nullptr; +Timeline *Timeline::_instance = nullptr; bool Timeline::_validInstance = false; void Timeline::init() { @@ -18,8 +18,8 @@ void Timeline::init() { Log::log("Timeline::init(): animation timeline was initialized"); } -void Timeline::animate(const AnimationListTag& listName, std::shared_ptr anim) { - if(!_validInstance) { +void Timeline::animate(const AnimationListTag &listName, std::shared_ptr anim) { + if (!_validInstance) { return; } @@ -29,23 +29,23 @@ void Timeline::animate(const AnimationListTag& listName, std::shared_ptr_animations) { + for (auto&[listName, animationList] : _instance->_animations) { animCounter += animationList.size(); animationList.clear(); } _instance->_animations.clear(); - Log::log("Timeline::deleteAllAnimations(): all " + std::to_string(animCounter) + " animations was deleted" ); + Log::log("Timeline::deleteAllAnimations(): all " + std::to_string(animCounter) + " animations was deleted"); } -void Timeline::deleteAnimationList(const AnimationListTag& listName) { - if(!_validInstance) { +void Timeline::deleteAnimationList(const AnimationListTag &listName) { + if (!_validInstance) { return; } @@ -53,11 +53,12 @@ void Timeline::deleteAnimationList(const AnimationListTag& listName) { _instance->_animations[listName].clear(); _instance->_animations.erase(listName); - Log::log("Timeline::deleteAnimationList(): list '" + listName.str() +"' with " + std::to_string(animCounter) + " animations was deleted" ); + Log::log("Timeline::deleteAnimationList(): list '" + listName.str() + "' with " + std::to_string(animCounter) + + " animations was deleted"); } -[[nodiscard]] bool Timeline::isInAnimList(const AnimationListTag& listName) { - if(!_validInstance) { +[[nodiscard]] bool Timeline::isInAnimList(const AnimationListTag &listName) { + if (!_validInstance) { return false; } @@ -65,11 +66,11 @@ void Timeline::deleteAnimationList(const AnimationListTag& listName) { } void Timeline::update() { - if(!_validInstance) { + if (!_validInstance) { return; } - for (auto& [listName, animationList] : _instance->_animations) { + for (auto&[listName, animationList] : _instance->_animations) { if (animationList.empty()) { _instance->_animations.erase(listName); continue; diff --git a/engine/animation/Timeline.h b/engine/animation/Timeline.h index f8c8296..1c207a7 100644 --- a/engine/animation/Timeline.h +++ b/engine/animation/Timeline.h @@ -12,34 +12,42 @@ private: const std::string _name; public: explicit AnimationListTag(std::string name = "") : _name(std::move(name)) {} + [[nodiscard]] std::string str() const { return _name; } - bool operator==(const AnimationListTag& tag) const { return _name == tag._name; } - bool operator!=(const AnimationListTag& tag) const { return _name != tag._name; } - bool operator<(const AnimationListTag& tag) const { return _name < tag._name; } + bool operator==(const AnimationListTag &tag) const { return _name == tag._name; } + + bool operator!=(const AnimationListTag &tag) const { return _name != tag._name; } + + bool operator<(const AnimationListTag &tag) const { return _name < tag._name; } }; class Timeline { private: std::map>> _animations; - static Timeline* _instance; + static Timeline *_instance; static bool _validInstance; Timeline() = default; + public: - Timeline(const Timeline&) = delete; - Timeline& operator=(Timeline&) = delete; + Timeline(const Timeline &) = delete; + + Timeline &operator=(Timeline &) = delete; static void update(); - static void animate(const AnimationListTag& listName, std::shared_ptr anim); + + static void animate(const AnimationListTag &listName, std::shared_ptr anim); static void deleteAllAnimations(); - static void deleteAnimationList(const AnimationListTag& listName); - [[nodiscard]] static bool isInAnimList(const AnimationListTag& listName); + static void deleteAnimationList(const AnimationListTag &listName); + + [[nodiscard]] static bool isInAnimList(const AnimationListTag &listName); static void init(); + static void free(); }; diff --git a/engine/gui/Button.cpp b/engine/gui/Button.cpp index b06e6e3..2698943 100644 --- a/engine/gui/Button.cpp +++ b/engine/gui/Button.cpp @@ -2,38 +2,35 @@ // Created by Иван Ильин on 26.03.2021. // -#include "Button.h" - #include + +#include "Button.h" #include "../ResourceManager.h" -void Button::select() -{ +void Button::select() { if (!_selected && !_pressed) { _button.setTextureRect(sf::IntRect(_selectedState.tx, _selectedState.ty, _w, _h)); _selected = true; } } -void Button::unSelect() -{ +void Button::unSelect() { if (_selected && !_pressed) { _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); _selected = false; } } -void Button::press() -{ +void Button::press() { if (!_pressed) { _button.setTextureRect(sf::IntRect(_pressedState.tx, _pressedState.ty, _w, _h)); - if(_checkBox) { + if (_checkBox) { _pressed = true; } _click(); } else { _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); - if(_checkBox) { + if (_checkBox) { _pressed = false; } } @@ -42,20 +39,24 @@ void Button::press() void Button::init() { _button.setTexture(*ResourceManager::loadTexture(_texture)); _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); - _button.scale((float)_sx, (float)_sy); - _button.setPosition((float)(_x - _w * _sx / 2), (float)(_y - _h * _sy / 2)); + _button.scale(static_cast(_sx), static_cast(_sy)); + _button.setPosition(static_cast(_x) - static_cast(_w * _sx) / 2.0f, + static_cast(_y) - static_cast(_h * _sy) / 2.0f); _text.setFont(*ResourceManager::loadFont(_font)); _text.setString(_textString); - _text.setCharacterSize((unsigned int)(_h * _sy / 2)); + _text.setCharacterSize(static_cast((_h * _sy) / 2)); _text.setFillColor(_textColor); - _text.setPosition((float)(_x - _text.getLocalBounds().width / 2), (float)(_y - _h * _sy / 2 + _text.getLocalBounds().height / 4)); + _text.setPosition(static_cast(_x) - _text.getLocalBounds().width / 2.0f, + static_cast(_y) - static_cast(_h * _sy) / 2.0f + _text.getLocalBounds().height / 4.0f); } Button::Button(int x, int y, int width, int height, std::function click, std::string text, double sx, double sy, std::string texture, tPos usualState, tPos selectedState, tPos pressedState, std::string font, sf::Color textColor) : _x(x), _y(y), _w(width), _h(height), _click(std::move(click)), - _textString(std::move(text)), _sx(sx), _sy(sy), _texture(std::move(texture)), _usualState(usualState), _selectedState(selectedState), _pressedState(pressedState), - _font(std::move(font)), _textColor(textColor) { + _textString(std::move(text)), _sx(sx), _sy(sy), + _texture(std::move(texture)), _usualState(usualState), + _selectedState(selectedState), _pressedState(pressedState), + _font(std::move(font)), _textColor(textColor) { } diff --git a/engine/gui/Button.h b/engine/gui/Button.h index c7098a8..672fefa 100644 --- a/engine/gui/Button.h +++ b/engine/gui/Button.h @@ -5,9 +5,10 @@ #ifndef ENGINE_BUTTON_H #define ENGINE_BUTTON_H +#include + #include #include -#include struct tPos final { const int tx; @@ -46,22 +47,34 @@ private: public: Button() = default; - Button(int x, int y, int width, int height, std::function click, std::string text, double sx, double sy, std::string texture, tPos usualState, tPos selectedState, tPos pressedState, std::string font, sf::Color textColor); + + Button(int x, int y, int width, int height, std::function click, std::string text, double sx, double sy, + std::string texture, tPos usualState, tPos selectedState, tPos pressedState, std::string font, + sf::Color textColor); void select(); + void unSelect(); + void press(); void init(); [[nodiscard]] int x() const { return _x; } + [[nodiscard]] int y() const { return _y; } + [[nodiscard]] int w() const { return _w; } + [[nodiscard]] int h() const { return _h; } + [[nodiscard]] double sx() const { return _sx; } + [[nodiscard]] double sy() const { return _sy; } - [[nodiscard]] sf::Sprite const& sprite() const { return _button; } - [[nodiscard]] sf::Text const& text() const { return _text; } + + [[nodiscard]] sf::Sprite const &sprite() const { return _button; } + + [[nodiscard]] sf::Text const &text() const { return _text; } }; diff --git a/engine/gui/Window.cpp b/engine/gui/Window.cpp index 160edb7..5a3cffb 100644 --- a/engine/gui/Window.cpp +++ b/engine/gui/Window.cpp @@ -2,15 +2,17 @@ // Created by Иван Ильин on 26.03.2021. // -#include "Window.h" - #include + +#include "Window.h" #include "../ResourceManager.h" -void Window::addButton(int x, int y, int w, int h, std::function click, const std::string &text, double sx, double sy, +void Window::addButton(int x, int y, int w, int h, std::function click, const std::string &text, double sx, + double sy, const std::string &texture, tPos usualState, tPos selectedState, tPos pressedState, - const std::string& font, sf::Color textColor) { - _buttons.emplace_back(x, y, w, h, std::move(click), text, sx, sy, texture, usualState, selectedState, pressedState, font, textColor); + const std::string &font, sf::Color textColor) { + _buttons.emplace_back(x, y, w, h, std::move(click), text, sx, sy, texture, usualState, selectedState, pressedState, + font, textColor); _buttons.back().init(); } @@ -21,20 +23,23 @@ void Window::update() { Vec2D mousePos = _mouse->getMousePosition(); Vec2D dMousePos = mousePos - _prevMousePosition; - _back.setPosition(_back.getPosition() - sf::Vector2f((float)(dMousePos.x() / 30), (float)(dMousePos.y() / 30))); + _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); - for(auto& button : _buttons) { - if( mousePos.x() > button.x() - button.w() * button.sx() / 2 && mousePos.y() > button.y() - button.h() * button.sy() / 2 && - mousePos.x() < button.x() + button.w() * button.sx() / 2 && mousePos.y() < button.y() + button.h() * button.sy() / 2) { + for (auto &button : _buttons) { + if (mousePos.x() > button.x() - button.w() * button.sx() / 2.0f && + mousePos.y() > button.y() - button.h() * button.sy() / 2.0f && + mousePos.x() < button.x() + button.w() * button.sx() / 2.0f && + mousePos.y() < button.y() + button.h() * button.sy() / 2.0f) { button.select(); - if(isPressed) + if (isPressed) button.press(); } else { button.unSelect(); } - if(_screen->isOpen()) { + if (_screen->isOpen()) { _screen->drawSprite(button.sprite()); _screen->drawText(button.text()); } @@ -47,7 +52,7 @@ void Window::setBackgroundTexture(const std::string &texture, double sx, double _backTexture = texture; std::shared_ptr t = ResourceManager::loadTexture(_backTexture); t->setRepeated(true); - _back = sf::Sprite(*t, sf::IntRect(0, 0, (int)(w + w / 30.0), (int)(h + h / 30.0))); - _back.scale((float)sx, (float)sy); - _back.setPosition(sf::Vector2f(-w / 30.0f, -h / 30.0f)); + _back = sf::Sprite(*t, sf::IntRect(0, 0, static_cast(w + w / 30.0), static_cast(h + h / 30.0))); + _back.scale((float) sx, (float) sy); + _back.setPosition(sf::Vector2f(static_cast(-w) / 30.0f, static_cast(-h) / 30.0f)); } diff --git a/engine/gui/Window.h b/engine/gui/Window.h index c06adca..d6e0208 100644 --- a/engine/gui/Window.h +++ b/engine/gui/Window.h @@ -25,17 +25,20 @@ private: std::shared_ptr _screen; std::shared_ptr _mouse; public: - explicit Window(std::shared_ptr screen, std::shared_ptr mouse, std::string name = "Menu", std::string backTexture = "") : _screen(std::move(screen)), _mouse(std::move(mouse)), _name(std::move(name)), _backTexture(std::move(backTexture)){} + explicit Window(std::shared_ptr screen, std::shared_ptr mouse, std::string name = "Menu", + std::string backTexture = "") : _screen(std::move(screen)), _mouse(std::move(mouse)), + _name(std::move(name)), _backTexture(std::move(backTexture)) {} void addButton(int x, int y, int w, int h, std::function click, - const std::string& text = "_button", double sx = 1, double sy = 1, - const std::string& texture = "", tPos usualState = {}, tPos selectedState = {}, tPos pressedState = {}, - const std::string& font = Consts::MEDIUM_FONT, sf::Color textColor = {255, 255, 255}); + const std::string &text = "_button", double sx = 1, double sy = 1, + const std::string &texture = "", tPos usualState = {}, tPos selectedState = {}, + tPos pressedState = {}, + const std::string &font = Consts::MEDIUM_FONT, sf::Color textColor = {255, 255, 255}); - void setTitle(const std::string& title) { _name = title; } + void setTitle(const std::string &title) { _name = title; } - void setBackgroundTexture(const std::string& texture, double sx = 1, double sy = 1, int w = 1920, int h = 1080); + void setBackgroundTexture(const std::string &texture, double sx = 1, double sy = 1, int w = 1920, int h = 1080); void update(); }; diff --git a/engine/network/ClientUDP.cpp b/engine/network/ClientUDP.cpp index 63938af..eda6cf3 100644 --- a/engine/network/ClientUDP.cpp +++ b/engine/network/ClientUDP.cpp @@ -4,14 +4,12 @@ #include "ClientUDP.h" #include "MsgType.h" -#include #include "../utils/Time.h" -#include #include "../utils/Log.h" #include "../Consts.h" ClientUDP::ClientUDP() { - _socket.setTimeoutCallback([this](sf::Uint16 id) {return ClientUDP::timeout(id); } ); + _socket.setTimeoutCallback([this](sf::Uint16 id) { return ClientUDP::timeout(id); }); } bool ClientUDP::connected() const { diff --git a/engine/network/ClientUDP.h b/engine/network/ClientUDP.h index 396f4ac..a50c8fe 100644 --- a/engine/network/ClientUDP.h +++ b/engine/network/ClientUDP.h @@ -18,6 +18,7 @@ protected: sf::IpAddress _ip{}; bool process(); + bool timeout(sf::Uint16 id); public: @@ -25,25 +26,33 @@ public: explicit ClientUDP(); [[nodiscard]] bool isWorking() const; + [[nodiscard]] bool connected() const; + void connect(sf::IpAddress ip, sf::Uint16 port); + void disconnect(); + void update(); [[nodiscard]] sf::IpAddress serverIp() const { return _ip; } + [[nodiscard]] sf::Uint16 serverPort() const { return _port; } // virtual functions - virtual void updatePacket(){}; + virtual void updatePacket() {}; - virtual void processInit(sf::Packet& packet){}; - virtual void processUpdate(sf::Packet& packet){}; - virtual void processNewClient(sf::Packet& packet){}; - virtual void processDisconnect(sf::Uint16 targetId){}; + virtual void processInit(sf::Packet &packet) {}; - virtual void processCustomPacket(sf::Packet& packet){}; + virtual void processUpdate(sf::Packet &packet) {}; - virtual void processDisconnected(){}; + virtual void processNewClient(sf::Packet &packet) {}; + + virtual void processDisconnect(sf::Uint16 targetId) {}; + + virtual void processCustomPacket(sf::Packet &packet) {}; + + virtual void processDisconnected() {}; virtual ~ClientUDP() = default; }; diff --git a/engine/network/MsgType.cpp b/engine/network/MsgType.cpp index 8445335..7dab0b2 100644 --- a/engine/network/MsgType.cpp +++ b/engine/network/MsgType.cpp @@ -4,15 +4,13 @@ #include "MsgType.h" -sf::Packet& operator<<(sf::Packet& packet, MsgType type) -{ - return packet << (sf::Uint16)type; +sf::Packet &operator<<(sf::Packet &packet, MsgType type) { + return packet << (sf::Uint16) type; } -sf::Packet& operator>>(sf::Packet& packet, MsgType& type) -{ +sf::Packet &operator>>(sf::Packet &packet, MsgType &type) { sf::Uint16 temp; packet >> temp; - type = (MsgType)temp; + type = (MsgType) temp; return packet; } diff --git a/engine/network/MsgType.h b/engine/network/MsgType.h index 5e5bee8..4d60f31 100644 --- a/engine/network/MsgType.h +++ b/engine/network/MsgType.h @@ -7,8 +7,7 @@ #include -enum class MsgType -{ +enum class MsgType { // internal messages Empty, // Empty message (there are no message) Error, // Error message (something went wrong) @@ -26,8 +25,9 @@ enum class MsgType Custom, }; -sf::Packet& operator<<(sf::Packet& packet, MsgType type); -sf::Packet& operator>>(sf::Packet& packet, MsgType& type); +sf::Packet &operator<<(sf::Packet &packet, MsgType type); + +sf::Packet &operator>>(sf::Packet &packet, MsgType &type); #endif //INC_3DZAVR_MSGTYPE_H diff --git a/engine/network/ReliableMsg.cpp b/engine/network/ReliableMsg.cpp index 82b8ac4..3c2e095 100644 --- a/engine/network/ReliableMsg.cpp +++ b/engine/network/ReliableMsg.cpp @@ -2,16 +2,19 @@ // Created by Neirokan on 30.04.2020 // -#include #include "ReliableMsg.h" #include "../utils/Time.h" #include "../Consts.h" -ReliableMsg::ReliableMsg(sf::Packet& packet, sf::IpAddress address, sf::Uint16 port) : packet(packet), address(address), port(port), lastTry(-std::numeric_limits::max()), firstTry(Time::time()) {} -ReliableMsg::ReliableMsg(const ReliableMsg& msg) : packet(msg.packet), address(msg.address), port(msg.port), lastTry(msg.lastTry), firstTry(msg.firstTry) {} +ReliableMsg::ReliableMsg(sf::Packet &packet, sf::IpAddress address, sf::Uint16 port) : packet(packet), address(address), + port(port), + lastTry(-std::numeric_limits::max()), + firstTry(Time::time()) {} -bool ReliableMsg::trySend(sf::UdpSocket& socket) -{ +ReliableMsg::ReliableMsg(const ReliableMsg &msg) : packet(msg.packet), address(msg.address), port(msg.port), + lastTry(msg.lastTry), firstTry(msg.firstTry) {} + +bool ReliableMsg::trySend(sf::UdpSocket &socket) { if (Time::time() - firstTry > Consts::NETWORK_TIMEOUT) { return false; } diff --git a/engine/network/ReliableMsg.h b/engine/network/ReliableMsg.h index 04edba0..7d6c469 100644 --- a/engine/network/ReliableMsg.h +++ b/engine/network/ReliableMsg.h @@ -16,10 +16,11 @@ private: double lastTry; public: - ReliableMsg(sf::Packet& packet, sf::IpAddress address, sf::Uint16 port); - ReliableMsg(const ReliableMsg& msg); + ReliableMsg(sf::Packet &packet, sf::IpAddress address, sf::Uint16 port); - bool trySend(sf::UdpSocket& socket); + ReliableMsg(const ReliableMsg &msg); + + bool trySend(sf::UdpSocket &socket); }; diff --git a/engine/network/ServerUDP.cpp b/engine/network/ServerUDP.cpp index 5170434..ad6b2c0 100644 --- a/engine/network/ServerUDP.cpp +++ b/engine/network/ServerUDP.cpp @@ -17,7 +17,7 @@ bool ServerUDP::isWorking() const { bool ServerUDP::start(sf::Uint16 port) { _working = _socket.bind(port); - if(_working) { + if (_working) { Log::log("ServerUDP::start(): the server was successfully started."); } else { Log::log("ServerUDP::start(): failed to start the server."); diff --git a/engine/network/ServerUDP.h b/engine/network/ServerUDP.h index 6e86683..29878a4 100644 --- a/engine/network/ServerUDP.h +++ b/engine/network/ServerUDP.h @@ -19,29 +19,36 @@ protected: bool _working = false; bool process(); + bool timeout(sf::Uint16 id); std::set _clients{}; public: explicit ServerUDP(); + [[nodiscard]] bool isWorking() const; + bool start(sf::Uint16 port); + void stop(); + void update(); - virtual void updateInfo(){}; + virtual void updateInfo() {}; // virtual functions - virtual void broadcast(){}; + virtual void broadcast() {}; // here you have to send Init message _back to 'targetId' and send NewClient message to all '_clients' - virtual void processConnect(sf::Uint16 senderId){}; - virtual void processClientUpdate(sf::Uint16 senderId, sf::Packet& packet){}; - virtual void processDisconnect(sf::Uint16 senderId){}; + virtual void processConnect(sf::Uint16 senderId) {}; - virtual void processCustomPacket(sf::Packet& packet, sf::Uint16 senderId){}; + virtual void processClientUpdate(sf::Uint16 senderId, sf::Packet &packet) {}; - virtual void processStop(){}; + virtual void processDisconnect(sf::Uint16 senderId) {}; + + virtual void processCustomPacket(sf::Packet &packet, sf::Uint16 senderId) {}; + + virtual void processStop() {}; virtual ~ServerUDP(); }; diff --git a/engine/network/UDPConnection.cpp b/engine/network/UDPConnection.cpp index 7bb10be..993699f 100644 --- a/engine/network/UDPConnection.cpp +++ b/engine/network/UDPConnection.cpp @@ -6,13 +6,14 @@ #include "../utils/Time.h" #include "../Consts.h" -UDPConnection::UDPConnection(sf::Uint16 id, sf::IpAddress ip, sf::Uint16 port) : _id(id), _ip(ip), _port(port), lastMsg(Time::time()) {} +UDPConnection::UDPConnection(sf::Uint16 id, sf::IpAddress ip, sf::Uint16 port) : _id(id), _ip(ip), _port(port), + lastMsg(Time::time()) {} sf::Uint16 UDPConnection::id() const { return _id; } -const sf::IpAddress& UDPConnection::ip() const { +const sf::IpAddress &UDPConnection::ip() const { return _ip; } @@ -24,7 +25,7 @@ bool UDPConnection::timeout() const { return Time::time() - lastMsg > Consts::NETWORK_TIMEOUT; } -bool UDPConnection::same(sf::IpAddress& ip, sf::Uint16 port) const { +bool UDPConnection::same(sf::IpAddress &ip, sf::Uint16 port) const { return _ip == ip && _port == port; } @@ -32,6 +33,6 @@ void UDPConnection::update() { lastMsg = Time::time(); } -void UDPConnection::send(sf::UdpSocket& socket, sf::Packet& packet) { +void UDPConnection::send(sf::UdpSocket &socket, sf::Packet &packet) { socket.send(packet, _ip, _port); } diff --git a/engine/network/UDPConnection.h b/engine/network/UDPConnection.h index 36cb681..87cbcf8 100644 --- a/engine/network/UDPConnection.h +++ b/engine/network/UDPConnection.h @@ -16,13 +16,20 @@ private: public: explicit UDPConnection(sf::Uint16 id, sf::IpAddress ip, sf::Uint16 port); + [[nodiscard]] sf::Uint16 id() const; - [[nodiscard]] const sf::IpAddress& ip() const; + + [[nodiscard]] const sf::IpAddress &ip() const; + [[nodiscard]] sf::Uint16 port() const; + [[nodiscard]] bool timeout() const; - bool same(sf::IpAddress& ip, sf::Uint16 port) const; + + [[nodiscard]] bool same(sf::IpAddress &ip, sf::Uint16 port) const; + void update(); - void send(sf::UdpSocket& socket, sf::Packet& packet); + + void send(sf::UdpSocket &socket, sf::Packet &packet); }; diff --git a/engine/network/UDPSocket.cpp b/engine/network/UDPSocket.cpp index 55f50a2..f0d4de2 100644 --- a/engine/network/UDPSocket.cpp +++ b/engine/network/UDPSocket.cpp @@ -2,9 +2,10 @@ // Created by Neirokan on 30.04.2020 // +#include + #include "UDPSocket.h" #include "../utils/Time.h" -#include #include "../Consts.h" UDPSocket::UDPSocket() : _ownId(0), _nextRelyMsgId(0) { @@ -12,7 +13,7 @@ UDPSocket::UDPSocket() : _ownId(0), _nextRelyMsgId(0) { } void UDPSocket::addConnection(sf::Uint16 id, sf::IpAddress ip, sf::Uint16 port) { - _connections.insert({ id, UDPConnection(id, ip, port) }); + _connections.insert({id, UDPConnection(id, ip, port)}); } void UDPSocket::removeConnection(sf::Uint16 id) { @@ -54,28 +55,28 @@ sf::Uint16 UDPSocket::serverId() const { return _serverId; } -void UDPSocket::sendRely(const sf::Packet& packet, const sf::IpAddress& ip, sf::Uint16 port) { +void UDPSocket::sendRely(const sf::Packet &packet, const sf::IpAddress &ip, sf::Uint16 port) { sf::Packet finalPacket; finalPacket << _ownId << true << _nextRelyMsgId; finalPacket.append(packet.getData(), packet.getDataSize()); - _relyPackets.insert({ _nextRelyMsgId++, ReliableMsg(finalPacket, ip, port) }); + _relyPackets.insert({_nextRelyMsgId++, ReliableMsg(finalPacket, ip, port)}); } -void UDPSocket::sendRely(const sf::Packet& packet, sf::Uint16 id) { +void UDPSocket::sendRely(const sf::Packet &packet, sf::Uint16 id) { if (!_connections.count(id)) { return; } this->sendRely(packet, _connections.at(id).ip(), _connections.at(id).port()); } -void UDPSocket::send(const sf::Packet& packet, const sf::IpAddress& ip, sf::Uint16 port) { +void UDPSocket::send(const sf::Packet &packet, const sf::IpAddress &ip, sf::Uint16 port) { sf::Packet finalPacket; finalPacket << _ownId << false << _serverId; finalPacket.append(packet.getData(), packet.getDataSize()); _socket.send(finalPacket, ip, port); } -void UDPSocket::send(const sf::Packet& packet, sf::Uint16 id) { +void UDPSocket::send(const sf::Packet &packet, sf::Uint16 id) { if (!_connections.count(id)) { return; } @@ -111,7 +112,7 @@ void UDPSocket::update() { } } -MsgType UDPSocket::receive(sf::Packet& packet, sf::Uint16& senderId) { +MsgType UDPSocket::receive(sf::Packet &packet, sf::Uint16 &senderId) { // Receive message sf::IpAddress ip; sf::Uint16 port; @@ -155,10 +156,11 @@ MsgType UDPSocket::receive(sf::Packet& packet, sf::Uint16& senderId) { } } } - _connections.insert({ senderId, UDPConnection(senderId, ip, port) }); + _connections.insert({senderId, UDPConnection(senderId, ip, port)}); } - if (!_connections.count(senderId) || !_connections.at(senderId).same(ip, port) || reply && confirmed(msgId, senderId)) { + if (!_connections.count(senderId) || !_connections.at(senderId).same(ip, port) || + reply && confirmed(msgId, senderId)) { return MsgType::Error; } return type; diff --git a/engine/network/UDPSocket.h b/engine/network/UDPSocket.h index ac8e815..5235642 100644 --- a/engine/network/UDPSocket.h +++ b/engine/network/UDPSocket.h @@ -8,6 +8,7 @@ #include #include #include + #include "ReliableMsg.h" #include "UDPConnection.h" #include "MsgType.h" @@ -28,23 +29,34 @@ private: public: explicit UDPSocket(); + bool bind(sf::Uint16 port); + void unbind(); + void setTimeoutCallback(std::function callback); + void addConnection(sf::Uint16 id, sf::IpAddress ip, sf::Uint16 port); + void removeConnection(sf::Uint16 id); void setId(sf::Uint16 id); + [[nodiscard]] sf::Uint16 ownId() const; + [[nodiscard]] sf::Uint16 serverId() const; - void send(const sf::Packet& packet, const sf::IpAddress& ip, sf::Uint16 port); - void send(const sf::Packet& packet, sf::Uint16 id); - void sendRely(const sf::Packet& packet, const sf::IpAddress& ip, sf::Uint16 port); - void sendRely(const sf::Packet& packet, sf::Uint16 id); + void send(const sf::Packet &packet, const sf::IpAddress &ip, sf::Uint16 port); + + void send(const sf::Packet &packet, sf::Uint16 id); + + void sendRely(const sf::Packet &packet, const sf::IpAddress &ip, sf::Uint16 port); + + void sendRely(const sf::Packet &packet, sf::Uint16 id); void update(); - MsgType receive(sf::Packet& packet, sf::Uint16& senderId); + + MsgType receive(sf::Packet &packet, sf::Uint16 &senderId); ~UDPSocket(); }; diff --git a/engine/physics/RigidBody.cpp b/engine/physics/RigidBody.cpp index 68cf415..206b4dd 100644 --- a/engine/physics/RigidBody.cpp +++ b/engine/physics/RigidBody.cpp @@ -2,28 +2,29 @@ // Created by Иван Ильин on 05.02.2021. // +#include +#include + #include "RigidBody.h" #include "../utils/Log.h" #include "../utils/Time.h" -#include -#include -#include -#include #include "../Consts.h" -RigidBody::RigidBody(ObjectNameTag nameTag, const std::string &filename, const Vec3D &scale) : Mesh(std::move(nameTag), filename, scale) { +RigidBody::RigidBody(ObjectNameTag nameTag, const std::string &filename, const Vec3D &scale) : Mesh(std::move(nameTag), + filename, scale) { } -Vec3D RigidBody::_findFurthestPoint(const Vec3D& direction) { +Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) { Vec3D maxPoint{0, 0, 0}; double maxDistance = -std::numeric_limits::max(); - for(auto& tri : triangles()){ - for(int i = 0; i < 3; i++){ - Vec3D point(model()*tri[i]); + for (auto &tri : triangles()) { + for (int i = 0; i < 3; i++) { + // TODO: multiplying model() * tri[i] costs too much time to compute + Vec3D point(model() * tri[i]); double distance = point.dot(direction.normalized()); - if(distance > maxDistance) { + if (distance > maxDistance) { maxDistance = distance; maxPoint = point; } @@ -33,7 +34,7 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D& direction) { return maxPoint; } -Vec3D RigidBody::_support(std::shared_ptr obj, const Vec3D& direction) { +Vec3D RigidBody::_support(std::shared_ptr obj, const Vec3D &direction) { Vec3D p1 = _findFurthestPoint(direction); Vec3D p2 = obj->_findFurthestPoint(-direction); Vec3D res = p1 - p2; @@ -43,15 +44,19 @@ Vec3D RigidBody::_support(std::shared_ptr obj, const Vec3D& direction NextSimplex RigidBody::_nextSimplex(const Simplex &points) { switch (points.type()) { - case SimplexType::Line: return _lineCase(points); - case SimplexType::Triangle: return _triangleCase(points); - case SimplexType::Tetrahedron: return _tetrahedronCase(points); + case SimplexType::Line: + return _lineCase(points); + case SimplexType::Triangle: + return _triangleCase(points); + case SimplexType::Tetrahedron: + return _tetrahedronCase(points); - default: throw std::logic_error{"RigidBody::_nextSimplex: simplex is not Line, Triangle or Tetrahedron"}; + default: + throw std::logic_error{"RigidBody::_nextSimplex: simplex is not Line, Triangle or Tetrahedron"}; } } -NextSimplex RigidBody::_lineCase(const Simplex& points) { +NextSimplex RigidBody::_lineCase(const Simplex &points) { Simplex newPoints(points); Vec3D newDirection; @@ -59,7 +64,7 @@ NextSimplex RigidBody::_lineCase(const Simplex& points) { Vec3D b = points[1]; Vec3D ab = b - a; - Vec3D ao = - a; + Vec3D ao = -a; if (ab.dot(ao) > 0) { newDirection = ab.cross(ao).cross(ab); @@ -81,27 +86,25 @@ NextSimplex RigidBody::_triangleCase(const Simplex &points) { Vec3D ab = b - a; Vec3D ac = c - a; - Vec3D ao = - a; + Vec3D ao = -a; Vec3D abc = ab.cross(ac); if (abc.cross(ac).dot(ao) > 0) { if (ac.dot(ao) > 0) { - newPoints = Simplex{ a, c }; + newPoints = Simplex{a, c}; newDirection = ac.cross(ao).cross(ac); - } - else { - return _lineCase(Simplex { a, b }); + } else { + return _lineCase(Simplex{a, b}); } } else { if (ab.cross(abc).dot(ao) > 0) { - return _lineCase(Simplex { a, b }); - } - else { + return _lineCase(Simplex{a, b}); + } else { if (abc.dot(ao) > 0) { newDirection = abc; } else { - newPoints = Simplex{ a, c, b }; + newPoints = Simplex{a, c, b}; newDirection = -abc; } } @@ -119,22 +122,22 @@ NextSimplex RigidBody::_tetrahedronCase(const Simplex &points) { Vec3D ab = b - a; Vec3D ac = c - a; Vec3D ad = d - a; - Vec3D ao = - a; + Vec3D ao = -a; Vec3D abc = ab.cross(ac); Vec3D acd = ac.cross(ad); Vec3D adb = ad.cross(ab); if (abc.dot(ao) > 0) { - return _triangleCase(Simplex{ a, b, c }); + return _triangleCase(Simplex{a, b, c}); } if (acd.dot(ao) > 0) { - return _triangleCase(Simplex{ a, c, d }); + return _triangleCase(Simplex{a, c, d}); } if (adb.dot(ao) > 0) { - return _triangleCase(Simplex{ a, d, b }); + return _triangleCase(Simplex{a, d, b}); } return NextSimplex{points, Vec3D(), true}; @@ -175,7 +178,7 @@ std::pair RigidBody::checkGJKCollision(std::shared_ptr points = nextSimplex.newSimplex; if (nextSimplex.finishSearching) { - if(obj->isCollider()) { + if (obj->isCollider()) { _inCollision = true; } return std::make_pair(true, points); @@ -184,7 +187,7 @@ std::pair RigidBody::checkGJKCollision(std::shared_ptr return std::make_pair(false, points); } -CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr obj) { +CollisionPoint RigidBody::EPA(const Simplex &simplex, std::shared_ptr obj) { // This is implementation of EPA algorithm for solving collision. // It uses a simplex from GJK around and expand it to the border. // The goal is to calculate the nearest normal and the intersection depth. @@ -193,7 +196,7 @@ CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr // https://blog.winter.dev/2020/epa-algorithm/ std::vector polytope(simplex.begin(), simplex.end()); - std::vector faces = { + std::vector faces = { 0, 1, 2, 0, 3, 1, 0, 2, 3, @@ -220,7 +223,7 @@ CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr std::vector> uniqueEdges; size_t f = 0; - for (auto & normal : normals) { + for (auto &normal : normals) { if (normal.normal.dot(support) > 0) { uniqueEdges = _addIfUniqueEdge(uniqueEdges, faces, f + 0, f + 1); uniqueEdges = _addIfUniqueEdge(uniqueEdges, faces, f + 1, f + 2); @@ -235,7 +238,7 @@ CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr } std::vector newFaces; - for (auto [edgeIndex1, edgeIndex2] : uniqueEdges) { + for (auto[edgeIndex1, edgeIndex2] : uniqueEdges) { newFaces.push_back(edgeIndex1); newFaces.push_back(edgeIndex2); newFaces.push_back(polytope.size()); @@ -252,14 +255,15 @@ CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr } _collisionNormal = minNormal; - if(std::abs(minDistance - std::numeric_limits::max()) < Consts::EPS) { + if (std::abs(minDistance - std::numeric_limits::max()) < Consts::EPS) { return CollisionPoint{minNormal, 0}; } return CollisionPoint{minNormal, minDistance + Consts::EPA_EPS}; } -std::pair, size_t> RigidBody::_getFaceNormals(const std::vector& polytope, const std::vector& faces) { +std::pair, size_t> +RigidBody::_getFaceNormals(const std::vector &polytope, const std::vector &faces) { std::vector normals; size_t nearestFaceIndex = 0; double minDistance = std::numeric_limits::max(); @@ -289,7 +293,9 @@ std::pair, size_t> RigidBody::_getFaceNormals(const std: return {normals, nearestFaceIndex}; } -std::vector> RigidBody::_addIfUniqueEdge(const std::vector>& edges, const std::vector& faces, size_t a, size_t b) { +std::vector> +RigidBody::_addIfUniqueEdge(const std::vector> &edges, const std::vector &faces, + size_t a, size_t b) { std::vector> newEdges = edges; @@ -309,12 +315,12 @@ std::vector> RigidBody::_addIfUniqueEdge(const std::ve return newEdges; } -void RigidBody::solveCollision(const CollisionPoint& collision) { +void RigidBody::solveCollision(const CollisionPoint &collision) { Vec3D velocity_parallel = collision.normal * velocity().dot(collision.normal); Vec3D velocity_perpendicular = velocity() - velocity_parallel; - if(velocity().dot(collision.normal) > 0) { + if (velocity().dot(collision.normal) > 0) { setVelocity(velocity_perpendicular); } @@ -326,7 +332,7 @@ void RigidBody::updatePhysicsState() { _velocity = _velocity + _acceleration * Time::deltaTime(); } -void RigidBody::setVelocity(const Vec3D& velocity) { +void RigidBody::setVelocity(const Vec3D &velocity) { _velocity = velocity; } @@ -334,7 +340,7 @@ void RigidBody::addVelocity(const Vec3D &velocity) { _velocity = _velocity + velocity; } -void RigidBody::setAcceleration(const Vec3D& acceleration) { +void RigidBody::setAcceleration(const Vec3D &acceleration) { _acceleration = acceleration; } diff --git a/engine/physics/RigidBody.h b/engine/physics/RigidBody.h index 30fd731..5600dee 100644 --- a/engine/physics/RigidBody.h +++ b/engine/physics/RigidBody.h @@ -9,6 +9,7 @@ #include #include #include + #include "../Triangle.h" #include "Simplex.h" #include "../Mesh.h" @@ -31,18 +32,26 @@ struct NextSimplex final { class RigidBody : public Mesh { private: - Vec3D _findFurthestPoint(const Vec3D& direction); - Vec3D _support(std::shared_ptr obj, const Vec3D& direction); + Vec3D _findFurthestPoint(const Vec3D &direction); - std::function)> _collisionCallBack; + Vec3D _support(std::shared_ptr obj, const Vec3D &direction); - static NextSimplex _nextSimplex(const Simplex& points); - static NextSimplex _lineCase(const Simplex& points); - static NextSimplex _triangleCase(const Simplex& points); - static NextSimplex _tetrahedronCase(const Simplex& points); + std::function)> _collisionCallBack; - static std::pair, size_t> _getFaceNormals(const std::vector& polytope, const std::vector& faces); - static std::vector> _addIfUniqueEdge(const std::vector>& edges, const std::vector& faces, size_t a, size_t b); + static NextSimplex _nextSimplex(const Simplex &points); + + static NextSimplex _lineCase(const Simplex &points); + + static NextSimplex _triangleCase(const Simplex &points); + + static NextSimplex _tetrahedronCase(const Simplex &points); + + static std::pair, size_t> + _getFaceNormals(const std::vector &polytope, const std::vector &faces); + + static std::vector> + _addIfUniqueEdge(const std::vector> &edges, const std::vector &faces, size_t a, + size_t b); protected: Vec3D _velocity{0, 0, 0}; @@ -56,34 +65,50 @@ protected: public: explicit RigidBody(ObjectNameTag nameTag) : Mesh(std::move(nameTag)) {}; - explicit RigidBody(const RigidBody& rigidBody) = default; - explicit RigidBody(const Mesh& mesh); - RigidBody(ObjectNameTag nameTag, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1}); + + RigidBody(const RigidBody &rigidBody) = default; + + explicit RigidBody(const Mesh &mesh); + + RigidBody(ObjectNameTag nameTag, const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); [[nodiscard]] std::pair checkGJKCollision(std::shared_ptr obj); - [[nodiscard]] CollisionPoint EPA(const Simplex& simplex, std::shared_ptr obj); - void solveCollision(const CollisionPoint& collision); + + [[nodiscard]] CollisionPoint EPA(const Simplex &simplex, std::shared_ptr obj); + + void solveCollision(const CollisionPoint &collision); [[nodiscard]] Vec3D collisionNormal() const { return _collisionNormal; } [[nodiscard]] bool isCollision() const { return _collision; } - [[nodiscard]] bool inCollision() const {return _inCollision; } - [[nodiscard]] bool isCollider() const {return _isCollider; } + + [[nodiscard]] bool inCollision() const { return _inCollision; } + + [[nodiscard]] bool isCollider() const { return _isCollider; } + void setInCollision(bool c) { _inCollision = c; } - void setCollision(bool c) { _collision= c; } + + void setCollision(bool c) { _collision = c; } + void setCollider(bool c) { _isCollider = c; } void updatePhysicsState(); - void setVelocity(const Vec3D& velocity); - void addVelocity(const Vec3D& velocity); - void setAcceleration(const Vec3D& acceleration); + void setVelocity(const Vec3D &velocity); + + void addVelocity(const Vec3D &velocity); + + void setAcceleration(const Vec3D &acceleration); [[nodiscard]] Vec3D velocity() const { return _velocity; } + [[nodiscard]] Vec3D acceleration() const { return _acceleration; } - [[nodiscard]] const std::function)>& collisionCallBack() const { return _collisionCallBack; } - void setCollisionCallBack(const std::function)>& f) { _collisionCallBack = f; } + [[nodiscard]] const std::function)> & + collisionCallBack() const { return _collisionCallBack; } + + void setCollisionCallBack(const std::function)> &f) { _collisionCallBack = f; } ~RigidBody() override = default; }; diff --git a/engine/physics/Simplex.h b/engine/physics/Simplex.h index b111c65..8371693 100644 --- a/engine/physics/Simplex.h +++ b/engine/physics/Simplex.h @@ -5,9 +5,10 @@ #ifndef ENGINE_SIMPLEX_H #define ENGINE_SIMPLEX_H -#include "../Vec3D.h" #include +#include "../Vec3D.h" + enum class SimplexType { Zero, Point, @@ -24,30 +25,35 @@ public: Simplex() = default; Simplex(std::initializer_list list) { - for (const auto & v : list) { + for (const auto &v : list) { _points.push_back(v); - if(_points.size() > 4) + if (_points.size() > 4) { _points.pop_front(); + } } } - void push_front(const Vec3D& point) { + void push_front(const Vec3D &point) { _points.push_front(point); - if(_points.size() > 4) + if (_points.size() > 4) { _points.pop_back(); + } } Vec3D operator[](unsigned i) const { auto it = _points.begin(); - for(unsigned k=0; k(_points.size()); } }; diff --git a/engine/utils/Log.cpp b/engine/utils/Log.cpp index 2326b3f..ff0e47c 100644 --- a/engine/utils/Log.cpp +++ b/engine/utils/Log.cpp @@ -4,18 +4,18 @@ #define _CRT_SECURE_NO_WARNINGS -#include "Log.h" -#include "Time.h" #include #include #include #include + +#include "Log.h" +#include "Time.h" #include "../Consts.h" -namespace Log -{ - void log(const std::string& message) { - if(Consts::USE_LOG_FILE) { +namespace Log { + void log(const std::string &message) { + if (Consts::USE_LOG_FILE) { std::time_t const now_c = std::time(nullptr); auto dt = std::put_time(std::localtime(&now_c), "%F %T"); diff --git a/engine/utils/Log.h b/engine/utils/Log.h index a9a003d..dc11b76 100644 --- a/engine/utils/Log.h +++ b/engine/utils/Log.h @@ -7,9 +7,8 @@ #include -namespace Log -{ - void log(const std::string& message); +namespace Log { + void log(const std::string &message); }; diff --git a/engine/utils/Time.cpp b/engine/utils/Time.cpp index 623ebdc..cdc50a5 100644 --- a/engine/utils/Time.cpp +++ b/engine/utils/Time.cpp @@ -3,12 +3,12 @@ // #include "Time.h" -#include "../Consts.h" #include "Log.h" +#include "../Consts.h" using namespace std::chrono; -Time* Time::_instance = nullptr; +Time *Time::_instance = nullptr; bool Time::_validInstance = false; void Time::init() { @@ -19,7 +19,7 @@ void Time::init() { } double Time::time() { - if(!_validInstance) { + if (!_validInstance) { return 0; } @@ -27,7 +27,7 @@ double Time::time() { } double Time::deltaTime() { - if(!_validInstance) { + if (!_validInstance) { return 0; } @@ -35,7 +35,7 @@ double Time::deltaTime() { } void Time::update() { - if(!_validInstance) { + if (!_validInstance) { return; } @@ -44,13 +44,15 @@ void Time::update() { _instance->_deltaTime = duration(t - _instance->_last).count(); _instance->_time = duration(t - _instance->_start).count(); // in case when fps < 10 it is useful to decrease _deltaTime (to avoid collision problems) - if(_instance->_deltaTime > Consts::LARGEST_TIME_STEP) + if (_instance->_deltaTime > Consts::LARGEST_TIME_STEP) { _instance->_deltaTime = Consts::LARGEST_TIME_STEP; + } _instance->_last = t; - if(_instance->_deltaTime > 10000) + if (_instance->_deltaTime > 10000) { return; + } _instance->_fpsCounter++; if (t - _instance->_fpsStart > _instance->_fpsCountTime) { @@ -61,7 +63,7 @@ void Time::update() { } int Time::fps() { - if(!_validInstance) { + if (!_validInstance) { return 0; } // Cast is faster than floor and has the same behavior for positive numbers diff --git a/engine/utils/Time.h b/engine/utils/Time.h index f2eb5ef..e4fddff 100644 --- a/engine/utils/Time.h +++ b/engine/utils/Time.h @@ -23,21 +23,26 @@ private: double _time = 0; double _deltaTime = 0; - static Time* _instance; + static Time *_instance; static bool _validInstance; Time() = default; public: - Time(const Time&) = delete; - Time& operator=(Time&) = delete; + Time(const Time &) = delete; + + Time &operator=(Time &) = delete; static int fps(); + static double time(); + static double deltaTime(); + static void update(); static void init(); + static void free(); }; diff --git a/weapon/Ak47.cpp b/weapon/Ak47.cpp index 9b3db28..6c5f400 100644 --- a/weapon/Ak47.cpp +++ b/weapon/Ak47.cpp @@ -8,5 +8,8 @@ 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}) { +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/Gold_Ak47.h b/weapon/Gold_Ak47.h index fafee57..891f684 100644 --- a/weapon/Gold_Ak47.h +++ b/weapon/Gold_Ak47.h @@ -10,7 +10,11 @@ class Gold_Ak47 final : public Weapon { public: - explicit Gold_Ak47() : Weapon(200, 60, 1.5, 0.05, 600, 1.0, ShooterConsts::GOLD_AK47_FIRE_SOUND, ShooterConsts::GOLD_AK47_RELOAD_SOUND, ObjectNameTag("gold_ak47"), ShooterConsts::GOLD_AK47_OBJ, Vec3D{3, 3, 3}, Vec3D{-2.2, 1.0, 1.3}, Vec3D{0, Consts::PI, 0}) { + explicit Gold_Ak47() : Weapon(200, 60, 1.5, 0.05, 600, 1.0, + ShooterConsts::GOLD_AK47_FIRE_SOUND, + ShooterConsts::GOLD_AK47_RELOAD_SOUND, ObjectNameTag("gold_ak47"), + ShooterConsts::GOLD_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 index 5d58d10..33b5968 100644 --- a/weapon/Gun.cpp +++ b/weapon/Gun.cpp @@ -8,5 +8,8 @@ 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}) { +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/Rifle.cpp b/weapon/Rifle.cpp index bb4382a..b6b89c1 100644 --- a/weapon/Rifle.cpp +++ b/weapon/Rifle.cpp @@ -6,5 +6,8 @@ #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}) { +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/Shotgun.cpp b/weapon/Shotgun.cpp index 0d8eabb..b43f76d 100644 --- a/weapon/Shotgun.cpp +++ b/weapon/Shotgun.cpp @@ -4,21 +4,23 @@ #include "../engine/ResourceManager.h" #include "Shotgun.h" -#include "../engine/animation/AFunction.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}) { +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) { +Shotgun::processFire(std::function rayCastFunction, + const Vec3D &position, const Vec3D &direction) { std::map damagedPlayers; - for(int i = 0; i < 15; i++) { + for (int i = 0; i < 15; i++) { std::map damaged = addTrace(rayCastFunction, position, direction); - for(auto& player : damaged) { + for (auto &player : damaged) { damagedPlayers[player.first] += player.second; } } diff --git a/weapon/Shotgun.h b/weapon/Shotgun.h index 5ff9032..56f12b0 100644 --- a/weapon/Shotgun.h +++ b/weapon/Shotgun.h @@ -10,7 +10,10 @@ class Shotgun final : public Weapon { public: explicit Shotgun(); - std::map processFire(std::function rayCastFunction, const Vec3D& position, const Vec3D& direction) override; + + std::map + processFire(std::function rayCastFunction, + const Vec3D &position, const Vec3D &direction) override; }; diff --git a/weapon/Weapon.cpp b/weapon/Weapon.cpp index 4d4d3e4..dedd3ac 100644 --- a/weapon/Weapon.cpp +++ b/weapon/Weapon.cpp @@ -11,7 +11,14 @@ using namespace std; -Weapon::Weapon(int initialPack, int clipCapacity, double reloadTime, double fireDelay, double damage, double spreading, std::string fireSound, std::string reloadSound, ObjectNameTag weaponName, const std::string& objFileName, const Vec3D& s, const Vec3D& t, const Vec3D& r) : RigidBody(std::move(weaponName), objFileName), _initialPack(initialPack), _clipCapacity(clipCapacity), _reloadTime(reloadTime), _fireDelay(fireDelay), _damage(damage), _spreading(spreading), _fireSound(std::move(fireSound)), _reloadSound(std::move(reloadSound)) { +Weapon::Weapon(int initialPack, int clipCapacity, double reloadTime, double fireDelay, double damage, double spreading, + std::string fireSound, std::string reloadSound, ObjectNameTag weaponName, const std::string &objFileName, + const Vec3D &s, const Vec3D &t, const Vec3D &r) : RigidBody(std::move(weaponName), objFileName), + _initialPack(initialPack), _clipCapacity(clipCapacity), + _reloadTime(reloadTime), _fireDelay(fireDelay), + _damage(damage), _spreading(spreading), + _fireSound(std::move(fireSound)), + _reloadSound(std::move(reloadSound)) { _stockAmmo = _initialPack - _clipCapacity; _clipAmmo = _clipCapacity; @@ -21,22 +28,24 @@ Weapon::Weapon(int initialPack, int clipCapacity, double reloadTime, double fire translate(t); } -FireInformation Weapon::fire(std::function rayCastFunction, const Vec3D& position, const Vec3D& direction) { - if(_clipAmmo == 0) { +FireInformation Weapon::fire(std::function rayCastFunction, + const Vec3D &position, const Vec3D &direction) { + if (_clipAmmo == 0) { reload(); - if(_clipAmmo == 0) { + if (_clipAmmo == 0) { SoundController::playSound(SoundTag("noAmmo"), ShooterConsts::NO_AMMO_SOUND); } } - if(_clipAmmo <= 0 || std::abs(Time::time() - _lastFireTime) < _fireDelay || std::abs(Time::time() - _lastReloadTime) < _reloadTime) { + if (_clipAmmo <= 0 || std::abs(Time::time() - _lastFireTime) < _fireDelay || + std::abs(Time::time() - _lastReloadTime) < _reloadTime) { return FireInformation{std::map(), false}; } _lastFireTime = Time::time(); _clipAmmo--; - if(_clipAmmo == 0) { + if (_clipAmmo == 0) { reload(); } @@ -50,7 +59,7 @@ void Weapon::reload() { if (_stockAmmo == 0 || std::abs(Time::time() - _lastReloadTime) < _reloadTime) { return; } - if(_clipCapacity - _clipAmmo <= _stockAmmo) { + if (_clipCapacity - _clipAmmo <= _stockAmmo) { _stockAmmo -= _clipCapacity - _clipAmmo; _clipAmmo = _clipCapacity; } else { @@ -63,27 +72,34 @@ void Weapon::reload() { _lastReloadTime = Time::time(); } -std::map Weapon::processFire(std::function rayCastFunction, const Vec3D& position, const Vec3D& direction) { +std::map +Weapon::processFire(std::function rayCastFunction, + const Vec3D &position, const Vec3D &direction) { return addTrace(std::move(rayCastFunction), position, direction); } -std::map Weapon::addTrace(std::function rayCastFunction, const Vec3D& from, const Vec3D& directionTo) { +std::map +Weapon::addTrace(std::function rayCastFunction, + const Vec3D &from, const Vec3D &directionTo) { std::map damagedPlayers; - double spreading = _spreading*ShooterConsts::FIRE_DISTANCE/100; + double spreading = _spreading * ShooterConsts::FIRE_DISTANCE / 100; //generate random vector - Vec3D randV(spreading*(1.0 - 2.0*(double)rand()/RAND_MAX), spreading*(1.0 - 2.0*(double)rand()/RAND_MAX), spreading*(1.0 - 2.0*(double)rand()/RAND_MAX)); + Vec3D randV(spreading * (1.0 - 2.0 * (double) rand() / RAND_MAX), + spreading * (1.0 - 2.0 * (double) rand() / RAND_MAX), + spreading * (1.0 - 2.0 * (double) rand() / RAND_MAX)); // damage player auto rayCast = rayCastFunction(from, from + directionTo * ShooterConsts::FIRE_DISTANCE + randV); - if(rayCast.objectName.str().find("Enemy") != std::string::npos) { + if (rayCast.objectName.str().find("Enemy") != std::string::npos) { damagedPlayers[rayCast.objectName] += _damage / (1.0 + rayCast.distanceToObject); } // add trace line - Vec3D lineFrom = position() + model()*Vec3D(triangles().back()[0]); - Vec3D lineTo = rayCast.intersected ? rayCast.pointOfIntersection : position() + -lookAt() * ShooterConsts::FIRE_DISTANCE + randV; + Vec3D lineFrom = position() + model() * Vec3D(triangles().back()[0]); + Vec3D lineTo = rayCast.intersected ? rayCast.pointOfIntersection : position() + + -lookAt() * ShooterConsts::FIRE_DISTANCE + randV; _addTraceCallBack(lineFrom, lineTo); return damagedPlayers; diff --git a/weapon/Weapon.h b/weapon/Weapon.h index 026995a..07ffafe 100644 --- a/weapon/Weapon.h +++ b/weapon/Weapon.h @@ -37,19 +37,29 @@ private: double _lastFireTime = std::numeric_limits::min(); double _lastReloadTime = std::numeric_limits::min(); - std::function _addTraceCallBack; + std::function _addTraceCallBack; protected: - std::map addTrace(std::function rayCastFunction, const Vec3D& position, const Vec3D& direction); - virtual std::map processFire(std::function rayCastFunction, const Vec3D& position, const Vec3D& direction); + std::map + addTrace(std::function rayCastFunction, + const Vec3D &position, const Vec3D &direction); + + virtual std::map + processFire(std::function rayCastFunction, + const Vec3D &position, const Vec3D &direction); public: - Weapon(int initialPack, int clipCapacity, double reloadTime, double fireDelay, double damage, double spreading, std::string fireSound, std::string reloadSound, ObjectNameTag weaponName, const std::string& objFileName, const Vec3D& s, const Vec3D& t, const Vec3D& r); + Weapon(int initialPack, int clipCapacity, double reloadTime, double fireDelay, double damage, double spreading, + std::string fireSound, std::string reloadSound, ObjectNameTag weaponName, const std::string &objFileName, + const Vec3D &s, const Vec3D &t, const Vec3D &r); + + FireInformation + fire(std::function rayCastFunction, const Vec3D &position, + const Vec3D &direction); - FireInformation fire(std::function rayCastFunction, const Vec3D& position, const Vec3D& direction); void reload(); - [[nodiscard]] std::pair balance() const{ return std::make_pair(_clipAmmo, _stockAmmo); } + [[nodiscard]] std::pair balance() const { return std::make_pair(_clipAmmo, _stockAmmo); } void setAddTraceCallBack(std::function add) { _addTraceCallBack = std::move(add); }