diff options
| author | iamcheeseman <[hidden email]> | 2026-01-13 22:28:59 -0500 |
|---|---|---|
| committer | iamcheeseman <[hidden email]> | 2026-01-13 22:28:59 -0500 |
| commit | 0bf86ef7b8f0ecad958f3fb592ceaf3d2f30336a (patch) | |
| tree | acc7ee1c60579b5547ee23e5311446d588075b20 /tools/compile_assets | |
| parent | e5319f53de9f5f8ba4cb67e1302ebc3d7036135c (diff) | |
add support for tiled objects
Diffstat (limited to 'tools/compile_assets')
| -rw-r--r-- | tools/compile_assets/main.odin | 31 | ||||
| -rw-r--r-- | tools/compile_assets/tiled.odin | 90 |
2 files changed, 101 insertions, 20 deletions
diff --git a/tools/compile_assets/main.odin b/tools/compile_assets/main.odin index e33dc37..ea1ff90 100644 --- a/tools/compile_assets/main.odin +++ b/tools/compile_assets/main.odin @@ -33,6 +33,9 @@ Room_Id :: enum { Tileset_Id :: enum { <tileset-enum>} +Object_Type :: enum { +<object-types>} + Resource_Id :: union { Image_Id, Animation_Id, @@ -46,8 +49,6 @@ images: [Image_Id]Image_Resource = { animations: [Animation_Id]Animation_Resource = { <anim-load>} -//odinfmt: disable - rooms: [Room_Id]Room_Resource = { <room-load>} @@ -57,8 +58,6 @@ tilesets: [Tileset_Id]Tileset_Resource = { tiles: []Tile_Resource = { <tiles-load>} -//odinfmt: enable - world: []Room_Position_Resource = { <room-positions>} @@ -79,6 +78,9 @@ tilesets: map[string]string world: [dynamic]string +// Don't want to add types multiple times, so it's a set +object_type_names: map[string]struct{} + output_dir: string paths_to_res_type: map[string]string @@ -355,6 +357,26 @@ main :: proc() { } content = set_placeholder(content, "<resource-paths>", res_paths) + object_type_enum := "" + + for object_type in object_type_names { + object_type_enum = strings.concatenate({ + object_type_enum, + " ", + object_type, + ",\n" + }, allocator = context.temp_allocator) + + delete(object_type) + } + + content, _ = strings.replace_all( + content, + "<object-types>", + object_type_enum, + allocator = context.temp_allocator, + ) + os.write_string(output_file, content) for image in images { @@ -369,4 +391,3 @@ main :: proc() { free_all(context.temp_allocator) } - diff --git a/tools/compile_assets/tiled.odin b/tools/compile_assets/tiled.odin index 7752273..5311fa5 100644 --- a/tools/compile_assets/tiled.odin +++ b/tools/compile_assets/tiled.odin @@ -210,7 +210,12 @@ Json_Property :: struct { name: string, type: string, propertytype: string, - value: any, + value: union{ + bool, + int, + string, + f32, + }, } Json_Point :: struct { @@ -415,33 +420,88 @@ load_json_room :: proc(path: string, file: ^os.File) { } } + objects: [dynamic]string + defer delete(objects) + layers: [dynamic][]u32 defer delete(layers) for layer in json_dat.layers { - if strings.compare(layer.type, "tilelayer") != 0 { - continue - } + switch layer.type { + case "tilelayer": + json_layer_dat, ok := layer.data.([]i32) + if !ok { + continue + } - json_layer_dat, ok := layer.data.([]i32) - if !ok { - continue - } + data := make( + []u32, + len(json_layer_dat), + allocator = context.temp_allocator, + ) + + for cell, i in json_layer_dat { + data[i] = tiled_to_real_gid[cell] + } + + append(&layers, data) + case "objectgroup": + parallax := [2]f32{f32(layer.parallaxx), f32(layer.parallaxy)} + + for obj in layer.objects { + type_name := strings.to_screaming_snake_case(obj.type) - data := make([]u32, len(json_layer_dat), allocator = context.temp_allocator) + // `object_type_names` now owns `type_name`; freed in main + object_type_names[type_name] = {} - for cell, i in json_layer_dat { - data[i] = tiled_to_real_gid[cell] + pos := [2]f32{f32(obj.x), f32(obj.y)} + + properties: map[string]union{ + bool, + int, + string, + f32, + } + defer delete(properties) + + for property in obj.properties { + fmt.println(property.name, property.value) + properties[property.name] = property.value + } + + line := fmt.tprintf( + "{{type = .%v, pos = %w, parallax = %w, properties = %w}}", + type_name, + pos, + parallax, + properties, + ) + + append(&objects, line) + } + case: + fmt.eprintfln("WARNING: Unsupported tile layer type '%v'", layer.type) } + } + + objects_arr_str := "" - append(&layers, data) + for obj in objects { + objects_arr_str = strings.concatenate({ + objects_arr_str, + obj, + ", " + }, allocator = context.temp_allocator) } line := fmt.aprintf( - "{{width=%v, height=%v, tile_width=%v, tile_height=%v, layers=%w, objects={{}}, background_image=nil}}", - width, height, - tile_width, tile_height, + "{{width=%v, height=%v, tile_width=%v, tile_height=%v, layers=%w, objects={{%v}}, background_image=nil}}", + width, + height, + tile_width, + tile_height, layers[:], + objects_arr_str, ) rooms[filepath.stem(path)] = line } |
