Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @function scr_ExportPrefab()
- /// @desc Exports all objects and tile data in the current room into a JSON prefab structure.
- function scr_ExportPrefab() {
- // === Object Types to Ignore During Export ===
- var skip_objects = [
- oRoomOrigin, oPrefabBuilderControl, oCamera, oNavigation,
- oCursor, oHUD, oItemManager, oInventory, oTech
- ];
- // === Gather Object Data ===
- var prefab_objects = [];
- with (all) {
- if (!array_contains(skip_objects, object_index)) {
- var entry = {
- object_id: object_index,
- type: object_get_name(object_index),
- x: x,
- y: y,
- xscale: image_xscale,
- yscale: image_yscale
- };
- show_debug_message("Exporting object: " + entry.type + " @ " + string(entry.x) + "," + string(entry.y));
- array_push(prefab_objects, entry);
- }
- }
- // === Gather Tile Data from "Tiles_Visual" Layer ===
- var tile_layer = "Tiles_Visual";
- var tile_data = [];
- var tilemap_id = layer_tilemap_get_id(tile_layer);
- if (tilemap_id != -1) {
- var tile_w = tilemap_get_tile_width(tilemap_id);
- var tile_h = tilemap_get_tile_height(tilemap_id);
- var grid_w = room_width div tile_w;
- var grid_h = room_height div tile_h;
- var tile_columns = 8; // Hardcoded: change if tileset changes
- for (var tx = 0; tx < grid_w; tx++) {
- for (var ty = 0; ty < grid_h; ty++) {
- var tile_index = tilemap_get(tilemap_id, tx, ty);
- if (tile_index != 0) {
- var tile_entry = {
- x: tx,
- y: ty,
- tile_x: tile_index mod tile_columns,
- tile_y: tile_index div tile_columns
- };
- show_debug_message("Exporting tile (" + string(tile_entry.tile_x) + "," + string(tile_entry.tile_y) + ") at " + string(tile_entry.x) + "," + string(tile_entry.y));
- array_push(tile_data, tile_entry);
- }
- }
- }
- }
- // === Create Final Prefab Structure ===
- var prefab = {
- objects: prefab_objects,
- tiles: tile_data
- };
- // === Write JSON to Disk ===
- var json = json_stringify(prefab, false);
- var file = file_text_open_write("prefab_output.json");
- file_text_write_string(file, json);
- file_text_close(file);
- show_debug_message("Export complete: prefab_output.json");
- }
- /// @function scr_BuildRoomFromPrefab(prefab, x_offset, y_offset)
- /// @desc Builds a room layout from a JSON prefab, placing objects and tiles with pixel offsets.
- /// @param prefab {struct} - The parsed JSON prefab data
- /// @param x_offset {int} - X pixel offset to position the prefab
- /// @param y_offset {int} - Y pixel offset to position the prefab
- function scr_BuildRoomFromPrefab(prefab, x_offset, y_offset) {
- show_debug_message("Spawning prefab at offset: " + string(x_offset) + "," + string(y_offset));
- // === OBJECT SPAWN ===
- for (var i = 0; i < array_length(prefab.objects); i++) {
- var obj = prefab.objects[i];
- if (object_exists(obj.object_id)) {
- var inst = instance_create_layer(x_offset + obj.x, y_offset + obj.y, "Instances", obj.object_id);
- inst.image_xscale = obj.xscale;
- inst.image_yscale = obj.yscale;
- } else {
- show_debug_message("WARNING: Invalid object ID " + string(obj.object_id));
- }
- }
- // === TILE PLACEMENT ===
- var tilemap_id = layer_tilemap_get_id("RoomTiles");
- if (tilemap_id == -1) {
- show_debug_message("ERROR: RoomTiles layer not found.");
- return;
- }
- tilemap_clear(tilemap_id, 0);
- var tile_w = tilemap_get_tile_width(tilemap_id);
- var tile_h = tilemap_get_tile_height(tilemap_id);
- var tile_columns = 8;
- var max_tiles = tile_columns * tile_columns;
- for (var j = 0; j < array_length(prefab.tiles); j++) {
- var tile = prefab.tiles[j];
- // Add *pixel* offset to grid cell
- var cx = tile.x + floor(x_offset / tile_w);
- var cy = tile.y + floor(y_offset / tile_h);
- var tile_index = tile.tile_y * tile_columns + tile.tile_x;
- if (tile_index >= 0 && tile_index < max_tiles) {
- tilemap_set(tilemap_id, cx, cy, tile_index);
- show_debug_message("Placed tile (" + string(tile.tile_x) + "," + string(tile.tile_y) + ") → index " + string(tile_index) + " at cell " + string(cx) + "," + string(cy));
- } else {
- show_debug_message("SKIPPED invalid tile index " + string(tile_index));
- }
- }
- show_debug_message("Prefab build complete.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement