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

Obliczanie współrzędnych mapy

Ostatnio zmodyfikowano 2024-08-23 15:43
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
» 2024-08-22 20:39:16
jednak masz rację:

C/C++
map_position.x =( int( map->terrain->coords.x ) * tileSide ) + 8 * tileSide;
map_position.y =( int( map->terrain->coords.y ) * tileSide ) + 8 * tileSide;
P-181500
pekfos
» 2024-08-22 20:42:00
To znaczy co? Wkleiłeś zły kod, czy dalej nie działa jak nie masz tego warunku?
P-181501
tBane
Temat założony przez niniejszego użytkownika
» 2024-08-22 20:52:47
O kurde. Jednak zmęczenie robi swoje. Zapomniałem jeszcze dopisać coordy do mapy i wczytywać pliki z coordów mapy a nie terenu ani podłogi. Wszystko mi się pomieszało ale naprawiłem kod. Visiblings te źle było obliczane.

C/C++
void load() {
   
   
string filename = "world/maps/map_" + to_string( coords.x ) + "_" + to_string( coords.y ) + ".txt";
   
ifstream file( filename );
   
   
if( !file.is_open() ) {
       
//cout << "cant open map: " << filename << "\n";
       
return;
   
}
   
...
   
   
void mapVisiblings() {
       
       
sf::Vector2f map_position;
       
       
for( auto & map: maps ) {
           
           
map_position.x =( int( map->terrain->coords.x ) * tileSide ) + 8 * tileSide;
           
map_position.y =( int( map->terrain->coords.y ) * tileSide ) + 8 * tileSide;
           
           
float width = screenWidth * 2.0f;
           
float height = screenHeight * 2.0f;
           
           
map->isVisible = intersectionTwoRectangles( cam->position.x, cam->position.y, width, height, map_position.x, map_position.y, 16 * tileSide, 16 * tileSide );
           
       
}
       
       
    }
   

Teraz edytuje mi tylko pierwszą mapę. Też gdzieś jest błąd zaraz kod wrzucę. Najgorzej muszę z backupa przywrócić projekt bo tak namieszałem w kodzie, że nic nie działa. Łatwiej będzie napisać od nowa :D
P-181502
tBane
Temat założony przez niniejszego użytkownika
» 2024-08-22 21:16:00
No więc. Zaczęło się od tego, że wymyśliłem sobie, że wygodnie będzie jak mapa lub budynek w grze będą miały zmienną terrain lub floors do przechowywania kafelków. Teraz wszystko działa ale ... pozycje terrain oraz floors są "pozycjami mapy" i w ten sposób nie mogę przypisać floors do budynku, bo pozycje floors są wielokrotnościami width=16 oraz height=16. A dla budynku potrzebuję np. aby pozycja była np. 15 i tu albo wykonam jakieś kosmiczne obliczenia ... albo napisze poprawnie klase Floors, a że klasa Floors to w sumie zmieniona klasa Terrain to obie są do naprawy.

C/C++
#ifndef TerrainAndFloors_hpp
#define TerrainAndFloors_hpp

float tileSide = 16.0f;

enum class terrainType { grass, sands, water, gravel };
enum class floorType { floor_0, floor_1, floor_2, floor_3 };

class TerrainPrefab
    : public GameObject
{
public:
   
Texture * texture;
   
terrainType ttype;
   
   
TerrainPrefab( string name, terrainType ttype )
        :
GameObject( name )
   
{
       
type = gameObjectType::Terrain;
       
texture = getTexture( name );
       
this->ttype = ttype;
   
}
   
}
;

class FloorPrefab
    : public GameObject
{
public:
   
Texture * texture;
   
floorType ftype;
   
   
FloorPrefab( string name, floorType ftype )
        :
GameObject( name )
   
{
       
type = gameObjectType::Floor;
       
texture = getTexture( name );
       
this->ftype = ftype;
   
}
   
}
;

class Terrain
    : public sf::Drawable
    , public sf::Transformable
{
public:
   
int width, height;
   
sf::Vector2i coords;
   
   
sf::VertexArray vertexes;
   
sf::Texture tileset;
   
   
std::vector < int > tiles;
   
   
Terrain( int x, int y, int width, int height ) {
       
       
coords.x = x;
       
coords.y = y;
       
       
this->width = width;
       
this->height = height;
       
       
tileset = sf::Texture();
       
tileset = * getTexture( "tiles/0_tileset" )->texture;
       
       
vertexes.setPrimitiveType( sf::Triangles );
       
vertexes.resize( width * height * 6 ); // widthMap * heightMap * TwoTrianglesVertices
       
       
tiles.resize( width * height );
       
       
int tu, tv;
       
int coord_x, coord_y;
       
       
// TERRAIN - GRASS
       
for( int y = 0; y < height; y++ )
       
for( int x = 0; x < width; x++ ) {
           
           
sf::Vertex * triangles = & vertexes[( y * width + x ) * 6 ];
           
           
coord_x =( coords.x * width + x );
           
coord_y =( coords.y * height + y );
           
           
triangles[ 0 ].position = sf::Vector2f( coord_x * tileSide, coord_y * tileSide );
           
triangles[ 1 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide, coord_y * tileSide );
           
triangles[ 2 ].position = sf::Vector2f( coord_x * tileSide,( coord_y + 1 ) * tileSide );
           
triangles[ 3 ].position = sf::Vector2f( coord_x * tileSide,( coord_y + 1 ) * tileSide );
           
triangles[ 4 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide, coord_y * tileSide );
           
triangles[ 5 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide,( coord_y + 1 ) * tileSide );
           
           
edit( x, y, 0 );
       
}
       
       
    }
   
   
void edit( int x, int y, int value ) {
       
       
if( x < 0 || x >= width || y < 0 || y >= height )
           
 return;
       
       
if( value > 3 || value < 0 )
           
 return;
       
       
tiles[ y * width + x ] = value;
       
       
int global_x = coords.x * width + x;
       
int global_y = coords.y * height + y;
       
       
sf::Vertex * triangles = & vertexes[( y * width + x ) * 6 ];
       
       
int tu =( int( global_x * tileSide ) % 64 ) +( value * 64 );
       
int tv =( int( global_y * tileSide ) % 64 );
       
       
//cout << "tu: " << tu << ", tv: " << tv << "\n";
       
       
triangles[ 0 ].texCoords = sf::Vector2f( tu, tv );
       
triangles[ 1 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 2 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 3 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 4 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 5 ].texCoords = sf::Vector2f( tu + tileSide, tv + tileSide );
   
}
   
   
void edit( sf::Vector2f worldMousePosition, int value ) {
       
       
int global_x = worldMousePosition.x / 16;
       
int global_y = worldMousePosition.y / 16;
       
       
int onMap_x = global_x - coords.x * width;
       
int onMap_y = global_y - coords.y * height;
       
       
if( onMap_x < 0 || onMap_x >= width || onMap_y < 0 || onMap_y >= height )
           
 return;
       
       
if( value > 3 || value < 0 )
           
 return;
       
       
tiles[ onMap_y * width + onMap_x ] = value;
       
       
sf::Vertex * triangles = & vertexes[( onMap_y * width + onMap_x ) * 6 ];
       
       
int tu =( int( global_x * tileSide ) % 64 ) +( value * 64 );
       
int tv =( int( global_y * tileSide ) % 64 );
       
       
//cout << "tu: " << tu << ", tv: " << tv << "\n";
       
       
triangles[ 0 ].texCoords = sf::Vector2f( tu, tv );
       
triangles[ 1 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 2 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 3 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 4 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 5 ].texCoords = sf::Vector2f( tu + tileSide, tv + tileSide );
   
}
   
   
void edit( sf::Vector2f worldMousePosition, TerrainPrefab * terrainPrefab ) {
       
       
if( terrainPrefab->name == "tiles/tile_0_grass" )
           
 edit( worldMousePosition, 0 );
       
       
if( terrainPrefab->name == "tiles/tile_1_sands" )
           
 edit( worldMousePosition, 1 );
       
       
if( terrainPrefab->name == "tiles/tile_2_water" )
           
 edit( worldMousePosition, 2 );
       
       
if( terrainPrefab->name == "tiles/tile_3_gravel" )
           
 edit( worldMousePosition, 3 );
       
   
}
   
private:
   
   
virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const
   
{
       
       
// draw tiles - terrain
       
states.transform *= getTransform();
       
states.texture = & tileset; // TO-DO
       
target.draw( vertexes, states );
   
}
}
;

class Floors
    : public sf::Drawable
    , public sf::Transformable
{
public:
   
int width, height;
   
sf::Vector2i coords;
   
   
sf::VertexArray vertexes;
   
sf::Texture floorset;
   
   
std::vector < int > floors;
   
   
Floors( int x, int y, int width, int height ) {
       
       
coords.x = x;
       
coords.y = y;
       
       
this->width = width;
       
this->height = height;
       
       
floorset = sf::Texture();
       
floorset = * getTexture( "floors/0_floorset" )->texture;
       
       
vertexes.setPrimitiveType( sf::Triangles );
       
vertexes.resize( width * height * 6 ); // widthMap * heightMap * TwoTrianglesVertices
       
       
floors.resize( width * height );
       
       
int coord_x, coord_y;
       
       
for( int y = 0; y < height; y++ )
       
for( int x = 0; x < width; x++ ) {
           
           
sf::Vertex * triangles = & vertexes[( y * width + x ) * 6 ];
           
           
coord_x =( coords.x * width + x );
           
coord_y =( coords.y * height + y );
           
           
triangles[ 0 ].position = sf::Vector2f( coord_x * tileSide, coord_y * tileSide );
           
triangles[ 1 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide, coord_y * tileSide );
           
triangles[ 2 ].position = sf::Vector2f( coord_x * tileSide,( coord_y + 1 ) * tileSide );
           
triangles[ 3 ].position = sf::Vector2f( coord_x * tileSide,( coord_y + 1 ) * tileSide );
           
triangles[ 4 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide, coord_y * tileSide );
           
triangles[ 5 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide,( coord_y + 1 ) * tileSide );
           
           
edit( x, y, 0 );
       
}
       
       
    }
   
   
void edit( int x, int y, int value ) {
       
       
if( x < 0 || x >= width || y < 0 || y >= height )
           
 return;
       
       
if( value > 3 || value < 0 )
           
 return;
       
       
floors[ y * width + x ] = value;
       
       
int global_x = coords.x * width + x;
       
int global_y = coords.y * height + y;
       
       
sf::Vertex * triangles = & vertexes[( y * width + x ) * 6 ];
       
       
int tu =( int( global_x * tileSide ) % 64 ) +( value * 64 );
       
int tv =( int( global_y * tileSide ) % 64 );
       
       
//cout << "tu: " << tu << ", tv: " << tv << "\n";
       
       
triangles[ 0 ].texCoords = sf::Vector2f( tu, tv );
       
triangles[ 1 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 2 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 3 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 4 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 5 ].texCoords = sf::Vector2f( tu + tileSide, tv + tileSide );
   
}
   
   
void edit( sf::Vector2f worldMousePosition, int value ) {
       
       
int global_x = worldMousePosition.x / 16;
       
int global_y = worldMousePosition.y / 16;
       
       
int onMap_x = global_x - coords.x * width;
       
int onMap_y = global_y - coords.y * height;
       
       
if( onMap_x < 0 || onMap_x >= width || onMap_y < 0 || onMap_y >= height )
           
 return;
       
       
if( value > 3 || value < 0 )
           
 return;
       
       
floors[ onMap_y * width + onMap_x ] = value;
       
       
sf::Vertex * triangles = & vertexes[( onMap_y * width + onMap_x ) * 6 ];
       
       
int tu =( int( global_x * tileSide ) % 64 ) +( value * 64 );
       
int tv =( int( global_y * tileSide ) % 64 );
       
       
//cout << "tu: " << tu << ", tv: " << tv << "\n";
       
       
triangles[ 0 ].texCoords = sf::Vector2f( tu, tv );
       
triangles[ 1 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 2 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 3 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 4 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 5 ].texCoords = sf::Vector2f( tu + tileSide, tv + tileSide );
   
}
   
   
void edit( sf::Vector2f worldMousePosition, FloorPrefab * floorPrefab ) {
       
       
if( floorPrefab->name == "floors/floor_0" )
           
 edit( worldMousePosition, 0 );
       
       
if( floorPrefab->name == "floors/floor_1" )
           
 edit( worldMousePosition, 1 );
       
       
if( floorPrefab->name == "floors/floor_2" )
           
 edit( worldMousePosition, 2 );
       
       
if( floorPrefab->name == "floors/floor_3" )
           
 edit( worldMousePosition, 3 );
       
   
}
   
   
private:
   
   
virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const
   
{
       
       
states.transform *= getTransform();
       
states.texture = & floorset; // TO-DO
       
target.draw( vertexes, states );
       
       
   
}
}
;
#endif

Czyli co trzeba zrobić?
A no trzeba zrobić tak, aby Terrain/Floors przyjmował jako argumenty: position.start.x, position.start.y, width, height
A
C/C++
Map( int x, int y ) {
   
   
terrain = new Terrain( x * 16, y * 16, 16, 16 );
   
floors = new Floors( x * 16, y * 16, 16, 16 );
   
   
clearAllLists();
   
//load();
   
   
isVisible = false;
}

A tutaj jest kod map:
C/C++
#ifndef Maps_hpp
#define Maps_hpp

// TO-DO - all to the rewrite

class Map {
public:
   
Terrain * terrain;
   
Floors * floors;
   
   
std::vector < Nature * > _natures;
   
std::vector < ItemOnMap * > _itemsOnMap;
   
std::vector < Path * > _paths;
   
std::vector < Furniture * > _furnitures;
   
std::vector < Wall * > _walls;
   
std::vector < Monster * > _monsters;
   
std::vector < Building * > _buildings;
   
std::vector < Character * > _characters;
   
std::vector < InventoryOnMap * > _inventoriesOnMap;
   
   
bool isVisible;
   
   
Map( int x, int y ) {
       
       
terrain = new Terrain( x, y, 16, 16 );
       
floors = new Floors( x, y, 16, 16 );
       
       
clearAllLists();
       
//load();
       
       
isVisible = false;
   
}
   
   
~Map() { }
   
   
void clearAllLists() {
       
       
_natures.clear();
       
_itemsOnMap.clear();
       
_paths.clear();
       
_furnitures.clear();
       
_walls.clear();
       
_monsters.clear();
       
_buildings.clear();
       
_characters.clear();
       
_inventoriesOnMap.clear();
       
   
}
   
   
void save() {
       
       
// create a folder when it no exists
       
if( !std::filesystem::exists( "world/maps" ) ) {
           
           
if( std::filesystem::create_directory( "world/maps" ) ) {
               
cout << "create directory \"world\/maps\"\n";
           
}
           
else {
               
cout << "error with create a directory \"world\/maps\n";
           
}
        }
       
       
string filename = "world/maps/map_" + to_string( int( terrain->coords.x ) ) + "_" + to_string( int( terrain->coords.y ) ) + ".txt";
       
std::ofstream file( filename );
       
       
if( !file.is_open() ) {
           
cout << "cant open file to save map: " << filename << "\n";
           
return;
       
}
       
       
// save tiles
       
for( int y = 0; y < 16; y++ ) {
           
for( int x = 0; x < 16; x++ ) {
               
               
file << terrain->tiles[ y * terrain->width + x ];
               
if( x != terrain->width - 1 )
                   
 file << " ";
               
           
}
           
           
file << "\n";
       
}
       
       
file << "\n";
       
       
// save floors
       
for( int y = 0; y < 16; y++ ) {
           
for( int x = 0; x < 16; x++ ) {
               
               
file << floors->floors[ y * floors->width + x ];
               
if( x != floors->width - 1 )
                   
 file << " ";
               
           
}
           
           
file << "\n";
       
}
       
       
file << "\n";
       
       
if( _natures.size() > 0 )
           
 file << "// NATURES\n";
       
       
for( auto & nature: _natures )
           
 file << "Nature " << char( 34 ) << nature->name << char( 34 ) << " " << int( nature->position.x ) << " " << int( nature->position.y ) << "\n";
       
       
if( _natures.size() > 0 )
           
 file << "\n";
       
       
       
if( _itemsOnMap.size() > 0 )
           
 file << "// ITEMS\n";
       
       
for( auto & item: _itemsOnMap )
           
 file << "Item " << char( 34 ) << item->name << char( 34 ) << " " << int( item->position.x ) << " " << int( item->position.y ) << "\n";
       
       
if( _itemsOnMap.size() > 0 )
           
 file << "\n";
       
       
       
if( _paths.size() > 0 )
           
 file << "// PATHS\n";
       
       
for( auto & path: _paths )
           
 file << "Path " << char( 34 ) << path->name << char( 34 ) << " " << int( path->position.x ) << " " << int( path->position.y ) << "\n";
       
       
if( _paths.size() > 0 )
           
 file << "\n";
       
       
       
if( _furnitures.size() > 0 )
           
 file << "// FURNITURES\n";
       
       
for( auto & furniture: _furnitures )
           
 file << "Furniture " << char( 34 ) << furniture->name << char( 34 ) << " " << int( furniture->position.x ) << " " << int( furniture->position.y ) << "\n";
       
       
if( _furnitures.size() > 0 )
           
 file << "\n";
       
       
       
if( _walls.size() > 0 )
           
 file << "// WALLS\n";
       
       
for( auto & wall: _walls )
           
 file << "Wall " << char( 34 ) << wall->name << char( 34 ) << " " << int( wall->position.x ) << " " << int( wall->position.y ) << "\n";
       
       
if( _walls.size() > 0 )
           
 file << "\n";
       
       
       
if( _monsters.size() > 0 )
           
 file << "// MONSTERS\n";
       
       
for( auto & monster: _monsters )
           
 file << "Monster " << char( 34 ) << monster->name << char( 34 ) << " " << int( monster->position.x ) << " " << int( monster->position.y ) << "\n";
       
       
if( _monsters.size() > 0 )
           
 file << "\n";
       
       
       
       
if( _buildings.size() > 0 )
           
 file << "// BUILDINGS\n";
       
       
for( auto & building: _buildings )
           
 file << "Building " << char( 34 ) << building->name << char( 34 ) << " " << int( building->position.x ) << " " << int( building->position.y ) << "\n";
       
       
if( _buildings.size() > 0 )
           
 file << "\n";
       
       
       
if( _characters.size() > 0 )
           
 file << "// CHARACTERS\n";
       
       
for( auto & character: _characters )
           
 file << "Character " << char( 34 ) << character->name << char( 34 ) << " " << int( character->position.x ) << " " << int( character->position.y ) << "\n";
       
       
if( _characters.size() > 0 )
           
 file << "\n";
       
       
       
if( _inventoriesOnMap.size() > 0 )
           
 file << "// INVENTORIES\n";
       
       
for( auto & inventory: _inventoriesOnMap )
           
 file << "Inventory " << char( 34 ) << inventory->name << char( 34 ) << " " << int( inventory->position.x ) << " " << int( inventory->position.y ) << "\n";
       
       
if( _inventoriesOnMap.size() > 0 )
           
 file << "\n";
       
       
       
       
file.close();
   
}
   
   
void load() {
       
       
string filename = "world/maps/map_" + to_string( int( terrain->coords.x ) ) + "_" + to_string( int( terrain->coords.y ) ) + ".txt";
       
ifstream file( filename );
       
       
if( !file.is_open() ) {
           
//cout << "cant open map: " << filename << "\n";
           
return;
       
}
       
       
//cout << "open map: " << filename << "\n";
       
       
string line;
       
string objectType;
       
string objectName;
       
       
// load tiles
       
int value;
       
for( int y = 0; y < 16; y++ ) {
           
for( int x = 0; x < 16; x++ ) {
               
file >> value;
               
editTile( x, y, value );;
           
}
        }
       
       
// load floors
       
for( int y = 0; y < 16; y++ ) {
           
for( int x = 0; x < 16; x++ ) {
               
file >> value;
               
editFloor( x, y, value );;
           
}
        }
       
       
// load GameObjects
       
while( std::getline( file, line ) ) {
           
           
if( line.empty() ) {
               
//cout << "empty line\n";
               
continue;
           
}
           
           
           
std::istringstream lineStream( line );
           
lineStream >> objectType;
           
           
if( objectType == "Nature" ) {
               
               
int x, y;
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
Nature * nature = new Nature( getPrefab( objectName ), x, y );
               
_natures.push_back( nature );
               
//cout << "Nature: " << objectName << "\n";
           
}
           
           
if( objectType == "Item" ) {
               
               
int x, y;
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
ItemOnMap * itemOnMap = new ItemOnMap( getItem( objectName ), x, y );
               
_itemsOnMap.push_back( itemOnMap );
               
//cout << "Item: " << objectName << "\n";
               
           
}
           
           
if( objectType == "Path" ) {
               
               
string name;
               
int x, y;
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
Path * path = new Path( getPrefab( objectName ), x, y );
               
_paths.push_back( path );
               
//cout << "Path: " << objectName << "\n";
           
}
           
           
if( objectType == "Furniture" ) {
               
string name;
               
int x, y;
               
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
Furniture * furniture = new Furniture( getPrefab( objectName ), x, y );
               
_furnitures.push_back( furniture );
               
//cout << "Furniture: " << objectName << "\n";
           
}
           
           
if( objectType == "Wall" ) {
               
string name;
               
int x, y;
               
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
Wall * wall = new Wall( getPrefab( objectName ), x, y );
               
_walls.push_back( wall );
               
//cout << "Wall: " << objectName << "\n";
           
}
           
           
if( objectType == "Monster" ) {
               
               
int x, y;
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
Monster * monster = new Monster( getPrefab( objectName ), x, y );
               
_monsters.push_back( monster );
               
//cout << "Monster: " << objectName << "\n";
           
}
           
           
if( objectType == "Building" ) {
               
int x, y;
               
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
Building * building = new Building( objectName, x, y );
               
_buildings.push_back( building );
               
//cout << "Building: " << objectName << "\n";
           
}
           
           
if( objectType == "Character" ) {
               
int x, y;
               
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
Character * character = new Character( getPrefab( objectName ), x, y );
               
_characters.push_back( character );
               
               
//cout << "Character: " << objectName << "\n";
           
}
           
           
if( objectType == "Inventory" ) {
               
int x, y;
               
               
getline( lineStream, objectName, '"' ); // Pomijamy pierwszy znak cudzysłowu
               
getline( lineStream, objectName, '"' ); // Wczytaj nazwę do kolejnego cudzysłowu
               
lineStream >> x;
               
lineStream >> y;
               
               
// TO-DO
           
}
        }
       
       
file.close();
   
}
   
   
void editTile( sf::Vector2f worldMousePosition, int value ) {
       
       
terrain->edit( worldMousePosition, value );
   
}
   
   
void editTile( int x, int y, int value ) {
       
terrain->edit( x, y, value );
       
   
}
   
   
void editFloor( sf::Vector2f worldMousePosition, int value ) {
       
       
floors->edit( worldMousePosition, value );
   
}
   
   
void editFloor( int x, int y, int value ) {
       
       
floors->edit( x, y, value );
   
}
   
   
void addGameObjectsToMainLists() {
       
       
for( auto & nature: _natures ) {
           
gameObjects.push_back( nature );
           
natures.push_back( nature );
       
}
       
       
for( auto & item: _itemsOnMap ) {
           
gameObjects.push_back( item );
           
itemsOnMap.push_back( item );
       
}
       
       
for( auto & path: _paths ) {
           
gameObjects.push_back( path );
           
paths.push_back( path );
       
}
       
       
for( auto & furniture: _furnitures ) {
           
gameObjects.push_back( furniture );
           
furnitures.push_back( furniture );
       
}
       
       
for( auto & wall: _walls ) {
           
gameObjects.push_back( wall );
           
walls.push_back( wall );
       
}
       
       
for( auto & monster: _monsters ) {
           
gameObjects.push_back( monster );
           
monsters.push_back( monster );
       
}
       
       
for( auto & building: _buildings ) {
           
building->addGameObjectsToMainLists();
       
}
       
       
for( auto & character: _characters ) {
           
gameObjects.push_back( character );
           
characters.push_back( character );
       
}
       
       
for( auto & inventory: _inventoriesOnMap ) {
           
gameObjects.push_back( inventory );
           
inventoriesOnMap.push_back( inventory );
       
}
    }
   
   
void deleteGameObjects() {
       
       
std::vector < Nature * > new_natures;
       
std::vector < ItemOnMap * > new_itemsOnMap;
       
std::vector < Path * > new_paths;
       
std::vector < Furniture * > new_furnitures;
       
std::vector < Wall * > new_walls;
       
std::vector < Monster * > new_monsters;
       
std::vector < Building * > new_buildings;
       
std::vector < Character * > new_characters;
       
std::vector < InventoryOnMap * > new_inventoriesOnMap;
       
       
new_natures.clear();
       
new_itemsOnMap.clear();
       
new_paths.clear();
       
new_furnitures.clear();
       
new_walls.clear();
       
new_monsters.clear();
       
new_buildings.clear();
       
new_characters.clear();
       
new_inventoriesOnMap.clear();
       
       
       
for( auto & nature: _natures )
       
if( nature->toDelete != true )
           
 new_natures.push_back( nature );
       
       
for( auto & item: _itemsOnMap )
       
if( item->toDelete != true )
           
 new_itemsOnMap.push_back( item );
       
       
for( auto & path: _paths )
       
if( path->toDelete != true )
           
 new_paths.push_back( path );
       
       
for( auto & furniture: _furnitures )
       
if( furniture->toDelete != true )
           
 new_furnitures.push_back( furniture );
       
       
for( auto & wall: _walls )
       
if( wall->toDelete != true )
           
 new_walls.push_back( wall );
       
       
for( auto & monster: _monsters )
       
if( monster->toDelete != true )
           
 new_monsters.push_back( monster );
       
       
for( auto & building: _buildings )
       
if( building->toDelete != true )
           
 new_buildings.push_back( building );
       
       
for( auto & character: _characters )
       
if( character->toDelete != true )
           
 new_characters.push_back( character );
       
       
for( auto & inventory: _inventoriesOnMap )
       
if( inventory->toDelete != true )
           
 new_inventoriesOnMap.push_back( inventory );
       
       
_natures = new_natures;
       
_itemsOnMap = new_itemsOnMap;
       
_paths = new_paths;
       
_furnitures = new_furnitures;
       
_walls = new_walls;
       
_monsters = new_monsters;
       
_buildings = new_buildings;
       
_characters = new_characters;
       
_inventoriesOnMap = new_inventoriesOnMap;
       
       
   
}
   
   
void draw()
   
{
       
window->draw( * terrain );
       
window->draw( * floors );
   
}
   
}
;

class World {
public:
   
std::vector < Map * > maps;
   
   
World() {
       
       
maps.clear();
   
}
   
   
void load() {
       
       
maps.clear();
       
       
// 8 x 4
       
int start_x = 0;
       
int end_x = 16;
       
int start_y = 0;
       
int end_y = 16;
       
       
// CREATING MAPS
       
for( int y = start_y; y <= end_y; y++ )
       
for( int x = start_x; x <= end_x; x++ )
           
 maps.push_back( new Map( x, y ) );
       
       
for( auto & map: maps ) {
           
map->load();
       
}
       
       
       
// MAP IS VISIBLE OR NOT
       
mapVisiblings();
       
       
// ADDING ALL THE GAMEOBJECTS FROM MAP TO MAIN LISTS OF GAMEOBJECTS
       
clearAllMainListsOfGameObjects();
       
       
for( auto & map: maps )
           
 map->addGameObjectsToMainLists();
       
   
}
   
   
Map * getMap( sf::Vector2f worldMousePosition ) {
       
       
float left, right, top, bottom;
       
       
for( auto & m: maps ) {
           
           
left = m->terrain->coords.x * 16 * tileSide;
           
right = left + m->terrain->width * tileSide;
           
top = m->terrain->coords.y * 16 * tileSide;
           
bottom = top + m->terrain->height * tileSide;
           
           
if( worldMousePosition.x >= left && worldMousePosition.x <= right && worldMousePosition.y >= top && worldMousePosition.y <= bottom )
               
 return m;
           
       
}
       
       
return nullptr;
   
}
   
   
void mapVisiblings() {
       
       
sf::Vector2f map_position;
       
       
for( auto & map: maps ) {
           
           
map_position.x =( int( map->terrain->coords.x ) * 16 * tileSide ) + 8 * tileSide;
           
map_position.y =( int( map->terrain->coords.y ) * 16 * tileSide ) + 8 * tileSide;
           
           
float width = screenWidth * 2.0f;
           
float height = screenHeight * 2.0f;
           
           
map->isVisible = intersectionTwoRectangles( cam->position.x, cam->position.y, width, height, map_position.x, map_position.y, 16 * tileSide, 16 * tileSide );
           
       
}
       
       
    }
   
   
   
void save() {
       
       
for( auto & map: maps )
           
 map->save();
       
   
}
   
   
void draw() {
       
//sf::Clock c;
        //sf::Time start = c.getElapsedTime();
       
       
int i = 0;
       
       
for( auto & map: maps ) {
           
if( map->isVisible )
               
 map->draw();
           
       
}
       
       
//sf::Time end = c.getElapsedTime();
        //
        //cout << (end - start).asMilliseconds() << " ms,\t";
        //cout << (end - start).asSeconds() << " s \n";
   
}
   
   
}
;

World * world = nullptr;

#endif
P-181503
pekfos
» 2024-08-22 21:28:44
bo pozycje floors są wielokrotnościami width=16 oraz height=16. A dla budynku potrzebuję np. aby pozycja była np. 15
Bo je tak ustawiasz, ale nie musisz. Nie widziałem w kodzie żebyś korzystał z założenia że pozycja mapy jest wielokrotnością 16.
Coś źle się stanie jak tak zrobisz?
C/C++
terrain = new Terrain( x * 16 + 1, y * 16 + 1, 16, 16 );
floors = new Floors( x * 16 + 1, y * 16 + 1, 16, 16 );
P-181504
tBane
Temat założony przez niniejszego użytkownika
» 2024-08-22 21:30:12
Nie widziałem w kodzie żebyś korzystał z założenia że pozycja mapy jest wielokrotnością 16.

C/C++
coord_x =( coords.x * width + x );
coord_y =( coords.y * height + y );
P-181505
pekfos
» 2024-08-22 21:30:48
Miałem na myśli w kodzie który spisałeś na straty.
P-181506
tBane
Temat założony przez niniejszego użytkownika
» 2024-08-22 21:35:22
Wtedy Terrain będzie na pozycji (x*16+1) *16 czyli tak jak na poniższym screenie. Chciałbym móc tak zrobić, żeby mapy miały współrzędne całkowite zaś Terrain dla mapy by był wielokrotnością 16 a dla budynku już nie. Trudno mi to słowami opisać. Dlatego, że mapy mają rozmiary 16x16 a pozycja budynku niekoniecznie jest wielokrotnością 16. Czyli muszę tak zrobić, by konstruktor mapy wyglądał tak:
C/C++
Map( int x, int y ) {
   
coords = sf::Vector2i( x, y );
   
terrain = new Terrain( x * 16, y * 16, 16, 16 );
   
floors = new Floors( x * 16, y * 16, 16, 16 );
}

Building() {
   
coords = sf::Vector2i( x, y );
   
floors = new Floors( x, y, 8, 8 );
}

RPG2D-087
RPG2D-087
P-181507
1 « 2 » 3
Poprzednia strona Strona 2 z 3 Następna strona