properly detect player hits

This commit is contained in:
yuki 2025-11-14 08:01:06 -03:00
parent d1822e98b1
commit 99a6cf2164
5 changed files with 30 additions and 18 deletions

View file

@ -33,7 +33,7 @@ version_control/autoload_on_startup=true
[global_group]
paddle="objects that hit the ball"
hit="hitboxes"
[input]

View file

@ -10,14 +10,26 @@ 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_random()
launch()
func launch_random():
var angle = randf_range(-PI/3, PI/3) + PI * float(randi()%2)
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(area: Area2D) -> void:
if area.is_in_group("paddle"):
linear_velocity = linear_velocity.normalized() * linear_velocity.length() * speed_mult
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: CharacterBody2D = hitbox.get_parent()
var timer: Timer = hitbox.timer
launch()
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

View file

@ -14,7 +14,7 @@ radius = 11.0
radius = 8.062258
[node name="Ball" type="RigidBody2D"]
collision_layer = 18
collision_layer = 16
collision_mask = 5
physics_material_override = SubResource("PhysicsMaterial_oqv3d")
gravity_scale = 0.0
@ -25,8 +25,8 @@ script = ExtResource("1_nv6q1")
texture = ExtResource("1_oqv3d")
[node name="HurtArea2D" type="Area2D" parent="."]
collision_layer = 16
collision_mask = 0
collision_layer = 32
collision_mask = 64
[node name="CollisionShape2D" type="CollisionShape2D" parent="HurtArea2D"]
shape = SubResource("CircleShape2D_oqv3d")

View file

@ -7,9 +7,9 @@
radius = 7.0
height = 18.0
[node name="Hit" type="Area2D" groups=["paddle"]]
collision_layer = 0
collision_mask = 16
[node name="Hit" type="Area2D" groups=["hit"]]
collision_layer = 64
collision_mask = 0
script = ExtResource("1_fxeki")
[node name="Sprite2D" type="Sprite2D" parent="."]

View file

@ -1,6 +1,6 @@
extends CharacterBody2D
@export var player: int = 1
@export var id: int = 1
@export var max_speed: float = 150
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@ -57,6 +57,10 @@ func hit(dir: String) -> void:
var sprite_texture: Texture2D = sprite.sprite_frames.get_frame_texture('up', 1)
var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate()
# flip entire node horizontally if spawning left
hit_node.scale = Vector2(-1 if dir == "left" else 1, 1)
add_child(hit_node)
# set position according to frame width and height
@warning_ignore("integer_division")
hit_node.global_position = global_position + Vector2(
@ -64,10 +68,6 @@ func hit(dir: String) -> void:
sprite_texture.get_height()*-1
)
# flip entire node horizontally if spawning left
hit_node.scale = Vector2(-1 if dir == "left" else 1, 1)
get_parent().add_child(hit_node)
hit_node.timer.connect("timeout", _on_hit_end)
is_hitting = true