From 99a6cf2164a3b041056173761dff7fed10146be9 Mon Sep 17 00:00:00 2001 From: yuki Date: Fri, 14 Nov 2025 08:01:06 -0300 Subject: [PATCH] properly detect player hits --- project.godot | 2 +- scenes/ball/ball.gd | 24 ++++++++++++++++++------ scenes/ball/ball.tscn | 6 +++--- scenes/hit/hit.tscn | 6 +++--- scenes/player/player.gd | 10 +++++----- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/project.godot b/project.godot index 9589e69..214d7d8 100644 --- a/project.godot +++ b/project.godot @@ -33,7 +33,7 @@ version_control/autoload_on_startup=true [global_group] -paddle="objects that hit the ball" +hit="hitboxes" [input] diff --git a/scenes/ball/ball.gd b/scenes/ball/ball.gd index 47185a4..4baaae4 100644 --- a/scenes/ball/ball.gd +++ b/scenes/ball/ball.gd @@ -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 diff --git a/scenes/ball/ball.tscn b/scenes/ball/ball.tscn index a058ae0..6e2c35f 100644 --- a/scenes/ball/ball.tscn +++ b/scenes/ball/ball.tscn @@ -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") diff --git a/scenes/hit/hit.tscn b/scenes/hit/hit.tscn index 4c37d13..ae48865 100644 --- a/scenes/hit/hit.tscn +++ b/scenes/hit/hit.tscn @@ -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="."] diff --git a/scenes/player/player.gd b/scenes/player/player.gd index 0d07910..906fecd 100644 --- a/scenes/player/player.gd +++ b/scenes/player/player.gd @@ -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