Exemplo n.º 1
0
void TileSetEditor::_import_node(Node *p_node, Ref<TileSet> p_library) {

	for (int i = 0; i < p_node->get_child_count(); i++) {

		Node *child = p_node->get_child(i);

		if (!child->cast_to<Sprite>()) {
			if (child->get_child_count() > 0) {
				_import_node(child, p_library);
			}

			continue;
		}

		Sprite *mi = child->cast_to<Sprite>();
		Ref<Texture> texture = mi->get_texture();
		Ref<ShaderMaterial> material = mi->get_material();

		if (texture.is_null())
			continue;

		int id = p_library->find_tile_by_name(mi->get_name());
		if (id < 0) {

			id = p_library->get_last_unused_tile_id();
			p_library->create_tile(id);
			p_library->tile_set_name(id, mi->get_name());
		}

		p_library->tile_set_texture(id, texture);
		p_library->tile_set_material(id, material);

		p_library->tile_set_modulate(id, mi->get_modulate());

		Vector2 phys_offset;
		Size2 s;

		if (mi->is_region()) {
			s = mi->get_region_rect().size;
			p_library->tile_set_region(id, mi->get_region_rect());
		} else {
			const int frame = mi->get_frame();
			const int hframes = mi->get_hframes();
			s = texture->get_size() / Size2(hframes, mi->get_vframes());
			p_library->tile_set_region(id, Rect2(Vector2(frame % hframes, frame / hframes) * s, s));
		}

		if (mi->is_centered()) {
			phys_offset += -s / 2;
		}

		Vector<Ref<Shape2D> > collisions;
		Ref<NavigationPolygon> nav_poly;
		Ref<OccluderPolygon2D> occluder;

		for (int j = 0; j < mi->get_child_count(); j++) {

			Node *child2 = mi->get_child(j);

			if (child2->cast_to<NavigationPolygonInstance>())
				nav_poly = child2->cast_to<NavigationPolygonInstance>()->get_navigation_polygon();

			if (child2->cast_to<LightOccluder2D>())
				occluder = child2->cast_to<LightOccluder2D>()->get_occluder_polygon();

			if (!child2->cast_to<StaticBody2D>())
				continue;
			StaticBody2D *sb = child2->cast_to<StaticBody2D>();
			int shape_count = sb->get_shape_count();
			if (shape_count == 0)
				continue;
			for (int shape_index = 0; shape_index < shape_count; ++shape_index) {
				Ref<Shape2D> collision = sb->get_shape(shape_index);
				if (collision.is_valid()) {
					collisions.push_back(collision);
				}
			}
		}

		if (collisions.size()) {

			p_library->tile_set_shapes(id, collisions);
			p_library->tile_set_shape_offset(id, -phys_offset);
		} else {
			p_library->tile_set_shape_offset(id, Vector2());
		}

		p_library->tile_set_texture_offset(id, mi->get_offset());
		p_library->tile_set_navigation_polygon(id, nav_poly);
		p_library->tile_set_light_occluder(id, occluder);
		p_library->tile_set_occluder_offset(id, -phys_offset);
		p_library->tile_set_navigation_polygon_offset(id, -phys_offset);
	}
}
Exemplo n.º 2
0
void TileSetEditor::_import_scene(Node *scene, Ref<TileSet> p_library, bool p_merge) {

	if (!p_merge)
	       p_library->clear();

	for(int i=0;i<scene->get_child_count();i++) {


	       Node *child = scene->get_child(i);

	       if (!child->cast_to<Sprite>()) {
		       if (child->get_child_count()>0) {
			       child=child->get_child(0);
			       if (!child->cast_to<Sprite>()) {
				       continue;
			       }

		       } else
			       continue;


	       }

	       Sprite *mi = child->cast_to<Sprite>();
	       Ref<Texture> texture=mi->get_texture();
	       if (texture.is_null())
			continue;

	       int id = p_library->find_tile_by_name(mi->get_name());
	       if (id<0) {

		       id=p_library->get_last_unused_tile_id();
		       p_library->create_tile(id);
		       p_library->tile_set_name(id,mi->get_name());
	       }


	       p_library->tile_set_texture(id,texture);
	       Vector2 phys_offset = mi->get_offset();

	       if (mi->is_centered()) {
		       Size2 s;
		       if (mi->is_region()) {
			       s=mi->get_region_rect().size;
		       } else {
			       s=texture->get_size();
		       }
		       phys_offset+=-s/2;
	       }
	       if (mi->is_region()) {
		       p_library->tile_set_region(id,mi->get_region_rect());
	       }

	       Vector<Ref<Shape2D> >collisions;

	       for(int j=0;j<mi->get_child_count();j++) {

		       Node *child2 = mi->get_child(j);
		       if (!child2->cast_to<StaticBody2D>())
			       continue;
		       StaticBody2D *sb = child2->cast_to<StaticBody2D>();
		       if (sb->get_shape_count()==0)
			       continue;
		       Ref<Shape2D> collision=sb->get_shape(0);
		       if (collision.is_valid()) {
			       collisions.push_back(collision);
			}
	       }

	       if (collisions.size()) {

		       p_library->tile_set_shapes(id,collisions);
		       p_library->tile_set_shape_offset(id,-phys_offset);
	       } else {
		       p_library->tile_set_shape_offset(id,Vector2());

	       }

	       p_library->tile_set_texture_offset(id,Vector2());
	}
}