Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Oct 26, 2024
1 parent 6eb7852 commit 67d1f55
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 50 deletions.
32 changes: 16 additions & 16 deletions detiled/detiled.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,28 @@ end
---@param map_path string
---@return decore.world.instance
function M.get_world_from_tiled_map(map_path)
return detiled_decore.create_world_from_tiled_map(map_path)
end


---@param maps_list_path string|table
---@return decore.worlds_pack_data[]
function M.get_worlds_from_tiled_maps(maps_list_path)
local map_list = detiled_internal.load_config(maps_list_path)
if not map_list then
local map = detiled_internal.load_json(map_path)
if not map then
detiled_internal.logger:error("Failed to load map", map_path)
return {}
end

local worlds_pack = {}
return detiled_decore.create_world_from_tiled_map(map)
end


for map_id, map_path in pairs(map_list) do
local world = detiled_decore.create_world_from_tiled_map(map_path)
if world then
worlds_pack[map_id] = world
end
---Split a Tiled map into multiple worlds, one for each layer. Main world contains all layers, except the with "exclude" property
---@param world_id string
---@param map_path string
---@return table<string, decore.world.instance> world_pack Each layer from the Tiled map is a sub-world with the world_id:layer_id format
function M.get_worlds_from_tiled_map(world_id, map_path)
local map = detiled_internal.load_json(map_path)
if not map then
detiled_internal.logger:error("Failed to load map", map_path)
return {}
end

return worlds_pack
return detiled_decore.create_worlds_from_tiled_map(world_id, map)
end


Expand Down
4 changes: 2 additions & 2 deletions detiled/detiled_annotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

---@class entity
---@field name string|nil @The entity name
---@field tiled_id number|nil @The entity id from Tiled, autofilled by decore.create_entity
---@field layer_id number|nil @The layer name from Tiled, autofilled by decore.create_entity
---@field tiled_id string|nil @The entity id from Tiled, autofilled by decore.create_entity
---@field layer_id string|nil @The layer name from Tiled, autofilled by decore.create_entity

---@class detiled.tileset
---@field class string
Expand Down
63 changes: 31 additions & 32 deletions detiled/detiled_decore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ local function get_entities_from_object_layer(layer, map)
if object.properties then
local tiled_components = detiled_internal.get_components_property(object.properties)
if tiled_components then
-- Unique case
if tiled_components.position_z then
components.transform.position_z = components.transform.position_z + tiled_components.position_z
tiled_components.position_z = nil
end

detiled_internal.apply_components(components, tiled_components)
end
end
Expand Down Expand Up @@ -168,6 +174,12 @@ local function get_entities_from_object_layer(layer, map)
if object.properties then
local tiled_components = detiled_internal.get_components_property(object.properties)
if tiled_components then
-- Unique case
if tiled_components.position_z then
entity.components.transform.position_z = entity.components.transform.position_z + tiled_components.position_z
tiled_components.position_z = nil
end

detiled_internal.apply_components(entity.components, tiled_components)
end
end
Expand Down Expand Up @@ -280,47 +292,20 @@ end



---@param tiled_map_path string
---@return decore.world.instance|nil
function M.create_world_from_tiled_map(tiled_map_path)
local map = detiled_internal.load_json(tiled_map_path)
if not map then
detiled_internal.logger:error("Failed to load map", tiled_map_path)
return {}
end

return M.get_decore_world(map)
end


---@param tiled_tileset_path string
---@return table<string, entity>
function M.create_entities_from_tiled_tileset(tiled_tileset_path)
local tileset = detiled_internal.load_json(tiled_tileset_path)
if not tileset then
detiled_internal.logger:error("Failed to load tileset", tiled_tileset_path)
return {}
end

detiled_internal.load_tileset(tileset)
return M.get_decore_entities(tileset)
end


---@param tiled_map detiled.map
---@return decore.world.instance
function M.get_decore_world(tiled_map)
function M.create_world_from_tiled_map(tiled_map)
return {
entities = M.get_entities(tiled_map),
}
end


---Split each layer to separate world. Uses included_worlds to main world
---Layers with "exclude" tag will be ignored in included_worlds, but will be added to world data pack
---Split each layer to separate world, return as map of worlds
---@param world_id string
---@param tiled_map detiled.map
---@return table<string, decore.world.instance>
function M.get_decore_worlds(world_id, tiled_map)
function M.create_worlds_from_tiled_map(world_id, tiled_map)
local entities = M.get_entities(tiled_map)
local worlds = {}
local world_ids = {}
Expand All @@ -344,6 +329,7 @@ function M.get_decore_worlds(world_id, tiled_map)

local main_world = worlds[world_id] or {
entities = {},
included_worlds = {},
}
worlds[world_id] = main_world

Expand All @@ -357,6 +343,20 @@ function M.get_decore_worlds(world_id, tiled_map)
end


---@param tiled_tileset_path string
---@return table<string, entity>
function M.create_entities_from_tiled_tileset(tiled_tileset_path)
local tileset = detiled_internal.load_json(tiled_tileset_path)
if not tileset then
detiled_internal.logger:error("Failed to load tileset", tiled_tileset_path)
return {}
end

detiled_internal.load_tileset(tileset)
return M.get_decore_entities(tileset)
end


---@param tiled_tileset detiled.tileset
---@return table<string, entity> entities Key is prefab_id
function M.get_decore_entities(tiled_tileset)
Expand All @@ -369,7 +369,6 @@ function M.get_decore_entities(tiled_tileset)
local prefab_id = tile.class
---@type entity
local entity = detiled_internal.get_components_property(tile.properties) or {}
--entity.transform = decore.create_component("transform")
assert(prefab_id, "The class field in entity in tiled tileset should be set")
entities[prefab_id] = entity
end
Expand Down

0 comments on commit 67d1f55

Please sign in to comment.