shooter/player/Player.cpp

200 lines
7.2 KiB
C++
Raw Permalink Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 14.03.2021.
//
#include <utility>
2023-08-03 01:48:04 +03:00
#include <animation/Animations.h>
#include <utils/EventHandler.h>
#include <utils/Log.h>
#include <io/Screen.h>
#include "Player.h"
2021-09-13 15:53:43 +03:00
2021-11-09 22:54:20 +03:00
Player::Player(ObjectNameTag name, const std::string &filename, const Vec3D &scale) : RigidBody(std::move(name), filename, scale) {
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);
2022-07-22 18:38:05 +03:00
setColor(sf::Color(0,0,0));
2021-10-29 19:43:39 +03:00
2021-11-09 22:54:20 +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) {
EventHandler::call<void(const std::string&)>(Event("take_bonus"), tag.str());
2021-09-13 15:53:43 +03:00
}
}
void Player::addWeapon(std::shared_ptr<Weapon> weapon) {
2022-02-23 17:29:42 +03:00
SoundController::loadAndPlay(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()) {
2021-11-09 22:54:20 +03:00
w->addAPack();
2021-10-02 20:36:07 +03:00
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-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
EventHandler::call<void(std::shared_ptr<Weapon>)>(Event("remove_weapon"),
_weapons[_selectedWeapon]);
2021-09-13 15:53:43 +03:00
_weapons.clear();
}
_selectedWeapon = 0;
addWeapon(std::make_shared<Gun>());
EventHandler::call<void(std::shared_ptr<Weapon>)>(Event("add_weapon"),
_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'
EventHandler::call<void(std::shared_ptr<Weapon>)>(Event("remove_weapon"),
_weapons[_selectedWeapon]);
2021-11-01 17:08:36 +03:00
_selectedWeapon = (_selectedWeapon + 1) % _weapons.size();
2021-10-31 19:01:06 +03:00
EventHandler::call<void(std::shared_ptr<Weapon>)>(Event("add_weapon"),
_weapons[_selectedWeapon]);
2021-10-09 13:41:12 +03:00
Log::log("selectedWeapon " + std::to_string(_selectedWeapon));
2022-02-23 17:29:42 +03:00
SoundController::loadAndPlay(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND);
selectWeaponAnimation();
}
2022-07-07 13:14:00 +03:00
}
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'
EventHandler::call<void(std::shared_ptr<Weapon>)>(Event("remove_weapon"),
_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
}
EventHandler::call<void(std::shared_ptr<Weapon>)>(Event("add_weapon"),
_weapons[_selectedWeapon]);
2021-10-09 13:41:12 +03:00
Log::log("selectedWeapon " + std::to_string(_selectedWeapon));
2022-02-23 17:29:42 +03:00
SoundController::loadAndPlay(SoundTag("changeWeapon"), ShooterConsts::CHANGE_WEAPON_SOUND);
selectWeaponAnimation();
}
}
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));
EventHandler::call<void(sf::Uint16, double)>(Event("damage_player"), 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;
2022-02-23 17:29:42 +03:00
SoundController::loadAndPlay(SoundTag("addHealth"), ShooterConsts::RESTORE_HEALTH_SOUND);
2021-10-16 20:22:55 +03:00
}
void Player::setFullAbility() {
_ability = ShooterConsts::ABILITY_MAX;
2022-02-23 17:29:42 +03:00
SoundController::loadAndPlay(SoundTag("addAbility"), ShooterConsts::RESTORE_ABILITY_SOUND);
2021-10-16 20:22:55 +03:00
}
2022-07-07 13:14:00 +03:00
void Player::selectWeaponAnimation() {
2022-07-07 13:14:00 +03:00
Timeline::addAnimation<ARotateLeft>(AnimationListTag("select_weapon"),
_weapons[_selectedWeapon],
-2 * Consts::PI,
0.3,
Animation::LoopOut::None,
Animation::InterpolationType::Cos);
}
void Player::fireWeaponAnimation() {
Timeline::addAnimation<ARotateLeft>(AnimationListTag("fire_weapon"),
_weapons[_selectedWeapon],
-_weapons[_selectedWeapon]->fireDelay(),
_weapons[_selectedWeapon]->fireDelay()/3,
Animation::LoopOut::None,
Animation::InterpolationType::Cos);
Timeline::addAnimation<AWait>(AnimationListTag("fire_weapon"), 0);
Timeline::addAnimation<ARotateLeft>(AnimationListTag("fire_weapon"),
_weapons[_selectedWeapon],
_weapons[_selectedWeapon]->fireDelay(),
_weapons[_selectedWeapon]->fireDelay()/3,
Animation::LoopOut::None,
Animation::InterpolationType::Cos);
}
void Player::reloadWeaponAnimation() {
Timeline::addAnimation<ARotateLeft>(AnimationListTag("reload_weapon"),
_weapons[_selectedWeapon],
-2 * Consts::PI,
_weapons[_selectedWeapon]->reloadTime() / 2,
Animation::LoopOut::None,
Animation::InterpolationType::Cos);
}