New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added export to Defold .tilemap files #1316

Closed
wants to merge 19 commits into
base: master
from

Added save unncesarry information in script file

  • Loading branch information...
yahuaru committed Jun 9, 2016
commit b4df62ec3c6e7c4a93e8403f213bc462e2be4894
@@ -12,6 +12,10 @@
#include <QSaveFile>
#include <QTextStream>
#include <QDebug>
#include <tilelayer.h>
#include <mapobject.h>
#include <objectgroup.h>
#include <algorithm>
#include "map.h"
#include "tile.h"
@@ -56,21 +60,28 @@ Defold::DefoldPlugin::DefoldPlugin()
bool Defold::DefoldPlugin::write(const Tiled::Map *map, const QString &fileName)
{
QVariantHash map_h;
//map_h["tile_set"] = map->tilesets()[0]->fileName().utf16();
QString layers;
foreach (const Tiled::Layer *layer, map->layers())
{
if (layer->layerType() != Tiled::Layer::TileLayerType)
continue;
const Tiled::TileLayer *tileLayer = static_cast<const Tiled::TileLayer*>(layer);
QList<QList<QString>> types;
int layerWidth = 0;
int layerHeight = 0;
QString layers = "";
foreach (Tiled::TileLayer *tileLayer, map->tileLayers())
{
QVariantHash layer_h;
layer_h["id"] = tileLayer->name();
layer_h["z"] = 0;
layer_h["is_visible"] = tileLayer->isVisible() ? 1 : 0;
QString cells = "";
layerWidth = std::max(tileLayer->width(), layerWidth);
layerHeight = std::max(tileLayer->height(), layerHeight);
for (int x = 0; x < tileLayer->width(); ++x)
{
QList<QString> t;
if (types.size() < tileLayer->width()) types.append(t);
for (int y = 0; y < tileLayer->height(); ++y)
{
const Tiled::Cell &cell = tileLayer->cellAt(x, y);
@@ -81,25 +92,22 @@ bool Defold::DefoldPlugin::write(const Tiled::Map *map, const QString &fileName)
cell_h["tile"] = cell.tile->id();
cell_h["h_flip"] = cell.flippedHorizontally ? 1 : 0;
cell_h["v_flip"] = cell.flippedVertically ? 1 : 0;
if (cell.tile->hasProperty("Type"))
{
QString prop = cell.tile->property("Type").toString();
}
cells.append(ReplaceTags(cell_t, cell_h));
if (types[x].size() < tileLayer->height())
types[x].append(cell.tile->property("Type").toString());
else if (!cell.tile->property("Type").toString().isEmpty())
types[x][tileLayer->height() - y - 1] = cell.tile->property("Type").toString();
}
}
layer_h["cells"] = cells;
layers.append(ReplaceTags(layer_t, layer_h));
qDebug() << layers;
}
map_h["layers"] = layers;
map_h["material"] = "/builtins/materials/tile_map.material";
map_h["blend_mode"] = "BLEND_MODE_ALPHA";
map_h["tile_set"] = "";
QString result = ReplaceTags(map_t, map_h);
QFile writeFile(fileName);
if (!writeFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
@@ -115,6 +123,44 @@ bool Defold::DefoldPlugin::write(const Tiled::Map *map, const QString &fileName)
return false;
}
QFile saveFile(fileName + ".script");
if (!saveFile.open(QIODevice::WriteOnly))
{
qWarning("Couldn't open save file.");
return false;
}
QTextStream unitsFileStream (&saveFile);
unitsFileStream << "return{" << endl;
unitsFileStream << "Types{" << endl;
for (int x = 0; x < layerWidth; ++x)
{
for (int y = 0; y < layerHeight; ++y)
{
unitsFileStream << "\t" << types[x][y] << ",";
}
unitsFileStream <<"\t" << endl;
}
unitsFileStream << "}," << endl;
unitsFileStream << "Objects{" << endl;
foreach (Tiled::ObjectGroup *group, map->objectGroups())
{
foreach (Tiled::MapObject *object, group->objects())
{
unitsFileStream << "\t{" << endl;
QString name = object->name();
QPointF pos = object->position();
unitsFileStream << "\tname=\"" << name << "\"," << endl;
unitsFileStream << "\tx=" << pos.x() << "," << endl;
unitsFileStream << "\ty=" << pos.y() << "," << endl;
unitsFileStream << "\t}," << endl;
}
}
unitsFileStream << "}" << endl;
unitsFileStream << "}" << endl;
return true;
}
@@ -50,7 +50,6 @@ class DEFOLDPLUGINSHARED_EXPORT DefoldPlugin : public Tiled::WritableMapFormat
private:
QString mError;
};
}
ProTip! Use n and p to navigate between commits in a pull request.