AABB collision and game_over

master
Sascha 2025-01-17 15:24:33 +07:00
parent 8480bbf1e4
commit 4c937ed9b7
5 changed files with 37 additions and 12 deletions

@ -1,27 +1,42 @@
extends RigidBody2D
signal game_over(winning_player: int)
@onready var label_position: Label = %LabelPosition
@onready var player_1: CharacterBody2D = %Player1
@onready var player_2: CharacterBody2D = %Player2
var speed = 400.0
var direction: Vector2
var window_size: Vector2
var player_1_shape: RectangleShape2D
var player_2_shape: RectangleShape2D
var winning_player: int
func _ready() -> void:
window_size = get_viewport().get_visible_rect().size
player_1_shape = player_1.get_node("CollisionShape2D").shape as RectangleShape2D
player_2_shape = player_2.get_node("CollisionShape2D").shape as RectangleShape2D
reset()
func reset() -> void:
print("Reset ball")
position = Vector2(578, 326)
position = Vector2(window_size.x / 2 - 5, window_size.y / 2 - 5)
direction = Vector2.ZERO
func serve() -> void:
print("Serve")
direction = Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized()
print(direction)
func _physics_process(delta: float) -> void:
position += direction * speed * delta
label_position.text = str(position)
var ball_rect = Rect2(position - Vector2(10, 10), Vector2(20, 20))
var player_1_rect = Rect2(player_1.position - player_1_shape.extents, player_1_shape.extents * 2)
var player_2_rect = Rect2(player_2.position - player_2_shape.extents, player_2_shape.extents * 2)
if ball_rect.intersects(player_1_rect):
direction.x *= -1
elif ball_rect.intersects(player_2_rect):
direction.x *= -1
if position.y < 0:
position.y = 0
@ -31,12 +46,12 @@ func _physics_process(delta: float) -> void:
position.y = window_size.y
direction.y *= -1
speed *= 1.03
if position.x < 0:
position.x = 0
direction.x *= -1
speed *= 1.03
winning_player = 2
reset()
game_over.emit(winning_player)
elif position.x > window_size.x:
position.x = window_size.x
direction.x *= -1
speed *= 1.03
winning_player = 1
reset()
game_over.emit(winning_player)

@ -30,6 +30,7 @@ shape = SubResource("CircleShape2D_41u45")
[node name="LabelPosition" type="Label" parent="."]
unique_name_in_owner = true
visible = false
offset_right = 40.0
offset_bottom = 23.0
text = "(0, 0)"

@ -6,6 +6,9 @@ extends Node2D
var game_mode = "idle"
func _ready() -> void:
ball.connect("game_over", Callable(self, "_on_game_over"))
func _input(event: InputEvent) -> void:
if event.is_action_pressed("space"):
if game_mode == "idle":
@ -17,6 +20,9 @@ func _input(event: InputEvent) -> void:
elif event.is_action_pressed("esc"):
get_tree().quit()
func _on_ball_body_entered(body: Node) -> void:
print("ball hits " + str(body.name))
func _on_game_over(winning_player: int) -> void:
print("Game Over! Winning Player: " + str(winning_player))
game_mode = "idle"

@ -27,10 +27,12 @@ position = Vector2(575.5, 323)
shape = SubResource("RectangleShape2D_feb5d")
[node name="Player1" parent="." instance=ExtResource("1_80nbo")]
unique_name_in_owner = true
position = Vector2(20, 70)
input_pickable = false
[node name="Player2" parent="." instance=ExtResource("1_80nbo")]
unique_name_in_owner = true
position = Vector2(1124, 578)
player_number = 2

@ -19,4 +19,5 @@ mesh = SubResource("QuadMesh_feb5d")
texture = ExtResource("2_onrkg")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
unique_name_in_owner = true
shape = SubResource("RectangleShape2D_e2o6t")