collision with game_area

master
Sascha 2025-01-17 15:09:45 +07:00
parent 6c4e1042e8
commit 8480bbf1e4
5 changed files with 73 additions and 20 deletions

@ -1,15 +1,18 @@
extends RigidBody2D
const SPEED = 444
@onready var label_position: Label = %LabelPosition
var speed = 400.0
var direction: Vector2
var window_size: Vector2
func _ready() -> void:
window_size = get_viewport().get_visible_rect().size
reset()
func reset() -> void:
print("Reset ball")
position = Vector2(578, 326)
direction = Vector2.ZERO
direction = Vector2.ZERO
func serve() -> void:
print("Serve")
@ -17,4 +20,23 @@ func serve() -> void:
print(direction)
func _physics_process(delta: float) -> void:
position += direction * SPEED * delta
position += direction * speed * delta
label_position.text = str(position)
if position.y < 0:
position.y = 0
direction.y *= -1
speed *= 1.03
elif position.y > window_size.y:
position.y = window_size.y
direction.y *= -1
speed *= 1.03
if position.x < 0:
position.x = 0
direction.x *= -1
speed *= 1.03
elif position.x > window_size.x:
position.x = window_size.x
direction.x *= -1
speed *= 1.03

@ -1,21 +1,36 @@
[gd_scene load_steps=5 format=3 uid="uid://buqqojsie0k8c"]
[gd_scene load_steps=7 format=3 uid="uid://buqqojsie0k8c"]
[ext_resource type="Script" uid="uid://sv6hcl04t1f6" path="res://ball.gd" id="1_41u45"]
[ext_resource type="Texture2D" uid="uid://bqvc42b35kys5" path="res://player_texture.tres" id="1_x8fbi"]
[sub_resource type="SphereMesh" id="SphereMesh_ktgx5"]
[sub_resource type="CircleShape2D" id="CircleShape2D_41u45"]
radius = 5.0
radius = 11.0
[sub_resource type="SphereMesh" id="SphereMesh_ktgx5"]
[sub_resource type="SystemFont" id="SystemFont_41u45"]
[sub_resource type="LabelSettings" id="LabelSettings_ktgx5"]
font = SubResource("SystemFont_41u45")
font_size = 8
font_color = Color(0.491891, 1, 0.676743, 1)
[node name="Ball" type="RigidBody2D"]
input_pickable = true
gravity_scale = 0.0
script = ExtResource("1_41u45")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_41u45")
[node name="MeshInstance2D" type="MeshInstance2D" parent="."]
scale = Vector2(10, 10)
scale = Vector2(20, 20)
mesh = SubResource("SphereMesh_ktgx5")
texture = ExtResource("1_x8fbi")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_41u45")
[node name="LabelPosition" type="Label" parent="."]
unique_name_in_owner = true
offset_right = 40.0
offset_bottom = 23.0
text = "(0, 0)"
label_settings = SubResource("LabelSettings_ktgx5")

@ -16,3 +16,7 @@ func _input(event: InputEvent) -> void:
ball.reset()
elif event.is_action_pressed("esc"):
get_tree().quit()
func _on_ball_body_entered(body: Node) -> void:
print("ball hits " + str(body.name))

@ -1,9 +1,12 @@
[gd_scene load_steps=7 format=3 uid="uid://cjxteipqurqy5"]
[gd_scene load_steps=8 format=3 uid="uid://cjxteipqurqy5"]
[ext_resource type="PackedScene" uid="uid://cm7lrhjnutqxx" path="res://player.tscn" id="1_80nbo"]
[ext_resource type="Script" uid="uid://dcr7q67iref5x" path="res://game.gd" id="1_feb5d"]
[ext_resource type="PackedScene" uid="uid://buqqojsie0k8c" path="res://ball.tscn" id="2_e2o6t"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_feb5d"]
size = Vector2(1149, 644)
[sub_resource type="SystemFont" id="SystemFont_80nbo"]
[sub_resource type="LabelSettings" id="LabelSettings_80nbo"]
@ -17,8 +20,15 @@ font_size = 64
[node name="Game" type="Node2D"]
script = ExtResource("1_feb5d")
[node name="GameArea" type="Area2D" parent="." groups=["game_area"]]
[node name="CollisionShape2D" type="CollisionShape2D" parent="GameArea"]
position = Vector2(575.5, 323)
shape = SubResource("RectangleShape2D_feb5d")
[node name="Player1" parent="." instance=ExtResource("1_80nbo")]
position = Vector2(20, 70)
input_pickable = false
[node name="Player2" parent="." instance=ExtResource("1_80nbo")]
position = Vector2(1124, 578)
@ -67,4 +77,6 @@ text = "0"
label_settings = SubResource("LabelSettings_e2o6t")
[node name="Ball" parent="." instance=ExtResource("2_e2o6t")]
position = Vector2(578, 326)
position = Vector2(578, 635)
[connection signal="body_entered" from="Ball" to="." method="_on_ball_body_entered"]

@ -3,20 +3,20 @@
[ext_resource type="Script" uid="uid://0884gjf31t4j" path="res://player.gd" id="1_4flbx"]
[ext_resource type="Texture2D" uid="uid://bqvc42b35kys5" path="res://player_texture.tres" id="2_onrkg"]
[sub_resource type="QuadMesh" id="QuadMesh_feb5d"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_e2o6t"]
size = Vector2(10, 100)
[sub_resource type="QuadMesh" id="QuadMesh_feb5d"]
[node name="Player" type="CharacterBody2D"]
[node name="Player" type="CharacterBody2D" groups=["players"]]
input_pickable = true
motion_mode = 1
script = ExtResource("1_4flbx")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_e2o6t")
[node name="MeshInstance2D" type="MeshInstance2D" parent="."]
position = Vector2(0.5, 0)
scale = Vector2(11, 100)
scale = Vector2(10, 100)
mesh = SubResource("QuadMesh_feb5d")
texture = ExtResource("2_onrkg")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_e2o6t")