shooter/Player.cpp

150 lines
4.7 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/utils/Log.h"
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +03:00
Player::Player(ObjectNameTag name) : RigidBody(name) {
2021-10-24 05:49:48 +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);
2021-10-29 19:43:39 +03:00
//setColor({240, 168, 168});
Vec3D randColor = Vec3D::Random();
setColor({static_cast<sf::Uint8>(randColor.x()*255), static_cast<sf::Uint8>(randColor.y()*255), static_cast<sf::Uint8>(randColor.z()*255)});
2021-10-16 20:22:55 +03:00
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-10-28 16:58:02 +03:00
for(auto& weapon : _weapons) {
2021-09-13 15:53:43 +03:00
weapon->rotateRelativePoint(point4D, v, val);
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
}
2021-10-17 10:21:10 +03:00
void Player::collisionWithObject(const ObjectNameTag& tag, std::shared_ptr<RigidBody> obj) {
2021-10-28 16:58:02 +03:00
if(tag.str().find("Bonus_gun") != std::string::npos) {
2021-09-13 15:53:43 +03:00
addWeapon(std::make_shared<Gun>());
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +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-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +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-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +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-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +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-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +03:00
if(tag.str().find("Bonus_hill") != std::string::npos) {
2021-09-13 15:53:43 +03:00
setFullHealth();
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +03:00
if(tag.str().find("Bonus_ability") != std::string::npos) {
2021-09-13 15:53:43 +03:00
setFullAbility();
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
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-28 16:58:02 +03:00
attach(weapon);
2021-09-13 15:53:43 +03:00
_weapons.back()->translate(position());
2021-10-28 16:58:02 +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
_weapons.back()->setAddTraceCallBack(_addTraceCallBack);
2021-09-13 15:53:43 +03:00
}
void Player::initWeapons() {
if(!_weapons.empty()) {
2021-10-28 16:58:02 +03:00
for(auto weapon : _weapons) {
2021-10-17 10:21:10 +03:00
unattach(ObjectNameTag(weapon->name()));
2021-10-28 16:58:02 +03:00
}
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]);
2021-10-28 16:58:02 +03:00
if (_selectedWeapon > 0) {
_selectedWeapon = (_selectedWeapon - 1) % _weapons.size();
2021-10-28 16:58:02 +03:00
} else {
_selectedWeapon = _weapons.size() - 1;
2021-10-28 16:58:02 +03:00
}
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);
}
}
2021-10-22 19:42:32 +03:00
bool Player::fire() {
auto camera = attached(ObjectNameTag("Camera"));
if(camera != nullptr) {
2021-10-22 19:42:32 +03:00
auto fireInfo = _weapons[_selectedWeapon]->fire(_rayCastFunction, camera->position(), camera->lookAt());
for(auto& [damagedPlayerName, damage] : fireInfo.damagedPlayers) {
2021-10-17 10:21:10 +03:00
sf::Uint16 targetId = std::stoi(damagedPlayerName.str().substr(6));
_damagePlayerCallBack(targetId, damage);
}
2021-10-22 19:42:32 +03:00
return fireInfo.shot;
}
2021-10-22 19:42:32 +03:00
return false;
}
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
}