destructible units

This commit is contained in:
devZoGok
2023-10-15 16:00:47 +03:00
parent f8328a540f
commit d32b98be3e
19 changed files with 217 additions and 46 deletions
+19 -1
View File
@@ -35,7 +35,7 @@ units = {
health = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500},
cost = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500},
range = {15, 15, 14, 14, 13, 13, 12, 12, 15, 25},
rateOfFire = {100, 100, 100, 100, 100, 100, 100},
rateOfFire = {5000, 100, 100, 100, 100, 100, 100},
unitCornerPoints = {
{
@@ -299,5 +299,23 @@ units = {
PATH .. 'Sounds/Units/Carriers/selection.ogg',
PATH .. 'Sounds/Units/Submarines/selection.ogg',
PATH .. 'Sounds/Units/Sample/selection.ogg',
},
fireSfx = {
PATH .. 'Sounds/Units/WarMechs/fire.ogg',
PATH .. 'Sounds/Units/WarMechs/fire.ogg',
PATH .. 'Sounds/Units/WarMechs/fire.ogg',
PATH .. 'Sounds/Units/WarMechs/fire.ogg',
PATH .. 'Sounds/Units/WarMechs/fire.ogg',
PATH .. 'Sounds/Units/WarMechs/fire.ogg',
PATH .. 'Sounds/Units/WarMechs/fire.ogg',
},
deathSfx = {
PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp)
set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp)
set(CORE gameManager.cpp game.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS})
set(PROJECTILES projectile.cpp)
set(FX explosion.cpp)
set(FX fx.cpp)
set(VEHICLES vehicle.cpp engineer.cpp)
set(UNITS gameObjectFactory.cpp unit.cpp structure.cpp ${VEHICLES})
set(CONTENT player.cpp pathfinder.cpp map.cpp gameObject.cpp gameObjectFrame.h resourceDeposit.cpp ${UNITS} ${PROJECTILES} ${FX})
+4 -16
View File
@@ -9,7 +9,7 @@ using namespace std;
using namespace vb01;
namespace battleship{
void detonate(Vector3 pos, Vector3 dir){
void detonate(string p){
sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer();
sf::Sound *sfx = nullptr;
string p = GameManager::getSingleton()->getPath() + "Sounds/Explosions/explosion0" + to_string(rand() % 4) + ".ogg";
@@ -19,11 +19,7 @@ namespace battleship{
sfx->play();
}
Fx fx;
fx.initTime = getTime();
fx.time = 2500;
fx.sfx = sfx;
Game::getSingleton()->addFx(fx);
Game::getSingleton()->addFx(Fx(sfx, 2500));
}
void detonateDepthCharge(){
@@ -36,11 +32,7 @@ namespace battleship{
sfx->play();
}
Fx fx;
fx.initTime = getTime();
fx.time = 2000;
fx.sfx = sfx;
Game::getSingleton()->addFx(fx);
Game::getSingleton()->addFx(Fx(sfx, 2000));
}
void detonateTorpedo(){
@@ -53,10 +45,6 @@ namespace battleship{
sfx->play();
}
Fx fx;
fx.initTime = getTime();
fx.time = 250;
fx.sfx = sfx;
Game::getSingleton()->addFx(fx);
Game::getSingleton()->addFx(Fx(sfx, 250));
}
}
+2 -4
View File
@@ -1,12 +1,10 @@
#ifndef EXPLOSION_H
#define EXPLOSION_H
#include <vector.h>
#include <string>
namespace battleship{
void detonate(vb01::Vector3, vb01::Vector3);
void detonateDepthCharge();
void detonateTorpedo();
void detonate(std::string);
}
#endif
+21
View File
@@ -0,0 +1,21 @@
#include "fx.h"
#include <particleEmitter.h>
#include <node.h>
#include <SFML/Audio.hpp>
namespace battleship{
using namespace vb01;
void Fx::activate(){
if(sfx)
sfx->play();
if(peNode){
}
}
}
+8
View File
@@ -7,10 +7,18 @@ namespace sf{
class Sound;
}
namespace vb01{
class Node;
}
namespace battleship{
struct Fx {
vb01::s64 initTime, time;
sf::Sound *sfx = nullptr;
vb01::Node *peNode = nullptr;
Fx(vb01::s64 t, sf::Sound *s = nullptr, vb01::Node *n = nullptr) : time(t), sfx(s), peNode(n), initTime(vb01::getTime()){}
void activate();
};
}
+22
View File
@@ -2,7 +2,11 @@
#include "player.h"
#include "projectile.h"
#include <SFML/Audio.hpp>
namespace battleship{
using namespace vb01;
static Game *game = nullptr;
Game* Game::getSingleton(){
@@ -18,6 +22,24 @@ namespace battleship{
for(Projectile *proj : projectiles)
proj->update();
for(int i = 0; i < fx.size(); i++)
if(getTime() - fx[i].initTime > fx[i].time)
removeFx(i);
}
void Game::removeFx(int id){
const sf::SoundBuffer *buffer = fx[id].sfx->getBuffer();
fx[id].sfx->stop();
delete fx[id].sfx;
delete buffer;
if(fx[id].peNode){
Root::getSingleton()->getRootNode()->dettachChild(fx[id].peNode);
delete fx[id].peNode;
}
fx.erase(fx.begin() + id);
}
void Game::togglePause(){
+1
View File
@@ -14,6 +14,7 @@ namespace battleship{
static Game* getSingleton();
void update();
void togglePause();
void removeFx(int);
inline void addFx(Fx f){fx.push_back(f);}
inline void addPlayer(Player *pl){players.push_back(pl);}
inline void addProjectile(Projectile *proj){projectiles.push_back(proj);}
+22 -2
View File
@@ -6,6 +6,8 @@
#include <material.h>
#include <SFML/Audio.hpp>
#include <string>
namespace battleship{
@@ -64,9 +66,27 @@ namespace battleship{
root->getRootNode()->attachChild(model);
}
void GameObject::destroySound(){}
void GameObject::destroySound(){
}
void GameObject::initSound(){}
sf::Sound* GameObject::prepareSfx(sf::SoundBuffer *buffer, string key){
sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()];
string sfxPath = SOL_LUA_STATE[key][id + 1];
sf::Sound *sfx = nullptr;
if(buffer->loadFromFile(sfxPath.c_str())){
sfx = new sf::Sound(*buffer);
sfx->setBuffer(*buffer);
}
return sfx;
}
void GameObject::initSound(){
deathSfxBuffer = new sf::SoundBuffer();
deathSfx = prepareSfx(deathSfxBuffer, "deathSfx");
}
string GameObject::getGameObjTableName(){
switch(type){
+8
View File
@@ -5,6 +5,11 @@
#include <quaternion.h>
#include <model.h>
namespace sf{
class SoundBuffer;
class Sound;
}
namespace battleship{
class Player;
@@ -43,10 +48,13 @@ namespace battleship{
virtual void initModel(bool = true);
virtual void destroySound();
virtual void initSound();
sf::Sound* prepareSfx(sf::SoundBuffer*, std::string);
Type type;
int id;
Player *player;
sf::SoundBuffer *deathSfxBuffer;
sf::Sound *deathSfx;
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;
+16
View File
@@ -62,4 +62,20 @@ namespace battleship{
selectedUnits.clear();
}
void Player::removeUnit(Unit *unit){
for(int i = 0; i < units.size(); i++)
if(unit == units[i]){
removeUnit(i);
break;
}
}
void Player::removeUnit(int id){
if(units[id]->isSelected())
deselectUnit(id);
delete units[id];
units.erase(units.begin() + id);
}
}
+2
View File
@@ -18,6 +18,8 @@ namespace battleship{
void issueOrder(Order::TYPE, std::vector<Order::Target>, bool);
void deselectUnit(int);
void deselectUnits();
void removeUnit(Unit*);
void removeUnit(int);
bool isThisPlayersUnit(GameObject*);
const inline std::vector<Unit*>& getSelectedUnits(){return selectedUnits;}
inline Unit* getSelectedUnit(int i){return selectedUnits[i];}
+2 -8
View File
@@ -14,7 +14,7 @@
#include "projectile.h"
#include "defConfigs.h"
#include "inGameAppState.h"
#include "explosion.h"
#include "fx.h"
using namespace std;
using namespace vb01;
@@ -111,13 +111,7 @@ namespace battleship{
StateManager *sm = GameManager::getSingleton()->getStateManager();
InGameAppState *inGameState = ((InGameAppState*)sm->getAppStateByType((int)AppStateType::IN_GAME_STATE));
if(id == 8)
detonateTorpedo();
else if(weaponTypeId == 1 && (id == 2 || id == 3))
detonateDepthCharge();
else
detonate(pos, -dirVec);
string path = GameManager::getSingleton()->getPath() + "Sounds/Explosions/explosion0" + to_string(rand() % 4) + ".ogg";
}
void Projectile::debug(){
+6
View File
@@ -13,6 +13,12 @@ namespace battleship{
buildStatusForeground = createBar(Unit::lenHpBar, Vector4(0, 0, 1, 1));
}
Structure::~Structure(){
removeBar(buildStatusForeground);
removeBar(buildStatusBackground);
Unit::~Unit();
}
void Structure::update(){
Unit::update();
+1
View File
@@ -11,6 +11,7 @@ namespace battleship{
class Structure : public Unit{
public:
Structure(Player*, int, vb01::Vector3, vb01::Quaternion, int = 0);
~Structure();
inline int getBuildStatus(){return buildStatus;}
inline void incrementBuildStatus(){buildStatus++;}
virtual void update();
+55 -8
View File
@@ -1,6 +1,7 @@
#include <model.h>
#include <quad.h>
#include <material.h>
#include <particleEmitter.h>
#include <texture.h>
#include <ray.h>
@@ -12,6 +13,7 @@
#include "unit.h"
#include "util.h"
#include "game.h"
#include "inGameAppState.h"
#include "defConfigs.h"
#include "pathfinder.h"
@@ -32,8 +34,11 @@ namespace battleship{
hpForegroundNode = createBar(lenHpBar, Vector4(0, 1, 0, 1));
}
//TODO complete implementation
Unit::~Unit() {
removeBar(hpBackgroundNode);
removeBar(hpForegroundNode);
destroySound();
destroyModel();
}
void Unit::init(){
@@ -46,6 +51,7 @@ namespace battleship{
void Unit::initProperties(){
sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()];
string name = SOL_LUA_STATE["name"][id + 1];
health = SOL_LUA_STATE["health"][id + 1];
maxHealth = health;
@@ -71,16 +77,22 @@ namespace battleship{
selectionSfx->stop();
delete selectionSfx;
delete selectionSfxBuffer;
if(fireSfx){
fireSfx->stop();
delete fireSfx;
delete fireSfxBuffer;
}
}
void Unit::initSound(){
sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()];
string name = SOL_LUA_STATE["name"][id + 1];
selectionSfxBuffer = new sf::SoundBuffer();
string sfxPath = SOL_LUA_STATE["selectionSfx"][id + 1];
GameObject::initSound();
if(selectionSfxBuffer->loadFromFile(sfxPath.c_str()))
selectionSfx = new sf::Sound(*selectionSfxBuffer);
selectionSfxBuffer = new sf::SoundBuffer();
selectionSfx = GameObject::prepareSfx(selectionSfxBuffer, "selectionSfx");
fireSfxBuffer = new sf::SoundBuffer();
fireSfx = prepareSfx(fireSfxBuffer, "fireSfx");
}
void Unit::reinit(){
@@ -110,6 +122,11 @@ namespace battleship{
return node;
}
void Unit::removeBar(Node *node){
Root::getSingleton()->getGuiNode()->dettachChild(node);
delete node;
}
void Unit::initUnitStats(){
}
@@ -134,7 +151,37 @@ namespace battleship{
}
void Unit::blowUp(){
working = false;
Root *root = Root::getSingleton();
const int numFrames = 1;
string p[numFrames];
for(int i = 0; i < numFrames; i++)
p[i] = GameManager::getSingleton()->getPath() + "Textures/Explosion/explosion0" + to_string(7) + ".png";
Texture *tex = new Texture(numFrames, p, false);
Material *mat = new Material(root->getLibPath() + "particle");
mat->addVec4Uniform("startColor", Vector4(1, 1, 1, 1));
mat->addVec4Uniform("endColor", Vector4(1, 1, 0, 1));
mat->addTexUniform("tex", tex, false);
ParticleEmitter *pe = new ParticleEmitter(1);
pe->setMaterial(mat);
pe->setLowLife(3);
pe->setHighLife(3);
pe->setSize(10 * Vector2::VEC_IJ);
pe->setSpeed(0);
Node *node = new Node(pos + Vector3(0, 2, 0));
node->attachParticleEmitter(pe);
node->lookAt(Vector3::VEC_J, Vector3::VEC_K);
root->getRootNode()->attachChild(node);
Fx fx(2500, deathSfx, node);
fx.activate();
Game::getSingleton()->addFx(fx);
player->removeUnit(this);
}
void Unit::displayUnitStats(Node *foreground, Node *background, int currVal, int maxVal, Vector2 offset) {
+8 -4
View File
@@ -6,12 +6,15 @@
#include <quaternion.h>
#include <lineRenderer.h>
#include <SFML/Audio.hpp>
#include "gameManager.h"
#include "gameObject.h"
#include "projectile.h"
namespace sf{
class SoundBuffer;
class Sound;
}
namespace vb01{
class Mesh;
class Quad;
@@ -59,7 +62,6 @@ namespace battleship{
virtual void addOrder(Order);
virtual void reinit();
inline vb01::Vector3 getCorner(int i){return corners[i];}
inline bool isWorking(){return working;}
inline vb01::Vector2 getScreenPos(){return screenPos;}
inline vb01::Vector3* getPosPtr() {return &pos;}
inline float getLineOfSight() {return lineOfSight;}
@@ -83,9 +85,10 @@ namespace battleship{
vb01::Vector2 screenPos;
std::vector<Order> orders;
vb01::Vector3 corners[8];
sf::SoundBuffer *fireSfxBuffer;
sf::Sound *fireSfx = nullptr;
int health, maxHealth, cost, id, playerId, lenHpBar = 200, rateOfFire;
s64 orderLineDispTime = 0, lastFireTime = 0;
bool working = true;
float lineOfSight, range;
void removeOrder(int);
@@ -101,6 +104,7 @@ namespace battleship{
virtual void patrol(Order){}
virtual void launch(Order){}
vb01::Node* createBar(float, vb01::Vector4);
void removeBar(vb01::Node*);
};
}
+16
View File
@@ -26,6 +26,13 @@ namespace battleship{
debugMat->addVec4Uniform("diffuseColor", Vector4::VEC_IJKL);
}
Vehicle::~Vehicle(){
removeAllPathpoints();
delete debugMat;
Unit::~Unit();
}
void Vehicle::halt(){
Unit::halt();
removeAllPathpoints();
@@ -221,4 +228,13 @@ namespace battleship{
fire();
}
}
void Vehicle::fire(){
fireSfx->play();
Unit *targetUnit = orders[0].targets[0].unit;
if(targetUnit)
targetUnit->takeDamage(1);
}
}
+2 -1
View File
@@ -11,6 +11,7 @@ namespace battleship{
class Vehicle : public Unit{
public:
Vehicle(Player*, int, vb01::Vector3, vb01::Quaternion);
~Vehicle();
void move(Order);
private:
bool pursuingTarget = false;
@@ -34,7 +35,7 @@ namespace battleship{
void navigate(Order, float = 0.);
void alignToSurface();
void attack(Order);
void fire(){}
virtual void fire();
};
}