Commit bf3a9aad authored by Corentin Bocquillon's avatar Corentin Bocquillon

Fixing map generation.

parent 4bf606b2
......@@ -124,7 +124,7 @@ MapGenerator::get_item_id (coordinates square_coordinates, int floor_id)
return get<1> (threshold);
}
return 0;
return -1;
}
unsigned
......@@ -175,20 +175,20 @@ MapGenerator::generate_items_thresholds ()
vector<tuple<int,int,double>> probabilities =
{
// Désert
make_tuple (1, 1, 0.0), // Arbre
make_tuple (1, 2, (1.0 / 24.0)), // Cuivre
make_tuple (1, 1, 0.0), // Arbre mort
make_tuple (1, 3, (1.0 / 24.0)), // Cuivre
make_tuple (1, 5, (1.0 / 24.0)), // Fer
make_tuple (1, 8, (1.0 / 24.0)), // Charbon
// Terre
make_tuple (2, 1, (1.0 / 3.0)),
make_tuple (2, 2, (1.0 / 36.0)),
make_tuple (2, 2, (1.0 / 3.0)), // Arbre
make_tuple (2, 3, (1.0 / 36.0)),
make_tuple (2, 5, (1.0 / 36.0)),
make_tuple (2, 8, (1.0 / 36.0)),
// Forêt
make_tuple (3, 1, (1.0 / 3.0)),
make_tuple (3, 2, (1.0 / 36.0)),
make_tuple (3, 2, (1.0 / 3.0)),
make_tuple (3, 3, (1.0 / 36.0)),
make_tuple (3, 5, (1.0 / 36.0)),
make_tuple (3, 8, (1.0 / 36.0))
};
......
......@@ -300,9 +300,8 @@ Game::set_event_handlers ()
if (this->m_map.get_save ().get_surface_item
(click_map_coords.chunk, click_map_coords.square) == -1)
{
this->m_map.get_save ().set_surface_item (click_map_coords.chunk,
click_map_coords.square,
1, 1);
this->m_map.set_surface_item (click_map_coords.chunk,
click_map_coords.square, 2);
}
});
......@@ -317,7 +316,7 @@ Game::set_event_handlers ()
if (this->m_map.get_save ().get_surface_item
(click_map_coords.chunk, click_map_coords.square) != -1)
{
this->m_map.get_save ().set_surface_item
this->m_map.set_surface_item
(click_map_coords.chunk, click_map_coords.square, -1, 0);
}
});
......
......@@ -130,31 +130,6 @@ Map::~Map ()
m_save.save ();
}
Json::Value
Map::load_json_document (string path)
{
ifstream document (path);
if (!document.is_open ())
throw runtime_error (string ("« ") + path + " » could not be opened.");
Json::Value root;
document >> root;
return root;
}
void
Map::set_item_max_height ()
{
m_item_max_height = 0;
for (const string &member : m_items_json.getMemberNames ())
{
unsigned current_height = m_items_json[member][3].asUInt ();
m_item_max_height = (current_height > m_item_max_height) ?
m_items_json[member][3].asUInt () : m_item_max_height;
}
}
Chunk&
Map::get_chunk (coordinates coords)
{
......@@ -169,9 +144,7 @@ Map::get_chunk (coordinates coords)
Chunk chunk (m_save.get_chunk_by_coordinates (coords),
make_shared<sf::Texture> (m_items),
make_shared<sf::Texture> (m_grounds));
pair<sf::VertexArray, vector<sf::VertexArray>> vertices
= get_chunk_vertex_arrays (chunk);
chunk.set_vertices (vertices.first, vertices.second);
update_chunk_vertices (chunk);
m_chunks.push_back (chunk);
return get_chunk (coords);
......@@ -183,6 +156,16 @@ Map::get_save ()
return m_save;
}
void
Map::set_surface_item (coordinates chunk_coordinates,
coordinates square_coordinates, int item_id,
int quantity)
{
m_save.set_surface_item (chunk_coordinates, square_coordinates,
item_id, quantity);
update_chunk_vertices (get_chunk (chunk_coordinates));
}
void
Map::set_vertices_position (sf::VertexArray &vertex_array,
const unsigned &first_position,
......@@ -348,3 +331,36 @@ Map::get_chunk_vertex_arrays (Chunk &chunk)
return make_pair (ground_vertices, surface_vertices);
}
Json::Value
Map::load_json_document (string path)
{
ifstream document (path);
if (!document.is_open ())
throw runtime_error (string ("« ") + path + " » could not be opened.");
Json::Value root;
document >> root;
return root;
}
void
Map::set_item_max_height ()
{
m_item_max_height = 0;
for (const string &member : m_items_json.getMemberNames ())
{
unsigned current_height = m_items_json[member][3].asUInt ();
m_item_max_height = (current_height > m_item_max_height) ?
m_items_json[member][3].asUInt () : m_item_max_height;
}
}
void
Map::update_chunk_vertices (Chunk &chunk)
{
pair<sf::VertexArray, vector<sf::VertexArray>> vertices
= get_chunk_vertex_arrays (chunk);
chunk.set_vertices (vertices.first, vertices.second);
}
......@@ -101,6 +101,10 @@ namespace Graphics
Save::Map&
get_save ();
void set_surface_item (coordinates chunk_coordinates,
coordinates square_coordinates, int item_id,
int quantity = 1);
private:
Save::Map m_save;
std::vector<Chunk> m_chunks;
......@@ -146,7 +150,8 @@ namespace Graphics
Json::Value load_json_document (std::string path);
void
set_item_max_height ();
void set_item_max_height ();
void update_chunk_vertices (Chunk &chunk);
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment