Projectiles detonate on raycast collision with unit hitboxes

This commit is contained in:
devZoGok
2024-01-17 20:30:16 +02:00
parent 827589c823
commit a956e754bf
11 changed files with 151 additions and 255 deletions
@@ -1,244 +1,47 @@
ProjectileClass = {GUIDED_MISSILE = 0, TORPEDO = 1}
projectiles = {
numberOfTypesOfWeapons = 2,
g = 2.2,
speed = {
{
{10,10,10,10,10,10,10,10,10,10,10}
},
{
{10,10,10,10,10,10,10,10,10,10,10}
},
{
{10, 10, 10, 10}
},
{
{10, 10, 10, 10}
},
{
{5,5}
},
{
{7,7,7,7,7}
},
{},
{},
{.05},
{
{.1, .1},
{.1, .1}
}
projectileClass = {
ProjectileClass.GUIDED_MISSILE,
ProjectileClass.TORPEDO,
},
length = {
{
{1,1,1,1,1,1,1,1,1,1,1,1,1}
},
{
{1,1,1,1,1,1,1,1,1,1,1,1,1}
},
{
{1,1,1,1}
},
{
{1,1,1,1}
},
{
{1,1}
},
{
{1,1,1,1,1}
},
{},
{},
{
{.5}
},
{
{1,1},
{1,1}
},
{}
speed = {.5, .06},
rayLength = {1.25, .75},
pos = {{x = 0, y = 2.44, z = -3.5}, {x = 0, y = -.27, z = 4.6}},
rot = {{w = .707, x = -.707, y = 0, z = 0}, {w = 1, x = 0, y = 0, z = 0}},
damage = {450, 300},
name = {
"Guided missile",
"Torpedo"
},
scale = {
{
{1, 1, 1, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5}
},
{
{1, 1, 1, .23, .23, .23, .23, .23, .23, .23, .23, .23, .23}
},
{
{.45, .45, .45, .45}
},
{
{.5, .5, .5, .5}
},
{
{.95, .95}
},
{
{.6, .6, .6, .6, .6}
}
},
pos = {
{
{
{x = 0, y = 0, z = 1.05},
{x = 0, y = 0, z = 1.05},
{x = 0, y = 0, z = 1.05},
{x = 0, y = 0, z = 1.5},
{x = 0, y = 0, z = 2.5},
{x = 0, y = 0, z = 2.5},
{x = 0, y = 0, z = 2.5},
{x = 0, y = 0, z = 2.5},
{x = 0, y = 0, z = 2.5},
{x = 0, y = 0, z = 2.5},
{x = 0, y = 0, z = 2.5},
{x = 0, y = 0, z = 2.5},
{x = 0, y = 0, z = 2.5}
}
},
{
{
{x = 0, y = 0, z = 0.79254},
{x = 0, y = 0, z = 0.79254},
{x = 0, y = 0, z = 0.79254},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723},
{x = 0, y = 0, z = 0.17723}
}
},
{
{
{x = 0, y = 0, z = -0.36178},
{x = 0, y = 0, z = -0.36178},
{x = 0, y = 0, z = -0.36178},
{x = 0, y = 0, z = -0.36178}
},
{x = 0, y = -3, z = 0}
},
{
{
{x = 0, y = 0, z = -0.30074},
{x = 0, y = 0, z = -0.30074},
{x = 0, y = 0, z = -0.30074},
{x = 0, y = 0, z = -0.30074}
},
{x = 0, y = -2, z = 0}
},
{
{
{x = 0, y = 0, z = -0.62118},
{x = 0, y = 0, z = -0.62118}
},
{
{x = 0.24482, y = 1.14985, z = 1.32775},
{x = 0.24482, y = 1.14985, z = 1.62698},
{x = 0.24482, y = 1.14985, z = 1.88017},
{x = -0.24482, y = 1.14985, z = 1.32775},
{x = -0.24482, y = 1.14985, z = 1.62698},
{x = -0.24482, y = 1.14985, z = 1.88017}
}
},
{
{
{x = 0, y = 0, z = -0.59117},
{x = 0, y = 0, z = -0.59117},
{x = 0, y = 0, z = -0.59117},
{x = 0, y = 0, z = -0.59117},
{x = 0, y = 0, z = -0.59117}
},
{{x = 0.17192, y = 0.57858, z = 0.64471}, {x = -0.17192, y = 0.57858, z = 0.64471}}
},
{},
{},
{
{x = 0, y =-.3, z =-1.5}
},
{
{
{x = -.25, y =0, z =0},
{x = .25, y =0, z =0}
},
{
{x = -.25, y =0, z =0},
{x = .25, y =0, z =0}
}
},
{}
},
damage = {
{
{50, 50, 50, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
},
{
{50, 50, 50, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
},
{
{50,50,50,50},
{50}
},
{
{50,50,50,50},
{50}
},
{
{10,10},
{}
},
{
{10,10,10,10,10},
{}
},
{},
{},
{
{}
},
{
{50,50},
{50,50}
},
{}
},
projectileName = {
{"shell"},
{"shell"},
{"shell"},
{"shell"},
{"shell","guidedMissile"},
{"shell","guidedMissile"},
{""},
{""},
{"torpedo"},
{"missile","missile"},
basePath = {
PATH .. "Models/GameObjects/Projectiles/Missiles/",
PATH .. "Models/GameObjects/Projectiles/Torpedos/",
},
meshPath = {
{PATH .. "Models/GameObjects/Shell/shell.x"},
{PATH .. "Models/GameObjects/Shell/shell.x"},
{PATH .. "Models/GameObjects/Shell/shell.x", PATH .. "Models/GameObjects/Depth charge/depthCharge.x"},
{PATH .. "Models/GameObjects/Shell/shell.x", PATH .. "Models/GameObjects/Depth charge/depthCharge.x"},
{PATH .. "Models/GameObjects/Shell/shell.x", PATH .. "Models/GameObjects/Guided missile/guidedMissile.x"},
{PATH .. "Models/GameObjects/Shell/shell.x", PATH .. "Models/GameObjects/Guided missile/guidedMissile.x"},
{},
{},
{PATH .. "Models/GameObjects/Torpedo/torpedo.x"},
{PATH .. "Models/GameObjects/Jets/aam.x", PATH .. "Models/GameObjects/Jets/awm.x"}
"guidedMissile.xml",
"torpedo.xml",
},
diffuseMapTextPath = {
{},
{},
{},
{},
{},
{},
{},
{},
{}
unitCornerPoints = {
{
{x = .592, y = -1.3475, z = -.2755},
{x = -.592, y = -1.3475, z = -.2755},
{x = -.592, y = -1.3475, z = .2755},
{x = .592, y = -1.3475, z = .2755},
{x = .592, y = 1.3475, z = -.2755},
{x = -.592, y = 1.3475, z = -.2755},
{x = -.592, y = 1.3475, z = .2755},
{x = .592, y = 1.3475, z = .2755}
},
{
{x = .1, y = -.796, z = -.1},
{x = -.1, y = -.796, z = -.1},
{x = -.1, y = -.796, z = .1},
{x = .1, y = -.796, z = .1},
{x = .1, y = .796, z = -.1},
{x = -.1, y = .796, z = -.1},
{x = -.1, y = .796, z = .1},
{x = .1, y = .796, z = .1}
}
}
}
@@ -562,6 +562,25 @@ units = {
{x = .1, y = .1, z = .1}
}
},
hitboxOffset = {
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
{x = 0, y = 0, z = 0},
},
unitAxisLength = {8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 5, 5, 8, 8, 7, 2, 2, 2, 2, 2, 2, 2},
lineOfSight = {5, 8, 4, 4, 3, 3, 3, 3, 3, 3, 3, 6, 6, 3, 3, 8, 1, 1, 1, 1, 1},
+2 -3
View File
@@ -1,6 +1,4 @@
resources = {
num = 1,
meshPath = {
"resource.xml"
},
@@ -21,5 +19,6 @@ resources = {
{x = -1, y = .571, z = 1},
{x = 1, y = .571, z = 1}
}
}
},
hitboxOffset = {{x = 0, y = 0, z = 0}}
}
+22
View File
@@ -5,6 +5,7 @@
#include <solUtil.h>
#include <material.h>
#include <box.h>
#include <SFML/Audio.hpp>
@@ -33,6 +34,27 @@ namespace battleship{
rot = rotQuat;
}
//TODO remove neccessity to create a material for an invisible mesh
void GameObject::initHitbox(){
sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()];
sol::table offsetPosTable = SOL_LUA_STATE["hitboxOffset"][id + 1];
Box *box = new Box(Vector3(width, height, length));
box->setMaterial(new Material(Root::getSingleton()->getLibPath() + "texture"));
box->setWireframe(false);
hitbox = new Node(Vector3(offsetPosTable["x"], offsetPosTable["y"], offsetPosTable["z"]));
hitbox->attachMesh(box);
hitbox->setVisible(false);
model->attachChild(hitbox);
}
void GameObject::destroyHitbox(){
hitbox->dettachMesh(0);
model->dettachChild(hitbox);
delete hitbox;
}
void GameObject::initProperties(){
sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()];
+4
View File
@@ -42,12 +42,15 @@ namespace battleship{
inline vb01::Vector3 getUpVec() {return upVec;}
inline int getId() {return id;}
inline Type getType(){return type;}
inline vb01::Node* getHitbox(){return hitbox;}
private:
int sortCorners(std::vector<vb01::Vector2>&, bool, bool);
protected:
virtual void initProperties();
virtual void destroyModel();
virtual void initModel(bool = true);
virtual void initHitbox();
virtual void destroyHitbox();
virtual void destroySound();
virtual void initSound();
sf::Sound* prepareSfx(sf::SoundBuffer*, std::string);
@@ -58,6 +61,7 @@ namespace battleship{
sf::SoundBuffer *deathSfxBuffer;
sf::Sound *deathSfx;
vb01::Model *model = nullptr;
vb01::Node *hitbox = nullptr;
vb01::Vector3 pos = vb01::Vector3(0, 0, 0), upVec = vb01::Vector3(0, 1, 0), dirVec = vb01::Vector3(0, 0, 1), leftVec = vb01::Vector3(1, 0, 0), corners[8];
vb01::Vector2 screenPos;
vb01::Quaternion rot;
+13
View File
@@ -122,6 +122,19 @@ namespace battleship{
resourceDeposits.erase(resourceDeposits.begin() + id);
}
void Player::removeProjectile(Projectile *proj){
for(int i = 0; i < projectiles.size(); i++)
if(proj == projectiles[i]){
removeProjectile(i);
break;
}
}
void Player::removeProjectile(int id){
delete projectiles[id];
projectiles.erase(projectiles.begin() + id);
}
vector<Unit*> Player::getSelectedUnitsByClass(UnitClass uc){
vector<Unit*> selectedUnits = getSelectedUnits(), unitsByClass;
+2
View File
@@ -19,6 +19,8 @@ namespace battleship{
void removeUnit(Unit*);
void removeUnit(int);
void removeResourceDeposit(int);
void removeProjectile(int);
void removeProjectile(Projectile*);
bool isThisPlayersUnit(GameObject*);
std::vector<Unit*> getSelectedUnitsByClass(UnitClass);
void selectUnits(std::vector<Unit*>);
+39 -13
View File
@@ -1,10 +1,9 @@
#include <algorithm>
#include <node.h>
#include <model.h>
#include <material.h>
#include <particleEmitter.h>
#include <quaternion.h>
#include <rayCaster.h>
#include <stateManager.h>
@@ -12,6 +11,7 @@
#include "unit.h"
#include "util.h"
#include "projectile.h"
#include "resourceDeposit.h"
#include "defConfigs.h"
#include "inGameAppState.h"
#include "fx.h"
@@ -32,7 +32,9 @@ namespace battleship{
orientAt(rot);
}
Projectile::~Projectile(){}
Projectile::~Projectile(){
destroyModel();
}
void Projectile::initProperties(){
GameObject::initProperties();
@@ -46,7 +48,7 @@ namespace battleship{
void Projectile::initSound(){
GameManager *gm = GameManager::getSingleton();
explosionSfxBuffer = new sf::SoundBuffer();
string p2 = gm->getPath() + "Sounds/Explosions/explosion0" + to_string(rand() % 4) + ".ogg";
string p2 = gm->getPath() + "Sounds/SFX/Explosions/explosion0" + to_string(rand() % 4) + ".ogg";
if(explosionSfxBuffer->loadFromFile(p2.c_str()))
explosionSfx = new sf::Sound(*explosionSfxBuffer);
@@ -55,21 +57,43 @@ namespace battleship{
void Projectile::update() {
GameObject::update();
placeAt(pos + speed * dirVec);
checkForCollision();
if(!exploded) checkForCollision();
}
void Projectile::checkForCollision() {
Map *map = Map::getSingleton();
int cellId = map->getCellId(pos);
if(map->getCells()[cellId].type == Map::Cell::LAND)
player->removeProjectile(this);
vector<Player*> players = Game::getSingleton()->getPlayers();
vector<Unit*> targetUnits;
vector<Node*> targetNodes;
for(Player *pl : players){
vector<Unit*> units = pl->getUnits();
for(Unit *u : units)
if(unit && unit != u){
targetUnits.push_back(u);
targetNodes.push_back(u->getHitbox());
}
}
vector<RayCaster::CollisionResult> results = RayCaster::cast(pos, dirVec, targetNodes, rayLength);
if(!results.empty()){
for(int i = 0; i < targetNodes.size(); i++)
if(targetNodes[i]->getMesh(0) == results[0].mesh)
explode(targetUnits[i]);
}
}
void Projectile::explode(Node *collNode) {
void Projectile::explode(Unit *target) {
exploded = true;
for (Player *p : Game::getSingleton()->getPlayers()) {
for (Unit *u : p->getUnits())
if (collNode == u->getNode())
u->takeDamage(damage);
}
target->takeDamage(damage);
Root *root = Root::getSingleton();
const int numFrames = 1;
@@ -98,6 +122,8 @@ namespace battleship{
Fx fx(50, 2500, explosionSfx, node);
fx.activate();
Game::getSingleton()->addFx(fx);
player->removeProjectile(this);
}
void Projectile::debug(){
+3 -1
View File
@@ -5,6 +5,8 @@
#include "gameObject.h"
#include <algorithm>
namespace vb01{
class Node;
}
@@ -25,7 +27,7 @@ namespace battleship {
void initSound();
protected:
virtual void checkForCollision();
void explode(vb01::Node*);
void explode(Unit*);
bool exploded = false;
float speed, rayLength;
+2
View File
@@ -6,11 +6,13 @@ namespace battleship{
ResourceDeposit::ResourceDeposit(Player *player, int id, Vector3 pos, Quaternion rot) : GameObject(GameObject::Type::RESOURCE_DEPOSIT, id, player, pos, rot){
initProperties();
initModel();
initHitbox();
placeAt(pos);
orientAt(rot);
}
ResourceDeposit::~ResourceDeposit(){
destroyHitbox();
destroyModel();
}
}
+6 -2
View File
@@ -40,12 +40,14 @@ namespace battleship{
removeBar(hpBackgroundNode);
removeBar(hpForegroundNode);
destroySound();
destroyHitbox();
destroyModel();
}
void Unit::init(){
initProperties();
initModel();
initHitbox();
initSound();
placeAt(pos);
orientAt(rot);
@@ -112,10 +114,12 @@ namespace battleship{
void Unit::reinit(){
destroySound();
destroyHitbox();
destroyModel();
initModel();
initSound();
initProperties();
initModel();
initHitbox();
initSound();
placeAt(pos);
orientAt(rot);
}