40 lines
1.2 KiB
GDScript3
40 lines
1.2 KiB
GDScript3
|
|
extends Area2D
|
||
|
|
|
||
|
|
var velocity: Vector2 = Vector2.ZERO
|
||
|
|
var rotation_speed: float = 0.0
|
||
|
|
|
||
|
|
var shape_points: PackedVector2Array = PackedVector2Array()
|
||
|
|
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
randomize()
|
||
|
|
add_to_group("asteroids")
|
||
|
|
area_entered.connect(_on_area_entered)
|
||
|
|
|
||
|
|
var point_count: int = 7
|
||
|
|
for i in range(point_count):
|
||
|
|
var angle: float = (float(i) / point_count) * TAU
|
||
|
|
var radius: float = randf_range(14.0, 22.0)
|
||
|
|
shape_points.append(Vector2(cos(angle), sin(angle)) * radius)
|
||
|
|
queue_redraw()
|
||
|
|
|
||
|
|
|
||
|
|
func _process(delta: float) -> void:
|
||
|
|
# Deliberately no queue_redraw() here. position and rotation are node
|
||
|
|
# TRANSFORM properties - Godot applies them when rendering, and the
|
||
|
|
# polygon built in _ready() is in local space, so it never changes.
|
||
|
|
# Redrawing each frame re-tessellated four antialiased polylines per
|
||
|
|
# asteroid for an identical result; with ~150 asteroids on screen that
|
||
|
|
# was the single largest source of stutter.
|
||
|
|
position += velocity * delta
|
||
|
|
rotation += rotation_speed * delta
|
||
|
|
|
||
|
|
|
||
|
|
func _on_area_entered(area: Area2D) -> void:
|
||
|
|
if area.is_in_group("player_ships"):
|
||
|
|
AsteroidExplosionVFX.spawn(get_tree().root, position)
|
||
|
|
queue_free()
|
||
|
|
|
||
|
|
|
||
|
|
func _draw() -> void:
|
||
|
|
NeonDraw.draw_glowing_polygon(self, shape_points, Color(0.65, 0.5, 0.35), 1.0)
|