#Refrences
@onready var main_menu_button : Button = UI/MainMenu@onreadyvarUI/ChromaticAbberation
Assistant
To reuse functionality for 2D planes and add a global gravity effect, we can abstract the shared functionality into a base class that other nodes can inherit from.
Let's create a Base2DPlane.gd script for the common properties and methods:
# Base2DPlane.gd
extends Node2D
# Global gravity that affects all instances
var global_gravity: Vector2 = Vector2(0, -98) # Assuming 'up' is negative Y
# Implementing gravity effect on objects, assuming they have a velocity
func apply_global_gravity(delta, velocity):
velocity += global_gravity * delta
return velocity
# This method can be extended by children who need specific _ready behavior
func _ready():
pass
# ... Include methods that are common across 2D planes here, e.g.,
# _unhandled_input, player_died, etc.
Now, in your existing node script, inherit from Base2DPlane.gd instead:
gd
extends "res://path/to/Base2DPlane.gd" # Update path
#.. rest of your properties (MOUSE, MOUSE_OFFSET, etc.)
# Override the _ready function if necessary, make sure to call .ready() from parent
func _ready():
# Call the parent's _ready() in case something is defined there
.ready()
EventManager.player_died.connect(player_died)
EventManager.frame_freeze.connect(frame_freeze)
GameManager.spawn_current_player()
# Now any other 2D planes you make can also inherit from Base2DPlane.gd
# and benefit from the shared code and global gravity implementation.
By inheriting from Base2DPlane.gd, any 2D plane can now reuse the common functionality. If you have objects that should be affected by gravity (like characters or physics objects), you can call the apply_global_gravity function in their _physics_process or _process functions with their velocity as the argument.
gd
# Any object that needs to be affected by gravity
extends "res://path/to/Base2DPlane.gd" # Update path to correct one
var velocity = Vector2()
func _physics_process(delta):
velocity = apply_global_gravity(delta, velocity)
# ... additional physics processing for the object
Remember, when you define global_gravity in Base2DPlane.gd as a variable and not as a constant, you can later adjust it from each instance or globally, as needed, to simulate different gravitational effects on different maps or contexts.