2020-01-30 21:35:51 +09:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: TilesDef.cpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 30/01/2020 17:27:16
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include <gk/core/Exception.hpp>
|
|
|
|
|
2020-01-31 15:04:04 +09:00
|
|
|
#include "NetworkUtils.hpp"
|
2020-01-30 21:35:51 +09:00
|
|
|
#include "TilesDef.hpp"
|
|
|
|
|
2020-01-31 15:04:04 +09:00
|
|
|
const std::string &TilesDef::getTextureForFace(u8 face, bool useAltTiles) const {
|
2020-01-31 15:45:23 +09:00
|
|
|
u8 size = (!useAltTiles) ? m_textureFilenames.size() : m_altTextureFilenames.size();
|
2020-01-30 21:35:51 +09:00
|
|
|
if (size == 0)
|
|
|
|
throw EXCEPTION("Trying to get texture from empty tiles definition");
|
|
|
|
|
2020-01-31 15:04:04 +09:00
|
|
|
u8 outFace = face;
|
|
|
|
|
2020-01-30 21:35:51 +09:00
|
|
|
if (size == 1)
|
2020-01-31 15:04:04 +09:00
|
|
|
outFace = BlockFace::Top;
|
2020-01-30 21:35:51 +09:00
|
|
|
else if (size == 2)
|
2020-01-31 15:04:04 +09:00
|
|
|
outFace = (face == BlockFace::Top || face == BlockFace::Bottom) ? 0 : 1;
|
2020-01-30 21:35:51 +09:00
|
|
|
else if (size == 3 && face >= size)
|
2020-01-31 15:04:04 +09:00
|
|
|
outFace = BlockFace::Left;
|
2020-01-30 21:35:51 +09:00
|
|
|
else if (size == 4 && face >= size)
|
2020-01-31 15:04:04 +09:00
|
|
|
outFace = BlockFace::Right;
|
2020-01-30 21:35:51 +09:00
|
|
|
else if (size == 5 && face >= size)
|
2020-01-31 15:04:04 +09:00
|
|
|
outFace = BlockFace::Front;
|
2020-01-30 21:35:51 +09:00
|
|
|
|
2020-01-31 15:04:04 +09:00
|
|
|
if (!useAltTiles)
|
2020-01-31 15:45:23 +09:00
|
|
|
return m_textureFilenames.at(outFace);
|
2020-01-31 15:04:04 +09:00
|
|
|
else {
|
2020-01-31 15:45:23 +09:00
|
|
|
const std::string &filename = m_altTextureFilenames.at(outFace);
|
2020-01-31 15:04:04 +09:00
|
|
|
return (!filename.empty()) ? filename : getTextureForFace(face, false);
|
|
|
|
}
|
2020-01-30 21:35:51 +09:00
|
|
|
}
|
|
|
|
|
2020-01-31 15:04:04 +09:00
|
|
|
void TilesDef::serialize(sf::Packet &packet) const {
|
2020-01-31 15:45:23 +09:00
|
|
|
packet << m_textureFilenames << m_altTextureFilenames;
|
2020-01-30 21:35:51 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
void TilesDef::deserialize(sf::Packet &packet) {
|
2020-01-31 15:45:23 +09:00
|
|
|
packet >> m_textureFilenames >> m_altTextureFilenames;
|
2020-01-31 15:04:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
void TilesDef::loadFromLuaTable(const sol::table &table) {
|
|
|
|
if (table["tiles"].get_type() == sol::type::table) {
|
|
|
|
sol::as_table_t<std::vector<std::string>> t = table["tiles"];
|
2020-01-31 15:45:23 +09:00
|
|
|
m_textureFilenames = t.source;
|
2020-01-31 15:04:04 +09:00
|
|
|
}
|
|
|
|
else
|
2020-01-31 15:45:23 +09:00
|
|
|
m_textureFilenames.emplace_back(table["tiles"].get<std::string>());
|
2020-01-31 15:04:04 +09:00
|
|
|
|
|
|
|
if (table["alt_tiles"].get_type() != sol::type::none) {
|
|
|
|
if (table["alt_tiles"].get_type() == sol::type::table) {
|
|
|
|
sol::as_table_t<std::vector<std::string>> t = table["alt_tiles"];
|
2020-01-31 15:45:23 +09:00
|
|
|
m_altTextureFilenames = t.source;
|
2020-01-31 15:04:04 +09:00
|
|
|
}
|
|
|
|
else
|
2020-01-31 15:45:23 +09:00
|
|
|
m_altTextureFilenames.emplace_back(table["alt_tiles"].get<std::string>());
|
2020-01-30 21:35:51 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|