smashball/scenes/ball/ball.gd

37 lines
948 B
GDScript

extends RigidBody2D
@onready var hurtbox: Area2D = $HurtArea2D
@export var launch_speed: float = 400
@export var speed_mult: float = 1.08
var is_hit: bool = false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
hurtbox.connect("area_entered", _on_hit)
launch()
func launch(angle: float = 0) -> void:
angle = randf_range(-PI/3, PI/3) + PI * float(randi()%2) if angle == 0 else angle
linear_velocity = Vector2(cos(angle), sin(angle)) * launch_speed
func _on_hit(hitbox: Area2D) -> void:
print("area detected")
if not is_hit:
if hitbox.is_in_group("hit"):
print("ball not hit yet")
var player: Player = hitbox.get_parent()
var timer: Timer = player.hit_timer
launch()
if not timer.is_connected("timeout", _on_hit_end):
timer.connect("timeout", _on_hit_end)
EventBus.ball_hit.emit(player.id, linear_velocity)
is_hit = true
func _on_hit_end() -> void:
is_hit = false