From c9b75431f313fae16e9f4c9e60d7f5eaed0aaec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Mon, 28 Mar 2022 15:48:38 +0200 Subject: Refactor GDScript/C# script templates logic to be editor-only Not a full refactor as it still goes through ScriptLanguage so it's hacky, but at least it can now compile without this. --- .../CharacterBody2D/basic_movement.cs | 41 ++++++++++++++ .../CharacterBody3D/basic_movement.cs | 44 +++++++++++++++ .../editor/script_templates/EditorPlugin/plugin.cs | 20 +++++++ .../EditorScript/basic_editor_script.cs | 15 ++++++ .../mono/editor/script_templates/Node/default.cs | 17 ++++++ .../mono/editor/script_templates/Object/empty.cs | 8 +++ modules/mono/editor/script_templates/SCsub | 16 ++++++ .../VisualShaderNodeCustom/basic.cs | 62 ++++++++++++++++++++++ 8 files changed, 223 insertions(+) create mode 100644 modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs create mode 100644 modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs create mode 100644 modules/mono/editor/script_templates/EditorPlugin/plugin.cs create mode 100644 modules/mono/editor/script_templates/EditorScript/basic_editor_script.cs create mode 100644 modules/mono/editor/script_templates/Node/default.cs create mode 100644 modules/mono/editor/script_templates/Object/empty.cs create mode 100644 modules/mono/editor/script_templates/SCsub create mode 100644 modules/mono/editor/script_templates/VisualShaderNodeCustom/basic.cs (limited to 'modules/mono/editor') diff --git a/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs b/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs new file mode 100644 index 0000000000..2ca81ab7cd --- /dev/null +++ b/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs @@ -0,0 +1,41 @@ +// meta-description: Classic movement for gravity games (platformer, ...) + +using _BINDINGS_NAMESPACE_; +using System; + +public partial class _CLASS_ : _BASE_ +{ + public const float Speed = 300.0f; + public const float JumpVelocity = -400.0f; + + // Get the gravity from the project settings to be synced with RigidDynamicBody nodes. + public float gravity = (float)ProjectSettings.GetSetting("physics/2d/default_gravity"); + + public override void _PhysicsProcess(float delta) + { + Vector2 velocity = Velocity; + + // Add the gravity. + if (!IsOnFloor()) + velocity.y += gravity * delta; + + // Handle Jump. + if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) + velocity.y = JumpVelocity; + + // Get the input direction and handle the movement/deceleration. + // As good practice, you should replace UI actions with custom gameplay actions. + Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down"); + if (direction != Vector2.Zero) + { + velocity.x = direction.x * Speed; + } + else + { + velocity.x = Mathf.MoveToward(Velocity.x, 0, Speed); + } + + Velocity = velocity; + MoveAndSlide(); + } +} diff --git a/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs b/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs new file mode 100644 index 0000000000..a6935fe497 --- /dev/null +++ b/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs @@ -0,0 +1,44 @@ +// meta-description: Classic movement for gravity games (FPS, TPS, ...) + +using _BINDINGS_NAMESPACE_; +using System; + +public partial class _CLASS_ : _BASE_ +{ + public const float Speed = 5.0f; + public const float JumpVelocity = 4.5f; + + // Get the gravity from the project settings to be synced with RigidDynamicBody nodes. + public float gravity = (float)ProjectSettings.GetSetting("physics/3d/default_gravity"); + + public override void _PhysicsProcess(float delta) + { + Vector3 velocity = Velocity; + + // Add the gravity. + if (!IsOnFloor()) + velocity.y -= gravity * delta; + + // Handle Jump. + if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) + velocity.y = JumpVelocity; + + // Get the input direction and handle the movement/deceleration. + // As good practice, you should replace UI actions with custom gameplay actions. + Vector2 inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down"); + Vector3 direction = Transform.basis.Xform(new Vector3(inputDir.x, 0, inputDir.y)).Normalized(); + if (direction != Vector3.Zero) + { + velocity.x = direction.x * Speed; + velocity.z = direction.z * Speed; + } + else + { + velocity.x = Mathf.MoveToward(Velocity.x, 0, Speed); + velocity.z = Mathf.MoveToward(Velocity.z, 0, Speed); + } + + Velocity = velocity; + MoveAndSlide(); + } +} diff --git a/modules/mono/editor/script_templates/EditorPlugin/plugin.cs b/modules/mono/editor/script_templates/EditorPlugin/plugin.cs new file mode 100644 index 0000000000..eba5fd12a4 --- /dev/null +++ b/modules/mono/editor/script_templates/EditorPlugin/plugin.cs @@ -0,0 +1,20 @@ +// meta-description: Basic plugin template + +#if TOOLS +using _BINDINGS_NAMESPACE_; +using System; + +[Tool] +public partial class _CLASS_ : _BASE_ +{ + public override void _EnterTree() + { + // Initialization of the plugin goes here. + } + + public override void _ExitTree() + { + // Clean-up of the plugin goes here. + } +} +#endif diff --git a/modules/mono/editor/script_templates/EditorScript/basic_editor_script.cs b/modules/mono/editor/script_templates/EditorScript/basic_editor_script.cs new file mode 100644 index 0000000000..1b347edc1c --- /dev/null +++ b/modules/mono/editor/script_templates/EditorScript/basic_editor_script.cs @@ -0,0 +1,15 @@ +// meta-description: Basic editor script template + +#if TOOLS +using _BINDINGS_NAMESPACE_; +using System; + +[Tool] +public partial class _CLASS_ : _BASE_ +{ + // Called when the script is executed (using File -> Run in Script Editor). + public override void _Run() + { + } +} +#endif diff --git a/modules/mono/editor/script_templates/Node/default.cs b/modules/mono/editor/script_templates/Node/default.cs new file mode 100644 index 0000000000..4c86d1666f --- /dev/null +++ b/modules/mono/editor/script_templates/Node/default.cs @@ -0,0 +1,17 @@ +// meta-description: Base template for Node with default Godot cycle methods + +using _BINDINGS_NAMESPACE_; +using System; + +public partial class _CLASS_ : _BASE_ +{ + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + } + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(float delta) + { + } +} diff --git a/modules/mono/editor/script_templates/Object/empty.cs b/modules/mono/editor/script_templates/Object/empty.cs new file mode 100644 index 0000000000..34526d514f --- /dev/null +++ b/modules/mono/editor/script_templates/Object/empty.cs @@ -0,0 +1,8 @@ +// meta-description: Empty template suitable for all Objects + +using _BINDINGS_NAMESPACE_; +using System; + +public partial class _CLASS_ : _BASE_ +{ +} diff --git a/modules/mono/editor/script_templates/SCsub b/modules/mono/editor/script_templates/SCsub new file mode 100644 index 0000000000..39f6cb5c01 --- /dev/null +++ b/modules/mono/editor/script_templates/SCsub @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +Import("env") + +import editor.template_builders as build_template_cs + +env["BUILDERS"]["MakeCSharpTemplateBuilder"] = Builder( + action=env.Run(build_template_cs.make_templates, "Generating C# templates header."), + suffix=".h", + src_suffix=".cs", +) + +# Template files +templates_sources = Glob("*/*.cs") + +env.Alias("editor_template_cs", [env.MakeCSharpTemplateBuilder("templates.gen.h", templates_sources)]) diff --git a/modules/mono/editor/script_templates/VisualShaderNodeCustom/basic.cs b/modules/mono/editor/script_templates/VisualShaderNodeCustom/basic.cs new file mode 100644 index 0000000000..a1b93e7daa --- /dev/null +++ b/modules/mono/editor/script_templates/VisualShaderNodeCustom/basic.cs @@ -0,0 +1,62 @@ +// meta-description: Visual shader's node plugin template + +using _BINDINGS_NAMESPACE_; +using System; + +public partial class VisualShaderNode_CLASS_ : _BASE_ +{ + public override string _GetName() + { + return "_CLASS_"; + } + + public override string _GetCategory() + { + return ""; + } + + public override string _GetDescription() + { + return ""; + } + + public override int _GetReturnIconType() + { + return 0; + } + + public override int _GetInputPortCount() + { + return 0; + } + + public override string _GetInputPortName(int port) + { + return ""; + } + + public override int _GetInputPortType(int port) + { + return 0; + } + + public override int _GetOutputPortCount() + { + return 1; + } + + public override string _GetOutputPortName(int port) + { + return "result"; + } + + public override int _GetOutputPortType(int port) + { + return 0; + } + + public override string _GetCode(Godot.Collections.Array inputVars, Godot.Collections.Array outputVars, Shader.Mode mode, VisualShader.Type type) + { + return ""; + } +} -- cgit v1.2.3