mirror of
https://github.com/ApfelTeeSaft/PlanetFleet.git
synced 2026-08-26 19:33:38 +00:00
260 lines
7.3 KiB
C++
Executable File
260 lines
7.3 KiB
C++
Executable File
#include <model.h>
|
|
#include <quad.h>
|
|
#include <material.h>
|
|
#include <texture.h>
|
|
#include <ray.h>
|
|
|
|
#include <stateManager.h>
|
|
#include <solUtil.h>
|
|
|
|
#include <glm.hpp>
|
|
#include <ext.hpp>
|
|
|
|
#include "unit.h"
|
|
#include "util.h"
|
|
#include "inGameAppState.h"
|
|
#include "defConfigs.h"
|
|
#include "pathfinder.h"
|
|
|
|
using namespace glm;
|
|
using namespace vb01;
|
|
using namespace gameBase;
|
|
using namespace std;
|
|
|
|
namespace battleship{
|
|
Unit::Unit(Player *player, int id, Vector3 pos, Quaternion rot) {
|
|
this->id = id;
|
|
this->player = player;
|
|
|
|
initProperties();
|
|
initModel();
|
|
initSound();
|
|
initUnitStats();
|
|
placeUnit(pos);
|
|
orientUnit(rot);
|
|
}
|
|
|
|
//TODO complete implementation
|
|
Unit::~Unit() {
|
|
}
|
|
|
|
void Unit::initProperties(){
|
|
sol::state_view SOL_LUA_STATE = generateView();
|
|
health = SOL_LUA_STATE["health"][id + 1];
|
|
maxHealth = health;
|
|
|
|
range = SOL_LUA_STATE["range"][id + 1];
|
|
lineOfSight = SOL_LUA_STATE["lineOfSight"][id + 1];
|
|
unitClass = (UnitClass)SOL_LUA_STATE["unitClass"][id + 1];
|
|
type = (UnitType)SOL_LUA_STATE["unitType"][id + 1];
|
|
|
|
string cornerInd = "unitCornerPoints";
|
|
|
|
for(int i = 0; i < 8; i++){
|
|
sol::table cornerTable = SOL_LUA_STATE[cornerInd][id + 1][i + 1];
|
|
corners[i] = Vector3(cornerTable["x"], cornerTable["y"], cornerTable["z"]);
|
|
}
|
|
|
|
width = corners[0].x - corners[1].x;
|
|
height = corners[4].y - corners[0].y;
|
|
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;
|
|
delete selectionSfxBuffer;
|
|
}
|
|
|
|
void Unit::initSound(){
|
|
sol::state_view SOL_LUA_STATE = generateView();
|
|
string name = SOL_LUA_STATE["name"][id + 1];
|
|
selectionSfxBuffer = new sf::SoundBuffer();
|
|
string p = GameManager::getSingleton()->getPath() + "Sounds/" + name + "s/selection.ogg";
|
|
|
|
if(selectionSfxBuffer->loadFromFile(p.c_str()))
|
|
selectionSfx = new sf::Sound(*selectionSfxBuffer);
|
|
}
|
|
|
|
void Unit::reinitUnit(){
|
|
destroySound();
|
|
destroyModel();
|
|
initModel();
|
|
initSound();
|
|
initProperties();
|
|
placeUnit(pos);
|
|
orientUnit(rot);
|
|
}
|
|
|
|
Node* Unit::createBar(float initLen, Vector4 color){
|
|
Root *root = Root::getSingleton();
|
|
Material *mat = new Material(root->getLibPath() + "gui");
|
|
mat->addBoolUniform("texturingEnabled", false);
|
|
mat->addVec4Uniform("diffuseColor", color);
|
|
|
|
Quad *quad = new Quad(Vector3(initLen, 10, 0), false);
|
|
quad->setMaterial(mat);
|
|
|
|
Node *node = new Node();
|
|
node->attachMesh(quad);
|
|
node->setVisible(false);
|
|
root->getGuiNode()->attachChild(node);
|
|
|
|
return node;
|
|
}
|
|
|
|
void Unit::initUnitStats(){
|
|
hpBackgroundNode = createBar(lenHpBar, Vector4(0, 0, 0, 1));
|
|
hpForegroundNode = createBar(lenHpBar, Vector4(0, 1, 0, 1));
|
|
}
|
|
|
|
void Unit::update() {
|
|
leftVec = model->getGlobalAxis(0);
|
|
upVec = model->getGlobalAxis(1);
|
|
dirVec = model->getGlobalAxis(2);
|
|
|
|
if (!orders.empty()){
|
|
LineRenderer *lineRenderer = LineRenderer::getSingleton();
|
|
lineRenderer->toggleVisibility(orders[0].line.id, selected && canDisplayOrderLine());
|
|
lineRenderer->changeLineField(orders[0].line.id, pos, LineRenderer::START);
|
|
}
|
|
|
|
executeOrders();
|
|
|
|
screenPos = spaceToScreen(pos);
|
|
displayUnitStats(hpForegroundNode, hpBackgroundNode, health, maxHealth);
|
|
|
|
if (health <= 0)
|
|
blowUp();
|
|
}
|
|
|
|
void Unit::blowUp(){
|
|
working = false;
|
|
}
|
|
|
|
void Unit::displayUnitStats(Node *foreground, Node *background, int currVal, int maxVal, Vector2 offset) {
|
|
foreground->setVisible(selected);
|
|
background->setVisible(selected);
|
|
|
|
if(selected){
|
|
Vector3 offset3d = Vector3(offset.x, offset.y, 0);
|
|
float shiftedX = screenPos.x - 0.5 * lenHpBar;
|
|
background->setPosition(Vector3(shiftedX, screenPos.y, .1) + offset3d);
|
|
|
|
|
|
Quad *fgQuad = (Quad*)foreground->getMesh(0);
|
|
fgQuad->setSize(Vector3((float)currVal / maxVal * lenHpBar, 10, 0));
|
|
fgQuad->updateVerts(fgQuad->getMeshBase());
|
|
foreground->setPosition(Vector3(shiftedX, screenPos.y, 0) + offset3d);
|
|
}
|
|
}
|
|
|
|
void Unit::executeOrders() {
|
|
if (orders.size() > 0) {
|
|
Order order = orders[0];
|
|
|
|
switch (order.type) {
|
|
case Order::TYPE::ATTACK:
|
|
attack(order);
|
|
break;
|
|
case Order::TYPE::BUILD:
|
|
build(order);
|
|
break;
|
|
case Order::TYPE::MOVE:
|
|
move(order);
|
|
break;
|
|
case Order::TYPE::PATROL:
|
|
patrol(order);
|
|
break;
|
|
case Order::TYPE::LAUNCH:
|
|
launch(order);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Unit::setOrder(Order order) {
|
|
while (!orders.empty())
|
|
removeOrder(0);
|
|
|
|
addOrder(order);
|
|
orderLineDispTime = getTime();
|
|
}
|
|
|
|
void Unit::halt() {
|
|
while (orders.size() > 0)
|
|
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));
|
|
|
|
for(Projectile *p: inGameState->getProjectiles())
|
|
if(p->getUnit() == this)
|
|
projectiles.push_back(p);
|
|
|
|
return projectiles;
|
|
}
|
|
|
|
void Unit::addOrder(Order order){
|
|
orders.push_back(order);
|
|
}
|
|
|
|
void Unit::removeOrder(int id) {
|
|
LineRenderer::getSingleton()->removeLine(orders[id].line.id);
|
|
orders.erase(orders.begin() + id);
|
|
}
|
|
|
|
void Unit::toggleSelection(bool selection) {
|
|
selected = selection;
|
|
|
|
if(selection && selectionSfx)
|
|
selectionSfx->play();
|
|
|
|
orderLineDispTime = getTime();
|
|
}
|
|
|
|
void Unit::addProjectile(Projectile *p) {
|
|
StateManager *stateManager = GameManager::getSingleton()->getStateManager();
|
|
((InGameAppState*)stateManager->getAppStateByType((int)AppStateType::IN_GAME_STATE))->addProjectile(p);
|
|
}
|
|
}
|