2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 01.06.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <sstream>
|
2021-10-02 20:36:07 +03:00
|
|
|
#include <utility>
|
2021-09-13 15:53:43 +03:00
|
|
|
#include "Weapon.h"
|
|
|
|
#include "ResourceManager.h"
|
|
|
|
#include "utils/Log.h"
|
2021-10-02 20:36:07 +03:00
|
|
|
#include "animation/AColor.h"
|
|
|
|
#include "animation/AFunction.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
Weapon::Weapon(const std::string& weaponName, const std::string& objFileName, const std::string& matFileName, const Point4D& scale, const Point4D& t, const Point4D& r) {
|
2021-09-13 15:53:43 +03:00
|
|
|
_name = weaponName;
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
/*
|
2021-09-13 15:53:43 +03:00
|
|
|
auto objs = Mesh::LoadObjects(objFileName, matFileName, scale);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
for(int i = 0; i < objs.size(); i++) {
|
|
|
|
string meshName = _name + "_" + to_string(i);
|
2021-09-19 11:25:10 +03:00
|
|
|
|
|
|
|
RigidBody obj(*objs[i]);
|
|
|
|
|
|
|
|
obj.setCollider(false);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
//transforms
|
2021-09-19 11:25:10 +03:00
|
|
|
obj.rotate(rotate);
|
|
|
|
obj.translate(translate);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
_objects.insert({meshName, std::make_shared<RigidBody>(obj)});
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
2021-10-02 20:36:07 +03:00
|
|
|
*/
|
|
|
|
loadObj(objFileName, matFileName, scale);
|
|
|
|
setCollider(false);
|
|
|
|
rotate(r);
|
|
|
|
translate(t);
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
noAmmoSound.setBuffer(*ResourceManager::loadSoundBuffer("../sound/weapons/no_ammo.ogg"));
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
std::map<std::string, double> Weapon::fire(std::function<std::pair<Point4D, std::string>(const Point4D&, const Point4D&)> rayCastFunction, const Point4D& position, const Point4D& direction) {
|
2021-09-13 15:53:43 +03:00
|
|
|
if(_clipAmmo == 0) {
|
|
|
|
reload();
|
|
|
|
if(_clipAmmo == 0)
|
|
|
|
noAmmoSound.play();
|
|
|
|
}
|
|
|
|
|
2021-09-13 17:01:26 +03:00
|
|
|
if(_clipAmmo <= 0 || std::abs(Time::time() - _lastFireTime) < _fireDelay || std::abs(Time::time() - _lastReloadTime) < _reloadTime)
|
2021-09-13 15:53:43 +03:00
|
|
|
return std::map<std::string, double>();
|
|
|
|
|
|
|
|
_lastFireTime = Time::time();
|
|
|
|
_clipAmmo--;
|
|
|
|
|
|
|
|
fireSound.play();
|
|
|
|
Log::log("Weapon::fire (" + std::to_string(_stockAmmo) + " : " + std::to_string(_clipAmmo) + ")");
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
return processFire(std::move(rayCastFunction), position, direction);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
reloadSound.play();
|
|
|
|
Log::log("Weapon::reload (" + std::to_string(_stockAmmo) + " : " + std::to_string(_clipAmmo) + ")");
|
|
|
|
_lastReloadTime = Time::time();
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
std::map<std::string, double> Weapon::processFire(std::function<std::pair<Point4D, std::string>(const Point4D&, const Point4D&)> rayCastFunction, const Point4D& pos, const Point4D& direction) {
|
2021-09-13 15:53:43 +03:00
|
|
|
std::map<std::string, double> damagedPlayers;
|
|
|
|
|
|
|
|
//generate random vector
|
|
|
|
Point4D randV(10.0*_spreading*(1.0 - 2.0*(double)rand()/RAND_MAX), 10.0*_spreading*(1.0 - 2.0*(double)rand()/RAND_MAX), 10.0*_spreading*(1.0 - 2.0*(double)rand()/RAND_MAX));
|
|
|
|
|
|
|
|
// damage player
|
2021-10-02 20:36:07 +03:00
|
|
|
auto rayCast = rayCastFunction(pos, pos + direction * 1000 + randV);
|
2021-09-13 15:53:43 +03:00
|
|
|
if(rayCast.second.find("Player") != std::string::npos) {
|
2021-10-02 20:36:07 +03:00
|
|
|
damagedPlayers[rayCast.second] += _damage/(1.0 + (pos - rayCast.first).abs());
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// add trace line
|
2021-10-02 20:36:07 +03:00
|
|
|
Point4D to = rayCast.first.w() == -1 ? pos + direction * 1000 + randV: rayCast.first;
|
|
|
|
Point4D from = position() + triangles().back()[0];
|
|
|
|
_addTraceCallBack(from, to);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
return damagedPlayers;
|
|
|
|
}
|