49 lines
1.4 KiB
GDScript
49 lines
1.4 KiB
GDScript
extends Node2D
|
|
|
|
class_name Projectile
|
|
|
|
@export var Speed : float = 300
|
|
@export var RotationSpeed : float = 0
|
|
@export var Damage : float = 1
|
|
@export var Collider : Area2D
|
|
@export var Lifetime : float = 10
|
|
|
|
var Fraction = "none"
|
|
var DestinationAngle : float
|
|
var Target : Node2D = self
|
|
|
|
func _ready():
|
|
get_tree().create_timer(Lifetime).timeout.connect(queue_free)
|
|
Collider.body_entered.connect(_on_collision)
|
|
areyouready()
|
|
|
|
func _physics_process(delta):
|
|
if RotationSpeed == 0: DestinationAngle = global_rotation_degrees
|
|
else: DestinationAngle = rad_to_deg(global_position.angle_to_point(Target.global_position))
|
|
global_position += Speed * delta * global_transform.x
|
|
var DestinationDifference : float
|
|
if DestinationAngle - global_rotation_degrees == clamp(DestinationAngle - global_rotation_degrees, -180, 180):
|
|
DestinationDifference = DestinationAngle - global_rotation_degrees
|
|
else:
|
|
DestinationDifference = global_rotation_degrees - DestinationAngle
|
|
if DestinationDifference != clamp(DestinationDifference, -1, 1):
|
|
global_rotation_degrees += sign(DestinationDifference) * RotationSpeed * delta
|
|
else:
|
|
global_rotation_degrees = DestinationAngle
|
|
|
|
func _on_collision(body):
|
|
match body.collision_layer:
|
|
1:
|
|
if body.Fraction != Fraction:
|
|
if Target != self:
|
|
Target.queue_free()
|
|
body.Shield.deal_damage(Damage)
|
|
queue_free()
|
|
2:
|
|
if Target != self:
|
|
Target.queue_free()
|
|
queue_free()
|
|
|
|
|
|
func areyouready():
|
|
pass
|