factored out common Unit, Projectile and GameObjectFrame behaviour into GameObject classes

This commit is contained in:
devZoGok
2023-10-01 17:20:56 +03:00
parent d62c5105cd
commit 1f82b1dfd9
18 changed files with 348 additions and 295 deletions
+2 -2
View File
@@ -18,14 +18,14 @@ set(GUI tooltip.cpp concreteGuiManager.cpp ${BUTTONS} ${LISTBOXES})
set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppState.cpp)
set(UTIL util.cpp binds.h)
set(CONTROLLERS unitFrameController.cpp cameraController.cpp)
set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp)
set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp)
set(CORE gameManager.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS})
set(PROJECTILES projectile.cpp)
set(FX explosion.cpp)
set(VEHICLES vehicle.cpp engineer.cpp)
set(UNITS unitFactory.cpp unit.cpp structure.cpp ${VEHICLES})
set(CONTENT player.cpp pathfinder.cpp map.cpp ${UNITS} ${PROJECTILES} ${FX})
set(CONTENT player.cpp pathfinder.cpp map.cpp gameObject.cpp resourceDeposit.cpp ${UNITS} ${PROJECTILES} ${FX})
set(GAME_SRC ${STATES} ${GUI} ${CORE} ${CONTENT} ${UTIL})
set(SFML_DIR external/SFML)
+13 -13
View File
@@ -20,7 +20,7 @@
#include "structure.h"
#include "util.h"
#include "tooltip.h"
#include "unitFrameController.h"
#include "gameObjectFrameController.h"
#include "cameraController.h"
#include "concreteGuiManager.h"
@@ -118,7 +118,7 @@ namespace battleship{
if(!camCtr->isLookingAround())
camCtr->updateCameraPosition();
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
if(ufCtr->isPlacingFrames())
ufCtr->update();
@@ -126,8 +126,8 @@ namespace battleship{
void ActiveGameState::deselectUnits(){
mainPlayer->deselectUnits();
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
ufCtr->removeUnitFrames();
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
ufCtr->removeGameObjectFrames();
ufCtr->setPlacingFrames(false);
ConcreteGuiManager::getSingleton()->removeAllGuiElements();
@@ -288,11 +288,11 @@ namespace battleship{
std::map<Node*, Unit*>::iterator it = unitData.find(node);
Unit *unit = nullptr;
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
if(ufCtr->isPlacingFrames()){
Model *model = ufCtr->getUnitFrame(0).model;
unit = new Structure(mainPlayer, ufCtr->getUnitFrame(0).id, model->getPosition(), model->getOrientation());
GameObjectFrameController::GameObjectFrame goFr= ufCtr->getGameObjectFrame(0);
unit = new Structure(mainPlayer, goFr.getId(), goFr.getModel()->getPosition(), goFr.getModel()->getOrientation());
mainPlayer->addUnit(unit);
}
@@ -338,7 +338,7 @@ namespace battleship{
void ActiveGameState::onAction(int bind, bool isPressed) {
GameManager *gm = GameManager::getSingleton();
InGameAppState *inGameState = (InGameAppState*) gm->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE);
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
switch((Bind)bind){
case Bind::DRAG_BOX:
@@ -359,7 +359,7 @@ namespace battleship{
type = Order::TYPE::BUILD;
ufCtr->setPlacingFrames(false);
ufCtr->setRotatingFrames(false);
ufCtr->removeUnitFrames();
ufCtr->removeGameObjectFrames();
}
issueOrder(type, shiftPressed);
@@ -405,7 +405,7 @@ namespace battleship{
{
shiftPressed = isPressed;
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
ufCtr->setPaintSelecting(shiftPressed);
if(isPressed){
@@ -463,7 +463,7 @@ namespace battleship{
break;
case Bind::DESELECT_STRUCTURE:
ufCtr->removeUnitFrames();
ufCtr->removeGameObjectFrames();
ufCtr->setPlacingFrames(false);
break;
}
@@ -471,7 +471,7 @@ namespace battleship{
void ActiveGameState::onAnalog(int bind, float strength) {
CameraController *camCtr = CameraController::getSingleton();
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
switch((Bind)bind){
case Bind::LOOK_UP:
@@ -488,7 +488,7 @@ namespace battleship{
if(camCtr->isLookingAround())
CameraController::getSingleton()->orientCamera(Vector3(0, 1, 0), strength);
else if(ufCtr->isPlacingFrames() && ufCtr->isRotatingFrames())
ufCtr->rotateUnitFrames(100 * strength);
ufCtr->rotateGameObjectFrames(100 * strength);
break;
}
+3 -6
View File
@@ -1,7 +1,7 @@
#include "buildButton.h"
#include "gameManager.h"
#include "unit.h"
#include "unitFrameController.h"
#include "gameObjectFrameController.h"
#include <solUtil.h>
@@ -17,11 +17,8 @@ namespace battleship{
}
void BuildButton::onClick(){
sol::state_view SOL_LUA_STATE = generateView();
string modelPath = (string)SOL_LUA_STATE["basePath"][structureId + 1] + (string)SOL_LUA_STATE["meshPath"][structureId + 1];
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
ufCtr->addUnitFrame(UnitFrameController::UnitFrame(modelPath, structureId, (int)UnitType::LAND));
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
ufCtr->addGameObjectFrame(GameObjectFrameController::GameObjectFrame(structureId, (int)UnitType::LAND));
ufCtr->setPlacingFrames(true);
}
}
+70
View File
@@ -0,0 +1,70 @@
#include "gameObject.h"
#include "gameManager.h"
#include "defConfigs.h"
#include <solUtil.h>
#include <material.h>
#include <string>
namespace battleship{
using namespace std;
using namespace vb01;
using namespace gameBase;
using namespace configData;
void GameObject::update(){
}
void GameObject::placeAt(Vector3 p) {
model->setPosition(p);
pos = p;
}
void GameObject::orientAt(Quaternion rotQuat){
model->setOrientation(rotQuat);
rot = rotQuat;
}
void GameObject::initProperties(){
}
void GameObject::destroyModel(){
Root::getSingleton()->getRootNode()->dettachChild(model);
delete model;
}
void GameObject::initModel(bool textured){
sol::state_view SOL_LUA_STATE = generateView();
string basePath = SOL_LUA_STATE["basePath"][id + 1];
string meshPath = SOL_LUA_STATE["meshPath"][id + 1];
model = new Model(basePath + meshPath);
Root *root = Root::getSingleton();
string libPath = root->getLibPath();
Material *mat = new Material(libPath + "texture");
if(textured){
string f[]{GameManager::getSingleton()->getPath() + configData::DEFAULT_TEXTURE};
Texture *diffuseTexture = new Texture(f, 1, false);
mat->addBoolUniform("texturingEnabled", true);
mat->addBoolUniform("lightingEnabled", false);
mat->addTexUniform("textures[0]", diffuseTexture, true);
}
else{
mat->addBoolUniform("texturingEnabled", false);
mat->addBoolUniform("lightingEnabled", false);
mat->addVec4Uniform("diffuseColor", Vector4::VEC_ZERO);
model->setWireframe(true);
}
model->setMaterial(mat);
root->getRootNode()->attachChild(model);
}
void GameObject::destroySound(){}
void GameObject::initSound(){}
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef GAME_OBJECT_H
#define GAME_OBJECT_H
#include <vector.h>
#include <quaternion.h>
#include <model.h>
namespace battleship{
class Player;
class GameObject{
public:
GameObject(int i, Player *pl, vb01::Vector3 vec, vb01::Quaternion quat) : id(i), player(pl), pos(vec), rot(quat){}
~GameObject(){}
virtual void update();
virtual void toggleSelection(bool sel){selected = sel;}
void placeAt(vb01::Vector3);
void orientAt(vb01::Quaternion);
inline vb01::Vector3 getCorner(int i){return corners[i];}
inline bool isSelected(){return selected;}
inline bool isSelectable(){return selectable;}
inline bool isDebuggable(){return debugging;}
inline vb01::Vector3 getPos() {return pos;}
inline vb01::Quaternion getRot(){return rot;}
inline float getWidth() {return width;}
inline float getHeight() {return height;}
inline float getLength() {return length;}
inline vb01::Model* getModel() {return model;}
inline Player* getPlayer(){return player;}
inline void toggleDebugging(bool d){this->debugging=d;}
inline vb01::Vector3 getDirVec() {return dirVec;}
inline vb01::Vector3 getLeftVec() {return leftVec;}
inline vb01::Vector3 getUpVec() {return upVec;}
inline int getId() {return id;}
private:
protected:
virtual void initProperties();
virtual void destroyModel();
virtual void initModel(bool = true);
virtual void destroySound();
virtual void initSound();
int id;
Player *player;
vb01::Model *model = nullptr;
vb01::Vector3 pos = vb01::Vector3(0, 0, 0), upVec = vb01::Vector3(0, 1, 0), dirVec = vb01::Vector3(0, 0, 1), leftVec = vb01::Vector3(1, 0, 0), corners[8];
vb01::Quaternion rot;
bool selected = false, selectable, debugging = false;
float width, height, length;
};
}
#endif
+148
View File
@@ -0,0 +1,148 @@
#include <solUtil.h>
#include "unitFrameController.h"
#include "unit.h"
#include "util.h"
#include "map.h"
#include <material.h>
#include <meshData.h>
#include <mesh.h>
#include <model.h>
#include <root.h>
#include <ray.h>
#include <string>
namespace battleship{
using namespace std;
using namespace vb01;
using namespace gameBase;
static GameObjectFrameController *gameObjectFrameController = nullptr;
GameObjectFrameController::GameObjectFrame::GameObjectFrame(int i, int t, Vector3 pos, Quaternion rot) : GameObject(i, nullptr, pos, rot), type(t) {
initModel(false);
}
GameObjectFrameController* GameObjectFrameController::getSingleton(){
if(!gameObjectFrameController)
gameObjectFrameController = new GameObjectFrameController();
return gameObjectFrameController;
}
void GameObjectFrameController::paintSelect(Vector3 rowEnd, float width, float length){
Vector3 rowDir = rowEnd - paintSelectRowStart;
float lenRow = rowDir.getLength();
float hypothenuse = sqrt(width * width + length * length);
int numStructsInRow = int(lenRow / hypothenuse);
while(gameObjectFrames.size() > numStructsInRow)
removeGameObjectFrame(gameObjectFrames.size() - 1);
if(gameObjectFrames.size() < numStructsInRow){
int structureId = gameObjectFrames[0].getId();
Vector3 buildDir = rowDir;
if(gameObjectFrames.size() > 1)
buildDir = gameObjectFrames[1].getModel()->getPosition() - gameObjectFrames[0].getModel()->getPosition();
Vector3 pos = paintSelectRowStart + buildDir.norm() * hypothenuse * gameObjectFrames.size();
addGameObjectFrame(GameObjectFrame(structureId, (int)UnitType::LAND, pos));
}
}
//TODO implement terrain evenness check
void GameObjectFrameController::placeGameObjectFrame(int id, Vector3 newPos, float width, float length){
GameObjectFrame &s = gameObjectFrames[id];
Model *model = s.getModel();
if(!rotatingStructure)
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;
float maxUnevenness = generateView()["maxUnevenness"], unevenness = 0;
for(int i = 0; i < numVerts; i++){
float diffX = fabs(model->getPosition().x - verts[i].pos.x);
float diffY = fabs(model->getPosition().y - verts[i].pos.y);
float diffZ = fabs(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 = GameObjectFrame::NOT_PLACEABLE;
}
else{
color = Vector4(0, 1, 0, 1);
s.status = GameObjectFrame::PLACEABLE;
}
Material *mat = model->getMaterial();
mat->setVec4Uniform("diffuseColor", color);
}
void GameObjectFrameController::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"][gameObjectFrames[0].getId() + 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 < gameObjectFrames.size(); i++){
if(paintSelecting){
float hypothenuse = sqrt(width * width + length * length);
Vector3 dir = (results[0].pos - paintSelectRowStart).norm();
newPos = paintSelectRowStart + dir * hypothenuse * i;
}
placeGameObjectFrame(i, newPos, width, length);
}
}
void GameObjectFrameController::removeGameObjectFrame(int i){
Model *model = gameObjectFrames[i].getModel();
model->getParent()->dettachChild(model);
delete model;
gameObjectFrames.erase(gameObjectFrames.begin() + i);
}
void GameObjectFrameController::removeGameObjectFrames(){
while(gameObjectFrames.size() > 0)
removeGameObjectFrame(0);
}
void GameObjectFrameController::rotateGameObjectFrames(float angle){
for(GameObjectFrame &s : gameObjectFrames)
if(s.status != GameObjectFrame::PLACED){
Model *model = s.getModel();
Quaternion rot = model->getOrientation();
model->setOrientation(Quaternion(angle, Vector3::VEC_J) * rot);
}
}
}
@@ -7,33 +7,33 @@
#include <vector.h>
#include <quaternion.h>
#include "gameObject.h"
namespace vb01{
class Model;
}
namespace battleship{
class UnitFrameController{
class GameObjectFrameController{
public:
struct UnitFrame{
struct GameObjectFrame : public GameObject{
enum Status{PLACEABLE, NOT_PLACEABLE, PLACED};
UnitFrame(std::string, int, int, vb01::Vector3 = vb01::Vector3::VEC_ZERO, vb01::Quaternion = vb01::Quaternion::QUAT_W);
~UnitFrame(){}
void update();
GameObjectFrame(int, int, vb01::Vector3 = vb01::Vector3::VEC_ZERO, vb01::Quaternion = vb01::Quaternion::QUAT_W);
~GameObjectFrame(){}
vb01::Model *model;
int id, type;
int type;
Status status;
};
static UnitFrameController* getSingleton();
static GameObjectFrameController* 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];}
void removeGameObjectFrame(int);
void removeGameObjectFrames();
void rotateGameObjectFrames(float);
inline int getNumGameObjectFrames(){return gameObjectFrames.size();}
inline void addGameObjectFrame(GameObjectFrame u){gameObjectFrames.push_back(u);}
inline GameObjectFrame& getGameObjectFrame(int i){return gameObjectFrames[i];}
inline bool isPlacingFrames(){return placingStructures;}
inline void setPlacingFrames(bool ps){placingStructures = ps;}
inline bool isPaintSelecting(){return paintSelecting;}
@@ -42,11 +42,11 @@ namespace battleship{
inline void setRotatingFrames(bool rs){rotatingStructure = rs;}
inline void setPaintSelectRowStart(vb01::Vector3 st){paintSelectRowStart = st;}
private:
UnitFrameController(){}
GameObjectFrameController(){}
void paintSelect(vb01::Vector3, float, float);
void placeUnitFrame(int, vb01::Vector3, float, float);
void placeGameObjectFrame(int, vb01::Vector3, float, float);
std::vector<UnitFrame> unitFrames;
std::vector<GameObjectFrame> gameObjectFrames;
vb01::Vector3 paintSelectRowStart, rowDir;
bool placingStructures = false, paintSelecting = false, rotatingStructure = false;
};
+3 -3
View File
@@ -12,7 +12,7 @@
#include "defConfigs.h"
#include "inGameAppState.h"
#include "console.h"
#include "unitFrameController.h"
#include "gameObjectFrameController.h"
#include "concreteGuiManager.h"
#include "vessel.h"
@@ -124,14 +124,14 @@ namespace battleship{
for(Player *p : Map::getSingleton()->getPlayers())
for(Unit *u : p->getUnits())
u->reinitUnit();
u->reinit();
}
}
void InGameAppState::onAction(int bind, bool isPressed) {
switch((Bind)bind){
case Bind::TOGGLE_MAIN_MENU:
if(isPressed && !UnitFrameController::getSingleton()->isPlacingFrames())
if(isPressed && !GameObjectFrameController::getSingleton()->isPlacingFrames())
toggleMainMenu();
break;
}
+6 -6
View File
@@ -2,7 +2,7 @@
#include "gameManager.h"
#include "defConfigs.h"
#include "cameraController.h"
#include "unitFrameController.h"
#include "gameObjectFrameController.h"
#include "player.h"
#include "map.h"
#include "mesh.h"
@@ -545,7 +545,7 @@ namespace battleship{
if(!(camCtr->isLookingAround() || mapEditor->isPushing()))
camCtr->updateCameraPosition();
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
if(ufCtr->isPlacingFrames())
ufCtr->update();
@@ -578,21 +578,21 @@ namespace battleship{
void MapEditorAppState::onDettached(){}
void MapEditorAppState::onAction(int bind, bool isPressed){
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
switch((Bind)bind){
case Bind::LOOK_AROUND:
if(ufCtr->isPlacingFrames()){
Player *player = Map::getSingleton()->getPlayer(0);
UnitFrameController::UnitFrame &frame = ufCtr->getUnitFrame(0);
player->addUnit(new Unit(player, frame.id, frame.model->getPosition(), frame.model->getOrientation()));
GameObjectFrameController::GameObjectFrame &frame = ufCtr->getGameObjectFrame(0);
player->addUnit(new Unit(player, frame.getId(), frame.getModel()->getPosition(), frame.getModel()->getOrientation()));
}
else
CameraController::getSingleton()->setLookingAround(isPressed);
break;
case Bind::DESELECT_STRUCTURE:
ufCtr->removeUnitFrames();
ufCtr->removeGameObjectFrames();
ufCtr->setPlacingFrames(false);
break;
case Bind::INCREASE_RADIUS:
+1 -1
View File
@@ -23,7 +23,7 @@ namespace battleship{
using namespace configData;
using namespace gameBase;
Projectile::Projectile(Unit *unit, Node *node, Vector3 pos, Vector3 dir, Vector3 left, Vector3 up, int id, int weaponTypeId, int weaponId) {
Projectile::Projectile(Unit *unit, Node *node, Vector3 pos, Vector3 dir, Vector3 left, Vector3 up, int id, int weaponTypeId, int weaponId) : GameObject(id, unit->getPlayer(), pos, Quaternion::QUAT_W){
this->unit = unit;
this->id = id;
this->weaponTypeId = weaponTypeId;
+3 -4
View File
@@ -1,4 +1,3 @@
#pragma once
#ifndef PROJECTILE_H
#define PROJECTILE_H
@@ -8,6 +7,7 @@
#include <quaternion.h>
#include "gameManager.h"
#include "gameObject.h"
#include "util.h"
namespace vb01{
@@ -19,7 +19,7 @@ namespace vb01{
namespace battleship {
class Unit;
class Projectile {
class Projectile : public GameObject{
public:
Projectile(Unit*, vb01::Node*, vb01::Vector3, vb01::Vector3, vb01::Vector3, vb01::Vector3, int, int, int);
virtual ~Projectile();
@@ -40,8 +40,7 @@ namespace battleship {
bool exploded = false;
float speed, rayLength, scale;
int damage, id, weaponTypeId;
vb01::Vector3 initPos, pos, dirVec, leftVec, upVec;
vb01::Quaternion rot;
vb01::Vector3 initPos;
s64 lastUpdateTime = 0;
Unit *unit = nullptr;
sf::SoundBuffer *shotSfxBuffer, *explosionSfxBuffer;
View File
View File
+16 -47
View File
@@ -22,22 +22,28 @@ using namespace gameBase;
using namespace std;
namespace battleship{
Unit::Unit(Player *player, int id, Vector3 pos, Quaternion rot) {
Unit::Unit(Player *player, int id, Vector3 pos, Quaternion rot) : GameObject(id, player, pos, rot){
this->id = id;
this->player = player;
initProperties();
initModel();
initSound();
initUnitStats();
placeUnit(pos);
orientUnit(rot);
init();
hpBackgroundNode = createBar(lenHpBar, Vector4(0, 0, 0, 1));
hpForegroundNode = createBar(lenHpBar, Vector4(0, 1, 0, 1));
}
//TODO complete implementation
Unit::~Unit() {
}
void Unit::init(){
initProperties();
initModel();
initSound();
placeAt(pos);
orientAt(rot);
}
void Unit::initProperties(){
sol::state_view SOL_LUA_STATE = generateView();
health = SOL_LUA_STATE["health"][id + 1];
@@ -60,31 +66,6 @@ namespace battleship{
length = corners[3].z - corners[0].z;
}
void Unit::destroyModel(){
Root::getSingleton()->getRootNode()->dettachChild(model);
delete model;
}
void Unit::initModel(){
sol::state_view SOL_LUA_STATE = generateView();
string basePath = SOL_LUA_STATE["basePath"][id + 1];
string meshPath = SOL_LUA_STATE["meshPath"][id + 1];
model = new Model(basePath + meshPath);
Root *root = Root::getSingleton();
string libPath = root->getLibPath();
Material *mat = new Material(libPath + "texture");
string f[]{GameManager::getSingleton()->getPath() + configData::DEFAULT_TEXTURE};
Texture *diffuseTexture = new Texture(f, 1, false);
mat->addBoolUniform("texturingEnabled", true);
mat->addBoolUniform("lightingEnabled", false);
mat->addTexUniform("textures[0]", diffuseTexture, true);
model->setMaterial(mat);
root->getRootNode()->attachChild(model);
}
void Unit::destroySound(){
selectionSfx->stop();
delete selectionSfx;
@@ -101,14 +82,14 @@ namespace battleship{
selectionSfx = new sf::Sound(*selectionSfxBuffer);
}
void Unit::reinitUnit(){
void Unit::reinit(){
destroySound();
destroyModel();
initModel();
initSound();
initProperties();
placeUnit(pos);
orientUnit(rot);
placeAt(pos);
orientAt(rot);
}
Node* Unit::createBar(float initLen, Vector4 color){
@@ -129,8 +110,6 @@ namespace battleship{
}
void Unit::initUnitStats(){
hpBackgroundNode = createBar(lenHpBar, Vector4(0, 0, 0, 1));
hpForegroundNode = createBar(lenHpBar, Vector4(0, 1, 0, 1));
}
void Unit::update() {
@@ -213,16 +192,6 @@ namespace battleship{
removeOrder(orders.size() - 1);
}
void Unit::placeUnit(Vector3 p) {
model->setPosition(p);
pos = p;
}
void Unit::orientUnit(Quaternion rotQuat){
model->setOrientation(rotQuat);
rot = rotQuat;
}
std::vector<Projectile*> Unit::getProjectiles(){
std::vector<Projectile*> projectiles;
InGameAppState *inGameState = ((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE));
+7 -28
View File
@@ -1,5 +1,3 @@
#pragma once
#include "external/vb01/quaternion.h"
#ifndef UNIT_H
#define UNIT_H
@@ -11,6 +9,7 @@
#include <SFML/Audio.hpp>
#include "gameManager.h"
#include "gameObject.h"
#include "projectile.h"
namespace vb01{
@@ -47,7 +46,7 @@ namespace battleship{
enum class UnitClass {VESSEL, ENGINEER, SAMPLE_BUILDING};
enum class UnitType {UNDERWATER, SEA_LEVEL, HOVER, LAND, AIR, NONE = -1};
class Unit {
class Unit : public GameObject{
public:
Unit(Player*, int, vb01::Vector3, vb01::Quaternion);
~Unit();
@@ -56,38 +55,23 @@ namespace battleship{
virtual void halt();
void toggleSelection(bool);
void setOrder(Order);
void placeUnit(vb01::Vector3);
void orientUnit(vb01::Quaternion);
void addProjectile(Projectile*);
std::vector<Projectile*> getProjectiles();
virtual void addOrder(Order);
virtual void reinitUnit();
virtual void reinit();
inline vb01::Vector3 getCorner(int i){return corners[i];}
inline bool isSelected(){return selected;}
inline bool isSelectable(){return selectable;}
inline bool isDebuggable(){return debugging;}
inline bool isWorking(){return working;}
inline vb01::Vector2 getScreenPos(){return screenPos;}
inline vb01::Vector3 getPos() {return pos;}
inline vb01::Vector3* getPosPtr() {return &pos;}
inline vb01::Quaternion getRot(){return rot;}
inline float getLineOfSight() {return lineOfSight;}
inline float getWidth() {return width;}
inline float getHeight() {return height;}
inline float getLength() {return length;}
inline vb01::Model* getNode() {return model;}
inline Player* getPlayer(){return player;}
inline UnitType getType() {return type;}
inline UnitClass getUnitClass() {return unitClass;}
inline void toggleDebugging(bool d){this->debugging=d;}
inline void takeDamage(int damage) {health -= damage;}
inline int getId() {return id;}
inline int getPlayerId() {return playerId;}
inline vb01::Vector3 getDirVec() {return dirVec;}
inline vb01::Vector3 getLeftVec() {return leftVec;}
inline vb01::Vector3 getUpVec() {return upVec;}
private:
void updateScreenCoordinates();
void init();
inline bool canDisplayOrderLine(){return vb01::getTime() - orderLineDispTime < orderVecDispLength;}
const int orderVecDispLength = 2000;
@@ -95,24 +79,19 @@ namespace battleship{
sf::Sound *selectionSfx = nullptr;
vb01::Node *hpBackgroundNode = nullptr, *hpForegroundNode = nullptr;
protected:
Player *player;
UnitClass unitClass;
UnitType type;
vb01::Vector2 screenPos;
std::vector<Order> orders;
vb01::Vector3 pos = vb01::Vector3(0, 0, 0), upVec = vb01::Vector3(0, 1, 0), dirVec = vb01::Vector3(0, 0, 1), leftVec = vb01::Vector3(1, 0, 0), corners[8];
vb01::Quaternion rot = vb01::Quaternion::QUAT_W;
vb01::Vector3 corners[8];
int health, maxHealth, cost, id, playerId, lenHpBar = 200;
s64 orderLineDispTime = 0;
vb01::Model *model;
bool selected = false, selectable, debugging = false, working = true;
float lineOfSight, range, width, height, length;
bool working = true;
float lineOfSight, range;
void removeOrder(int);
void displayUnitStats(vb01::Node*, vb01::Node*, int, int, vb01::Vector2 = vb01::Vector2::VEC_ZERO);
virtual void initProperties();
virtual void destroyModel();
virtual void initModel();
virtual void destroySound();
virtual void initSound();
virtual void initUnitStats();
-158
View File
@@ -1,158 +0,0 @@
#include <solUtil.h>
#include "unitFrameController.h"
#include "unit.h"
#include "util.h"
#include "map.h"
#include <material.h>
#include <meshData.h>
#include <mesh.h>
#include <model.h>
#include <root.h>
#include <ray.h>
#include <string>
namespace battleship{
using namespace std;
using namespace vb01;
using namespace gameBase;
static UnitFrameController *unitFrameController = nullptr;
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);
mat->addBoolUniform("lightingEnabled", false);
mat->addVec4Uniform("diffuseColor", Vector4::VEC_ZERO);
model = new Model(modelPath);
model->setWireframe(true);
model->setMaterial(mat);
model->setPosition(pos);
model->setOrientation(rot);
root->getRootNode()->attachChild(model);
}
UnitFrameController* UnitFrameController::getSingleton(){
if(!unitFrameController)
unitFrameController = new UnitFrameController();
return unitFrameController;
}
void UnitFrameController::paintSelect(Vector3 rowEnd, float width, float length){
Vector3 rowDir = rowEnd - paintSelectRowStart;
float lenRow = rowDir.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];
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];
if(!rotatingStructure)
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;
float maxUnevenness = generateView()["maxUnevenness"], 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);
}
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::removeUnitFrame(int i){
unitFrames[i].model->getParent()->dettachChild(unitFrames[i].model);
delete unitFrames[i].model;
unitFrames.erase(unitFrames.begin() + i);
}
void UnitFrameController::removeUnitFrames(){
while(unitFrames.size() > 0)
removeUnitFrame(0);
}
void UnitFrameController::rotateUnitFrames(float angle){
for(UnitFrame &s : unitFrames)
if(s.status != UnitFrame::PLACED){
Quaternion rot = s.model->getOrientation();
s.model->setOrientation(Quaternion(angle, Vector3::VEC_J) * rot);
}
}
}
+3 -7
View File
@@ -2,7 +2,7 @@
#include "unitListbox.h"
#include "unit.h"
#include "unitFrameController.h"
#include "gameObjectFrameController.h"
namespace battleship{
using namespace std;
@@ -14,13 +14,9 @@ namespace battleship{
}
void UnitListbox::onClose(){
UnitFrameController *ufCtr = UnitFrameController::getSingleton();
int id = selectedOption;
sol::state_view SOL_LUA_STATE = generateView();
string modelPath = (string)SOL_LUA_STATE["basePath"][id + 1] + (string)SOL_LUA_STATE["meshPath"][id + 1];
ufCtr->addUnitFrame(UnitFrameController::UnitFrame(modelPath, id, (int)UnitType::LAND));
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
ufCtr->addGameObjectFrame(GameObjectFrameController::GameObjectFrame(id, (int)UnitType::LAND));
ufCtr->setPlacingFrames(true);
}
}
+3 -3
View File
@@ -60,7 +60,7 @@ namespace battleship{
break;
}
placeUnit(pos + dir * speed);
placeAt(pos + dir * speed);
}
void Vehicle::initProperties(){
@@ -122,12 +122,12 @@ namespace battleship{
Ray::sortResults(res);
if(!res.empty()){
placeUnit(res[0].pos);
placeAt(res[0].pos);
float angle = upVec.getAngleBetween(res[0].norm);
Vector3 axis = upVec.cross(res[0].norm).norm();
Quaternion rotQuat = Quaternion(angle, axis);
orientUnit(rotQuat * rot);
orientAt(rotQuat * rot);
}
*/
}