Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Jak dodać prefabrykaty kafelków wody do palety

Ostatnio zmodyfikowano 2024-11-01 20:30
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
Jak dodać prefabrykaty kafelków wody do palety
» 2024-11-01 17:27:53
Witam.
Mam obiekty Terrain oraz Water dla każdego z chunków mapy. Chciałbym dodać prefaby wody do palety, tak aby można było je wybrać i rysować tę wodę po terenie. Problem, który napotkałem polega na tym, że wybranie prefabu wody jest również prefabem terenu - set_0_water_sands - set_0_sands_water. Nie wiem jak to pogodzić i jak uporządkować kod. Planuję również w niedalekiej przyszłości rysować shaderem na tym szarym polu. No i właśnie to jest ten problem - jak stworzyć prefabrykaty dla wody ?

Poniżej obrazek przedstawiający kafelki granicze dla terenu

Tak wczytuje prefaby Terenu:
C/C++
void createTerrainPrefabs() {
   
terrainGameObjects.clear();
   
   
// TO-DO
   
terrainGameObjects.push_back( new TerrainPrefab( "tiles/tile_0_water", 0 ) );
   
terrainGameObjects.push_back( new TerrainPrefab( "tiles/tile_1_sands", 1 ) );
   
terrainGameObjects.push_back( new TerrainPrefab( "tiles/tile_2_grass", 2 ) );
   
terrainGameObjects.push_back( new TerrainPrefab( "tiles/tile_3_gravel", 3 ) );
   
terrainGameObjects.push_back( new TerrainPrefab( "tiles/tile_4_steps", 4 ) );
   
   
countOfBasicTerrain = 5;
   
   
std::vector < string > sets_names;
   
sets_names.push_back( "tiles/set_0_water_sands" );
   
sets_names.push_back( "tiles/set_0_sands_water" );
   
sets_names.push_back( "tiles/set_1_sands_grass" );
   
sets_names.push_back( "tiles/set_1_grass_sands" );
   
   
short id = countOfBasicTerrain;
   
   
for( auto & texture: textures ) {
       
for( auto & name: sets_names ) {
           
if( texture->name.find( name ) != std::string::npos ) {
               
               
TerrainPrefab * tpref = new TerrainPrefab( texture->name, id );
               
terrainGameObjects.push_back( tpref );
               
//cout << tpref->name << "\t" << id << "\n";
               
id += 1;
           
}
        }
    }
   
   
   
// create tileset
   
sf::RenderTexture rtex;
   
rtex.create( id * 64, 64 );
   
rtex.clear( sf::Color::Transparent );
   
   
short offsetX = 0;
   
   
for( auto & t: terrainGameObjects ) {
       
sf::Texture tex = * dynamic_cast < TerrainPrefab * >( t )->texture->texture;
       
sf::Sprite spr( tex );
       
spr.setPosition( offsetX, 0 );
       
rtex.draw( spr );
       
offsetX += 64;
   
}
   
rtex.display();
   
   
tileset = rtex.getTexture();
   
* getTexture( "tiles/0_tileset" )->texture = tileset;
   
}

A tak przypisuję te obiekty do palety:
C/C++
void setTerrainObjectsToPalette() {
   
   
availableGameObjects.clear();
   
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 3 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 4 ] );
   
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
   
// SET - WATER_SANDS
   
short id = 4;
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 3 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 6 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 8 ] );
   
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
   
// SET - SANDS_WATER
   
id += 8;
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 3 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 0 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 6 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 8 ] );
   
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
   
// SET - GRASS_SANDS
   
id += 8;
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 3 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 6 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 8 ] );
   
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
   
// SET - GRASS_SANDS
   
id += 8;
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 2 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 3 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 4 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ 1 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 5 ] );
   
   
availableGameObjects.push_back( terrainGameObjects[ id + 6 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 7 ] );
   
availableGameObjects.push_back( terrainGameObjects[ id + 8 ] );
   
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
   
availableGameObjects.push_back( nullptr );
}
P-181797
tBane
Temat założony przez niniejszego użytkownika
» 2024-11-01 20:30:06
Znalazłem rozwiązanie :


C/C++
class WaterPrefab
    : public GameObject
{
public:
   
short id;
   
TerrainPrefab * terrain;
   
Texture * texture;
   
Shader * shader;
   
   
WaterPrefab( string name, short id, TerrainPrefab * terrain )
        :
GameObject( name )
   
{
       
type = GameObjectType::Water;
       
       
this->terrain = terrain;
       
this->id = id;
       
       
texture = terrain->texture;
       
shader = getShader( "shaders/" + name );
       
collider->shape->setPosition( position );
   
}
}
;

void createWaterPrefabs() {
   
   
waterGameObjects.clear();
   
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ 0 ] ) ) );
   
countOfBasicWater = 1;
   
   
/////////////////////////////////////////////////////
   
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ countOfBasicTerrain + 0 ] ) ) );
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ countOfBasicTerrain + 1 ] ) ) );
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ countOfBasicTerrain + 2 ] ) ) );
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ countOfBasicTerrain + 3 ] ) ) );
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ countOfBasicTerrain + 4 ] ) ) );
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ countOfBasicTerrain + 5 ] ) ) );
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ countOfBasicTerrain + 6 ] ) ) );
   
waterGameObjects.push_back( new WaterPrefab( "water2", 1, dynamic_cast < TerrainPrefab * >( terrainGameObjects[ countOfBasicTerrain + 7 ] ) ) );
}
P-181799
« 1 »
  Strona 1 z 1