first submarine submerging iteration

This commit is contained in:
devZoGok
2022-09-16 22:22:25 +03:00
parent 723994652f
commit 11afaa32d3
7 changed files with 100 additions and 42 deletions
+2 -2
View File
@@ -6,7 +6,7 @@ UnitType = {UNDERWATER = 0, SEA_LEVEL = 1, LAND = 2, AIR = 3};
unitClass = {
UnitClass.VESSEL, UnitClass.VESSEL,
UnitClass.DESTROYER, UnitClass.DESTROYER,
UnitClass.VESSEL, UnitClass.DESTROYER,
UnitClass.CRUISER, UnitClass.CRUISER,
UnitClass.AIRCRAFT_CARRIER, UnitClass.AIRCRAFT_CARRIER,
UnitClass.SUBMARINE,
@@ -14,7 +14,7 @@ unitClass = {
};
unitType = {
UnitType.SEA_LEVEL, UnitType.LAND,
UnitType.SEA_LEVEL, UnitType.UNDERWATER,
UnitType.SEA_LEVEL, UnitType.SEA_LEVEL,
UnitType.SEA_LEVEL, UnitType.SEA_LEVEL,
UnitType.SEA_LEVEL, UnitType.SEA_LEVEL,
+46 -16
View File
@@ -3,6 +3,7 @@
#include <model.h>
#include <material.h>
#include <quad.h>
#include <text.h>
#include <ray.h>
#include <stateManager.h>
@@ -58,6 +59,16 @@ namespace battleship{
dragboxNode = new Node();
dragboxNode->attachMesh(dragbox);
root->getGuiNode()->attachChild(dragboxNode);
Text *t = new Text(GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", L"depth: ");
Material *tm = new Material(root->getLibPath() + "text");
tm->addBoolUniform("texturingEnabled", false);
tm->addVec4Uniform("diffuseColor", Vector4(1, 1, 1, 1));
t->setMaterial(tm);
textNode = new Node(Vector3(0, 100, 0));
textNode->addText(t);
depth = 1;
}
ActiveGameState::~ActiveGameState() {
@@ -65,12 +76,17 @@ namespace battleship{
void ActiveGameState::onAttached() {
AbstractAppState::onAttached();
Root::getSingleton()->getGuiNode()->attachChild(textNode);
}
void ActiveGameState::onDettached() { AbstractAppState::onDettached();
void ActiveGameState::onDettached() {
AbstractAppState::onDettached();
Root::getSingleton()->getGuiNode()->attachChild(textNode);
}
void ActiveGameState::update() {
textNode->getText(0)->setText(L"Depth: " + to_wstring(depth));
if (isSelectionBox)
updateSelectionBox();
@@ -222,14 +238,26 @@ namespace battleship{
targets[i + 1] = targets[i];
LineRenderer *lineRenderer = LineRenderer::getSingleton();
Map *map = Map::getSingleton();
for (int i = 0; i < selectedUnits.size(); ++i) {
Unit *u = selectedUnits[i];
if(u->getType() == UnitType::UNDERWATER && o.type == Order::TYPE::MOVE){
for(int j = 0; j < map->getNumWaterBodies(); j++){
WaterBody w = map->getWaterBody(j);
if(w.isPointWithin(u->getPos()) && w.isPointWithin(targets[i].pos)){
targets[i].pos.y = -map->getBottom() + depth * fabs(w.pos.y - map->getBottom());
break;
}
}
}
lineRenderer->addLine(u->getPos(), targets[i].pos, color);
vector<LineRenderer::Line> lines = lineRenderer->getLines();
o.line = lines[lines.size() - 1];
if (type != Order::TYPE::LAUNCH || (u->getId() == 4 || u->getId() == 5)) {
if(type == Order::TYPE::PATROL){
Vector3 p = u->getPos();
@@ -247,23 +275,23 @@ namespace battleship{
}
void ActiveGameState::addTarget() {
Camera *cam = Root::getSingleton()->getCamera();
Vector3 camPos = cam->getPosition();
Vector3 endPos = screenToSpace(getCursorPos());
Camera *cam = Root::getSingleton()->getCamera();
Vector3 camPos = cam->getPosition();
Vector3 endPos = screenToSpace(getCursorPos());
vector<Ray::CollisionResult> results;
Vector3 rayDir = (endPos - camPos).norm();
Ray::retrieveCollisions(camPos, rayDir, Map::getSingleton()->getNodeParent(), results);
std::map<Node*, Unit*> unitData;
vector<Ray::CollisionResult> results;
Vector3 rayDir = (endPos - camPos).norm();
Ray::retrieveCollisions(camPos, rayDir, Map::getSingleton()->getNodeParent(), results);
std::map<Node*, Unit*> unitData;
Ray::sortResults(results);
Ray::sortResults(results);
if(!results.empty()){
vector<Node*> ancestors = results[0].mesh->getNode()->getAncestors();
Node *node = ancestors[ancestors.size() - 2];
std::map<Node*, Unit*>::iterator it = unitData.find(node);
targets.push_back(Order::Target((it != unitData.end() ? unitData[node] : nullptr), results[0].pos));
}
if(!results.empty()){
vector<Node*> ancestors = results[0].mesh->getNode()->getAncestors();
Node *node = ancestors[ancestors.size() - 2];
std::map<Node*, Unit*>::iterator it = unitData.find(node);
targets.push_back(Order::Target((it != unitData.end() ? unitData[node] : nullptr), results[0].pos));
}
}
void ActiveGameState::onAction(int bind, bool isPressed) {
@@ -334,9 +362,11 @@ namespace battleship{
break;
case Bind::LEFT_CONTROL:
controlPressed = isPressed;
if(isPressed) depth -= 0.05;
break;
case Bind::LEFT_SHIFT:
shiftPressed = isPressed;
if(isPressed) depth += 0.05;
break;
case Bind::SELECT_PATROL_POINTS:
selectingPatrolPoints = isPressed;
+8 -7
View File
@@ -25,28 +25,29 @@ namespace battleship{
inline std::vector<Unit*>& getSelectedUnits(){return selectedUnits;}
inline std::vector<Unit*>& getUnitGroup(int i){return unitGroups[i];}
private:
void deselectUnits();
void deselectUnits();
void renderUnits();
void updateCameraPosition();
void updateSelectionBox();
void addTarget();
void issueOrder(Order::TYPE, bool);
void lookAround(bool);
void orientCamera(vb01::Vector3, double);
void orientCamera(vb01::Vector3, double);
bool isInLineOfSight(vb01::Vector3, float, Unit*);
GuiAppState *guiState;
std::vector<Player*> players;
Player *mainPlayer;
vb01::Quad *dragbox = nullptr;
vb01::Node *dragboxNode = nullptr;
vb01::Vector2 clickPoint;
vb01::Quad *dragbox = nullptr;
vb01::Node *dragboxNode = nullptr, *textNode = nullptr;
vb01::Vector2 clickPoint;
std::vector<vb01::Vector2> unitScreenPosVec;
std::vector<vb01::Node*> unitLightNodes;
std::vector<Unit*> unitGroups[9], selectedUnits;
std::vector<Order::Target> targets;
std::vector<Order::Target> targets;
bool isSelectionBox = false, shiftPressed = false, controlPressed = false, selectingPatrolPoints = false, selectingGuidedMissileTarget = false,lookingAround=false;
bool isInLineOfSight(vb01::Vector3, float, Unit*);
int playerId, zooms = 0;
float depth = 1;
};
}
+7 -4
View File
@@ -76,7 +76,6 @@ namespace battleship{
mat->addTexUniform("textures[0]", t, true);
model->setMaterial(mat);
root->getRootNode()->attachChild(model);
nodeParent->attachChild(model);
terrainModel = model;
}
@@ -92,7 +91,8 @@ namespace battleship{
float sizeY = luaManager->getFloatFromTable(table, vector<Index>{Index("sizeY", true)});
Quad *quad = new Quad(Vector3(sizeX, sizeY, 1) * 1, true);
Material *mat = new Material(root->getLibPath() + "texture");
mat->addBoolUniform("texturingEnabled", true);
mat->addBoolUniform("texturingEnabled", false);
mat->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 0.5));
mat->addBoolUniform("lightingEnabled", false);
string fr[]{GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + luaManager->getStringFromTable(table, vector<Index>{Index("albedoMap", true)})};
@@ -167,8 +167,11 @@ namespace battleship{
return (numCellsX * numCellsZ * y + (numCellsX * z + x));
}
bool Map::isPointWithin(int id, Vector3 point, Vector3 cellSize){
bool Map::isPointWithin(int id, Vector3 point, Vector3 cellSize, bool cellSpatial){
Vector3 cellPos = getCellPos(id, cellSize);
return ((fabs(point.x - cellPos.x) < 0.5 * cellSize.x) && (fabs(point.z - cellPos.z) < 0.5 * cellSize.z));
bool withinX = (fabs(point.x - cellPos.x) < 0.5 * cellSize.x);
bool withinY = (cellSpatial ? (fabs(point.y - cellPos.y) < 0.5 * cellSize.y) : true);
bool withinZ = (fabs(point.z - cellPos.z) < 0.5 * cellSize.z);
return withinX && withinY && withinZ;
}
}
+4 -2
View File
@@ -34,19 +34,21 @@ namespace battleship{
void unload();
vb01::Vector3 getCellPos(int, vb01::Vector3);
int getCellId(vb01::Vector3, vb01::Vector3);
bool isPointWithin(int, vb01::Vector3, vb01::Vector3);
bool isPointWithin(int, vb01::Vector3, vb01::Vector3, bool = false);
inline WaterBody getWaterBody(int i){return waterBodies[i];}
inline int getNumWaterBodies(){return waterBodies.size();}
inline vb01::Model* getTerrainModel(){return terrainModel;}
inline vb01::Node* getNodeParent(){return nodeParent;}
inline vb01::Vector3 getSize(){return size;}
inline float getBottom(){return bottom;}
private:
vb01::Node *nodeParent = nullptr;
vb01::Model *terrainModel = nullptr;
std::string mapName;
std::vector<WaterBody> waterBodies;
//TODO replace hardcoded values
vb01::Vector3 size = vb01::Vector3(100, 33, 100);
vb01::Vector3 size = vb01::Vector3(100, 20, 100);
float bottom = 14;
Map(){}
void loadSkybox(gameBase::LuaManager*);
+31 -8
View File
@@ -151,26 +151,34 @@ namespace battleship{
}
void Unit::move(Order order, float destOffset) {
Vector3 pointDir = (pathPoints[0] - pos).norm();
float angle = dirVec.getAngleBetween(pointDir);
Vector3 linDest = Vector3(pathPoints[0].x, pos.y, pathPoints[0].z);
Vector3 destDir = (linDest - pos).norm();
float angle = dirVec.getAngleBetween(destDir);
UnitDataManager *udm = UnitDataManager::getSingleton();
if(angle > udm->getAnglePrecision()[id]){
float rotSpeed = (maxTurnAngle > angle ? angle : maxTurnAngle);
if(leftVec.getAngleBetween(pointDir) > PI / 2)
if(leftVec.getAngleBetween(destDir) > PI / 2)
rotSpeed *= -1;
turn(rotSpeed);
}
else{
if(pos.getDistanceFrom(pathPoints[0]) > destOffset){
if(pos.getDistanceFrom(linDest) > destOffset){
float dist = pos.getDistanceFrom(pathPoints[0]);
float movementAmmount = (speed > dist ? dist : speed);
advance(movementAmmount);
}
else
pathPoints.erase(pathPoints.begin());
if(fabs(pos.y - pathPoints[0].y) > destOffset){
float dist = pos.getDistanceFrom(pathPoints[0]);
float movementAmmount = -(speed > dist ? dist : speed);
advance(movementAmmount, MoveDir::UP);
}
}
if(pathPoints.empty())
@@ -183,8 +191,22 @@ namespace battleship{
void Unit::launch(Order order) {}
void Unit::advance(float speed) {
pos = pos + dirVec * speed;
void Unit::advance(float speed, MoveDir moveDir) {
Vector3 dir;
switch(moveDir){
case MoveDir::FORW:
dir = dirVec;
break;
case MoveDir::LEFT:
dir = leftVec;
break;
case MoveDir::UP:
dir = upVec;
break;
}
pos = pos + dir * speed;
model->setPosition(pos);
}
@@ -293,16 +315,17 @@ namespace battleship{
int numVerts = 3 * meshData.numTris;
for(int k = 0; k < numVerts; k++){
bool pointWithin = map->isPointWithin(j, verts[k].pos, cellSize);
bool pointWithin = map->isPointWithin(j, verts[k].pos, cellSize, type == UnitType::UNDERWATER);
WaterBody waterbody;
if(waterbodyId != -1)
waterbody = map->getWaterBody(waterbodyId);
bool impassibleForSeaUnits = (type == UnitType::SEA_LEVEL && pointWithin && waterbodyId != -1 && verts[k].pos.y > waterbody.pos.y);
bool impassibleForSubs = (type == UnitType::UNDERWATER && pointWithin && waterbodyId != -1);
bool impassibleForLandUnits = (type == UnitType::LAND && pointWithin && (waterbodyId != -1 && verts[k].pos.y < waterbody.pos.y));
if(impassibleForSeaUnits || impassibleForLandUnits){
if(impassibleForSeaUnits || impassibleForSubs || impassibleForLandUnits){
weight = impassibleNodeVal;
break;
}
+2 -3
View File
@@ -43,7 +43,7 @@ namespace battleship{
std::vector<Target> targets;
};
enum class MoveDir {FORWARD, LEFT, RIGHT};
enum class MoveDir {LEFT, UP, FORW};
enum class Corner {FRONT_LEFT, FRONT_RIGHT, REAR_LEFT, REAR_RIGHT};
@@ -101,7 +101,6 @@ namespace battleship{
std::vector<vb01::Vector3> pathPoints;
protected:
Player *player;
MoveDir moveDir = MoveDir::FORWARD;
UnitClass unitClass;
UnitType type;
vb01::Vector2 screenPos;
@@ -121,7 +120,7 @@ namespace battleship{
virtual void patrol(Order);
virtual void launch(Order);
virtual void turn(float);
virtual void advance(float);
virtual void advance(float, MoveDir = MoveDir::FORW);
void drawCuboid();
};
}