2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 01.06.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include "Weapon.h"
|
|
|
|
#include "ResourceManager.h"
|
|
|
|
#include "utils/Log.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
Weapon::Weapon(const std::string& weaponName, const std::string& objFileName, const std::string& matFileName, const Point4D& scale, const Point4D& translate, const Point4D& rotate) {
|
|
|
|
_name = weaponName;
|
|
|
|
auto objs = Mesh::LoadObjects(objFileName, matFileName, scale);
|
|
|
|
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
|
|
|
}
|
|
|
|
noAmmoSound.setBuffer(*ResourceManager::loadSoundBuffer("../sound/weapons/no_ammo.ogg"));
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
std::map<std::string, double> Weapon::fire(std::shared_ptr<World> world, std::shared_ptr<Camera> camera) {
|
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) + ")");
|
|
|
|
|
|
|
|
return processFire(world, camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-14 13:47:53 +03:00
|
|
|
void Weapon::addToWorld(shared_ptr<World> world) {
|
2021-09-13 15:53:43 +03:00
|
|
|
for(auto& obj : _objects) {
|
2021-09-19 11:25:10 +03:00
|
|
|
world->addBody(obj.second, obj.first);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
void Weapon::removeFromWorld(shared_ptr<World> world) {
|
2021-09-13 15:53:43 +03:00
|
|
|
for(auto& obj : _objects) {
|
2021-09-19 11:25:10 +03:00
|
|
|
world->removeBodyInstantly(obj.first);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Weapon::attachToPlayer(Mesh &player) {
|
|
|
|
for(auto& obj : _objects) {
|
|
|
|
player.attach(obj.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Weapon::rotate(const Point4D& point4D, double val) {
|
|
|
|
for(auto& mesh : _objects)
|
|
|
|
mesh.second->rotate(point4D, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Weapon::translate(const Point4D &point4D) {
|
|
|
|
for(auto& mesh : _objects)
|
|
|
|
mesh.second->translate(point4D);
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
void Weapon::deleteTrace(shared_ptr<World> world, const std::string& traceName) {
|
2021-09-19 11:25:10 +03:00
|
|
|
world->removeBody(traceName);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Weapon::rotateRelativePoint(const Point4D &point4D, const Point4D &v, double val) {
|
|
|
|
for(auto& mesh : _objects)
|
|
|
|
mesh.second->rotateRelativePoint(point4D, v, val);
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
std::map<std::string, double> Weapon::processFire(shared_ptr<World> world, shared_ptr<Camera> camera) {
|
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
|
|
|
|
auto rayCast = world->rayCast(camera->position(), camera->position() + camera->lookAt() * 1000 + randV);
|
|
|
|
if(rayCast.second.find("Player") != std::string::npos) {
|
|
|
|
damagedPlayers[rayCast.second] += _damage/(1.0 + (camera->position() - rayCast.first).abs());
|
|
|
|
}
|
|
|
|
|
|
|
|
// add trace line
|
|
|
|
Point4D to = rayCast.first.w() == -1 ? camera->position() + camera->lookAt() * 1000 + randV: rayCast.first;
|
|
|
|
string traceName = _name + "_trace_nr_" + std::to_string(fireTraces++);
|
|
|
|
Point4D from = _objects[_name + "_" + to_string(_objects.size()-1)]->position() + _objects[_name + "_" + to_string(_objects.size()-1)]->triangles()[0][0];
|
2021-09-19 11:25:10 +03:00
|
|
|
world->addBody(make_shared<RigidBody>(Mesh::LineTo(from, to, 0.05)), traceName);
|
|
|
|
world->body(traceName)->setCollider(false);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
// remove trace line after some time
|
2021-09-19 11:25:10 +03:00
|
|
|
world->body(traceName)->a_color("color_trace", {255, 255, 255, 0}, 1, Animation::None, Animation::linear);
|
|
|
|
world->body("Player_im")->a_function(traceName + "delete", [world, traceName](){deleteTrace(world, traceName); }, 1, 2);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
addTraceCallBack(from, to);
|
|
|
|
|
|
|
|
return damagedPlayers;
|
|
|
|
}
|