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] [global_group]
paddle="objects that hit the ball" hit="hitboxes"
[input] [input]

View file

@ -10,14 +10,26 @@ var is_hit: bool = false
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:
hurtbox.connect("area_entered", _on_hit) hurtbox.connect("area_entered", _on_hit)
launch_random()
launch()
func launch_random(): func launch(angle: float = 0) -> void:
var angle = randf_range(-PI/3, PI/3) + PI * float(randi()%2) 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 linear_velocity = Vector2(cos(angle), sin(angle)) * launch_speed
func _on_hit(area: Area2D) -> void: func _on_hit(hitbox: Area2D) -> void:
if area.is_in_group("paddle"): print("area detected")
linear_velocity = linear_velocity.normalized() * linear_velocity.length() * speed_mult 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 radius = 8.062258
[node name="Ball" type="RigidBody2D"] [node name="Ball" type="RigidBody2D"]
collision_layer = 18 collision_layer = 16
collision_mask = 5 collision_mask = 5
physics_material_override = SubResource("PhysicsMaterial_oqv3d") physics_material_override = SubResource("PhysicsMaterial_oqv3d")
gravity_scale = 0.0 gravity_scale = 0.0
@ -25,8 +25,8 @@ script = ExtResource("1_nv6q1")
texture = ExtResource("1_oqv3d") texture = ExtResource("1_oqv3d")
[node name="HurtArea2D" type="Area2D" parent="."] [node name="HurtArea2D" type="Area2D" parent="."]
collision_layer = 16 collision_layer = 32
collision_mask = 0 collision_mask = 64
[node name="CollisionShape2D" type="CollisionShape2D" parent="HurtArea2D"] [node name="CollisionShape2D" type="CollisionShape2D" parent="HurtArea2D"]
shape = SubResource("CircleShape2D_oqv3d") shape = SubResource("CircleShape2D_oqv3d")

View file

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

View file

@ -1,6 +1,6 @@
extends CharacterBody2D extends CharacterBody2D
@export var player: int = 1 @export var id: int = 1
@export var max_speed: float = 150 @export var max_speed: float = 150
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D @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 sprite_texture: Texture2D = sprite.sprite_frames.get_frame_texture('up', 1)
var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate() 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 # set position according to frame width and height
@warning_ignore("integer_division") @warning_ignore("integer_division")
hit_node.global_position = global_position + Vector2( hit_node.global_position = global_position + Vector2(
@ -64,10 +68,6 @@ func hit(dir: String) -> void:
sprite_texture.get_height()*-1 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) hit_node.timer.connect("timeout", _on_hit_end)
is_hitting = true is_hitting = true