RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
entity_definition_adapter.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7
8
9func from_world(world:Dictionary, options:Dictionary = {}) -> Array:
10 var entities := []
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))
16 if world.has("exit"):
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)
24 return entities
25
26
27
28func from_placements(placements:Array, default_kind:String = "entity", options:Dictionary = {}) -> Array:
29 return _from_section(placements, default_kind, options)
30
31
32
33func to_placement(entity:Dictionary) -> Dictionary:
34 var placement := {
35 "id": str(entity.get("id", "")),
36 "kind": str(entity.get("kind", entity.get("type", "entity"))),
37 "cell": entity.get("cell", [0, 0])
38 }
39 for key in ["name", "type", "sprite_id", "tags", "metadata"]:
40 if entity.has(key):
41 placement[key] = entity[key]
42 return placement
43
44
45func _from_section(section, default_kind:String, options:Dictionary) -> Array:
46 var result := []
47 if not section is Array:
48 return result
49 for index in range(section.size()):
50 var raw = section[index]
51 if not raw is Dictionary:
52 continue
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", ""))
63 result.append(entity)
64 return result
65
66
67func _default_team(kind:String) -> String:
68 match kind:
69 "enemy", "hazard":
70 return "enemy"
71 "actor", "player":
72 return "player"
73 _:
74 return "neutral"
75
76
77func _merged_tags(raw_tags, extra:Array) -> Array:
78 var tags := []
79 if raw_tags is Array:
80 for tag in raw_tags:
81 var text = str(tag)
82 if text != "" and not tags.has(text):
83 tags.append(text)
84 for tag in extra:
85 var text = str(tag)
86 if text != "" and not tags.has(text):
87 tags.append(text)
88 return tags