improved mouse handling

added plane to simulate water
This commit is contained in:
devZoGok
2021-11-04 21:16:17 +02:00
parent 20a1a036f4
commit 71c5f5cd63
6 changed files with 62 additions and 98 deletions
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 MiB

After

Width:  |  Height:  |  Size: 1.8 MiB

+39 -31
View File
@@ -179,30 +179,36 @@ namespace battleship{
void ActiveGameState::update() {
vector<Unit*> units;
for (Player *p : players)
for (Unit *u : p->getUnits())
units.push_back(u);
if (isSelectionBox)
updateSelectionBox();
for (Unit *u : units) {
//u->updateUnitGUIInfo(cam, camDir, camLeft, camUp);
Vector2 pos = u->getScreenPos();
if (mainPlayer->isThisPlayersUnit(u)){
bool selected=false;
for(int i=0;i<selectedUnits.size()&&!selected;i++)
if(u==selectedUnits[i])
selected=true;
if(!u->isSelected()&&selected)
bool selected = false;
for(int i = 0; i < selectedUnits.size() && !selected; i++)
if(u == selectedUnits[i])
selected = true;
if(!u->isSelected() && selected)
u->toggleSelection(true);
else if(!selected)
u->toggleSelection(false);
if(isSelectionBox && u->isSelectable() &&
pos.x >= selectionBoxOrigin.x && pos.x <= selectionBoxEnd.x &&
pos.y >= selectionBoxOrigin.y && pos.y <= selectionBoxEnd.y){
if(!selected){
if(!shiftPressed&&u==mainPlayer->getUnit(0))
if(!shiftPressed && u == mainPlayer->getUnit(0))
while(!selectedUnits.empty())
selectedUnits.pop_back();
@@ -210,29 +216,17 @@ namespace battleship{
}
}
}
if (u->isDebuggable())
u->debug();
}
updateVectors();
renderUnits(units);
if (cam->getPosition().x > map->getSize().x / 2)
cam->setPosition(Vector3(map->getSize().x / 2, cam->getPosition().y, cam->getPosition().z));
else if (cam->getPosition().x < -map->getSize().x / 2)
cam->setPosition(Vector3(-map->getSize().x / 2, cam->getPosition().y, cam->getPosition().z));
if (cam->getPosition().z > map->getSize().y / 2)
cam->setPosition(Vector3(cam->getPosition().x, cam->getPosition().y, map->getSize().y / 2));
else if (cam->getPosition().z < -map->getSize().y / 2)
cam->setPosition(Vector3(cam->getPosition().x, cam->getPosition().y, -map->getSize().y / 2));
//cam->setTarget(cam->getPosition() + camDir);
renderGUIBorders();
renderActionButtons();
if(!lookingAround)
updateVectors();
updateCameraPosition();
}
void ActiveGameState::renderUnits(vector<Unit*> units) {
@@ -386,26 +380,40 @@ namespace battleship{
//driver->draw2DRectangle(SColor(10, 255, 255, 255), rect<s32>(selectionBoxOrigin.X, selectionBoxOrigin.Y, selectionBoxEnd.X, selectionBoxEnd.Y), nullptr);
}
void ActiveGameState::updateVectors() {
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.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.norm()) * camPanSpeed);
cam->setPosition(cam->getPosition() + forwVec * camPanSpeed);
if (cursorPos.y >= height && 0 < cursorPos.x && cursorPos.x < width)
cam->setPosition(cam->getPosition() - (forwVec.norm()) * camPanSpeed);
cam->setPosition(cam->getPosition() - forwVec * camPanSpeed);
if (cam->getPosition().x > map->getSize().x / 2)
cam->setPosition(Vector3(map->getSize().x / 2, cam->getPosition().y, cam->getPosition().z));
else if (cam->getPosition().x < -map->getSize().x / 2)
cam->setPosition(Vector3(-map->getSize().x / 2, cam->getPosition().y, cam->getPosition().z));
if (cam->getPosition().z > map->getSize().y / 2)
cam->setPosition(Vector3(cam->getPosition().x, cam->getPosition().y, map->getSize().y / 2));
else if (cam->getPosition().z < -map->getSize().y / 2)
cam->setPosition(Vector3(cam->getPosition().x, cam->getPosition().y, -map->getSize().y / 2));
}
void ActiveGameState::issueOrder(Order::TYPE type, vector<Vector3*> pos, bool addOrder) {
@@ -517,15 +525,15 @@ namespace battleship{
break;
case Mapping::ZOOM_IN:
if (zooms > -10) {
cam->setPosition(cam->getPosition() + camDir.norm() * .5f);
//cam->setTarget(cam->getPosition() + camDir);
Camera *cam = Root::getSingleton()->getCamera();
cam->setPosition(cam->getPosition() + cam->getDirection().norm() * .5f);
zooms--;
}
break;
case Mapping::ZOOM_OUT:
if (zooms < 10) {
cam->setPosition(cam->getPosition() - (camDir.norm() * .5f));
//cam->setTarget(cam->getPosition() + camDir);
Camera *cam = Root::getSingleton()->getCamera();
cam->setPosition(cam->getPosition() - (cam->getDirection().norm().norm() * .5f));
zooms++;
}
break;
@@ -590,7 +598,7 @@ namespace battleship{
void ActiveGameState::orientCamera(Vector3 rotAxis, double str){
float minStr = .001;
Quaternion rotQuat = Quaternion(.35 * (fabs(str) > minStr ? str : 0), rotAxis);
Quaternion rotQuat = Quaternion(.5 * (fabs(str) > minStr ? str : 0), rotAxis);
Camera *cam = Root::getSingleton()->getCamera();
Vector3 dir = rotQuat * cam->getDirection();
Vector3 up = rotQuat * cam->getUp();
+1 -3
View File
@@ -53,7 +53,7 @@ namespace battleship{
void renderGUIBorders();
void renderActionButtons();
void renderUnits(std::vector<Unit*>);
void updateVectors();
void updateCameraPosition();
void updateSelectionBox();
void addPos();
void issueOrder(Order::TYPE, std::vector<vb01::Vector3*>, bool);
@@ -64,8 +64,6 @@ namespace battleship{
Map *map;
std::vector<Player*> players;
Player *mainPlayer;
vb01::Camera *cam;
vb01::Vector3 camDir = vb01::Vector3(0, 0, 1), camLeft = vb01::Vector3(1, 0, 0), camUp = vb01::Vector3(0, 1, 0);
vb01::Vector2 clickPoint, selectionBoxOrigin, selectionBoxEnd;
std::vector<vb01::Vector2> unitScreenPosVec;
std::vector<vb01::Vector3*> orderPos;
-55
View File
@@ -114,61 +114,6 @@ namespace battleship{
{0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0}
};
/*
const int guiBindsNumber = 3;
const std::string guiAppStateBinds[guiBindsNumber]{"leftClick", "upScroll", "downScroll"};
const int guiAppStateTriggers[guiBindsNumber]{0, 1, 1};
const bool guiAppStateIsKey[guiBindsNumber]{0, 0, 0};
const bool guiAppStateIsAnalog[guiBindsNumber]{0, 0, 0};
const std::string guiAppStateKeys[guiBindsNumber]{"LeftMouse", "UpScroll", "DownScroll"};
const int inGameAppStateBindsNumber = 1;
const std::string inGameAppStateBinds[inGameAppStateBindsNumber]{"toggleMainMenu"};
const int inGameAppStateTriggers[inGameAppStateBindsNumber]{irr::KEY_ESCAPE};
const bool inGameAppStateIsKey[inGameAppStateBindsNumber]{1};
const bool inGameAppStateIsAnalog[inGameAppStateBindsNumber]{0};
const std::string inGameAppStateKeys[inGameAppStateBindsNumber]{"Esc"};
const int activeGameStateBindsNumber = 10;
const std::string activeGameStateBinds[activeGameStateBindsNumber]{
"halt",
"zoomIn",
"zoomOut",
"lookAround",
"dragBox",
"deselect",
"leftControl",
"selectingPatrolPoints",
"launch",
"toggleSub"
};
const int activeGameStateTriggers[activeGameStateBindsNumber]{
irr::KEY_KEY_H,
3,
4,
1,
0,
2,
irr::KEY_LCONTROL,
irr::KEY_KEY_P,
irr::KEY_KEY_C,
irr::KEY_KEY_S
};
const bool activeGameStateIsKey[activeGameStateBindsNumber]{1, 0, 0, 0, 0, 0, 1, 1, 1, 1};
const bool activeGameStateIsAnalog[activeGameStateBindsNumber]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const std::string activeGameStateKeys[activeGameStateBindsNumber]{
"H",
"UpScroll",
"DownScroll",
"MiddleMouse",
"LeftMouse",
"RightMouse",
"leftControl",
"P",
"C",
"S"
};
*/
}
}
+22 -9
View File
@@ -1,4 +1,7 @@
#include <root.h>
#include <quad.h>
#include <node.h>
#include <material.h>
#include "map.h"
#include "gameManager.h"
@@ -26,19 +29,29 @@ namespace battleship{
PATH + "Textures/left.jpg",
PATH + "Textures/right.jpg",
PATH + "Textures/front.jpg",
PATH + "Textures/back.jpg",
PATH + "Textures/back.jpg"
};
Root::getSingleton()->createSkybox(path);
Root *root = Root::getSingleton();
root->createSkybox(path);
/*
waterMesh = smgr->addHillPlaneMesh("", dimension2d<float>(1, 1), dimension2d<u32>(30, 30), 0, .1, dimension2d<float>(2., 2.), dimension2d<float>(10, 10));
waterNode = smgr->addWaterSurfaceSceneNode(waterMesh, .1, 900, 3);
waterNode->setPosition(vector3df(0, 0, 0));
waterNode->setMaterialTexture(0, driver->getTexture(PATH + "Textures/water-texture.png"));
waterNode->setMaterialFlag(EMF_LIGHTING, true);
waterNode->setMaterialType(EMT_TRANSPARENT_ALPHA_CHANNEL);
*/
Quad *quad = new Quad(Vector3(size.x, size.y, 1) * 1, true);
Material *mat = new Material();
mat->setTexturingEnabled(true);
mat->setLightingEnabled(false);
string fr[]{PATH + "Textures/water.jpg"};
Texture *t = new Texture(fr, 1);
mat->addDiffuseMap(t);
quad->setMaterial(mat);
Node *water = new Node();
water->attachMesh(quad);
root->getRootNode()->attachChild(water);
water->setOrientation(Quaternion(-1.37, Vector3::VEC_I));
Camera *cam = root->getCamera();
cam->setPosition(Vector3(1, 1, 1) * 10);
cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm());
}
void Map::unload() {}