git repos / saland

commit a815ec9c

Poul Sander · 2019-04-15 19:43
a815ec9cf1d9e6b0a85d9b1a3427fc9c5d70ad1b patch · browse files
parent 10ca4b89595b822958d3d6bd4f180d7aa484de72

Add properties to Tile objects

Changed files

M data/maps/sample1.tmx before
M src/sagotmx/tmx_struct.h before
M src/saland/Game.cpp before
diff --git a/data/maps/sample1.tmx b/data/maps/sample1.tmx index 9537802..d0f804d 100644 --- a/data/maps/sample1.tmx +++ b/data/maps/sample1.tmx
@@ -31,6 +31,7 @@
<object id="10" x="-245" y="-80"/>
<object id="11" name="text_basic_sign" type="text" x="1194" y="464" width="71" height="67">
<properties>
+ <property name="hello" type="int" value="5"/>
<property name="text" value="This is a text on a sign"/>
</properties>
</object>
diff --git a/src/sagotmx/tmx_struct.h b/src/sagotmx/tmx_struct.h index bd9a02d..df785bc 100644 --- a/src/sagotmx/tmx_struct.h +++ b/src/sagotmx/tmx_struct.h
@@ -241,6 +241,11 @@ struct TileLayer {
TileLayerData data;
};
+struct TileProperty {
+ std::string name;
+ std::string type;
+ std::string value;
+};
struct TileObject {
int id = 0;
@@ -253,6 +258,7 @@ struct TileObject {
bool isEllipse = false;
bool isPoint = false;
std::vector<std::pair<int,int> > polygon_points;
+ std::map<std::string, TileProperty> properties;
};
struct TileObjectGroup {
@@ -411,6 +417,17 @@ inline TileMap string2tilemap(const std::string& tmx_content) {
to.polygon_points.push_back(std::make_pair(x, y));
}
}
+ rapidxml::xml_node<> * properties_node = object_node->first_node("properties");
+ if (properties_node) {
+ for (rapidxml::xml_node<> * property_node = properties_node->first_node("property");
+ property_node; property_node = property_node->next_sibling("property") ) {
+ TileProperty tp;
+ setValueFromAttribute(property_node, "name", tp.name);
+ setValueFromAttribute(property_node, "type", tp.type);
+ setValueFromAttribute(property_node, "value", tp.value);
+ to.properties[tp.name] = tp;
+ }
+ }
group.objects.push_back(to);
}
m.object_groups.push_back(group);
@@ -491,6 +508,18 @@ inline void xml_add_objectgroup(std::iostream& io, const TileMap& m, size_t obje
}
io << "\"/>\n";
}
+ if (to.properties.size() > 0) {
+ io << "<properties>\n";
+ for (const auto& prop : to.properties) {
+ io << "<property name=\"" << prop.first << "\" ";
+ if (prop.second.type.size()> 0) {
+ io << "type=\"" << prop.second.type << "\" ";
+ }
+ io << "value=\"" << prop.second.value << "\" ";
+ io << "/>\n";
+ }
+ io << "</properties>\n";
+ }
io << "</object>\n";
}
io << "</objectgroup>\n";
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 6cebe7e..21561e2 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -425,6 +425,18 @@ void Game::Update() {
data->lastUpdate = nowTime;
data->gameRegion.physicsBox->Step(deltaTime / 1000.0f / 60.0f, velocityIterations, positionIterations);
std::sort(data->gameRegion.placeables.begin(), data->gameRegion.placeables.end(), sort_placeable);
+ const std::vector<sago::tiled::TileObjectGroup>& object_groups = data->gameRegion.world.tm.object_groups;
+ for (const auto& group : object_groups) {
+ for (const auto& item : group.objects) {
+ if (item.isEllipse) {
+ continue;
+ }
+ if (item.x < data->human->X && item.y < data->human->Y && item.width+item.x > data->human->X
+ && item.height+item.y > data->human->Y && item.type == "text") {
+ std::cout << "text\n";
+ }
+ }
+ }
if (data->human->X < 0) {
ResetWorld(data->gameRegion.GetRegionX()-1, data->gameRegion.GetRegionY(), false);
data->human->body->SetTransform(b2Vec2(data->gameRegion.world.tm.width, data->gameRegion.world.tm.height/2),data->human->body->GetAngle()) ;