SQCSimulator2023/SQCSim-common/bullet.cpp

77 lines
1.9 KiB
C++
Raw Normal View History

2023-09-18 15:56:17 -04:00
#include "bullet.h"
#include "world.h"
2023-09-29 17:02:57 -04:00
Bullet::Bullet(Vector3f pos, Vector3f dir) : m_startpos(pos), m_currentpos(pos), m_velocity(dir) {}
2023-12-06 11:16:39 -05:00
Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t shooter_id): m_startpos(pos), m_currentpos(pos), m_velocity(dir), m_shooter_id(shooter_id) {}
2023-09-18 15:56:17 -04:00
Bullet::~Bullet() {}
2023-12-05 13:44:54 -05:00
bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordered_map<uint64_t, Player*> mapPlayer, netprot::ChunkMod** chunkmod) {
2023-10-16 12:02:37 -04:00
int max = 100 / perframe;
2023-10-30 14:58:17 -04:00
float damage = 0.057f;
2023-10-16 12:02:37 -04:00
for (int x = 0; x < max; ++x) {
2023-09-18 15:56:17 -04:00
m_currentpos += m_velocity * elapsedtime;
2023-10-30 15:42:02 -04:00
for (auto& [key, player] : mapPlayer) {
2023-12-06 11:16:39 -05:00
bool hit = false;
2023-12-06 14:49:36 -05:00
if ((m_currentpos - player->GetPosition()).Length() < .6f) {
std::cout << "hit" << std::endl;
2023-12-06 11:16:39 -05:00
hit = true;
2023-12-06 14:49:36 -05:00
}
2023-12-06 11:16:39 -05:00
if ((m_currentpos - player->GetPOV()).Length() < .2f) {
2023-12-06 14:49:36 -05:00
std::cout << "headshot" << std::endl;
2023-12-06 11:16:39 -05:00
damage *= 2; // HEADSHOT!
hit = true;
}
if (hit && !player->AmIDead()) {
2023-10-30 14:36:44 -04:00
player->InflictDamage(damage);
2023-12-06 15:47:14 -05:00
player->m_hit = true;
2023-12-06 11:16:39 -05:00
if (player->AmIDead())
player->Killer = m_shooter_id;
2023-10-30 14:36:44 -04:00
return true;
}
}
2023-09-18 15:56:17 -04:00
if (!world->ChunkAt(m_currentpos))
return true;
else if (world->BlockAt(m_currentpos) != BTYPE_AIR) {
2023-12-05 13:44:54 -05:00
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;
}
2023-09-18 15:56:17 -04:00
world->ChangeBlockAtPosition(BTYPE_AIR, m_currentpos);
return true;
}
else if ((m_currentpos - m_startpos).Length() > VIEW_DISTANCE) return true;
}
2023-10-30 14:36:44 -04:00
2023-09-18 15:56:17 -04:00
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;
}
2023-09-29 12:04:08 -04:00
2023-10-16 12:02:37 -04:00
Vector3f Bullet::getPos() const {
2023-09-29 12:04:08 -04:00
return m_currentpos;
}
2023-09-29 17:02:57 -04:00
2023-10-16 12:02:37 -04:00
Vector3f Bullet::getVel() const {
return m_velocity;
}
2023-12-06 11:16:39 -05:00
//uint64_t Bullet::getTeamID(){
// return m_tid;
//}