shooter/Player.cpp

123 lines
3.7 KiB
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 14.03.2021.
//
#include "Player.h"
#include "Screen.h"
#include "ResourceManager.h"
#include "utils/Log.h"
void Player::rotateWeaponsRelativePoint(const Point4D& point4D, const Point4D& v, double val) {
for(auto& weapon : _weapons)
weapon->rotateRelativePoint(point4D, v, val);
}
void Player::playDeath() {
_deathSound.setBuffer(*ResourceManager::loadSoundBuffer("../sound/classic_hurt.ogg"));
_deathSound.play();
2021-09-13 15:53:43 +03:00
}
void Player::playKill() {
_killSound.setBuffer(*ResourceManager::loadSoundBuffer("../sound/kill.ogg"));
_killSound.play();
2021-09-13 15:53:43 +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) {
_takeBonusCallBack(objName);
2021-09-13 15:53:43 +03:00
}
}
void Player::addWeapon(std::shared_ptr<Weapon> weapon) {
_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());
_weapons.back()->rotateRelativePoint(position() + Point4D{0, 1.8, 0}, Point4D{0, 1, 0}, _angle.y());
2021-10-02 20:36:07 +03:00
_weapons.back()->rotateRelativePoint(position() + Point4D{0, 1.8, 0}, left(), headAngle());
2021-09-13 15:53:43 +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
}
void Player::nextWeapon() {
if(_weapons.size() > 1) {
// change '_selectedWeapon'
2021-10-02 20:36:07 +03:00
_removeWeaponCallBack(_weapons[_selectedWeapon]);
_selectedWeapon = (_selectedWeapon + 1) % _weapons.size();
2021-10-02 20:36:07 +03:00
_addWeaponCallBack(_weapons[_selectedWeapon]);
Log::log("selected _selectedWeapon " + std::to_string(_selectedWeapon));
_changeWeaponSound.play();
}
}
void Player::previousWeapon() {
if(_weapons.size() > 1) {
// change '_selectedWeapon'
2021-10-02 20:36:07 +03:00
_removeWeaponCallBack(_weapons[_selectedWeapon]);
if (_selectedWeapon > 0)
_selectedWeapon = (_selectedWeapon - 1) % _weapons.size();
else
_selectedWeapon = _weapons.size() - 1;
2021-10-02 20:36:07 +03:00
_addWeaponCallBack(_weapons[_selectedWeapon]);
Log::log("selected _selectedWeapon " + std::to_string(_selectedWeapon));
_changeWeaponSound.play();
}
}
void Player::fire() {
2021-10-02 20:36:07 +03:00
auto damagedPlayers = _weapons[_selectedWeapon]->fire(_rayCastFunction, attached("camera")->position(), attached("camera")->lookAt());
for(auto& damagedPlayer : damagedPlayers) {
sf::Uint16 targetId = std::stoi(damagedPlayer.first.substr(7));
_damagePlayerCallBack(targetId, damagedPlayer.second);
}
}
void Player::reload() {
_weapons[_selectedWeapon]->reload();
}