diff --git a/Assets/Scripts/GameObjects/Projectiles/projectileData.lua b/Assets/Scripts/GameObjects/Projectiles/projectileData.lua index 42677b7..b172ef2 100644 --- a/Assets/Scripts/GameObjects/Projectiles/projectileData.lua +++ b/Assets/Scripts/GameObjects/Projectiles/projectileData.lua @@ -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} + } } } diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index cae7275..f190968 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -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}, diff --git a/Assets/Scripts/GameObjects/resourceData.lua b/Assets/Scripts/GameObjects/resourceData.lua index efbc997..19acb1f 100644 --- a/Assets/Scripts/GameObjects/resourceData.lua +++ b/Assets/Scripts/GameObjects/resourceData.lua @@ -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}} } diff --git a/gameObject.cpp b/gameObject.cpp index 2ec1519..7fc180b 100644 --- a/gameObject.cpp +++ b/gameObject.cpp @@ -5,6 +5,7 @@ #include #include +#include #include @@ -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()]; diff --git a/gameObject.h b/gameObject.h index 16e450d..3c2b87b 100644 --- a/gameObject.h +++ b/gameObject.h @@ -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&, 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; diff --git a/player.cpp b/player.cpp index c51a9ad..5a626be 100755 --- a/player.cpp +++ b/player.cpp @@ -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 Player::getSelectedUnitsByClass(UnitClass uc){ vector selectedUnits = getSelectedUnits(), unitsByClass; diff --git a/player.h b/player.h index 5bdd54a..0941def 100755 --- a/player.h +++ b/player.h @@ -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 getSelectedUnitsByClass(UnitClass); void selectUnits(std::vector); diff --git a/projectile.cpp b/projectile.cpp index 2beed2c..bfacb2c 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -1,10 +1,9 @@ -#include - #include #include #include #include #include +#include #include @@ -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 players = Game::getSingleton()->getPlayers(); + vector targetUnits; + vector targetNodes; + + for(Player *pl : players){ + vector units = pl->getUnits(); + + for(Unit *u : units) + if(unit && unit != u){ + targetUnits.push_back(u); + targetNodes.push_back(u->getHitbox()); + } + } + + vector 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(){ diff --git a/projectile.h b/projectile.h index 0757608..7ec3970 100755 --- a/projectile.h +++ b/projectile.h @@ -5,6 +5,8 @@ #include "gameObject.h" +#include + 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; diff --git a/resourceDeposit.cpp b/resourceDeposit.cpp index a59b023..7fe4ec0 100644 --- a/resourceDeposit.cpp +++ b/resourceDeposit.cpp @@ -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(); } } diff --git a/unit.cpp b/unit.cpp index 9113299..78c3b9d 100755 --- a/unit.cpp +++ b/unit.cpp @@ -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); }