39 lines
1.1 KiB
GDScript
39 lines
1.1 KiB
GDScript
extends RigidBody2D
|
|
|
|
@onready var hurtbox: Area2D = $HurtArea2D
|
|
|
|
@export var launch_speed: float = 200
|
|
@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, min_speed: float = 150, max_speed: float = 250) -> void:
|
|
angle = randf_range(-PI/3, PI/3) + PI * float(randi()%2) if angle == 0 else angle
|
|
linear_velocity = Vector2(cos(angle), sin(angle)) * randf_range(min_speed, max_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
|
|
var angle: float = randf_range(3*PI/4, PI/4) * (-1 if player.id == 1 else 1)
|
|
launch(angle)
|
|
print("ball hit")
|
|
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
|