extends Spatial onready var kabuto = $Kabuto var kabuto_path: PoolVector3Array onready var player = $PlayerController.character onready var nav = $Navigation 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() # mesh_instance.get_node("StaticBody").add_to_group("objects") # mesh_instance.add_to_group("objects") 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) func angle_between(a: Vector3, b: Vector3) -> float: var angle = rad2deg(atan2(b.y - a.y, b.x - a.x)) if angle < 0: angle += 360 angle = (int(angle) + 90) % 360 return float(angle) func _physics_process(delta): if player.translation.distance_to(last_target_position) > 10: last_target_position = player.translation calculate_path() if kabuto_path: var direction = kabuto_path[path_index] print(direction) print("kabuto.translation: %s, distance = %s" % [kabuto.translation, kabuto.translation.distance_to(direction)]) while kabuto.translation.distance_to(direction) < 10: path_index = wrapi(path_index + 1, 0, kabuto_path.size()) direction = kabuto_path[path_index] - kabuto.translation var move_axis = Vector3(direction - kabuto.translation) move_axis.y = 0 var look_at = kabuto_path[path_index] look_at.y = kabuto.transform.origin.y kabuto.look_at(look_at, Vector3.UP) kabuto.move_axis = Vector3(1, 0, 0) print("kabuto.move_axis: %s" % kabuto.move_axis) else: kabuto.move_axis = Vector3.ZERO var visual_path = [] var path_index = 0 onready var last_target_position = player.translation func _on_Timer_timeout(): pass func calculate_path(): for o in visual_path: o.queue_free() visual_path.clear() kabuto_path = nav.get_simple_path(kabuto.translation, player.translation, true) path_index = 0 print("Path length: %s" % len(kabuto_path)) for i in range(len(kabuto_path)): var t = kabuto_path[i] var cube = CubeMesh.new() cube.size = Vector3(1, 1, 1) var m = MeshInstance.new() m.translation = t m.material_override = SpatialMaterial.new() m.material_override.albedo_color = Color.green.linear_interpolate(Color.red, float(i)/float((len(kabuto_path)-1))) m.mesh = cube visual_path.append(m) add_child(m)