diff --git a/CMakeLists.txt b/CMakeLists.txt index f0c283d..404add4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ cmake_policy(SET CMP0015 NEW) set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppState.cpp) set(UTIL util.cpp binds.h) set(GUI tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) -set(CORE gameManager.cpp defConfigs.cpp) +set(CORE gameManager.cpp defConfigs.cpp cameraController.cpp) set(PROJECTILES projectile.cpp) set(FX explosion.cpp) set(VEHICLES vehicle.cpp engineer.cpp) diff --git a/activeGameState.cpp b/activeGameState.cpp index e9c397d..4826ff2 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -20,6 +20,7 @@ #include "structure.h" #include "util.h" #include "tooltip.h" +#include "cameraController.h" using namespace vb01; using namespace vb01Gui; @@ -114,8 +115,10 @@ namespace battleship{ renderUnits(); - if(!lookingAround) - updateCameraPosition(); + CameraController *camCtr = CameraController::getSingleton(); + + if(!camCtr->isLookingAround()) + camCtr->updateCameraPosition(); if(placingStructures) updateStructureFrames(); @@ -270,32 +273,6 @@ namespace battleship{ } } - void ActiveGameState::updateCameraPosition() { - GameManager *gm = GameManager::getSingleton(); - Vector2 cursorPos = getCursorPos(); - - Camera *cam = Root::getSingleton()->getCamera(); - Vector3 camDir = cam->getDirection(); - Vector3 forwVec = Vector3(camDir.x, 0, camDir.z).norm(); - Vector3 camLeft = cam->getLeft(); - camLeft = Vector3(camLeft.x, 0, camLeft.z).norm(); - - int width, height; - glfwGetWindowSize(Root::getSingleton()->getWindow(), &width, &height); - - if (cursorPos.x <= 0 && 0 < cursorPos.y && cursorPos.y < height) - cam->setPosition(cam->getPosition() - camLeft * camPanSpeed); - - if (cursorPos.x >= width && 0 < cursorPos.y && cursorPos.y < height) - cam->setPosition(cam->getPosition() + camLeft * camPanSpeed); - - if (cursorPos.y <= 0 && 0 < cursorPos.x && cursorPos.x < width) - cam->setPosition(cam->getPosition() + forwVec * camPanSpeed); - - if (cursorPos.y >= height && 0 < cursorPos.x && cursorPos.x < width) - cam->setPosition(cam->getPosition() - forwVec * camPanSpeed); - } - void ActiveGameState::issueOrder(Order::TYPE type, bool addOrder) { Vector3 color; @@ -445,7 +422,7 @@ namespace battleship{ break; case Bind::LOOK_AROUND: if(!placingStructures) - lookingAround = isPressed; + CameraController::getSingleton()->setLookingAround(isPressed); break; case Bind::HALT: for (Unit *u : mainPlayer->getSelectedUnits()) @@ -543,29 +520,23 @@ namespace battleship{ structureFrames.clear(); } - void ActiveGameState::orientCamera(Vector3 rotAxis, double str){ - float minStr = .001; - Quaternion rotQuat = Quaternion(.5 * (fabs(str) > minStr ? str : 0), rotAxis); - Camera *cam = Root::getSingleton()->getCamera(); - Vector3 dir = rotQuat * cam->getDirection(), up = rotQuat * cam->getUp(); - cam->lookAt(dir, up); - } - void ActiveGameState::onAnalog(int bind, float strength) { + CameraController *camCtr = CameraController::getSingleton(); + switch((Bind)bind){ case Bind::LOOK_UP: case Bind::LOOK_DOWN: - if(lookingAround) { + if(camCtr->isLookingAround()) { Vector3 dirProj = Root::getSingleton()->getCamera()->getDirection(); dirProj = Vector3(dirProj.x, 0, dirProj.z).norm(); - orientCamera(Vector3(0, 1, 0).cross(dirProj), strength); + CameraController::getSingleton()->orientCamera(Vector3(0, 1, 0).cross(dirProj), strength); } break; case Bind::LOOK_LEFT: case Bind::LOOK_RIGHT: - if(lookingAround) - orientCamera(Vector3(0, 1, 0), strength); + if(camCtr->isLookingAround()) + CameraController::getSingleton()->orientCamera(Vector3(0, 1, 0), strength); else if(placingStructures && rotatingStructure) for(StructureFrame &s : structureFrames) if(s.status != StructureFrame::PLACED){ diff --git a/activeGameState.h b/activeGameState.h index a91b43d..76c3abe 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -40,13 +40,10 @@ namespace battleship{ void initDepthText(); void deselectUnits(); void renderUnits(); - void updateCameraPosition(); void updateSelectionBox(); void updateStructureFrames(); void addTarget(); void issueOrder(Order::TYPE, bool); - void lookAround(bool); - void orientCamera(vb01::Vector3, double); void removeStructtureFrames(); bool isInLineOfSight(vb01::Vector3, float, Unit*); @@ -61,7 +58,7 @@ namespace battleship{ std::vector unitLightNodes; std::vector unitGroups[9]; std::vector targets; - bool isSelectionBox = false, shiftPressed = false, controlPressed = false, selectingPatrolPoints = false, selectingGuidedMissileTarget = false, lookingAround = false, placingStructures = false, rotatingStructure = false; + bool isSelectionBox = false, shiftPressed = false, controlPressed = false, selectingPatrolPoints = false, selectingGuidedMissileTarget = false, placingStructures = false, rotatingStructure = false; int playerId, zooms = 0; const int NUM_MAX_ZOOMS = 10; float depth = 1; diff --git a/cameraController.cpp b/cameraController.cpp new file mode 100644 index 0000000..2409e01 --- /dev/null +++ b/cameraController.cpp @@ -0,0 +1,54 @@ +#include "cameraController.h" +#include "defConfigs.h" + +#include +#include +#include + +namespace battleship{ + using namespace vb01; + using namespace configData; + + static CameraController *cameraController = nullptr; + + CameraController* CameraController::getSingleton(){ + if(!cameraController) + cameraController = new CameraController(); + + return cameraController; + } + + void CameraController::updateCameraPosition() { + GameManager *gm = GameManager::getSingleton(); + Vector2 cursorPos = getCursorPos(); + + Camera *cam = Root::getSingleton()->getCamera(); + Vector3 camDir = cam->getDirection(); + Vector3 forwVec = Vector3(camDir.x, 0, camDir.z).norm(); + Vector3 camLeft = cam->getLeft(); + camLeft = Vector3(camLeft.x, 0, camLeft.z).norm(); + + int width, height; + glfwGetWindowSize(Root::getSingleton()->getWindow(), &width, &height); + + if (cursorPos.x <= 0 && 0 < cursorPos.y && cursorPos.y < height) + cam->setPosition(cam->getPosition() - camLeft * camPanSpeed); + + if (cursorPos.x >= width && 0 < cursorPos.y && cursorPos.y < height) + cam->setPosition(cam->getPosition() + camLeft * camPanSpeed); + + if (cursorPos.y <= 0 && 0 < cursorPos.x && cursorPos.x < width) + cam->setPosition(cam->getPosition() + forwVec * camPanSpeed); + + if (cursorPos.y >= height && 0 < cursorPos.x && cursorPos.x < width) + cam->setPosition(cam->getPosition() - forwVec * camPanSpeed); + } + + void CameraController::orientCamera(Vector3 rotAxis, double str){ + float minStr = .001; + Quaternion rotQuat = Quaternion(.5 * (fabs(str) > minStr ? str : 0), rotAxis); + Camera *cam = Root::getSingleton()->getCamera(); + Vector3 dir = rotQuat * cam->getDirection(), up = rotQuat * cam->getUp(); + cam->lookAt(dir, up); + } +} diff --git a/cameraController.h b/cameraController.h new file mode 100644 index 0000000..c17abc3 --- /dev/null +++ b/cameraController.h @@ -0,0 +1,21 @@ +#ifndef CAMERA_CONTROLLER_H +#define CAMERA_CONTROLLER_H + +#include + +namespace battleship{ + class CameraController{ + public: + static CameraController* getSingleton(); + void updateCameraPosition(); + void orientCamera(vb01::Vector3, double); + inline bool isLookingAround(){return lookingAround;} + inline void setLookingAround(bool l){lookingAround = l;} + private: + CameraController(){} + + bool lookingAround = false; + }; +} + +#endif diff --git a/defConfigs.h b/defConfigs.h index 88b0913..b4590df 100755 --- a/defConfigs.h +++ b/defConfigs.h @@ -20,7 +20,7 @@ namespace battleship{ const int maxNumGroups = 10; const static int numAppStates = 4; - const static int numStaticBinds[numAppStates]{6, 0, 5, 0}; + const static int numStaticBinds[numAppStates]{6, 0, 5, 4}; const static int numConfBinds[numAppStates]{0, 1, 22, 0}; const static int maxStaticBinds = 6; const static int maxConfBinds = 22; @@ -52,7 +52,13 @@ namespace battleship{ Bind::LOOK_RIGHT, Bind::LOOK_AROUND, }, - {} + { + Bind::LOOK_UP, + Bind::LOOK_DOWN, + Bind::LOOK_LEFT, + Bind::LOOK_RIGHT, + Bind::LOOK_AROUND, + } }; const static Bind confBinds[numAppStates][maxConfBinds]{ {}, @@ -83,7 +89,7 @@ namespace battleship{ Bind::SELECT_STRUCTURE, Bind::DESELECT_STRUCTURE }, - {} + {} }; const static int staticTriggers[numAppStates][maxStaticBinds]{ @@ -103,7 +109,13 @@ namespace battleship{ Mapping::MOUSE_AXIS_RIGHT, 0 }, - {} + { + Mapping::MOUSE_AXIS_UP, + Mapping::MOUSE_AXIS_DOWN, + Mapping::MOUSE_AXIS_LEFT, + Mapping::MOUSE_AXIS_RIGHT, + 0 + } }; const static int confTriggers[numAppStates][maxConfBinds]{ {}, @@ -134,14 +146,14 @@ namespace battleship{ GLFW_KEY_B, GLFW_KEY_ESCAPE }, - {} + {} }; const static bool isStaticKey[numAppStates][maxStaticBinds]{ {0, 1, 1, 1, 1, 1}, {}, {0, 0, 0, 0, 1}, - {} + {0, 0, 0, 0, 1} }; const static bool isConfKey[numAppStates][maxConfBinds]{ {}, diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index 611702a..a5b88bd 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -1,6 +1,7 @@ #include "mapEditorAppState.h" #include "gameManager.h" #include "defConfigs.h" +#include "cameraController.h" #include "map.h" #include "mesh.h" #include "util.h" @@ -8,89 +9,115 @@ #include namespace battleship{ - using namespace configData; - using namespace vb01; - using namespace gameBase; - using namespace std; + using namespace configData; + using namespace vb01; + using namespace gameBase; + using namespace std; - MapEditorAppState::MapEditor::MapEditor(string name, Vector2 size){ - map = Map::getSingleton(); - map->load(name, true); + MapEditorAppState::MapEditor::MapEditor(string name, Vector2 size){ + map = Map::getSingleton(); + map->load(name, true); - generatePlane(size); + generatePlane(size); + } + + void MapEditorAppState::MapEditor::generatePlane(Vector2 size){ + int numSideVerts = 100, numIndices = 6 * (numSideVerts - 1) * (numSideVerts - 1); + MeshData::Vertex *vertices = new MeshData::Vertex[numSideVerts * numSideVerts]; + u32 *indices = new u32[numIndices]; + + for(int i = 0; i < numSideVerts; i++){ + for(int j = 0; j < numSideVerts; j++){ + MeshData::Vertex vert; + + float x = size.x * (2 * j - numSideVerts) / (2 * numSideVerts); + float z = size.y * (numSideVerts - 2 * i) / (2 * numSideVerts); + + vert.pos = Vector3(x, 0, z); + vert.uv = Vector2((float)(j / numSideVerts), (float)(i / numSideVerts)); + vert.tan = Vector3::VEC_I; + vert.biTan = Vector3::VEC_K; + vert.norm = vert.tan.cross(vert.biTan); + + vertices[i * numSideVerts + j] = vert; + } } - void MapEditorAppState::MapEditor::generatePlane(Vector2 size){ - int numSideVerts = 100, numIndices = 6 * (numSideVerts - 1) * (numSideVerts - 1); - MeshData::Vertex *vertices = new MeshData::Vertex[numSideVerts * numSideVerts]; - u32 *indices = new u32[numIndices]; + for(int i = 0; i < numSideVerts - 1; i++){ + for(int j = 0; j < numSideVerts - 1; j++){ + int id = (i * (numSideVerts - 1) + j) * 6; + indices[id] = i * numSideVerts + j; + indices[id + 1] = i * numSideVerts + j + 1; + indices[id + 2] = (i + 1) * numSideVerts + j + 1; - for(int i = 0; i < numSideVerts; i++){ - for(int j = 0; j < numSideVerts; j++){ - MeshData::Vertex vert; + indices[id + 3] = (i + 1) * numSideVerts + j + 1; + indices[id + 4] = (i + 1) * numSideVerts + j; + indices[id + 5] = i * numSideVerts + j; + } + } - float x = size.x * (2 * j - numSideVerts) / (2 * numSideVerts); - float z = size.y * (numSideVerts - 2 * i) / (2 * numSideVerts); + Root *root = Root::getSingleton(); + Material *mat = new Material(root->getLibPath() + "texture"); + mat->addBoolUniform("texturingEnabled", false); + mat->addBoolUniform("lightingEnabled", false); + mat->addVec4Uniform("diffuseColor", Vector4(1, 0, 0, 1)); - vert.pos = Vector3(x, 0, z); - vert.uv = Vector2((float)(j / numSideVerts), (float)(i / numSideVerts)); - vert.tan = Vector3::VEC_I; - vert.biTan = Vector3::VEC_K; - vert.norm = vert.tan.cross(vert.biTan); + MeshData meshData(vertices, indices, numIndices / 6); + Mesh *mesh = new Mesh(meshData); + mesh->construct(); + mesh->setMaterial(mat); - vertices[i * numSideVerts + j] = vert; - } + Node *node = new Node(); + node->attachMesh(mesh); + root->getRootNode()->attachChild(node); + + map->addTerrainObject(TerrainObject(Vector3(0, 0, 0), Vector3(size.x, 0, size.y), Vector3(14, 6, 14), TerrainObject::LANDMASS, node, 0, new Cell, new vb01::u32*)); + } + + MapEditorAppState::MapEditorAppState(string name, Vector2 size) : AbstractAppState( + AppStateType::MAP_EDITOR, + configData::calcSumBinds(AppStateType::MAP_EDITOR, true), + configData::calcSumBinds(AppStateType::MAP_EDITOR, false), + GameManager::getSingleton()->getPath() + "Scripts/options.lua"){ + mapName = name; + mapSize = size; + } + + void MapEditorAppState::update(){ + CameraController *camCtr = CameraController::getSingleton(); + + if(!camCtr->isLookingAround()) + camCtr->updateCameraPosition(); + } + + void MapEditorAppState::onAttached(){ + mapEditor = new MapEditor(mapName, mapSize); + } + + void MapEditorAppState::onDettached(){} + + void MapEditorAppState::onAction(int bind, bool isPressed){ + } + + void MapEditorAppState::onAnalog(int bind, float strength){ + CameraController *camCtr = CameraController::getSingleton(); + + switch((Bind)bind){ + case Bind::LOOK_UP: + case Bind::LOOK_DOWN: + if(camCtr->isLookingAround()){ + Vector3 dirProj = Root::getSingleton()->getCamera()->getDirection(); + dirProj = Vector3(dirProj.x, 0, dirProj.z).norm(); + CameraController::getSingleton()->orientCamera(Vector3(0, 1, 0).cross(dirProj), strength); } - for(int i = 0; i < numSideVerts - 1; i++){ - for(int j = 0; j < numSideVerts - 1; j++){ - int id = (i * (numSideVerts - 1) + j) * 6; - indices[id] = i * numSideVerts + j; - indices[id + 1] = i * numSideVerts + j + 1; - indices[id + 2] = (i + 1) * numSideVerts + j + 1; + break; + case Bind::LOOK_LEFT: + case Bind::LOOK_RIGHT: + if(camCtr->isLookingAround()) + camCtr->orientCamera(Vector3(0, 1, 0), strength); - indices[id + 3] = (i + 1) * numSideVerts + j + 1; - indices[id + 4] = (i + 1) * numSideVerts + j; - indices[id + 5] = i * numSideVerts + j; - } - } - - Root *root = Root::getSingleton(); - Material *mat = new Material(root->getLibPath() + "texture"); - mat->addBoolUniform("texturingEnabled", false); - mat->addBoolUniform("lightingEnabled", false); - mat->addVec4Uniform("diffuseColor", Vector4(1, 0, 0, 1)); - - MeshData meshData(vertices, indices, numIndices / 6); - Mesh *mesh = new Mesh(meshData); - mesh->construct(); - mesh->setMaterial(mat); - - Node *node = new Node(); - node->attachMesh(mesh); - root->getRootNode()->attachChild(node); - - map->addTerrainObject(TerrainObject(Vector3(0, 0, 0), Vector3(size.x, 0, size.y), Vector3(14, 6, 14), TerrainObject::LANDMASS, node, 0, new Cell, new vb01::u32*)); + break; } - - MapEditorAppState::MapEditorAppState(string name, Vector2 size) : AbstractAppState( - AppStateType::MAP_EDITOR, - configData::calcSumBinds(AppStateType::MAP_EDITOR, true), - configData::calcSumBinds(AppStateType::MAP_EDITOR, false), - GameManager::getSingleton()->getPath() + "Scripts/options.lua"){ - mapName = name; - mapSize = size; - } - - void MapEditorAppState::update(){} - - void MapEditorAppState::onAttached(){ - mapEditor = new MapEditor(mapName, mapSize); - } - - void MapEditorAppState::onDettached(){} - - void MapEditorAppState::onAction(int bind, bool isPressed){} - - void MapEditorAppState::onAnalog(int bind, float str){} + } }