diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index a99e3aa..58441d8 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -64,8 +64,10 @@ units = { { vfx = true, duration = 50, - path = PATH .. vfxPrefix .. 'muzzleFlash.xml', - color = {x = 1, y = 1, z = 0, a = 1}, + mesh = { + path = PATH .. vfxPrefix .. 'muzzleFlash.xml', + color = {x = 1, y = 1, z = 0, a = 1}, + }, pos = {x = 0.214, y = 2.742, z = 1.54}, rot = {w = 1, x = 0, y = 0, z = 0}, --scale = .5 @@ -257,7 +259,33 @@ units = { garrisonCategory = 3 }, { - weapons = {{type = WeaponClass.HITSCAN, rateOfFire = 200, fireSfx = PATH .. 'Sounds/Units/Cruisers/fire.ogg', damage = 300, maxRange = 15}}, + weapons = { + { + type = WeaponClass.HITSCAN, + rateOfFire = 200, + damage = 300, + maxRange = 15, + fireFx = { + { + vfx = true, + duration = 50, + mesh = { + size = {x = .16, y = .16}, + color = {x = 1, y = 0, z = 0, a = 1}, + }, + pos = {x = 0., y = 2.8, z = -.1}, + rot = {w = 1, x = 0, y = 0, z = 0}, + --scale = .5 + }, + { + vfx = false, + duration = 50, + path = PATH .. 'Sounds/Units/Cruisers/fire.ogg', + } + }, + hitFx = {} + } + }, unitClass = UnitClass.CRUISER, unitType = UnitType.SEA_LEVEL, armor = {ArmorType.STEEL}, diff --git a/fxManager.cpp b/fxManager.cpp index f276e05..cec3399 100644 --- a/fxManager.cpp +++ b/fxManager.cpp @@ -10,7 +10,7 @@ namespace battleship{ using namespace vb01; using namespace sf; - FxManager::Fx::Component::Component(void *c, bool v, vb01::s64 dur, vb01::s64 ot) : comp(c), vfx(v), duration(dur), offsetTime(ot) { + FxManager::Fx::Component::Component(void *c, bool v, s64 dur, vb01::Vector3 p, s64 ot) : comp(c), vfx(v), duration(dur), pos(p), offsetTime(ot) { if(v) ((vb01::Node*)c)->setVisible(false); } diff --git a/fxManager.h b/fxManager.h index b5e9569..95a20f6 100644 --- a/fxManager.h +++ b/fxManager.h @@ -9,10 +9,11 @@ namespace battleship{ struct Fx { struct Component{ vb01::s64 duration, initTime = 0, offsetTime = 0; + vb01::Vector3 pos = vb01::Vector3::VEC_ZERO; bool vfx, active = false; void *comp = nullptr; - Component(void*, bool, vb01::s64, vb01::s64 = 0); + Component(void*, bool, vb01::s64, vb01::Vector3 = vb01::Vector3::VEC_ZERO, vb01::s64 = 0); }; bool reuse; diff --git a/game.cpp b/game.cpp index fde4b66..203c333 100644 --- a/game.cpp +++ b/game.cpp @@ -113,8 +113,10 @@ namespace battleship{ generateView().script_file(gm->getPath() + f); guiManager->readLuaScreenScript("inGame.lua", activeState->getButtons()); - string mp = generateView()["modelPrefix"]; - AssetManager::getSingleton()->load(gm->getPath() + mp, true); + sol::state_view SOL_LUA_VIEW = generateView(); + string gop = SOL_LUA_VIEW["gameObjPrefix"], vfxp = SOL_LUA_VIEW["vfxPrefix"]; + AssetManager::getSingleton()->load(gm->getPath() + gop, true); + AssetManager::getSingleton()->load(gm->getPath() + vfxp, true); vector gameObjs; diff --git a/unit.cpp b/unit.cpp index 129b34b..0231c5a 100755 --- a/unit.cpp +++ b/unit.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -28,6 +29,8 @@ using namespace gameBase; using namespace std; namespace battleship{ + string Unit::Weapon::LASER_FLAG = "laser"; + Unit::Weapon::Weapon(Unit *u, sol::table weaponTable) : unit(u), type((Weapon::Type)weaponTable["type"]), @@ -72,7 +75,6 @@ namespace battleship{ projPos = Vector3(posTable["x"], posTable["y"], posTable["z"]); sol::table rotTable = weaponTable[projTableKey]["rot"]; projRot = Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"]); - } } @@ -96,34 +98,51 @@ namespace battleship{ if(vfx){ Material *mat = new Material(Root::getSingleton()->getLibPath() + "texture"); + sol::table meshTbl = compTbl["mesh"]; - if((sol::optional)compTbl["texture"] != sol::nullopt){ - string p[]{compTbl["texture"]}; + if((sol::optional)meshTbl["texture"] != sol::nullopt){ + string p[]{meshTbl["texture"]}; Texture *tex = new Texture(p, 1, false); mat->addBoolUniform("texturingEnabled", true); mat->addTexUniform("diffuseMap[0]", tex, false); } else{ - sol::table colorTable = compTbl["color"]; + sol::table colorTable = meshTbl["color"]; mat->addVec4Uniform("diffuseColor", Vector4(colorTable["x"], colorTable["y"], colorTable["z"], colorTable["a"])); mat->addBoolUniform("texturingEnabled", false); } - Model *flashModel = new Model((string)compTbl["path"]); - flashModel->setMaterial(mat); - flashModel->setVisible(false); - unit->getModel()->attachChild(flashModel); + string meshPath = ""; + Node *flashNode = nullptr; sol::table posTable = compTbl["pos"], rotTable = compTbl["rot"]; - flashModel->setPosition(Vector3(posTable["x"], posTable["y"], posTable["z"])); - flashModel->setOrientation(Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"])); + Vector3 compPos = Vector3(posTable["x"], posTable["y"], posTable["z"]); + Quaternion compRot = Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"]); + float sc = 1; - if((sol::optional)compTbl["scale"] != sol::nullopt){ - float sc = compTbl["scale"]; - flashModel->setScale(Vector3(sc, sc, sc)); + if((sol::optional)compTbl["scale"] != sol::nullopt) float sc = compTbl["scale"]; + + if((sol::optional)meshTbl["path"] != sol::nullopt){ + meshPath = meshTbl["path"]; + flashNode = new Model(meshPath); + ((Model*)flashNode)->setMaterial(mat); + flashNode->setPosition(compPos); + flashNode->setOrientation(compRot); + flashNode->setScale(Vector3(sc, sc, sc)); + } + else{ + sol::table sizeTbl = meshTbl["size"]; + Box *box = new Box(Vector3(sizeTbl["x"], sizeTbl["y"], 0)); + box->setMaterial(mat); + + flashNode = new Node(compPos, compRot, Vector3(sc, sc, sc), LASER_FLAG); + flashNode->attachMesh(box); } - fxComponents.push_back(FxManager::Fx::Component((void*)flashModel, vfx, duration)); + flashNode->setVisible(false); + unit->getModel()->attachChild(flashNode); + + fxComponents.push_back(FxManager::Fx::Component((void*)flashNode, vfx, duration, compPos)); } else{ sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); @@ -147,9 +166,33 @@ namespace battleship{ void Unit::Weapon::fire(Order order){ if(!canFire()) return; - if(fireFx)fireFx->toggleComponents(true); - Unit *targetUnit = order.targets[0].unit; + Vector3 targPos = (targetUnit ? targetUnit->getPos() : order.targets[0].pos); + + if(fireFx){ + fireFx->toggleComponents(true); + + for(int i = 0; i < fireFx->components.size(); i++){ + FxManager::Fx::Component &comp = fireFx->components[i]; + + if(comp.vfx && ((Node*)comp.comp)->getName() == LASER_FLAG){ + Vector3 initPos = unit->getPos(), laserDir = targPos - initPos; + float targDist = laserDir.getLength(); + float angle = unit->getDirVec().getAngleBetween(laserDir); + Vector3 crossProd = unit->getDirVec().cross(laserDir); + + Node *compNode = (Node*)comp.comp; + compNode->setOrientation(Quaternion(angle, crossProd) * compNode->getOrientation()); + + Box *box = (Box*)compNode->getMesh(0); + Vector3 size = box->getSize(); + box->setSize(Vector3(size.x, size.y, targDist)); + box->updateVerts(box->getMeshBase()); + + compNode->setPosition(comp.pos + .5 * targDist * Vector3::VEC_K); + } + } + } if(targetUnit && projId == -1){ targetUnit->takeDamage(damage); diff --git a/unit.h b/unit.h index ad8363b..7b2a544 100755 --- a/unit.h +++ b/unit.h @@ -104,6 +104,7 @@ namespace battleship{ vb01::s64 lastFireTime = 0; Unit *unit = nullptr; FxManager::Fx *fireFx = nullptr, *hitFx = nullptr; + static std::string LASER_FLAG; void initProjectileData(sol::table); FxManager::Fx* initFx(sol::table, std::string);