vectozavr-shooter/Player.cpp

185 lines
5.9 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-11-01 17:08:36 +03:00
#include "engine/animation/Timeline.h"
#include "engine/animation/ARotateLeft.h"
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);
2021-10-30 11:18:31 +03:00
setVisible(false);
2021-10-29 19:43:39 +03:00
Vec3D randColor = Vec3D::Random();
2021-10-31 11:39:08 +03:00
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-31 11:39:08 +03:00
setCollisionCallBack(
[this](const ObjectNameTag &tag, std::shared_ptr<RigidBody> obj) { collisionWithObject(tag, obj); });
2021-10-16 20:22:55 +03:00
}
2021-10-31 11:39:08 +03:00
void Player::rotateWeaponsRelativePoint(const Vec3D &point4D, const Vec3D &v, double val) {
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-31 11:39:08 +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-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-31 11:39:08 +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-31 11:39:08 +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-31 11:39:08 +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-31 11:39:08 +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-31 11:39:08 +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-31 11:39:08 +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-31 11:39:08 +03:00
if (tag.str().find("Bonus") != std::string::npos) {
2021-10-31 19:01:06 +03:00
if (_takeBonusCallBack != nullptr) {
_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-31 11:39:08 +03:00
for (auto &w : _weapons) {
2021-10-02 20:36:07 +03:00
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-29 23:44:37 +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-11-01 17:08:36 +03:00
// add animation of reloading
_weapons.back()->setReloadCallBack([this]() {
Timeline::animate(AnimationListTag("reload_weapon"),
std::make_shared<ARotateLeft>(_weapons[_selectedWeapon],
2021-11-01 19:04:10 +03:00
4 * Consts::PI,
_weapons[_selectedWeapon]->reloadTime()/2,
2021-11-01 17:48:26 +03:00
Animation::LoopOut::None,
2021-11-01 19:04:10 +03:00
Animation::InterpolationType::Cos));
2021-11-01 17:08:36 +03:00
});
// add call back function to create fire traces
2021-10-31 19:01:06 +03:00
if (_addTraceCallBack != nullptr) {
_weapons.back()->setAddTraceCallBack(_addTraceCallBack);
}
2021-09-13 15:53:43 +03:00
}
2021-10-31 19:01:06 +03:00
void Player::reInitWeapons() {
2021-09-13 15:53:43 +03:00
2021-10-31 11:39:08 +03:00
if (!_weapons.empty()) {
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
2021-10-31 19:01:06 +03:00
if (_removeWeaponCallBack != nullptr) {
_removeWeaponCallBack(_weapons[_selectedWeapon]);
}
2021-09-13 15:53:43 +03:00
_weapons.clear();
}
_selectedWeapon = 0;
addWeapon(std::make_shared<Gun>());
2021-10-31 19:01:06 +03:00
if (_addWeaponCallBack != nullptr) {
_addWeaponCallBack(_weapons[_selectedWeapon]);
}
2021-09-13 15:53:43 +03:00
}
2021-10-31 19:01:06 +03:00
void Player::selectNextWeapon() {
2021-10-31 11:39:08 +03:00
if (_weapons.size() > 1) {
// change '_selectedWeapon'
2021-10-31 19:01:06 +03:00
if (_removeWeaponCallBack != nullptr) {
_removeWeaponCallBack(_weapons[_selectedWeapon]);
}
2021-11-01 17:08:36 +03:00
_selectedWeapon = (_selectedWeapon + 1) % _weapons.size();
2021-10-31 19:01:06 +03:00
if (_addWeaponCallBack != nullptr) {
_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-31 19:01:06 +03:00
void Player::selectPreviousWeapon() {
2021-10-31 11:39:08 +03:00
if (_weapons.size() > 1) {
// change '_selectedWeapon'
2021-10-31 19:01:06 +03:00
if (_removeWeaponCallBack != nullptr) {
_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-31 19:01:06 +03:00
if (_addWeaponCallBack != nullptr) {
_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"));
2021-10-31 11:39:08 +03:00
if (camera != nullptr) {
2021-10-22 19:42:32 +03:00
auto fireInfo = _weapons[_selectedWeapon]->fire(_rayCastFunction, camera->position(), camera->lookAt());
2021-10-31 11:39:08 +03:00
for (auto&[damagedPlayerName, damage] : fireInfo.damagedPlayers) {
2021-10-17 10:21:10 +03:00
sf::Uint16 targetId = std::stoi(damagedPlayerName.str().substr(6));
2021-10-31 19:01:06 +03:00
if (_damagePlayerCallBack != nullptr) {
_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
}