diff options
| author | Anson Bridges <bridges.anson@gmail.com> | 2022-09-15 16:43:08 -0400 |
|---|---|---|
| committer | Anson Bridges <bridges.anson@gmail.com> | 2022-09-15 16:43:08 -0400 |
| commit | 7a52122ef736a9525f98c37d3e56c4c3095b61e5 (patch) | |
| tree | 4eda6ebf8ed300bb32559a1cdbe8c4015f3f77d6 | |
| parent | aad319ddb5be108e9ca81d407bd90c843bae502d (diff) | |
Script for converting properly formatted .gd to a c++ template
| -rw-r--r-- | godot/default_env.tres | 8 | ||||
| -rw-r--r-- | godot/maps/Main.tscn | 2 | ||||
| -rw-r--r-- | godot/project.godot | 4 | ||||
| -rw-r--r-- | godot/scenes/vehicles/Airplane.tscn | 2 | ||||
| -rw-r--r-- | godot/scripts/machines/NetworkedMachineGDS.gd | 20 | ||||
| -rw-r--r-- | src/gdn_header_template | 27 | ||||
| -rw-r--r-- | src/gdn_src_template | 17 | ||||
| -rw-r--r-- | src/script_to_header.py | 127 |
8 files changed, 195 insertions, 12 deletions
diff --git a/godot/default_env.tres b/godot/default_env.tres index 55082ac..a10be49 100644 --- a/godot/default_env.tres +++ b/godot/default_env.tres @@ -1,9 +1,17 @@ [gd_resource type="Environment" load_steps=2 format=2] [sub_resource type="ProceduralSky" id=1] +sky_top_color = Color( 0.74902, 0.819608, 0.890196, 1 ) +sky_horizon_color = Color( 0.501961, 0.6, 0.701961, 1 ) ground_bottom_color = Color( 0.00392157, 0.0862745, 0.172549, 1 ) ground_horizon_color = Color( 0.00392157, 0.733333, 0.901961, 1 ) [resource] background_mode = 2 background_sky = SubResource( 1 ) +fog_enabled = true +fog_color = Color( 0.501961, 0.6, 0.701961, 1 ) +fog_sun_color = Color( 0.615686, 0.964706, 1, 1 ) +fog_depth_begin = 170.9 +fog_depth_end = 476.2 +fog_transmit_enabled = true diff --git a/godot/maps/Main.tscn b/godot/maps/Main.tscn index e6e8c27..68ad166 100644 --- a/godot/maps/Main.tscn +++ b/godot/maps/Main.tscn @@ -100,7 +100,7 @@ transform = Transform( -0.854422, 0, 0.519579, 0, 1, 0, -0.519579, 0, -0.854422, transform = Transform( -4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 30.8999, 5.13129, -8.23008 ) [node name="Gunbrig" parent="MACHINES" instance=ExtResource( 15 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -15.9478, 0.974679, 31.6985 ) +transform = Transform( 0.313296, 0, -0.949655, 0, 1, 0, 0.949655, 0, 0.313296, -15.9478, 0.974679, 31.6985 ) [node name="BALLISTICS" type="Spatial" parent="."] diff --git a/godot/project.godot b/godot/project.godot index c8de89c..995213a 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -15,6 +15,10 @@ run/main_scene="res://ui/MainMenu.tscn" boot_splash/show_image=false config/icon="res://icon.png" +[audio] + +default_bus_layout="" + [display] window/vsync/use_vsync=false diff --git a/godot/scenes/vehicles/Airplane.tscn b/godot/scenes/vehicles/Airplane.tscn index ec79c66..43c3a17 100644 --- a/godot/scenes/vehicles/Airplane.tscn +++ b/godot/scenes/vehicles/Airplane.tscn @@ -161,4 +161,4 @@ shape = SubResource( 9 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 2.72698, -0.196514, 0 ) [node name="PilotExit" type="Spatial" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 6.1653, -0.196514, 0 ) +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 2.50863, 2.36377, 0 ) diff --git a/godot/scripts/machines/NetworkedMachineGDS.gd b/godot/scripts/machines/NetworkedMachineGDS.gd index 3f2d420..c1d755d 100644 --- a/godot/scripts/machines/NetworkedMachineGDS.gd +++ b/godot/scripts/machines/NetworkedMachineGDS.gd @@ -1,26 +1,26 @@ extends RigidBody -var in_use : bool = false -var user = null -var world = null +var in_use: bool = false +var user: RigidBody = null +var world: Spatial = null func _ready(): print("net machine loads world") world = get_tree().get_root().find_node("GAMEWORLD", true, false) -remote func update_phys_transform(t, lv, av): +remote func update_phys_transform(t: Transform, lv: Vector3, av: Vector3): transform = t linear_velocity = lv angular_velocity = av -remotesync func net_apply_impulse(impulse_v): +remotesync func net_apply_impulse(impulse_v: Vector3): apply_central_impulse(impulse_v) -func _integrate_forces(_state): +func _integrate_forces(_state: PhysicsDirectBodyState): if is_network_master() and mode == MODE_RIGID: rpc("update_phys_transform", transform, linear_velocity, angular_velocity) -remotesync func set_net_owner(id, char_name): +remotesync func set_net_owner(id: int, char_name: String): set_network_master(id) if id == 1 and char_name == "NONE": #not under control on_no_control() @@ -51,11 +51,11 @@ func attack1(): func attack2(): pass -func direction_input(_fwd,_bwd,_left,_right,_left2,_right2): +func direction_input(_fwd: float,_bwd: float,_left: float,_right: float,_left2: float,_right2: float): pass -func misc_input(_ctrl,_space,_shift): +func misc_input(_ctrl: float,_space: float,_shift: float): pass -func mouse_input(_m1,_m3,_m2): #used for long-press actions +func mouse_input(_m1: float,_m3: float,_m2: float): #used for long-press actions pass diff --git a/src/gdn_header_template b/src/gdn_header_template new file mode 100644 index 0000000..d6e3d3e --- /dev/null +++ b/src/gdn_header_template @@ -0,0 +1,27 @@ +#ifndef {HEADER_NAME} +#define {HEADER_NAME} + +#include <Godot.hpp> +#include <{BASE_CLASS_NAME}.hpp> + +namespace godot { + +class {CLASS_NAME} : public {BASE_CLASS_NAME} { + GODOT_CLASS({CLASS_NAME}, {BASE_CLASS_NAME}) + +private: +{VARS} + +public: + static void _register_methods(); + + {CLASS_NAME}(); + ~{CLASS_NAME}(); + + void _init(); + +{FUNCS} +}; + +} +#endif diff --git a/src/gdn_src_template b/src/gdn_src_template new file mode 100644 index 0000000..35c86fb --- /dev/null +++ b/src/gdn_src_template @@ -0,0 +1,17 @@ +#include "{NEW_FILE_BASE}.h" + +using namespace godot; + +{CLASS_NAME}::{CLASS_NAME} () { } +{CLASS_NAME}::~{CLASS_NAME} () { } + +void {CLASS_NAME}::_register_methods() { +{REGISTER_METHODS} +{REGISTER_VARS} +} + +void {CLASS_NAME}::_init() { + {INIT_VARS} +} + +{FULL_FUNCS} diff --git a/src/script_to_header.py b/src/script_to_header.py new file mode 100644 index 0000000..452a09d --- /dev/null +++ b/src/script_to_header.py @@ -0,0 +1,127 @@ +import sys +from os.path import exists + +RPC_DICT = {"remote" : "GODOT_METHOD_RPC_MODE_REMOTE", "remotesync" : "GODOT_METHOD_RPC_MODE_REMOTESYNC", \ + "master" : "GODOT_METHOD_RPC_MODE_MASTER", "mastersync" : "GODOT_METHOD_RPC_MODE_MASTERSYNC", \ + "puppet" : "GODOT_METHOD_RPC_MODE_PUPPET", "puppetsync" : "GODOT_METHOD_RPC_MODE_PUPPETSYNC"} + +def script_to_gdn(filename): + if not exists(filename) or not filename.endswith(".gd"): + return + class_name = filename[:-3] + new_file_base = class_name.lower() + header_name = class_name.upper() + "_H" + base_class_name = "Node" + class_vars = [] # {export bool, onready bool, type str, varname str, initialvalue} + funcs = [] # {net type, return type, name, [args], [content]} args = [name, type], content = func content lines list + + script = open(filename, "r") + template_h = open("gdn_header_template", "r") + template_c = open("gdn_src_template", "r") + + header_file_text = template_h.read(); template_h.close(); + src_file_text = template_c.read(); template_c.close(); + + lines = script.readlines() + for I in range(len(lines)): + line = lines[I] + if line.startswith("extends"): + base_class_name = line.split(" ")[-1].strip(); + + if line.startswith("var") or line.startswith("export var") or line.startswith("const") or line.startswith("onready var"): + class_vars.append( [ line.startswith("export"), line.startswith("onready"), line.split(":")[1].split('=')[0].strip(), line.split(":")[0].split(" ")[-1], "" if "=" not in line else line.split("=")[-1].strip() ]) + + if line.startswith("func") or line.startswith("remote") or line.startswith("master") or line.startswith("puppet"): + functype = line[0:line.index(" ")] + + args = line[line.index("(")+1 : line.index(")")].split(",") + for i, arg in enumerate(args): + if arg == "": + args = [] + break + args[i] = [arg.split(":")[0].strip(), arg.split(":")[1].strip()] + + returntype = "void" + if "->" in line: + returntype = line[line.index("->"+1),line.index(":").strip()] + + funcname = line[ line[0:line.index("(")].rindex(" ") : int(line.index("(")) ].strip() + + content = [] + I+=1 + line = lines[I] + while line.startswith('\t'): + content.append(line) + I+=1 + if I != len(lines): + line = lines[I] + else: + line = "EOF" + I-=1 + + funcs.append( [functype, returntype, funcname, args, content] ) + script.close() + + header_file_text = header_file_text.replace("{HEADER_NAME}", header_name).replace("{BASE_CLASS_NAME}", base_class_name).replace("{CLASS_NAME}", class_name) + + var_text = "" + for var in class_vars: + var_text += '\t' + var[2] + " " + var[3] + ";\n" # type varname; + header_file_text = header_file_text.replace("{VARS}", var_text) + + func_text = "" + for func in funcs: + func_text += '\t' + func[1] + " " + func[2] + "(" # type functionname( + for i,arg in enumerate(func[3]): + func_text += arg[1] + " " + arg[0] + if i != len(func[3])-1: + func_text += ", " + func_text += ");\n" + header_file_text = header_file_text.replace("{FUNCS}", func_text) + #print(header_file_text) + with open(new_file_base+".h", "w") as f: + f.write(header_file_text) + + #SOURCE CODE: + src_file_text = src_file_text.replace("{CLASS_NAME}", class_name).replace("{NEW_FILE_BASE}",new_file_base) + + reg_vars = "" + vars_init = "" + vars_ready = "" + for var in class_vars: + if var[0]: #export + reg_vars += "\tregister_property<"+class_name+", "+var[2]+">(\""+var[3]+"\", &"+class_name+"::"+var[3]+", "+var[2]+"("+var[4]+"));\n" + elif var[1]: #onready + vars_ready += "\t"+var[3]+" = "+var[4]+";\n" + else: + vars_init += "\t"+var[3]+" = "+var[4]+";\n" + reg_vars = reg_vars[:-1] + reg_methods = "" + src_func_text = "" + for func in funcs: + reg_methods += "\tregister_method(\"" + func[2]+"\", &"+class_name+"::"+func[2] + if func[0] != "func": + reg_methods += ", " + RPC_DICT[func[0]] + reg_methods += ");\n" + + src_func_text += func[1] + " " + class_name + "::" + func[2] + "(" #returntype ClassName::functionname( + for i,arg in enumerate(func[3]): + src_func_text += arg[1] + " " + arg[0] + if i != len(func[3])-1: + src_func_text += ", " + src_func_text += ") {\n" + if func[2] == "_ready": + src_func_text += vars_ready + src_func_text += "//" + "//".join(func[4]) #join content (commented out) + src_func_text += "}\n\n" + + src_file_text = src_file_text.replace("{INIT_VARS}", vars_init).replace("{REGISTER_METHODS}",reg_methods).replace("{REGISTER_VARS}", reg_vars).replace("{FULL_FUNCS}", src_func_text) + + with open(new_file_base+".cpp", "w") as f: + f.write(src_file_text) + +if __name__ == "__main__": + n = len(sys.argv) + if n != 2: + quit() + script_to_gdn(sys.argv[1]) |
