forked from andreyratz/block
Cleaning up the messy sections of my code and making the debug console output easier to read
33 lines
1.2 KiB
GDScript
33 lines
1.2 KiB
GDScript
extends Area2D
|
|
|
|
var debug_normal: Vector2 = Vector2.ZERO
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
area_entered.connect(_on_area_entered)
|
|
|
|
func _on_area_entered(area):
|
|
if area.is_in_group("Projectiles"):
|
|
print("Event: Projectile hit shield")
|
|
var shield_normal = global_transform.x.normalized()
|
|
print("Readout: Shield normal: ", Vec2str(shield_normal))
|
|
# Gets the direction from the parent Node2D NOT the Area2d component
|
|
var projectile_velocity = area.get_parent().get_facing_direction()
|
|
print("Readout: Projectile dir: ", Vec2str(projectile_velocity))
|
|
var bounceTrajectory = projectile_velocity.bounce(shield_normal)
|
|
print("Readout: Bounce dir: ", Vec2str(bounceTrajectory))
|
|
if area.get_parent().has_method("changeDirection"):
|
|
area.get_parent().changeDirection(bounceTrajectory)
|
|
|
|
# Debug functions
|
|
func _draw():
|
|
# draw_line(Vector2(1,0) * 1000, debug_normal * 50, Color.RED, 2.0) # normal
|
|
pass
|
|
|
|
# Makes the string a bit easier to read in debug console
|
|
func Vec2str(vec: Vector2) -> String:
|
|
var x_sign = " " if vec.x >= 0 else ""
|
|
var y_sign = " " if vec.y >= 0 else ""
|
|
|
|
return "(%s%.2f, %s%.2f)" % [x_sign, vec.x, y_sign, vec.y]
|