2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 14.03.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Player.h"
|
2021-10-09 13:41:12 +03:00
|
|
|
#include "engine/Screen.h"
|
|
|
|
#include "engine/ResourceManager.h"
|
|
|
|
#include "engine/utils/Log.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-16 20:22:55 +03:00
|
|
|
Player::Player() {
|
|
|
|
loadObj(ShooterConsts::CUBE_OBJ, "", Vec3D{0.5, 1.9, 0.5});
|
|
|
|
setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0});
|
|
|
|
setCollision(true);
|
|
|
|
setVisible(false);
|
|
|
|
setColor({240, 168, 168});
|
|
|
|
|
|
|
|
_changeWeaponSound.setBuffer(*ResourceManager::loadSoundBuffer(ShooterConsts::CHANGE_WEAPON_SOUND));
|
|
|
|
|
|
|
|
_fullHealthSound.setBuffer(*ResourceManager::loadSoundBuffer(ShooterConsts::RESTORE_HEALTH_SOUND));
|
|
|
|
_fullAbilitySound.setBuffer(*ResourceManager::loadSoundBuffer(ShooterConsts::RESTORE_ABILITY_SOUND));
|
|
|
|
|
|
|
|
setCollisionCallBack([this](const std::string& objName, std::shared_ptr<RigidBody> obj) {collisionWithObject(objName, obj);});
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Player::rotateWeaponsRelativePoint(const Vec3D& point4D, const Vec3D& v, double val) {
|
2021-09-13 15:53:43 +03:00
|
|
|
for(auto& weapon : _weapons)
|
|
|
|
weapon->rotateRelativePoint(point4D, v, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::playDeath() {
|
2021-10-16 20:22:55 +03:00
|
|
|
_deathSound.setBuffer(*ResourceManager::loadSoundBuffer(ShooterConsts::DEATH_SOUND));
|
2021-09-19 11:25:10 +03:00
|
|
|
_deathSound.play();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::playKill() {
|
2021-10-16 20:22:55 +03:00
|
|
|
_killSound.setBuffer(*ResourceManager::loadSoundBuffer(ShooterConsts::KILL_SOUND));
|
2021-09-19 11:25:10 +03:00
|
|
|
_killSound.play();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
void Player::collisionWithObject(const std::string &objName, std::shared_ptr<RigidBody> obj) {
|
2021-09-13 15:53:43 +03:00
|
|
|
if(objName.find("Bonus_gun") != std::string::npos)
|
|
|
|
addWeapon(std::make_shared<Gun>());
|
|
|
|
|
|
|
|
if(objName.find("Bonus_shotgun") != std::string::npos)
|
|
|
|
addWeapon(std::make_shared<Shotgun>());
|
|
|
|
|
|
|
|
if(objName.find("Bonus_ak47") != std::string::npos)
|
|
|
|
addWeapon(std::make_shared<Ak47>());
|
|
|
|
|
|
|
|
if(objName.find("Bonus_gold_ak47") != std::string::npos)
|
|
|
|
addWeapon(std::make_shared<Gold_Ak47>());
|
|
|
|
|
|
|
|
if(objName.find("Bonus_rifle") != std::string::npos)
|
|
|
|
addWeapon(std::make_shared<Rifle>());
|
|
|
|
|
|
|
|
if(objName.find("Bonus_hill") != std::string::npos)
|
|
|
|
setFullHealth();
|
|
|
|
|
|
|
|
if(objName.find("Bonus_ability") != std::string::npos)
|
|
|
|
setFullAbility();
|
|
|
|
|
|
|
|
if(objName.find("Bonus") != std::string::npos) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_takeBonusCallBack(objName);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
void Player::addWeapon(std::shared_ptr<Weapon> weapon) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_changeWeaponSound.play();
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
for(auto& w : _weapons) {
|
|
|
|
if (w->name() == weapon->name()) {
|
|
|
|
w->addAmmo(w->initialPack());
|
|
|
|
return;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_weapons.push_back(weapon);
|
2021-10-02 20:36:07 +03:00
|
|
|
attach(weapon, weapon->name());
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
_weapons.back()->translate(position());
|
2021-10-12 17:12:47 +03:00
|
|
|
_weapons.back()->rotateRelativePoint(position() + Vec3D{0, 1.8, 0}, Vec3D{0, 1, 0}, _angle->y());
|
|
|
|
_weapons.back()->rotateRelativePoint(position() + Vec3D{0, 1.8, 0}, left(), headAngle());
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
_weapons.back()->setAddTraceCallBack(_addTraceCallBack);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::initWeapons() {
|
|
|
|
|
|
|
|
if(!_weapons.empty()) {
|
2021-10-02 20:36:07 +03:00
|
|
|
for(auto weapon : _weapons)
|
|
|
|
unattach(weapon->name());
|
|
|
|
|
|
|
|
_removeWeaponCallBack(_weapons[_selectedWeapon]);
|
2021-09-13 15:53:43 +03:00
|
|
|
_weapons.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
_selectedWeapon = 0;
|
|
|
|
addWeapon(std::make_shared<Gun>());
|
2021-10-02 20:36:07 +03:00
|
|
|
_addWeaponCallBack(_weapons[_selectedWeapon]);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
2021-09-19 15:44:31 +03:00
|
|
|
|
|
|
|
void Player::nextWeapon() {
|
|
|
|
if(_weapons.size() > 1) {
|
|
|
|
// change '_selectedWeapon'
|
2021-10-02 20:36:07 +03:00
|
|
|
_removeWeaponCallBack(_weapons[_selectedWeapon]);
|
2021-09-19 15:44:31 +03:00
|
|
|
_selectedWeapon = (_selectedWeapon + 1) % _weapons.size();
|
2021-10-02 20:36:07 +03:00
|
|
|
_addWeaponCallBack(_weapons[_selectedWeapon]);
|
2021-10-09 13:41:12 +03:00
|
|
|
Log::log("selectedWeapon " + std::to_string(_selectedWeapon));
|
2021-09-19 15:44:31 +03:00
|
|
|
_changeWeaponSound.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::previousWeapon() {
|
|
|
|
if(_weapons.size() > 1) {
|
|
|
|
// change '_selectedWeapon'
|
2021-10-02 20:36:07 +03:00
|
|
|
_removeWeaponCallBack(_weapons[_selectedWeapon]);
|
2021-09-19 15:44:31 +03:00
|
|
|
if (_selectedWeapon > 0)
|
|
|
|
_selectedWeapon = (_selectedWeapon - 1) % _weapons.size();
|
|
|
|
else
|
|
|
|
_selectedWeapon = _weapons.size() - 1;
|
2021-10-02 20:36:07 +03:00
|
|
|
_addWeaponCallBack(_weapons[_selectedWeapon]);
|
2021-10-09 13:41:12 +03:00
|
|
|
Log::log("selectedWeapon " + std::to_string(_selectedWeapon));
|
2021-09-19 15:44:31 +03:00
|
|
|
_changeWeaponSound.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::fire() {
|
2021-10-16 16:14:51 +03:00
|
|
|
if(attached("camera") != nullptr) {
|
2021-10-16 20:22:55 +03:00
|
|
|
auto damagedPlayers = _weapons[_selectedWeapon]->fire(_rayCastFunction);
|
2021-10-16 16:14:51 +03:00
|
|
|
for(auto& damagedPlayer : damagedPlayers) {
|
|
|
|
sf::Uint16 targetId = std::stoi(damagedPlayer.first.substr(6));
|
|
|
|
_damagePlayerCallBack(targetId, damagedPlayer.second);
|
|
|
|
}
|
2021-09-19 15:44:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::reload() {
|
|
|
|
_weapons[_selectedWeapon]->reload();
|
|
|
|
}
|
2021-10-16 20:22:55 +03:00
|
|
|
|
|
|
|
void Player::setFullHealth() {
|
|
|
|
_health = ShooterConsts::HEALTH_MAX;
|
|
|
|
_fullHealthSound.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::setFullAbility() {
|
|
|
|
_ability = ShooterConsts::ABILITY_MAX;
|
|
|
|
_fullAbilitySound.play();
|
|
|
|
}
|