2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 01.06.2021.
|
|
|
|
//
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
#include <utility>
|
2021-09-13 15:53:43 +03:00
|
|
|
#include "Weapon.h"
|
2021-10-09 13:41:12 +03:00
|
|
|
#include "../engine/ResourceManager.h"
|
|
|
|
#include "../engine/utils/Log.h"
|
|
|
|
#include "../engine/animation/AColor.h"
|
|
|
|
#include "../engine/animation/AFunction.h"
|
2021-10-16 20:22:55 +03:00
|
|
|
#include "../ShooterConsts.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2021-10-17 20:52:21 +03:00
|
|
|
Weapon::Weapon(const std::string& weaponName, const std::string& objFileName, const Vec3D& scale, const Vec3D& t, const Vec3D& r) {
|
2021-09-13 15:53:43 +03:00
|
|
|
_name = weaponName;
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-17 20:52:21 +03:00
|
|
|
loadObj(objFileName, scale);
|
2021-10-02 20:36:07 +03:00
|
|
|
setCollider(false);
|
|
|
|
rotate(r);
|
|
|
|
translate(t);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
std::map<ObjectNameTag, double> Weapon::fire(std::function<std::pair<Vec3D, ObjectNameTag>(const Vec3D&, const Vec3D&)> rayCastFunction) {
|
2021-09-13 15:53:43 +03:00
|
|
|
if(_clipAmmo == 0) {
|
|
|
|
reload();
|
|
|
|
if(_clipAmmo == 0)
|
2021-10-17 19:38:16 +03:00
|
|
|
SoundController::playSound(SoundTag("noAmmo"), ShooterConsts::NO_AMMO_SOUND);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 17:01:26 +03:00
|
|
|
if(_clipAmmo <= 0 || std::abs(Time::time() - _lastFireTime) < _fireDelay || std::abs(Time::time() - _lastReloadTime) < _reloadTime)
|
2021-10-17 10:21:10 +03:00
|
|
|
return std::map<ObjectNameTag, double>();
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
_lastFireTime = Time::time();
|
|
|
|
_clipAmmo--;
|
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
SoundController::playSound(SoundTag("fire"), fireSound);
|
2021-09-13 15:53:43 +03:00
|
|
|
Log::log("Weapon::fire (" + std::to_string(_stockAmmo) + " : " + std::to_string(_clipAmmo) + ")");
|
|
|
|
|
2021-10-16 20:22:55 +03:00
|
|
|
return processFire(std::move(rayCastFunction));
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Weapon::reload() {
|
2021-09-13 17:01:26 +03:00
|
|
|
if (_stockAmmo == 0 || std::abs(Time::time() - _lastReloadTime) < _reloadTime)
|
2021-09-13 15:53:43 +03:00
|
|
|
return;
|
|
|
|
if(_clipCapacity - _clipAmmo <= _stockAmmo) {
|
|
|
|
_stockAmmo -= _clipCapacity - _clipAmmo;
|
|
|
|
_clipAmmo = _clipCapacity;
|
|
|
|
} else {
|
|
|
|
_clipAmmo += _stockAmmo;
|
|
|
|
_stockAmmo = 0;
|
|
|
|
}
|
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
SoundController::playSound(SoundTag("fire"), reloadSound);
|
2021-09-13 15:53:43 +03:00
|
|
|
Log::log("Weapon::reload (" + std::to_string(_stockAmmo) + " : " + std::to_string(_clipAmmo) + ")");
|
|
|
|
_lastReloadTime = Time::time();
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
std::map<ObjectNameTag, double> Weapon::processFire(std::function<std::pair<Vec3D, ObjectNameTag>(const Vec3D&, const Vec3D&)> rayCastFunction) {
|
2021-10-16 20:22:55 +03:00
|
|
|
return addTrace(std::move(rayCastFunction), position() + Vec3D(triangles().back()[0]), -lookAt());
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
std::map<ObjectNameTag, double> Weapon::addTrace(std::function<std::pair<Vec3D, ObjectNameTag>(const Vec3D&, const Vec3D&)> rayCastFunction, const Vec3D& from, const Vec3D& directionTo) {
|
|
|
|
std::map<ObjectNameTag, double> damagedPlayers;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-16 20:22:55 +03:00
|
|
|
double spreading = _spreading*ShooterConsts::FIRE_DISTANCE/100;
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
//generate random vector
|
2021-10-16 20:22:55 +03:00
|
|
|
Vec3D randV(spreading*(1.0 - 2.0*(double)rand()/RAND_MAX), spreading*(1.0 - 2.0*(double)rand()/RAND_MAX), spreading*(1.0 - 2.0*(double)rand()/RAND_MAX));
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
// damage player
|
2021-10-16 20:22:55 +03:00
|
|
|
auto rayCast = rayCastFunction(from, from + directionTo * ShooterConsts::FIRE_DISTANCE + randV);
|
2021-10-17 10:21:10 +03:00
|
|
|
if(rayCast.second.str().find("Enemy") != std::string::npos) {
|
2021-10-16 20:22:55 +03:00
|
|
|
damagedPlayers[rayCast.second] += _damage/(1.0 + (from - rayCast.first).abs());
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// add trace line
|
2021-10-16 20:22:55 +03:00
|
|
|
Vec3D to = rayCast.first == Vec3D(0) ? from + directionTo * ShooterConsts::FIRE_DISTANCE + randV: rayCast.first;
|
2021-10-02 20:36:07 +03:00
|
|
|
_addTraceCallBack(from, to);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
return damagedPlayers;
|
|
|
|
}
|