diff --git a/CMakeLists.txt b/CMakeLists.txt index d653727..9e4cdd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,11 +11,9 @@ set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp) set(UTIL util.cpp binds.h) set(GUI tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) set(CORE gameManager.cpp defConfigs.cpp) -set(SHIPS vessel.cpp destroyer.cpp cruiser.cpp aircraftCarrier.cpp submarine.cpp) set(PROJECTILES projectile.cpp guidedMissile.cpp depthCharge.cpp missile.cpp shell.cpp torpedo.cpp) -set(AIRCRAFT jet.cpp demoJet.cpp missileJet.cpp) set(FX explosion.cpp) -set(CONTENT player.cpp unitDataManager.cpp unit.cpp pathfinder.cpp map.cpp ${SHIPS} ${PROJECTILES} ${AIRCRAFT} ${FX}) +set(CONTENT player.cpp unitDataManager.cpp unit.cpp pathfinder.cpp map.cpp ${PROJECTILES} ${FX}) set(GAME_SRC ${STATES} ${GUI} ${CORE} ${CONTENT} ${UTIL}) set(SFML_DIR external/SFML) diff --git a/activeGameState.cpp b/activeGameState.cpp index dfc80eb..63423a5 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -15,13 +15,8 @@ #include "activeGameState.h" #include "inGameAppState.h" +#include "defConfigs.h" #include "util.h" -#include "cruiser.h" -#include "destroyer.h" -#include "submarine.h" -#include "aircraftCarrier.h" -#include "demoJet.h" -#include "missileJet.h" #include "tooltip.h" using namespace vb01; @@ -334,18 +329,6 @@ namespace battleship{ if (!isPressed) deselectUnits(); break; case Bind::TOGGLE_SUB: - if (isPressed) { - for (Unit *u : selectedUnits) { - if (u->getPlayer() == mainPlayer && u->getUnitClass() == UnitClass::SUBMARINE) { - Submarine *s = (Submarine*) u; - - if (s->isSubmerged()) - s->emerge(); - else - s->submerge(); - } - } - } break; case Bind::ZOOM_IN: if (zooms > -NUM_MAX_ZOOMS) { diff --git a/aircraftCarrier.cpp b/aircraftCarrier.cpp deleted file mode 100755 index 15e4af1..0000000 --- a/aircraftCarrier.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "aircraftCarrier.h" -#include "aircraftCarrierData.h" -#include "missileJet.h" -#include "demoJet.h" - -using namespace vb01; -using namespace std; - -namespace battleship{ - AircraftCarrier::AircraftCarrier(Player *player, Vector3 pos, int unitId) : Vessel(player,pos, unitId) { - maxNumJets = unitData::numJets[unitId]; - jets = new Jet*[maxNumJets]; - - for(int i=0;iid == 6) { - id = 9; - j = (MissileJet*)new MissileJet(player,getJetPos(slot), id); - } else { - id = 10; - j = (DemoJet*)new DemoJet(player,getJetPos(slot), id); - } - - j->setJetId(slot); - jets[slot]=j; - j->setAircraftCarrier(this); - player->addUnit(j); - } - } - - void AircraftCarrier::move(Order order, float offset) { - Unit::move(order, offset); - - for (int i=0;iisOnBoard()) { - Vector3 oP = j->getOffsetPos(); - //j->orientUnit(dirVec); - j->placeUnit(pos + leftVec * oP.x + upVec * oP.y - dirVec * oP.z); - } - } - } -} diff --git a/aircraftCarrier.h b/aircraftCarrier.h deleted file mode 100755 index 99be3fa..0000000 --- a/aircraftCarrier.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#ifndef AIRCRAFT_CARRIER_H -#define AIRCRAFT_CARRIER_H - -#include "vessel.h" -#include "jet.h" -#include "aircraftCarrierData.h" - -namespace battleship{ - class AircraftCarrier : public Vessel { - public: - AircraftCarrier(Player*, vb01::Vector3, int); - ~AircraftCarrier(); - void makeJet(); - inline void setJet(int i, Jet *j){this->jets[i]=j;} - inline int getMaxNumJets(){return maxNumJets;} - inline Jet* getJet(int i){return jets[i];} - inline Jet** getJets(){return jets;} - inline vb01::Vector3 getJetPos(int i){return pos + leftVec * unitData::jetPos[id][i].x - dirVec * unitData::jetPos[id][i].z + upVec * unitData::jetPos[id][i].y;} - inline float getRunwayLength(){return runwayLength;} - private: - Jet** jets; - int maxNumJets; - float runwayLength; - void move(Order, float = 0.); - }; -} - -#endif diff --git a/aircraftCarrierData.h b/aircraftCarrierData.h deleted file mode 100755 index e8a0362..0000000 --- a/aircraftCarrierData.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#ifndef AIRCRAFT_CARRIER_DATA_H -#define AIRCRAFT_CARRIER_DATA_H - -#include "unitData.h" - -/* -0:戦艦 -1:駆逐艦 -2:巡洋艦 -3:航空母艦 -4:潜水艦 -5: 戦闘機 -*/ - -namespace battleship { - namespace unitData { - const int numJets[numberOfUnits]{0, 0, 0, 0, 0, 0, 6, 6}; - const vb01::Vector3 jetPos[numberOfUnits][20]{ - {}, - {}, - {}, - {}, - {}, - {}, - { - vb01::Vector3(0.13674, 1.60034, 7.78832), - vb01::Vector3(1.5889, 1.60034, 7.78832), - vb01::Vector3(0.13674, 1.60034, 5.47672), - vb01::Vector3(1.5889, 1.60034, 5.47672), - vb01::Vector3(0.13674, 1.60034, 3.37257), - vb01::Vector3(1.5889, 1.60034, 3.37257) - }, - { - vb01::Vector3(-0.61651, 2.78469, 7.88753), - vb01::Vector3(0.40384, 2.78469, 7.88753), - vb01::Vector3(-0.61651, 2.78469, 5.82783), - vb01::Vector3(0.40384, 2.78469, 5.82783), - vb01::Vector3(-0.61651, 2.78469, 3.87186), - vb01::Vector3(0.40384, 2.78469, 3.87186) - } - }; - const float runwayLenght[numberOfUnits]{0, 0, 0, 0, 0, 0, 10, 10}; - } -} - -#endif diff --git a/consoleCommand.cpp b/consoleCommand.cpp index 182251d..ca52a83 100755 --- a/consoleCommand.cpp +++ b/consoleCommand.cpp @@ -6,23 +6,14 @@ #include #include "consoleCommand.h" -#include "unitData.h" -#include "aircraftCarrier.h" -#include "cruiser.h" -#include "destroyer.h" -#include "submarine.h" -#include "demoJet.h" -#include "missileJet.h" #include "inGameAppState.h" -#include "unitData.h" using namespace std; using namespace vb01; using namespace vb01Gui; +using namespace gameBase; namespace battleship{ - using namespace unitData; - void ConsoleCommand::execute(string commandStr) { vector fullCommand; parse(commandStr, fullCommand); @@ -38,70 +29,42 @@ namespace battleship{ } } - void ConsoleCommand::parse(string commandStr, vector &fullCommand){ - vector spaceIds; + void ConsoleCommand::parse(string commandStr, vector &fullCommand){ + vector spaceIds; - for(int i = 0; i < commandStr.length(); i++) - if(commandStr[i] == ' ') - spaceIds.push_back(i); + for(int i = 0; i < commandStr.length(); i++) + if(commandStr[i] == ' ') + spaceIds.push_back(i); - fullCommand.push_back(commandStr.substr(0, spaceIds[0])); + fullCommand.push_back(commandStr.substr(0, spaceIds[0])); - for(int i = 0; i < spaceIds.size(); i++){ - bool lastSpace = (i == spaceIds.size() - 1); - string argument = commandStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1); - fullCommand.push_back(argument); - } + for(int i = 0; i < spaceIds.size(); i++){ + bool lastSpace = (i == spaceIds.size() - 1); + string argument = commandStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1); + fullCommand.push_back(argument); } + } - void ConsoleCommand::validateAddUnitArguments(vector &fullCommand){ - //addUnit [ ] - if(fullCommand.size() != 3 || fullCommand.size() != 6) - return; + void ConsoleCommand::validateAddUnitArguments(vector &fullCommand){ + //addUnit [ ] + if(fullCommand.size() != 3 || fullCommand.size() != 6) + return; - int playerId = atoi(fullCommand[1].c_str()); + int playerId = atoi(fullCommand[1].c_str()); - if(playerId != 0 && playerId != 1) - return; + if(playerId != 0 && playerId != 1) + return; - int unitId = atoi(fullCommand[2].c_str()); + int unitId = atoi(fullCommand[2].c_str()); - if(!(0 <= unitId && unitId <= unitData::numberOfUnits)) - return; - } + if(!(0 <= unitId && unitId <= UnitDataManager::getSingleton()->getNumUnits())) + return; + } void ConsoleCommand::executeAddUnit(int playerId, int unitId, Vector3 pos) { - StateManager *stateManager = GameManager::getSingleton()->getStateManager(); - InGameAppState *inGameState = (InGameAppState*)stateManager->getAppStateByType(AppStateType::IN_GAME_STATE); + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + InGameAppState *inGameState = (InGameAppState*)stateManager->getAppStateByType(AppStateType::IN_GAME_STATE); vector players = inGameState->getPlayers(); - Unit *u = nullptr; - - switch (unitType[unitId]) { - case UNIT_TYPE::VESSEL: - u = new Unit(players[playerId], pos, unitId); - break; - case UNIT_TYPE::DESTROYER: - u = new Destroyer(players[playerId], pos, unitId); - break; - case UNIT_TYPE::CRUISER: - u = new Cruiser(players[playerId], pos, unitId); - break; - case UNIT_TYPE::SUBMARINE: - u = new Submarine(players[playerId], pos, unitId); - break; - case UNIT_TYPE::AIRCRAFT_CARRIER: - u = new AircraftCarrier(players[playerId], pos, unitId); - break; - case UNIT_TYPE::MISSILE_JET: - u = new MissileJet(players[playerId], pos, unitId, false); - break; - case UNIT_TYPE::DEMO_JET: - u = new DemoJet(players[playerId], pos, unitId, false); - break; - default: - break; - } - - players[playerId]->addUnit(u); + players[playerId]->addUnit(new Unit(players[playerId], pos, unitId)); } } diff --git a/cruiser.cpp b/cruiser.cpp deleted file mode 100755 index 03fa3e4..0000000 --- a/cruiser.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include - -#include "cruiser.h" -#include "cruiserData.h" -#include "guidedMissile.h" -#include "util.h" -#include "projectileData.h" - -using namespace vb01; - -namespace battleship{ - Cruiser::Cruiser(Player *player, Vector3 pos, int id) : Vessel(player, pos, id) { - guidedMissiles = unitData::maxGuidedMissiles[id]; - } - - void Cruiser::launch(Order order) { - if (guidedMissiles > 0) { - float angle = order.targets[0].pos.getAngleBetween(dirVec); - Quaternion rotQuat = Quaternion(dirVec.x < 0 ? angle : -angle, Vector3(0, 1, 0)); - Vector3 basePos = leftVec * projectileData::pos[getId()][1][guidedMissiles - 1].x + upVec * projectileData::pos[getId()][1][guidedMissiles - 1].y - dirVec * projectileData::pos[getId()][1][guidedMissiles - 1].z; - addProjectile(new GuidedMissile(this, pos + basePos, order.targets[0].pos, rotQuat * Vector3(0, 1, 0), rotQuat * Vector3(1, 0, 0), rotQuat * Vector3(0, 0, -1), getId(), 1, 0)); - removeOrder(0); - guidedMissiles--; - } - } -} diff --git a/cruiser.h b/cruiser.h deleted file mode 100755 index e8b82dc..0000000 --- a/cruiser.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#ifndef CRUISER_H -#define CRUISER_H - -#include "vessel.h" -#include "player.h" - -namespace battleship { - class Cruiser : public Vessel { - public: - Cruiser(Player*, vb01::Vector3, int); - void launch(Order); - private: - bool canFire(); - int guidedMissiles, reloadTime = 5000; - s64 lastUpdateTime; - }; -} - -#endif diff --git a/cruiserData.h b/cruiserData.h deleted file mode 100755 index bbeffc0..0000000 --- a/cruiserData.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#ifndef CRUISER_DATA_H -#define CRUISER_DATA_H - -#include "vesselData.h" - -namespace battleship{ - namespace unitData { - const int maxGuidedMissiles[numberOfUnits]{0, 0, 0, 0, 6, 2}; - } -} - -#endif diff --git a/demoJet.cpp b/demoJet.cpp deleted file mode 100755 index 2eb3cfb..0000000 --- a/demoJet.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include - -#include - -#include "demoJet.h" -#include "inGameAppState.h" -#include "util.h" - -using namespace vb01; - -namespace battleship{ - DemoJet::DemoJet(Player *player, Vector3 pos, int id, bool onBoard) : Jet(player, pos, id,onBoard) {} - - void DemoJet::attack(Order order){ - if(!onBoard){ - Unit::attack(order); - Vector3 target = order.targets[0].pos; - float angle = dirVec.getAngleBetween(target - pos); - Vector3 axis = dirVec.cross(target - pos); - - if(maxTurnAngle < angle / PI * 180){ - Quaternion rotQuat = Quaternion(maxTurnAngle / 180 * PI, axis); - //orientUnit(rotQuat*dirVec); - } - } - else - takeOff(); - } - - void DemoJet::update(){ - Jet::update(); - } -} diff --git a/demoJet.h b/demoJet.h deleted file mode 100755 index 9b23ed3..0000000 --- a/demoJet.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#ifndef DEMO_JET_H -#define DEMO_JET_H - -#include "jet.h" -#include "player.h" - -namespace battleship { - class DemoJet : public Jet { - public: - DemoJet(Player*, vb01::Vector3, int, bool = 1); - void attack(Order); - void update(); - private: - float length = 1; - int damage = 250; - }; -} - -#endif diff --git a/destroyer.cpp b/destroyer.cpp deleted file mode 100755 index 01fa521..0000000 --- a/destroyer.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include - -#include - -#include "destroyer.h" -#include "destroyerData.h" -#include "defConfigs.h" -#include "depthCharge.h" -#include "inGameAppState.h" -#include "projectileData.h" -#include "util.h" - -using namespace std; -using namespace vb01; - -namespace battleship{ - using namespace unitData; - - Destroyer::Destroyer(Player *player, vb01::Vector3 pos,int id) : Vessel(player, pos, id) { - this->maxDepthCharges = unitData::maxDepthCharges[id]; - depthCharges = maxDepthCharges; - this->rateOfDrops = unitData::rateOfDrops[id]; - this->reloadRate = unitData::reloadRate[id]; - this->maxDepthChargeDropRange = unitData::maxDepthChargeDropRange[id]; - } - - void Destroyer::update(){ - Vessel::update(); - reload(); - } - - void Destroyer::attack(Order order){ - Vector3 t = order.targets[0].pos; - bool sub = false; - InGameAppState *inGameState=((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE)); - - for(Player *p : inGameState->getPlayers()) - for(Unit *u : p->getUnits()) - if(u->getUnitClass() == UnitClass::SUBMARINE && order.targets[0].pos == u->getPos()) - sub = true; - - if(sub) - dropDepthCharge(); - else - Vessel::attack(order); - } - - void Destroyer::dropDepthCharge() { - if(canDropDepthCharge()){ - float angle = rand() % 360; - float radius = 1; - Vector3 offsetVec = Vector3(cos(angle), 0, sin(angle)) * radius; - addProjectile(new DepthCharge(this, pos + projectileData::pos[id][1][1] + offsetVec, dirVec, leftVec, upVec, id, 1, 0)); - lastDropTime = getTime(); - - if(depthCharges == maxDepthCharges) - reloadStartTime = getTime(); - - } - } - void Destroyer::reload(){ - if(depthCharges < maxDepthCharges){ - reloading = true; - - if(getTime() - reloadStartTime > reloadRate) { - reloadStartTime += reloadRate; - depthCharges++; - } - } - else - reloading = false; - } -} diff --git a/destroyer.h b/destroyer.h deleted file mode 100755 index 65060e1..0000000 --- a/destroyer.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#ifndef DESTROYER_H -#define DESTROYER_H - -#include "vessel.h" -#include "destroyerData.h" -#include "player.h" -#include "util.h" - -namespace battleship { - class Destroyer : public Vessel { - public: - Destroyer(Player*, vb01::Vector3, int); - void dropDepthCharge(); - void attack(Order); - void update(); - private: - void reload(); - inline bool canDropDepthCharge(){return vb01::getTime() - lastDropTime > rateOfDrops;} - bool reloading = false; - float maxDepthChargeDropRange; - int depthCharges, maxDepthCharges, rateOfDrops, reloadRate; - s64 lastShotTime = 0, reloadStartTime = 0, lastDropTime = 0; - }; -} - -#endif diff --git a/destroyerData.h b/destroyerData.h deleted file mode 100755 index 652ab0c..0000000 --- a/destroyerData.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#ifndef DESTROYER_DATA_H -#define DESTROYER_DATA_H - -#include "vesselData.h" - -namespace battleship{ - namespace unitData { - const int rateOfDrops[numberOfUnits]{0, 0, 500, 500}; - const int reloadRate[numberOfUnits]{0, 0, 2000, 2000}; - const int maxDepthCharges[numberOfUnits]{0, 0, 15, 15}; - const float maxDepthChargeDropRange[numberOfUnits]{0, 0, 3, 3}; - } -} - -#endif diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 3dc2fee..6f1349f 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -7,10 +7,10 @@ #include +#include "defConfigs.h" #include "inGameAppState.h" #include "unitDataManager.h" #include "consoleCommand.h" -#include "aircraftCarrier.h" #include "vessel.h" using namespace vb01; diff --git a/jet.cpp b/jet.cpp deleted file mode 100755 index 78d4a09..0000000 --- a/jet.cpp +++ /dev/null @@ -1,145 +0,0 @@ -#include -#include - -#include "jet.h" -#include "jetData.h" -#include "aircraftCarrier.h" - -using namespace vb01; - -namespace battleship{ - using namespace unitData; - - Jet::Jet(Player *player, Vector3 pos, int unitId, bool onBoard) : Unit(player, pos, unitId) { - offsetPos = pos; - this->onBoard = onBoard; - this->pitchSpeed = unitData::pitchSpeed[id]; - } - - void Jet::update() { - Unit::update(); - Vector3 lp = aircraftCarrier->getDirVec() * aircraftCarrier->getRunwayLength() + aircraftCarrier->getJetPos(jetId); - - if (!onBoard && aircraftCarrier) { - if (orders.size() == 0) { - Order o; - o.type = Order::TYPE::MOVE; - o.targets.push_back(Order::Target(nullptr, lp)); - setOrder(o); - } - - if (!landing - && aircraftCarrier - && pos.getDistanceFrom(lp) <= unitData::destinationOffset[id] - && orders[0].targets[0].pos == lp){ - float angle = dirVec.getAngleBetween(aircraftCarrier->getDirVec()), maxAngle = PI - anglePrecision[id] / 180 * PI; - - if(!toTurnPoint && anglePrecision[id] / 180 * PI < angle && angle < maxAngle){ - float radius = getCircleRadius(); - Vector3 carrierSideVec = aircraftCarrier->getLeftVec().norm(); - Vector3 jetSideVec = leftVec.norm(); - - if(carrierSideVec.getAngleBetween(dirVec) < PI / 2){ - carrierSideVec = -carrierSideVec; - jetSideVec = -jetSideVec; - } - - Vector3 tanPoint = pos + (jetSideVec + carrierSideVec) * radius; - float ang1 = (tanPoint - lp).getAngleBetween(carrierSideVec); - float ang2 = dirVec.getAngleBetween(-carrierSideVec); - float base = (tanPoint - lp).getLength() * cos(ang1); - float hyp = base / cos(ang2); - landingPos = (pos + dirVec * hyp); - toTurnPoint = true; - } - else if(PI - angle < anglePrecision[id] / 180 * PI){ - onBoard = true; - landing = true; - //orientUnit(-aircraftCarrier->getDirVec()); - } - } - } - } - - void Jet::move(Order order, float offset) { - if(toTurnPoint) - turnAround(); - else{ - if(!onBoard) - Unit::move(order, offset); - else{ - if(landing) - land(); - else - takeOff(); - } - } - } - - void Jet::lap(Vector3 destDir) { - Vector3 center = pos - leftVec * getCircleRadius(); - Vector3 initCircleDir = -leftVec; - float angle = initCircleDir.getAngleBetween(destDir); - angle = (Quaternion(angle, upVec) * initCircleDir == destDir ? angle : 2 * PI - angle); - Vector3 offsetDir = Quaternion(PI / 2, upVec) * destDir; - Vector3 destDirPos = Quaternion(angle, upVec) * dirVec; - Vector3 offsetDirPos = Quaternion(angle + PI / 2, upVec) * dirVec; - Vector3 hypVec = orders[0].targets[0].pos - destDirPos; - float triAngle = hypVec.getAngleBetween(offsetDir); - float distance = cos(triAngle) * hypVec.getLength(); - Vector3 destPoint = offsetDirPos + offsetDirPos.norm() * distance; - float dirAngle = dirVec.getAngleBetween(offsetDir) * 180 / PI; - float movementAmount; - turn(maxTurnAngle > dirAngle ? dirAngle : maxTurnAngle); - - if(dirAngle > unitData::anglePrecision[id]) - movementAmount = speed; - else{ - float destDistance = (destPoint - pos).getLength(); - movementAmount = (speed > destDistance ? destDistance : speed); - } - - advance(movementAmount); - } - - void Jet::takeOff() { - float distance = aircraftCarrier->getRunwayLength() - (pos - aircraftCarrier->getJetPos(jetId)).getLength(); - float movementAmmount = speed > distance ? distance : speed; - advance(movementAmmount); - distance -= movementAmmount; - - if (distance <= 0.) - onBoard = false; - } - - void Jet::land(){ - float distance = (pos - aircraftCarrier->getJetPos(jetId)).getLength(); - float movementAmmount = (speed > distance ? distance : speed); - advance(movementAmmount); - distance -= movementAmmount; - - if (distance <= 0.){ - landing = false; - - while(!orders.empty()) - orders.pop_back(); - } - } - - void Jet::turnAround() { - float distance=pos.getDistanceFrom(landingPos); - float movementAmmount = (speed > distance ? distance : speed); - advance(movementAmmount); - distance -= movementAmmount; - - if (distance <= 0.) - toTurnPoint = false; - } - - void Jet::pitch(float angle) { - Quaternion rotQuat = Quaternion(-angle / 180 * PI, leftVec); - dirVec = rotQuat * dirVec, upVec = rotQuat * upVec; - horAngle = angle; - float yAngle = model->getOrientation().y; - } -} diff --git a/jet.h b/jet.h deleted file mode 100755 index 265da92..0000000 --- a/jet.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#ifndef JET_H -#define JET_H - -#include - -#include "unit.h" -#include "player.h" -#include "util.h" - -namespace battleship{ - class AircraftCarrier; - - class Jet : public Unit { - public: - Jet(Player*, vb01::Vector3, int, bool); - virtual void update(); - inline void setJetId(int i){this->jetId = i;} - inline void setAircraftCarrier(AircraftCarrier *a){aircraftCarrier = a;} - inline bool isOnBoard(){return onBoard;} - inline int getJetId(){return jetId;} - inline AircraftCarrier* getAircraftCarrier(){return aircraftCarrier;} - inline vb01::Vector3 getOffsetPos(){return offsetPos;} - private: - int jetId; - vb01::Vector3 destDir,offsetPos; - float horAngle = 0, pitchSpeed; - void turnAround(); - void pitch(float); - void lap(vb01::Vector3); - protected: - AircraftCarrier *aircraftCarrier = nullptr; - vb01::Vector3 landingPos; - bool onBoard = true, toTurnPoint = false, landing = false; - void move(Order, float = 0.); - void takeOff(); - void land(); - }; -} - -#endif diff --git a/jetData.h b/jetData.h deleted file mode 100755 index 9600dac..0000000 --- a/jetData.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#ifndef JET_DATA_H -#define JET_DATA_H - -#include "unitData.h" - -/* -0:戦艦 -1:駆逐艦 -2:巡洋艦 -3:航空母艦 -4:潜水艦 -5: 戦闘機 -6:巡航ミサイル -*/ - -namespace battleship { - namespace unitData { - const float pitchSpeed[numberOfUnits]{0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5}; - } -} - -#endif diff --git a/missileJet.cpp b/missileJet.cpp deleted file mode 100755 index 0a7cbe4..0000000 --- a/missileJet.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include - -#include - -#include "missileJet.h" -#include "projectileData.h" -#include "inGameAppState.h" - -using namespace vb01; - -namespace battleship{ - using namespace unitData; - - MissileJet::MissileJet(Player *player, Vector3 pos, int id, bool onBoard) : Jet(player, pos, id, onBoard) {} - - void MissileJet::attack(Order order) { - if(!onBoard && canFire() && missilesInstalled){ - Vector3 target = order.targets[0].pos; - float distance=pos.getDistanceFrom(target); - - if(distance <= range){ - Vector3 targetPtr = Vector3::VEC_ZERO; - InGameAppState *inGameState = ((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE)); - - for(Player *p : inGameState->getPlayers()) - for(Unit *u : p->getUnits()){ - bool jet = (u->getUnitClass() == UnitClass::MISSILE_JET || u->getUnitClass() == UnitClass::DEMO_JET); - - if((jet && type == AAM) && (!jet && type == AWM) && u->getPos() == order.targets[0].pos) - targetPtr = u->getPos(); - } - - if(targetPtr != Vector3::VEC_ZERO) - targetPtr = target; - - fireMissile(targetPtr); - } - else - Unit::attack(order); - } - else if(onBoard) - takeOff(); - else - removeOrder(0); - } - - void MissileJet::fireMissile(Vector3 t) { - Vector3 p = pos + leftVec * projectileData::pos[id][0][missiles - 1].x + upVec * projectileData::pos[id][0][missiles - 1].y - dirVec * projectileData::pos[id][0][missiles - 1].z; - addProjectile(new Missile(this, missileNodes[missiles-1], t, p, dirVec, leftVec, upVec, id, 0, 0)); - missileNodes[missiles-1]->setParent(Root::getSingleton()->getRootNode()); - missileNodes[missiles-1] = nullptr; - missiles--; - lastFireTime=getTime(); - } -} diff --git a/missileJet.h b/missileJet.h deleted file mode 100755 index 02188a4..0000000 --- a/missileJet.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#ifndef MISSILE_JET_H -#define MISSILE_JET_H - -#include - -#include "jet.h" -#include "player.h" -#include "missile.h" - -namespace battleship { - class MissileJet : public Jet { - public: - MissileJet(Player*, vb01::Vector3, int, bool = 1); - void installMissiles(bool); - private: - void attack(Order); - void fireMissile(vb01::Vector3); - inline bool canFire() {return missiles > 0 && vb01::getTime() - lastFireTime > rateOfFire;} - MissileType type; - vb01::Node *missileNodes[2]{nullptr,nullptr}; - bool missilesInstalled = false; - int missiles = 0, rateOfFire = 2001; - s64 lastFireTime = 0; - }; -} - -#endif diff --git a/submarine.cpp b/submarine.cpp deleted file mode 100755 index 251fa23..0000000 --- a/submarine.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include - -#include "submarine.h" -#include "defConfigs.h" -#include "torpedo.h" -#include "projectileData.h" - -using namespace vb01; - -namespace battleship{ - Submarine::Submarine(Player *player, Vector3 pos, int id) : Unit(player, pos, id) {} - - void Submarine::attack(Order order) { - float angle = dirVec.getAngleBetween(order.targets[0].pos - pos); - - if(angle / PI * 180 > 50) - Unit::move(order, range); - - if (canFire()) { - Vector3 p = pos + leftVec * projectileData::pos[id][0][0].x + upVec * projectileData::pos[id][0][0].y - dirVec * projectileData::pos[id][0][0].z; - addProjectile(new Torpedo(this, p, dirVec, leftVec, upVec, getId(), 0, 0)); - lastShotTime = getTime(); - } - } - - void Submarine::emerge() { - submerged = false; - pos.y += 5; - model->setPosition(pos); - } - - void Submarine::submerge() { - submerged = true; - pos.y -= 5; - model->setPosition(pos); - } -} diff --git a/submarine.h b/submarine.h deleted file mode 100755 index e474b11..0000000 --- a/submarine.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#ifndef SUBMARINE_H -#define SUBMARINE_H - -#include - -#include "unit.h" -#include "player.h" - -namespace battleship{ - class Submarine : public Unit { - public: - Submarine(Player*, vb01::Vector3, int); - inline bool isSubmerged() {return submerged;} - void emerge(); - void submerge(); - private: - void attack(Order); - inline bool canFire(){return vb01::getTime() - lastShotTime > rateOfFire;} - bool submerged = false; - int rateOfFire=2000; - s64 lastShotTime=0; - }; -} - -#endif diff --git a/unit.cpp b/unit.cpp index 775c6d8..8b89e91 100755 --- a/unit.cpp +++ b/unit.cpp @@ -17,12 +17,14 @@ using namespace glm; using namespace vb01; +using namespace gameBase; using namespace std; namespace battleship{ Unit::Unit(Player *player, vb01::Vector3 pos, int id) { this->id = id; this->player = player; + UnitDataManager *udm = UnitDataManager::getSingleton(); health = udm->getHealth()[id]; maxTurnAngle = udm->getMaxTurnAngle()[id]; @@ -35,7 +37,17 @@ namespace battleship{ height = udm->getUnitCornerPoints()[id][4].y - udm->getUnitCornerPoints()[id][0].y; length = udm->getUnitCornerPoints()[id][3].z - udm->getUnitCornerPoints()[id][0].z; - GameManager *gm = GameManager::getSingleton(); + initModel(); + placeUnit(pos); + initSound(); + initUnitStats(); + } + + Unit::~Unit() { + } + + void Unit::initModel(){ + UnitDataManager *udm = UnitDataManager::getSingleton(); model = new Model(udm->getBasePath()[id] + udm->getMeshPath()[id]); Root *root = Root::getSingleton(); string libPath = root->getLibPath(); @@ -49,23 +61,29 @@ namespace battleship{ model->setMaterial(mat); root->getRootNode()->attachChild(model); - placeUnit(pos); + } + void Unit::initSound(){ + UnitDataManager *udm = UnitDataManager::getSingleton(); selectionSfxBuffer = new sf::SoundBuffer(); string p = GameManager::getSingleton()->getPath() + "Sounds/" + udm->getName()[id] + "s/selection.ogg"; if(selectionSfxBuffer->loadFromFile(p.c_str())) selectionSfx = new sf::Sound(*selectionSfxBuffer); + } - Node *guiNode = root->getGuiNode(); + void Unit::initUnitStats(){ + Root *root = Root::getSingleton(); hpBackground = new Quad(Vector3(lenHpBar, 10, 0), false); + string libPath = root->getLibPath(); Material *hpBackgroundMat = new Material(libPath + "gui"); hpBackgroundMat->addBoolUniform("texturingEnabled", false); hpBackgroundMat->addVec4Uniform("diffuseColor", Vector4(0, 0, 0, 1)); hpBackground->setMaterial(hpBackgroundMat); hpBackgroundNode = new Node(); hpBackgroundNode->attachMesh(hpBackground); + Node *guiNode = root->getGuiNode(); guiNode->attachChild(hpBackgroundNode); hpForeground = new Quad(Vector3(lenHpBar, 10, 0), false); @@ -76,10 +94,7 @@ namespace battleship{ hpForegroundNode = new Node(); hpForegroundNode->attachMesh(hpForeground); guiNode->attachChild(hpForegroundNode); - } - - Unit::~Unit() { - } + } void Unit::update() { leftVec = model->getGlobalAxis(0); @@ -110,10 +125,10 @@ namespace battleship{ } void Unit::displayUnitStats() { - float shiftedX = screenPos.x - 0.5 * lenHpBar; - hpForegroundNode->setPosition(Vector3(shiftedX, screenPos.y, 0)); - hpForeground->setSize(Vector3(health / UnitDataManager::getSingleton()->getHealth()[id] * lenHpBar, 10, 0)); - hpBackgroundNode->setPosition(Vector3(shiftedX, screenPos.y, .1)); + float shiftedX = screenPos.x - 0.5 * lenHpBar; + hpForegroundNode->setPosition(Vector3(shiftedX, screenPos.y, 0)); + hpForeground->setSize(Vector3(health / UnitDataManager::getSingleton()->getHealth()[id] * lenHpBar, 10, 0)); + hpBackgroundNode->setPosition(Vector3(shiftedX, screenPos.y, .1)); } void Unit::executeOrders() { @@ -125,7 +140,7 @@ namespace battleship{ attack(order); break; case Order::TYPE::MOVE: - move(order, 0.5 * Map::getSingleton()->getCellSize().x); + move(order); break; case Order::TYPE::PATROL: patrol(order); @@ -150,7 +165,7 @@ namespace battleship{ void Unit::attack(Order order) { } - void Unit::move(Order order, float destOffset) { + void Unit::navigate(Order order, float destOffset){ Vector3 hypVec = (pathPoints[0] - pos); float hypAngle = hypVec.norm().getAngleBetween(upVec) - PI / 2; float offset = hypVec.getLength() * sin(hypAngle); @@ -193,28 +208,34 @@ namespace battleship{ if(pathPoints.empty()) removeOrder(0); + } + void Unit::alignToSurface(){ + Map *map = Map::getSingleton(); + TerrainObject terr = map->getTerrainObject(0); vector res; - - if(type == UnitType::LAND){ - Map *map = Map::getSingleton(); - TerrainObject terr = map->getTerrainObject(0); - Ray::retrieveCollisions(Vector3(pos.x, terr.size.y, pos.z), Vector3(0, -1, 0), terr.node, res); - Ray::sortResults(res); - - if(!res.empty()){ - placeUnit(res[0].pos); - - float angle = upVec.getAngleBetween(res[0].norm); - Vector3 axis = upVec.cross(res[0].norm).norm(); - Quaternion rotQuat = Quaternion(angle, axis); - orientUnit(rotQuat * rot); - } + Ray::retrieveCollisions(Vector3(pos.x, terr.size.y, pos.z), Vector3(0, -1, 0), terr.node, res); + Ray::sortResults(res); + + if(!res.empty()){ + placeUnit(res[0].pos); + + float angle = upVec.getAngleBetween(res[0].norm); + Vector3 axis = upVec.cross(res[0].norm).norm(); + Quaternion rotQuat = Quaternion(angle, axis); + orientUnit(rotQuat * rot); } + } + + void Unit::move(Order order) { + navigate(order, 0.5 * Map::getSingleton()->getCellSize().x); + + if(type == UnitType::LAND) + alignToSurface(); } void Unit::patrol(Order order) { - move(order, UnitDataManager::getSingleton()->getDestinationOffset()[id]); + move(order); } void Unit::launch(Order order) {} @@ -251,15 +272,6 @@ namespace battleship{ patrolPointId = 0; } - float Unit::getCircleRadius() { - float radius = 0.; - - for (int i = 0; i <= 90 / maxTurnAngle; i++) - radius += cos(i * (maxTurnAngle / 180 * PI)) * speed; - - return radius; - } - void Unit::placeUnit(Vector3 p) { model->setPosition(p); pos = p; @@ -286,7 +298,7 @@ namespace battleship{ return projectiles; } - void Unit::addOrder(Order order){ + void Unit::preparePathpoints(Order order){ int srcObjId = 0, destObjId = 0; Map *map = Map::getSingleton(); @@ -315,12 +327,16 @@ namespace battleship{ if(!impassibleNodePresent && weights[path[i - 1]][path[i]] == pathfinder->getImpassibleNodeVal()) impassibleNodePresent = true; - if(!(impassibleNodePresent || path.empty())){ + if(!(impassibleNodePresent || path.empty())) for(int p : path) pathPoints.push_back(map->getTerrainObject(srcObjId).cells[p].pos); + } + void Unit::addOrder(Order order){ + preparePathpoints(order); + + if(!pathPoints.empty()) orders.push_back(order); - } } void Unit::removeOrder(int id) { @@ -337,5 +353,8 @@ namespace battleship{ orderLineDispTime = getTime(); } - void Unit::addProjectile(Projectile *p) {((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE))->addProjectile(p);} + void Unit::addProjectile(Projectile *p) { + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + ((InGameAppState*)stateManager->getAppStateByType((int)AppStateType::IN_GAME_STATE))->addProjectile(p); + } } diff --git a/unit.h b/unit.h index 939b245..04e5fe9 100755 --- a/unit.h +++ b/unit.h @@ -14,10 +14,10 @@ #include "projectile.h" namespace vb01{ - class Mesh; - class Quad; - class Node; - class Camera; + class Mesh; + class Quad; + class Node; + class Camera; } namespace battleship{ @@ -26,20 +26,20 @@ namespace battleship{ struct Order { enum class TYPE {ATTACK, MOVE, PATROL, LAUNCH}; - struct Target{ - Unit *unit = nullptr; - vb01::Vector3 pos; + struct Target{ + Unit *unit = nullptr; + vb01::Vector3 pos; - Target(){} + Target(){} - Target(Unit *unit, vb01::Vector3 pos){ - this->unit = unit; - this->pos = pos; - } - }; + Target(Unit *unit, vb01::Vector3 pos){ + this->unit = unit; + this->pos = pos; + } + }; TYPE type; - vb01::LineRenderer::Line line; + vb01::LineRenderer::Line line; std::vector targets; }; @@ -59,7 +59,6 @@ namespace battleship{ void placeUnit(vb01::Vector3); void orientUnit(vb01::Quaternion); void addProjectile(Projectile*); - float getCircleRadius(); vb01::Vector3 getCorner(int); std::vector getProjectiles(); void addOrder(Order); @@ -86,8 +85,14 @@ namespace battleship{ inline vb01::Vector3 getLeftVec() {return leftVec;} inline vb01::Vector3 getUpVec() {return upVec;} private: + void initModel(); + void initSound(); + void initUnitStats(); void updateScreenCoordinates(); void displayUnitStats(); + void navigate(Order, float = 0.); + void alignToSurface(); + void preparePathpoints(Order); inline int getNextPatrolPointId(int numPoints) {return patrolPointId == numPoints - 1 ? 0 : patrolPointId + 1;} inline bool canDisplayOrderLine(){return vb01::getTime() - orderLineDispTime < orderVecDispLength;} @@ -104,7 +109,7 @@ namespace battleship{ UnitType type; vb01::Vector2 screenPos; std::vector orders; - 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); + 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); vb01::Quaternion rot = vb01::Quaternion::QUAT_W; int health, cost, id, patrolPointId = 0, playerId; s64 orderLineDispTime = 0; @@ -115,7 +120,7 @@ namespace battleship{ void removeOrder(int); virtual void executeOrders(); virtual void attack(Order); - virtual void move(Order, float = 0.); + virtual void move(Order); virtual void patrol(Order); virtual void launch(Order); virtual void turn(float);