mirror of
https://github.com/devZoGok/PlanetFleet.git
synced 2026-08-26 19:43:29 +00:00
implemented paint selection of constructable buildings
This commit is contained in:
@@ -54,14 +54,14 @@ unitCornerPoints = {
|
||||
{x = .5, y = 2, z = 3.4}
|
||||
},
|
||||
{
|
||||
{x = .5, y = -.25, z = -4.8},
|
||||
{x = -.5, y = -.25, z = -4.8},
|
||||
{x = -.5, y = -.25, z = 4.7},
|
||||
{x = .5, y = -.25, z = 4.7},
|
||||
{x = .5, y = .8, z = -4.8},
|
||||
{x = -.5, y = .8, z = -4.8},
|
||||
{x = -.5, y = .8, z = 4.7},
|
||||
{x = .5, y = .8, z = 4.7}
|
||||
{x = 1, y = -.25, z = -1},
|
||||
{x = -1, y = -.25, z = -1},
|
||||
{x = -1, y = -.25, z = 1},
|
||||
{x = 1, y = -.25, z = 1},
|
||||
{x = 1, y = .8, z = -1},
|
||||
{x = -1, y = .8, z = -1},
|
||||
{x = -1, y = .8, z = 1},
|
||||
{x = 1, y = .8, z = 1}
|
||||
},
|
||||
{
|
||||
{x = .7, y = -.3, z = -4},
|
||||
|
||||
+62
-11
@@ -107,10 +107,44 @@ 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();
|
||||
@@ -222,22 +256,21 @@ namespace battleship{
|
||||
break;
|
||||
}
|
||||
|
||||
Order o;
|
||||
o.type = type;
|
||||
o.targets = targets;
|
||||
targets.push_back(Order::Target());
|
||||
|
||||
if(o.type == Order::TYPE::PATROL)
|
||||
if(type == Order::TYPE::PATROL)
|
||||
for(int i = targets.size() - 2; i >= 0; i--)
|
||||
targets[i + 1] = targets[i];
|
||||
|
||||
LineRenderer *lineRenderer = LineRenderer::getSingleton();
|
||||
Order order;
|
||||
|
||||
for (int i = 0; i < mainPlayer->getNumSelectedUnits(); ++i) {
|
||||
Unit *u = mainPlayer->getSelectedUnit(i);
|
||||
lineRenderer->addLine(u->getPos(), o.targets[i].pos, color);
|
||||
lineRenderer->addLine(u->getPos(), targets[i].pos, color);
|
||||
vector<LineRenderer::Line> lines = lineRenderer->getLines();
|
||||
o.line = lines[lines.size() - 1];
|
||||
LineRenderer::Line line = lines[lines.size() - 1];
|
||||
order = Order(type, line, targets);
|
||||
|
||||
if (type != Order::TYPE::LAUNCH) {
|
||||
if(type == Order::TYPE::PATROL){
|
||||
@@ -246,9 +279,9 @@ namespace battleship{
|
||||
}
|
||||
|
||||
if (addOrder)
|
||||
u->addOrder(o);
|
||||
u->addOrder(order);
|
||||
else
|
||||
u->setOrder(o);
|
||||
u->setOrder(order);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,8 +418,26 @@ namespace battleship{
|
||||
if(isPressed) depth -= 0.05;
|
||||
break;
|
||||
case Bind::LEFT_SHIFT:
|
||||
shiftPressed = isPressed;
|
||||
if(isPressed) depth += 0.05;
|
||||
{
|
||||
shiftPressed = isPressed;
|
||||
|
||||
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
|
||||
ufCtr->setPaintSelecting(shiftPressed);
|
||||
|
||||
if(isPressed){
|
||||
depth += 0.05;
|
||||
Camera *cam = Root::getSingleton()->getCamera();
|
||||
Vector3 startPos = cam->getPosition();
|
||||
Vector3 endPos = screenToSpace(getCursorPos());
|
||||
|
||||
vector<Ray::CollisionResult> results;
|
||||
Ray::retrieveCollisions(startPos, (endPos - startPos).norm(), Map::getSingleton()->getNodeParent()->getChild(0), results);
|
||||
Ray::sortResults(results);
|
||||
|
||||
if(!results.empty())
|
||||
ufCtr->setPaintSelectRowStart(results[0].pos);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Bind::SELECT_PATROL_POINTS:
|
||||
selectingPatrolPoints = isPressed;
|
||||
|
||||
+2
-1
@@ -33,6 +33,7 @@ namespace battleship{
|
||||
void issueOrder(Order::TYPE, bool);
|
||||
bool isInLineOfSight(vb01::Vector3, float, Unit*);
|
||||
bool engineersSelected();
|
||||
void paintSelectUnitFrames();
|
||||
|
||||
GuiAppState *guiState;
|
||||
Player *mainPlayer;
|
||||
@@ -44,7 +45,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;
|
||||
int playerId, zooms = 0, buildableStructId;
|
||||
const int NUM_MAX_ZOOMS = 10;
|
||||
float depth = 1;
|
||||
};
|
||||
|
||||
@@ -37,6 +37,9 @@ namespace battleship{
|
||||
TYPE type;
|
||||
vb01::LineRenderer::Line line;
|
||||
std::vector<Target> targets;
|
||||
|
||||
Order(){}
|
||||
Order(TYPE t, vb01::LineRenderer::Line l, std::vector<Target> targ) : type(t), line(l), targets(targ){}
|
||||
};
|
||||
|
||||
enum class MoveDir {LEFT, UP, FORW};
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "unitButton.h"
|
||||
|
||||
namespace battleship{
|
||||
using namespace std;
|
||||
using namespace vb01;
|
||||
using namespace vb01Gui;
|
||||
|
||||
UnitButton::UnitButton(Vector2 pos, Vector2 size, string name, string fontPath, int trigger, string imagePath) : Button(pos, size, name, fontPath, true, trigger, imagePath){}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef UNIT_BUTTON_H
|
||||
#define UNIT_BUTTON_H
|
||||
|
||||
#include <button.h>
|
||||
|
||||
namespace battleship{
|
||||
class Unit;
|
||||
|
||||
class UnitButton : public vb01Gui::Button{
|
||||
public:
|
||||
UnitButton(vb01::Vector2 pos, vb01::Vector2 size, std::string name, std::string fontPath, int trigger, std::string imagePath) : Button(pos, size, name, fontPath, trigger, true, imagePath){}
|
||||
protected:
|
||||
std::vector<Unit*> units;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
+81
-52
@@ -1,6 +1,7 @@
|
||||
#include <solUtil.h>
|
||||
|
||||
#include "unitFrameController.h"
|
||||
#include "unit.h"
|
||||
#include "util.h"
|
||||
#include "map.h"
|
||||
|
||||
@@ -40,72 +41,100 @@ namespace battleship{
|
||||
return unitFrameController;
|
||||
}
|
||||
|
||||
void UnitFrameController::paintSelect(Vector3 rowEnd, float width, float length){
|
||||
float lenRow = (rowEnd - paintSelectRowStart).getLength();
|
||||
float hypothenuse = sqrt(width * width + length * length);
|
||||
int numStructsInRow = int(lenRow / hypothenuse);
|
||||
|
||||
while(unitFrames.size() > numStructsInRow)
|
||||
removeUnitFrame(unitFrames.size() - 1);
|
||||
|
||||
if(unitFrames.size() < numStructsInRow){
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
//TODO implement terrain evenness check
|
||||
void UnitFrameController::update(){
|
||||
void UnitFrameController::placeUnitFrame(int id, Vector3 newPos, float width, float length){
|
||||
UnitFrame &s = unitFrames[id];
|
||||
s.model->setPosition(newPos);
|
||||
|
||||
Map *map = Map::getSingleton();
|
||||
MeshData meshData = map->getNodeParent()->getChild(0)->getMesh(0)->getMeshBase();
|
||||
MeshData::Vertex *verts = meshData.vertices;
|
||||
int numVerts = 3 * meshData.numTris;
|
||||
|
||||
sol::state_view SOL_LUA_STATE = generateView();
|
||||
float maxUnevenness = SOL_LUA_STATE["maxUnevenness"];
|
||||
float maxUnevenness = generateView()["maxUnevenness"], unevenness = 0;
|
||||
|
||||
Camera *cam = Root::getSingleton()->getCamera();
|
||||
Vector3 startPos = cam->getPosition();
|
||||
Vector3 endPos = screenToSpace(getCursorPos());
|
||||
for(int i = 0; i < numVerts; i++){
|
||||
float diffX = fabs(s.model->getPosition().x - verts[i].pos.x);
|
||||
float diffY = fabs(s.model->getPosition().y - verts[i].pos.y);
|
||||
float diffZ = fabs(s.model->getPosition().z - verts[i].pos.z);
|
||||
|
||||
for(UnitFrame &s : unitFrames){
|
||||
vector<Ray::CollisionResult> results;
|
||||
Ray::retrieveCollisions(startPos, (endPos - startPos).norm(), map->getNodeParent()->getChild(0), results);
|
||||
Ray::sortResults(results);
|
||||
if(diffX < 0.5 * width && diffZ < 0.5 * length && diffY > unevenness){
|
||||
unevenness = diffY;
|
||||
|
||||
if(!results.empty()){
|
||||
if(!rotatingStructure)
|
||||
s.model->setPosition(results[0].pos);
|
||||
|
||||
sol::table unitTable = SOL_LUA_STATE["unitCornerPoints"][s.id + 1];
|
||||
float width = (float)unitTable[1]["x"] - (float)unitTable[2]["x"];
|
||||
float length = (float)unitTable[4]["z"] - (float)unitTable[1]["z"];
|
||||
|
||||
float unevenness = 0;
|
||||
|
||||
for(int i = 0; i < numVerts; i++){
|
||||
float diffX = fabs(s.model->getPosition().x - verts[i].pos.x);
|
||||
float diffY = fabs(s.model->getPosition().y - verts[i].pos.y);
|
||||
float diffZ = fabs(s.model->getPosition().z - verts[i].pos.z);
|
||||
|
||||
if(diffX < 0.5 * width && diffZ < 0.5 * length && diffY > unevenness){
|
||||
unevenness = diffY;
|
||||
|
||||
if(unevenness > maxUnevenness)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Vector4 color;
|
||||
|
||||
if(unevenness > maxUnevenness){
|
||||
color = Vector4(1, 0, 0, 1);
|
||||
s.status = UnitFrame::NOT_PLACEABLE;
|
||||
}
|
||||
else{
|
||||
color = Vector4(0, 1, 0, 1);
|
||||
s.status = UnitFrame::PLACEABLE;
|
||||
}
|
||||
|
||||
Material *mat = s.model->getMaterial();
|
||||
mat->setVec4Uniform("diffuseColor", color);
|
||||
if(unevenness > maxUnevenness)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Vector4 color;
|
||||
|
||||
if(unevenness > maxUnevenness){
|
||||
color = Vector4(1, 0, 0, 1);
|
||||
s.status = UnitFrame::NOT_PLACEABLE;
|
||||
}
|
||||
else{
|
||||
color = Vector4(0, 1, 0, 1);
|
||||
s.status = UnitFrame::PLACEABLE;
|
||||
}
|
||||
|
||||
Material *mat = s.model->getMaterial();
|
||||
mat->setVec4Uniform("diffuseColor", color);
|
||||
}
|
||||
|
||||
void UnitFrameController::update(){
|
||||
Vector3 startPos = Root::getSingleton()->getCamera()->getPosition();
|
||||
vector<Ray::CollisionResult> results;
|
||||
Ray::retrieveCollisions(startPos, (screenToSpace(getCursorPos()) - startPos).norm(), Map::getSingleton()->getNodeParent()->getChild(0), results);
|
||||
Ray::sortResults(results);
|
||||
if(results.empty()) return;
|
||||
|
||||
Vector3 newPos, rowEnd;
|
||||
sol::state_view SOL_LUA_STATE = generateView();
|
||||
sol::table unitTable = SOL_LUA_STATE["unitCornerPoints"][unitFrames[0].id + 1];
|
||||
float width = (float)unitTable[1]["x"] - (float)unitTable[2]["x"];
|
||||
float length = (float)unitTable[4]["z"] - (float)unitTable[1]["z"];
|
||||
|
||||
if(paintSelecting)
|
||||
paintSelect(results[0].pos, width, length);
|
||||
else if(!(paintSelecting || rotatingStructure))
|
||||
newPos = results[0].pos;
|
||||
|
||||
for(int i = 0; i < unitFrames.size(); i++){
|
||||
if(paintSelecting){
|
||||
float hypothenuse = sqrt(width * width + length * length);
|
||||
Vector3 dir = (results[0].pos - paintSelectRowStart).norm();
|
||||
newPos = paintSelectRowStart + dir * hypothenuse * i;
|
||||
}
|
||||
|
||||
placeUnitFrame(i, newPos, width, length);
|
||||
}
|
||||
}
|
||||
|
||||
void UnitFrameController::removeUnitFrames(){
|
||||
for(UnitFrame &u : unitFrames){
|
||||
u.model->getParent()->dettachChild(u.model);
|
||||
delete u.model;
|
||||
}
|
||||
void UnitFrameController::removeUnitFrame(int i){
|
||||
unitFrames[i].model->getParent()->dettachChild(unitFrames[i].model);
|
||||
delete unitFrames[i].model;
|
||||
unitFrames.erase(unitFrames.begin() + i);
|
||||
}
|
||||
|
||||
unitFrames.clear();
|
||||
void UnitFrameController::removeUnitFrames(){
|
||||
while(unitFrames.size() > 0)
|
||||
removeUnitFrame(0);
|
||||
}
|
||||
|
||||
void UnitFrameController::rotateUnitFrames(float angle){
|
||||
|
||||
+11
-1
@@ -4,6 +4,8 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <vector.h>
|
||||
|
||||
namespace vb01{
|
||||
class Model;
|
||||
}
|
||||
@@ -25,19 +27,27 @@ namespace battleship{
|
||||
|
||||
static UnitFrameController* getSingleton();
|
||||
void update();
|
||||
void removeUnitFrame(int);
|
||||
void removeUnitFrames();
|
||||
void rotateUnitFrames(float);
|
||||
inline int getNumUnitFrames(){return unitFrames.size();}
|
||||
inline void addUnitFrame(UnitFrame u){unitFrames.push_back(u);}
|
||||
inline UnitFrame& getUnitFrame(int i){return unitFrames[i];}
|
||||
inline bool isPlacingFrames(){return placingStructures;}
|
||||
inline void setPlacingFrames(bool ps){placingStructures = ps;}
|
||||
inline bool isPaintSelecting(){return paintSelecting;}
|
||||
inline void setPaintSelecting(bool ps){paintSelecting = ps;}
|
||||
inline bool isRotatingFrames(){return rotatingStructure;}
|
||||
inline void setRotatingFrames(bool rs){rotatingStructure = rs;}
|
||||
inline void setPaintSelectRowStart(vb01::Vector3 st){paintSelectRowStart = st;}
|
||||
private:
|
||||
UnitFrameController(){}
|
||||
void paintSelect(vb01::Vector3, float, float);
|
||||
void placeUnitFrame(int, vb01::Vector3, float, float);
|
||||
|
||||
std::vector<UnitFrame> unitFrames;
|
||||
bool placingStructures = false, rotatingStructure = false;
|
||||
vb01::Vector3 paintSelectRowStart, rowDir;
|
||||
bool placingStructures = false, paintSelecting = false, rotatingStructure = false;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user