paintselecting buildable structures while rotating

This commit is contained in:
devZoGok
2023-09-23 15:50:20 +03:00
parent 35996a0298
commit 4ba11bb276
4 changed files with 24 additions and 47 deletions
+6 -40
View File
@@ -107,44 +107,10 @@ namespace battleship{
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
if(ufCtr->isPlacingFrames()){
if(ufCtr->isPlacingFrames())
ufCtr->update();
}
}
void ActiveGameState::paintSelectUnitFrames(){
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
Vector3 rowStartPos = ufCtr->getUnitFrame(0).model->getPosition();
Camera *cam = Root::getSingleton()->getCamera();
Vector3 camPos = cam->getPosition(), rayDir = (screenToSpace(getCursorPos()) - camPos).norm();
Node *node = Map::getSingleton()->getNodeParent()->getChild(0);
vector<Ray::CollisionResult> results;
Ray::retrieveCollisions(camPos, rayDir, node, results);
if(results.empty()) return;
Vector3 rowDir = results[0].pos - rowStartPos;
float lenRow = rowDir.getLength();
sol::state_view SOL_LUA_STATE = generateView();
sol::table cornerTable = SOL_LUA_STATE[buildableStructId];
float width = (float)cornerTable[0]["x"] - (float)cornerTable[1]["x"];
float length = (float)cornerTable[3]["z"] - (float)cornerTable[0]["z"];
float hypothenuse = sqrt(width * width + length * length);
int numStructsInRow = int(lenRow / hypothenuse);
if(numStructsInRow != ufCtr->getNumUnitFrames()){
ufCtr->removeUnitFrames();
for(int i = 0; i < numStructsInRow; i++){
string modelPath = (string)SOL_LUA_STATE["basePath"][buildableStructId + 1] + (string)SOL_LUA_STATE["meshPath"][buildableStructId + 1];
ufCtr->addUnitFrame(UnitFrameController::UnitFrame(modelPath, buildableStructId, (int)UnitType::LAND));
}
}
}
void ActiveGameState::deselectUnits(){
mainPlayer->deselectUnits();
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
@@ -309,7 +275,8 @@ namespace battleship{
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
if(ufCtr->isPlacingFrames()){
unit = new Structure(mainPlayer, ufCtr->getUnitFrame(0).id, results[0].pos, ufCtr->getUnitFrame(0).model->getOrientation());
Model *model = ufCtr->getUnitFrame(0).model;
unit = new Structure(mainPlayer, ufCtr->getUnitFrame(0).id, model->getPosition(), model->getOrientation());
mainPlayer->addUnit(unit);
}
@@ -375,6 +342,7 @@ namespace battleship{
else if(ufCtr->isPlacingFrames()){
type = Order::TYPE::BUILD;
ufCtr->setPlacingFrames(false);
ufCtr->setRotatingFrames(false);
ufCtr->removeUnitFrames();
}
@@ -426,12 +394,10 @@ namespace battleship{
if(isPressed){
depth += 0.05;
Camera *cam = Root::getSingleton()->getCamera();
Vector3 startPos = cam->getPosition();
Vector3 endPos = screenToSpace(getCursorPos());
Vector3 startPos = Root::getSingleton()->getCamera()->getPosition();
vector<Ray::CollisionResult> results;
Ray::retrieveCollisions(startPos, (endPos - startPos).norm(), Map::getSingleton()->getNodeParent()->getChild(0), results);
Ray::retrieveCollisions(startPos, (screenToSpace(getCursorPos()) - startPos).norm(), Map::getSingleton()->getNodeParent()->getChild(0), results);
Ray::sortResults(results);
if(!results.empty())
+1 -2
View File
@@ -33,7 +33,6 @@ namespace battleship{
void issueOrder(Order::TYPE, bool);
bool isInLineOfSight(vb01::Vector3, float, Unit*);
bool engineersSelected();
void paintSelectUnitFrames();
GuiAppState *guiState;
Player *mainPlayer;
@@ -45,7 +44,7 @@ namespace battleship{
std::vector<Unit*> unitGroups[9];
std::vector<Order::Target> targets;
bool isSelectionBox = false, shiftPressed = false, controlPressed = false, selectingPatrolPoints = false, selectingGuidedMissileTarget = false;
int playerId, zooms = 0, buildableStructId;
int playerId, zooms = 0;
const int NUM_MAX_ZOOMS = 10;
float depth = 1;
};
+15 -4
View File
@@ -21,7 +21,7 @@ namespace battleship{
static UnitFrameController *unitFrameController = nullptr;
UnitFrameController::UnitFrame::UnitFrame(string modelPath, int i, int t) : id(i), type(t) {
UnitFrameController::UnitFrame::UnitFrame(string modelPath, int i, int t, Vector3 pos, Quaternion rot) : id(i), type(t) {
Root *root = Root::getSingleton();
Material *mat = new Material(root->getLibPath() + "texture");
mat->addBoolUniform("texturingEnabled", false);
@@ -31,6 +31,8 @@ namespace battleship{
model = new Model(modelPath);
model->setWireframe(true);
model->setMaterial(mat);
model->setPosition(pos);
model->setOrientation(rot);
root->getRootNode()->attachChild(model);
}
@@ -42,7 +44,8 @@ namespace battleship{
}
void UnitFrameController::paintSelect(Vector3 rowEnd, float width, float length){
float lenRow = (rowEnd - paintSelectRowStart).getLength();
Vector3 rowDir = rowEnd - paintSelectRowStart;
float lenRow = rowDir.getLength();
float hypothenuse = sqrt(width * width + length * length);
int numStructsInRow = int(lenRow / hypothenuse);
@@ -53,14 +56,22 @@ namespace battleship{
int structureId = unitFrames[0].id;
sol::state_view SOL_LUA_STATE = generateView();
string modelPath = (string)SOL_LUA_STATE["basePath"][structureId + 1] + (string)SOL_LUA_STATE["meshPath"][structureId + 1];
addUnitFrame(UnitFrame(modelPath, structureId, (int)UnitType::LAND));
Vector3 buildDir = rowDir;
if(unitFrames.size() > 1)
buildDir = unitFrames[1].model->getPosition() - unitFrames[0].model->getPosition();
Vector3 pos = paintSelectRowStart + buildDir.norm() * hypothenuse * unitFrames.size();
addUnitFrame(UnitFrame(modelPath, structureId, (int)UnitType::LAND, pos));
}
}
//TODO implement terrain evenness check
void UnitFrameController::placeUnitFrame(int id, Vector3 newPos, float width, float length){
UnitFrame &s = unitFrames[id];
s.model->setPosition(newPos);
if(!rotatingStructure)
s.model->setPosition(newPos);
Map *map = Map::getSingleton();
MeshData meshData = map->getNodeParent()->getChild(0)->getMesh(0)->getMeshBase();
+2 -1
View File
@@ -5,6 +5,7 @@
#include <string>
#include <vector.h>
#include <quaternion.h>
namespace vb01{
class Model;
@@ -16,7 +17,7 @@ namespace battleship{
struct UnitFrame{
enum Status{PLACEABLE, NOT_PLACEABLE, PLACED};
UnitFrame(std::string, int, int);
UnitFrame(std::string, int, int, vb01::Vector3 = vb01::Vector3::VEC_ZERO, vb01::Quaternion = vb01::Quaternion::QUAT_W);
~UnitFrame(){}
void update();