37 lines
1.2 KiB
GDScript
37 lines
1.2 KiB
GDScript
extends Area2D
|
|
|
|
# Toggles debugger console output
|
|
const isVerbose: bool = false
|
|
|
|
func _ready() -> void:
|
|
area_entered.connect(_on_area_entered)
|
|
|
|
func _on_area_entered(area):
|
|
if area.is_in_group("Projectiles"):
|
|
# gets parent node because parent is what's moving, not the child area node
|
|
var parent = area.get_parent()
|
|
readout("Event: Projectile has hit player shield")
|
|
var projectile_dir = Vector2.from_angle(parent.rotation)
|
|
vec2Readout("Projectile Dir", projectile_dir)
|
|
var shield_normal = global_transform.x.normalized()
|
|
vec2Readout("Shield Normal: ", shield_normal)
|
|
if parent.has_method("changeDirection"):
|
|
var bounce_dir = projectile_dir.bounce(shield_normal)
|
|
parent.changeDirection(bounce_dir)
|
|
|
|
# makes debug output cleaner when printing Vector2
|
|
func vec2str(vec: Vector2) -> String:
|
|
var x_str = String.num(vec.x, 2)
|
|
var y_str = String.num(vec.y, 2)
|
|
if not x_str.begins_with("-"): x_str = " " + x_str
|
|
if not y_str.begins_with("-"): y_str = " " + y_str
|
|
return "(%s, %s)" % [x_str, y_str]
|
|
|
|
func vec2Readout(label: String, vec: Vector2):
|
|
if isVerbose:
|
|
print("Readout: ", label, vec2str(vec))
|
|
|
|
func readout(str: String):
|
|
if isVerbose:
|
|
print(str)
|