shooter/Player.cpp

135 lines
4.3 KiB
C++
Raw Normal View History

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() {
2021-10-17 20:52:21 +03:00
loadObj(ShooterConsts::CUBE_OBJ, Vec3D{0.5, 1.9, 0.5});
2021-10-16 20:22:55 +03:00
setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0});
setCollision(true);
setVisible(false);
setColor({240, 168, 168});
2021-10-17 10:21:10 +03:00
setCollisionCallBack([this](const ObjectNameTag& tag, std::shared_ptr<RigidBody> obj) {collisionWithObject(tag, obj);});
2021-10-16 20:22:55 +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);
}
2021-10-17 10:21:10 +03:00
void Player::collisionWithObject(const ObjectNameTag& tag, std::shared_ptr<RigidBody> obj) {
if(tag.str().find("Bonus_gun") != std::string::npos)
2021-09-13 15:53:43 +03:00
addWeapon(std::make_shared<Gun>());
2021-10-17 10:21:10 +03:00
if(tag.str().find("Bonus_shotgun") != std::string::npos)
2021-09-13 15:53:43 +03:00
addWeapon(std::make_shared<Shotgun>());
2021-10-17 10:21:10 +03:00
if(tag.str().find("Bonus_ak47") != std::string::npos)
2021-09-13 15:53:43 +03:00
addWeapon(std::make_shared<Ak47>());
2021-10-17 10:21:10 +03:00
if(tag.str().find("Bonus_gold_ak47") != std::string::npos)
2021-09-13 15:53:43 +03:00
addWeapon(std::make_shared<Gold_Ak47>());
2021-10-17 10:21:10 +03:00
if(tag.str().find("Bonus_rifle") != std::string::npos)
2021-09-13 15:53:43 +03:00
addWeapon(std::make_shared<Rifle>());
2021-10-17 10:21:10 +03:00
if(tag.str().find("Bonus_hill") != std::string::npos)
2021-09-13 15:53:43 +03:00
setFullHealth();
2021-10-17 10:21:10 +03:00
if(tag.str().find("Bonus_ability") != std::string::npos)
2021-09-13 15:53:43 +03:00
setFullAbility();
2021-10-17 10:21:10 +03:00
if(tag.str().find("Bonus") != std::string::npos) {
_takeBonusCallBack(tag.str());
2021-09-13 15:53:43 +03:00
}
}
void Player::addWeapon(std::shared_ptr<Weapon> weapon) {
2021-10-17 19:38:16 +03:00
SoundController::playSound(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND);
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-17 10:21:10 +03:00
attach(weapon, ObjectNameTag(weapon->name()));
2021-09-13 15:53:43 +03:00
_weapons.back()->translate(position());
_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
_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)
2021-10-17 10:21:10 +03:00
unattach(ObjectNameTag(weapon->name()));
2021-10-02 20:36:07 +03:00
_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]);
2021-10-09 13:41:12 +03:00
Log::log("selectedWeapon " + std::to_string(_selectedWeapon));
2021-10-17 19:38:16 +03:00
SoundController::playSound(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND);
}
}
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]);
2021-10-09 13:41:12 +03:00
Log::log("selectedWeapon " + std::to_string(_selectedWeapon));
2021-10-17 19:38:16 +03:00
SoundController::playSound(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND);
}
}
void Player::fire() {
2021-10-17 10:21:10 +03:00
if(attached(ObjectNameTag("camera")) != nullptr) {
2021-10-16 20:22:55 +03:00
auto damagedPlayers = _weapons[_selectedWeapon]->fire(_rayCastFunction);
2021-10-17 10:21:10 +03:00
for(auto& [damagedPlayerName, damage] : damagedPlayers) {
sf::Uint16 targetId = std::stoi(damagedPlayerName.str().substr(6));
_damagePlayerCallBack(targetId, damage);
}
}
}
void Player::reload() {
_weapons[_selectedWeapon]->reload();
}
2021-10-16 20:22:55 +03:00
void Player::setFullHealth() {
_health = ShooterConsts::HEALTH_MAX;
2021-10-17 19:38:16 +03:00
SoundController::playSound(SoundTag("addHealth"), ShooterConsts::RESTORE_HEALTH_SOUND);
2021-10-16 20:22:55 +03:00
}
void Player::setFullAbility() {
_ability = ShooterConsts::ABILITY_MAX;
2021-10-17 19:38:16 +03:00
SoundController::playSound(SoundTag("addAbility"), ShooterConsts::RESTORE_ABILITY_SOUND);
2021-10-16 20:22:55 +03:00
}