SQCSimulator2023/SQCSim-common/bullet.cpp
MarcEricMartel db854c77b2 👌
2023-12-06 15:47:14 -05:00

77 lines
1.9 KiB
C++

#include "bullet.h"
#include "world.h"
Bullet::Bullet(Vector3f pos, Vector3f dir) : m_startpos(pos), m_currentpos(pos), m_velocity(dir) {}
Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t shooter_id): m_startpos(pos), m_currentpos(pos), m_velocity(dir), m_shooter_id(shooter_id) {}
Bullet::~Bullet() {}
bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordered_map<uint64_t, Player*> mapPlayer, netprot::ChunkMod** chunkmod) {
int max = 100 / perframe;
float damage = 0.057f;
for (int x = 0; x < max; ++x) {
m_currentpos += m_velocity * elapsedtime;
for (auto& [key, player] : mapPlayer) {
bool hit = false;
if ((m_currentpos - player->GetPosition()).Length() < .6f) {
std::cout << "hit" << std::endl;
hit = true;
}
if ((m_currentpos - player->GetPOV()).Length() < .2f) {
std::cout << "headshot" << std::endl;
damage *= 2; // HEADSHOT!
hit = true;
}
if (hit && !player->AmIDead()) {
player->InflictDamage(damage);
player->m_hit = true;
if (player->AmIDead())
player->Killer = m_shooter_id;
return true;
}
}
if (!world->ChunkAt(m_currentpos))
return true;
else if (world->BlockAt(m_currentpos) != BTYPE_AIR) {
if (chunkmod) {
using namespace netprot;
ChunkMod* cmod = *chunkmod;
cmod = new ChunkMod();
cmod->old_b_type = world->BlockAt(m_currentpos);
cmod->b_type = BTYPE_AIR;
cmod->pos = m_currentpos;
}
world->ChangeBlockAtPosition(BTYPE_AIR, m_currentpos);
return true;
}
else if ((m_currentpos - m_startpos).Length() > VIEW_DISTANCE) return true;
}
return false;
}
void Bullet::Transpose(int& x, int& z) {
m_currentpos.x -= x * CHUNK_SIZE_X;
m_currentpos.z -= z * CHUNK_SIZE_Z;
m_startpos.x -= x * CHUNK_SIZE_X;
m_startpos.z -= z * CHUNK_SIZE_Z;
}
Vector3f Bullet::getPos() const {
return m_currentpos;
}
Vector3f Bullet::getVel() const {
return m_velocity;
}
//uint64_t Bullet::getTeamID(){
// return m_tid;
//}