68 lines
1.9 KiB
GDScript
68 lines
1.9 KiB
GDScript
class_name CelestialBody
|
|
extends RigidBody2D
|
|
|
|
# The celestial body that this body orbits.
|
|
@export var primary: CelestialBody
|
|
|
|
# This is a placeholder for your pixel art texture.
|
|
@export var texture: Texture2D
|
|
|
|
# The radius of the body, used for drawing and future collision detection.
|
|
@export var radius: float = 10.0
|
|
|
|
# Default color based on body type for visualization.
|
|
var body_color: Color = Color.ORANGE_RED
|
|
|
|
var orbit_radius_real : float = 0.0
|
|
|
|
var direction_to_primary : Vector2 = Vector2.ZERO
|
|
|
|
func get_class_name() -> String:
|
|
return "CelestialBody"
|
|
|
|
func _ready() -> void:
|
|
# We will handle gravity manually, so we set the built-in gravity scale to 0.
|
|
gravity_scale = 0.0
|
|
|
|
# To make the simulation work without drag, we must set linear damping to 0.
|
|
linear_damp = 0.0
|
|
angular_damp = 0.0
|
|
|
|
can_sleep = false
|
|
|
|
# Set the color based on the class name for easy differentiation.
|
|
match get_class_name():
|
|
"Star":
|
|
body_color = Color.GOLD
|
|
"Planet":
|
|
body_color = Color.BLUE
|
|
"Moon":
|
|
body_color = Color.PURPLE
|
|
"Station":
|
|
body_color = Color.WHITE
|
|
"Asteroid":
|
|
body_color = Color.BROWN
|
|
_:
|
|
body_color = Color.ORANGE_RED
|
|
|
|
# This callback is the correct place to apply custom forces to a RigidBody2D.
|
|
func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
|
|
if primary and is_instance_valid(primary):
|
|
var force = OrbitalMechanics.simple_n_body_grav(self, primary)
|
|
state.apply_central_force(force)
|
|
|
|
# We force a redraw here to update the body's visual representation.
|
|
queue_redraw()
|
|
|
|
# Override the default drawing function to draw the body.
|
|
# This is useful for debugging and visualization.
|
|
func _draw() -> void:
|
|
if texture:
|
|
# If a texture is assigned, draw it.
|
|
var size = Vector2(radius * 2, radius * 2)
|
|
var offset = -size / 2.0
|
|
draw_texture_rect(texture, Rect2(offset, size), false)
|
|
else:
|
|
# Otherwise, draw a simple placeholder circle.
|
|
draw_circle(Vector2.ZERO, radius, body_color)
|