giants-godot/scenes/playground.gd

112 lines
3.4 KiB
GDScript3
Raw Normal View History

2021-09-17 15:04:05 +02:00
extends Spatial
tool
func dict_of_lowercase_filenames(path):
var dir_files = {}
var dirpath = path
var dir = Directory.new()
dir.open(dirpath)
dir.list_dir_begin()
while true:
var file = dir.get_next()
if file == "":
break
elif not file.begins_with("."):
if file.ends_with(".import"):
dir_files[file.to_lower().replace(".import", "")] = path+"/"+file.replace(".import", "")
else:
if dir.file_exists(file+".import"):
continue
else:
dir_files[file.to_lower()] = path+"/"+file
dir.list_dir_end()
return dir_files
func load_json_file(path):
"""Loads a JSON file from the given res path and return the loaded JSON object."""
var file = File.new()
var open_err = file.open(path, file.READ)
if open_err != OK:
print("[file.open] Error loading file '" + str(path) + "'.")
print("\tError: ", open_err)
return null
var text = file.get_as_text()
var result_json = JSON.parse(text)
if result_json.error != OK:
print("[load_json_file] Error loading JSON file '" + str(path) + "'.")
print("\tError: ", result_json.error)
print("\tError Line: ", result_json.error_line)
print("\tError String: ", result_json.error_string)
return null
var obj = result_json.result
return obj
# Called when the node enters the scene tree for the first time.
func _ready():
var assets_files = dict_of_lowercase_filenames("res://assets/all_gbs")
var audio_files = dict_of_lowercase_filenames("res://assets/audio/Stream/Ambient")
var ids = load_json_file("res://assets/id_to_gbs.json")
print("Loaded %s valid objects" % len(ids))
print("Godot found %s GBS available to load" % str(len(assets_files)))
var map_objs = load_json_file("res://assets/terrains/square_one_1/w_M_3Way_Tigs - Threeway - Square One.bin.txt.json")
#var map_origin = $MeshInstance.global_transform.origin
#var map_stretch = 1
for obj in map_objs:
var _id = str(obj["id"])
if not _id in ids:
push_warning("Giants obj %s could not be loaded" % _id)
continue
var gbs_file = ids[_id]["model"] + ".gbs.obj"
if not gbs_file in assets_files:
push_warning("GBS %s could not be loaded" % gbs_file)
continue
var mesh_instance = MeshInstance.new()
var real_file_for_gbs = assets_files[gbs_file]
var scene_name = real_file_for_gbs
var scene = load(scene_name)
if not scene:
print("could not load "+ scene_name)
continue
var mesh = scene
mesh_instance.mesh = mesh
add_child(mesh_instance)
var obj_position = Vector3(
obj["x"],
obj["z"]-5,
-obj["y"])
mesh_instance.global_transform.origin = obj_position
var obj_rotation = Vector3(
deg2rad(obj["angle_2"]-90), deg2rad(obj["angle"]), deg2rad(obj["angle_3"])
)
mesh_instance.rotation = obj_rotation
var obj_scale = Vector3(obj["scale"], obj["scale"], obj["scale"])
mesh_instance.scale = obj_scale
mesh_instance.create_trimesh_collision()
if "audio" in ids[_id]:
var audio_raw = ids[_id]["audio"] + ".wav"
if not audio_raw in audio_files:
push_warning(obj["audio"] + ".wav could not be found")
continue
var audiostream = AudioStreamPlayer3D.new()
audiostream.autoplay = true
audiostream.stream = load(audio_files[audio_raw])
audiostream.stream.loop_mode = AudioStreamSample.LOOP_FORWARD
audiostream.unit_size = ids[_id]["audiodist"] /2
if ids[_id]["audiodist"] > 1000:
print(ids[_id]["model"]+" has audiodist "+ids[_id]["audiodist"])
audiostream.bus = "Ambiants"
mesh_instance.add_child(audiostream)
2021-09-22 18:04:03 +02:00
func _process(_delta):
2021-09-17 15:04:05 +02:00
pass