88 lines
2.4 KiB
GDScript
88 lines
2.4 KiB
GDScript
extends Node
|
|
var PATH = "res://scoom/"
|
|
var EXTERNAL_PATH = "res://scoom/"
|
|
const ENTITY_CONFIG = "entity.cfg"
|
|
|
|
func _ready():
|
|
if(not OS.has_feature("editor")):
|
|
EXTERNAL_PATH = OS.get_executable_path().get_base_dir() + "/scoom/"
|
|
|
|
|
|
func _input(event):
|
|
if event is InputEventKey and event.is_pressed():
|
|
if event.keycode == KEY_T:
|
|
load_gltf("models/runtime_loader_test.glb")
|
|
|
|
func get_all_children(in_node, arr = []):
|
|
arr.push_back(in_node)
|
|
for child in in_node.get_children():
|
|
arr = get_all_children(child,arr)
|
|
return arr
|
|
|
|
func load_gltf(file, parent = self, hasCollision = false, trimesh = false):
|
|
var gltf := GLTFDocument.new()
|
|
var gltf_state := GLTFState.new()
|
|
|
|
var snd_file = FileAccess.open(PATH + file, FileAccess.READ)
|
|
|
|
var fileBytes = PackedByteArray()
|
|
fileBytes = snd_file.get_buffer(snd_file.get_length())
|
|
|
|
gltf.append_from_buffer(fileBytes, "", gltf_state)
|
|
|
|
print("Loading ", PATH + file)
|
|
var node = gltf.generate_scene(gltf_state)
|
|
var entity_count = 0;
|
|
for o in get_all_children(node):
|
|
if o.name.begins_with("entity_") && not o.name.ends_with("_geometry"):
|
|
print("Loading entity ", o.name)
|
|
if loadEntity(o):
|
|
entity_count += 1;
|
|
elif o is MeshInstance3D:
|
|
if hasCollision:
|
|
if(trimesh):
|
|
o.create_trimesh_collision()
|
|
else:
|
|
o.create_convex_collision()
|
|
parent.add_child(node)
|
|
if(entity_count > 0):
|
|
print("Loaded ", entity_count, " entities.")
|
|
return node
|
|
|
|
func get_all_entities(at_node):
|
|
var entity_count = 0
|
|
for o in get_all_children(at_node):
|
|
if o.name.begins_with("entity_") && not o.name.ends_with("_geometry"):
|
|
print("Loading entity ", o.name)
|
|
if loadEntity(o):
|
|
entity_count += 1;
|
|
if(entity_count > 0):
|
|
print("Loaded ", entity_count, " entities.")
|
|
|
|
func loadEntity(node):
|
|
var config = ConfigFile.new()
|
|
var err = config.load(PATH + ENTITY_CONFIG)
|
|
|
|
if err != OK:
|
|
printerr("COULD NOT LOAD ENTITY : ", node.name)
|
|
return false
|
|
|
|
var x = config.get_section_keys(node.name)
|
|
if(x.size() < 1):
|
|
printerr("NO CONFIG FOUND FOR ENTITY : ", node.name)
|
|
return false
|
|
|
|
var entity = load_gltf(config.get_value(node.name, "model"), node.get_parent()) as Node3D
|
|
|
|
entity.transform.origin = node.transform.origin
|
|
entity.scale = config.get_value(node.name, "scale")
|
|
node.queue_free()
|
|
return true
|
|
|
|
func loadPCK(file, parent = self):
|
|
var success = ProjectSettings.load_resource_pack(EXTERNAL_PATH + file)
|
|
|
|
if success:
|
|
print("Resource pack loaded ", EXTERNAL_PATH + file)
|
|
|