9func from_world(world:Dictionary, options:Dictionary = {}) -> Array:
11 entities.append_array(_from_section(world.get(
"enemies", []),
"enemy", options))
12 entities.append_array(_from_section(world.get(
"actors", []),
"actor", options))
13 entities.append_array(_from_section(world.get(
"pickups", world.get(
"material_nodes", [])),
"pickup", options))
14 entities.append_array(_from_section(world.get(
"hazards", []),
"hazard", options))
15 entities.append_array(_from_section(world.get(
"interactables", world.get(
"spell_interactions", [])),
"interactable", options))
17 var exit = world.get(
"exit", {})
18 if exit
is Dictionary:
19 var exit_entity = exit.duplicate(true)
20 exit_entity[
"id"] = str(exit_entity.get(
"id",
"exit"))
21 exit_entity[
"kind"] =
"exit"
22 exit_entity[
"tags"] = _merged_tags(exit_entity.get(
"tags", []), [
"exit",
"interactable"])
23 entities.append(exit_entity)
28func from_placements(placements:Array, default_kind:String =
"entity", options:Dictionary = {}) -> Array:
29 return _from_section(placements, default_kind, options)
33func to_placement(entity:Dictionary) -> Dictionary:
35 "id": str(entity.get(
"id",
"")),
36 "kind": str(entity.get(
"kind", entity.get(
"type",
"entity"))),
37 "cell": entity.get(
"cell", [0, 0])
39 for key
in [
"name",
"type",
"sprite_id",
"tags",
"metadata"]:
41 placement[key] = entity[key]
45func _from_section(section, default_kind:String, options:Dictionary) -> Array:
47 if not section
is Array:
49 for index
in range(section.size()):
50 var raw = section[index]
51 if not raw
is Dictionary:
53 var entity = raw.duplicate(true)
54 entity[
"kind"] = str(entity.get(
"kind", entity.get(
"type", default_kind)))
55 entity[
"type"] = str(entity.get(
"type", entity.get(
"kind", default_kind)))
56 entity[
"id"] = str(entity.get(
"id",
"%s_%d" % [default_kind, index + 1]))
57 entity[
"team"] = str(entity.get(
"team", _default_team(str(entity.get(
"kind", default_kind)))))
58 entity[
"tags"] = _merged_tags(entity.get(
"tags", []), [default_kind])
59 if entity.has(
"grid")
and not entity.has(
"cell"):
60 entity[
"cell"] = entity.get(
"grid")
61 if options.has(
"source"):
62 entity[
"source"] = str(options.get(
"source",
""))
67func _default_team(kind:String) -> String:
77func _merged_tags(raw_tags, extra:Array) -> Array:
82 if text !=
"" and not tags.has(text):
86 if text !=
"" and not tags.has(text):