From f83f6989b4bba53d46d9ffcb285ac0833dd474b2 Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Thu, 7 Mar 2024 22:17:10 +0100 Subject: [PATCH] first work --- Ball.tscn | 15 ------ Rack2et.gd | 28 ----------- Racket.tscn | 16 ------- project.godot | 2 +- scenes/playground/Playground.gd | 47 +++++++++++++++++++ .../playground/Playground.tscn | 29 +++++++++--- scenes/playground/ball/Ball.gd | 21 +++++++++ scenes/playground/ball/Ball.tscn | 27 +++++++++++ .../playground/racket/Racket.gd | 8 ++-- scenes/playground/racket/Racket.tscn | 25 ++++++++++ 10 files changed, 146 insertions(+), 72 deletions(-) delete mode 100644 Ball.tscn delete mode 100644 Rack2et.gd delete mode 100644 Racket.tscn create mode 100644 scenes/playground/Playground.gd rename Playground.tscn => scenes/playground/Playground.tscn (57%) create mode 100644 scenes/playground/ball/Ball.gd create mode 100644 scenes/playground/ball/Ball.tscn rename Racket.gd => scenes/playground/racket/Racket.gd (66%) create mode 100644 scenes/playground/racket/Racket.tscn diff --git a/Ball.tscn b/Ball.tscn deleted file mode 100644 index 8ba9a21..0000000 --- a/Ball.tscn +++ /dev/null @@ -1,15 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://dmsd30hc2ob1w"] - -[sub_resource type="RectangleShape2D" id="RectangleShape2D_bdxrg"] -size = Vector2(40, 40) - -[node name="Ball" type="RigidBody2D"] -gravity_scale = 1.66533e-16 - -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -position = Vector2(20, 20) -shape = SubResource("RectangleShape2D_bdxrg") - -[node name="ColorRect" type="ColorRect" parent="."] -offset_right = 40.0 -offset_bottom = 40.0 diff --git a/Rack2et.gd b/Rack2et.gd deleted file mode 100644 index 3524013..0000000 --- a/Rack2et.gd +++ /dev/null @@ -1,28 +0,0 @@ -extends CharacterBody2D - - -const SPEED = 300.0 -const JUMP_VELOCITY = -400.0 - -# Get the gravity from the project settings to be synced with RigidBody nodes. -var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") - - -func _physics_process(delta): - # Add the gravity. - if not is_on_floor(): - velocity.y += gravity * delta - - # Handle jump. - if Input.is_action_just_pressed("ui_accept") and is_on_floor(): - velocity.y = JUMP_VELOCITY - - # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var direction = Input.get_axis("ui_left", "ui_right") - if direction: - velocity.x = direction * SPEED - else: - velocity.x = move_toward(velocity.x, 0, SPEED) - - move_and_slide() diff --git a/Racket.tscn b/Racket.tscn deleted file mode 100644 index 8c1009e..0000000 --- a/Racket.tscn +++ /dev/null @@ -1,16 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://ce4mnfokb6pkn"] - -[sub_resource type="RectangleShape2D" id="RectangleShape2D_ehve4"] -size = Vector2(40, 144) - -[node name="Racket" type="CharacterBody2D"] -up_direction = Vector2(2.94434e-12, 1) - -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -position = Vector2(20, 72) -shape = SubResource("RectangleShape2D_ehve4") - -[node name="ColorRect" type="ColorRect" parent="."] -offset_right = 40.0 -offset_bottom = 144.0 -color = Color(0.0117647, 0.0117647, 0.0117647, 1) diff --git a/project.godot b/project.godot index f99bf1a..4f2cbd1 100644 --- a/project.godot +++ b/project.godot @@ -11,7 +11,7 @@ config_version=5 [application] config/name="pong" -run/main_scene="res://Playground.tscn" +run/main_scene="res://scenes/playground/Playground.tscn" config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://icon.svg" diff --git a/scenes/playground/Playground.gd b/scenes/playground/Playground.gd new file mode 100644 index 0000000..5dc07c2 --- /dev/null +++ b/scenes/playground/Playground.gd @@ -0,0 +1,47 @@ +extends Node2D + +var Ball = preload("res://scenes/playground/ball/Ball.tscn") + +var left_point = 0 +var right_point = 0 + +func start(): + var ball = Ball.instantiate() + add_child(ball) + +func set_score(): + get_node("Score").text = "{right} - {left}".format({ + "left": left_point, + "right": right_point + }) + +func new_round(ball): + remove_child(ball) + set_score() + start() + +func _ready(): + start() + +func _process(delta): + pass + +func _on_goal_area_left_body_entered(body): + if body.name == "Ball": + left_point += 1 + new_round(body) + +func _on_goal_area_right_body_entered(body): + if body.name == "Ball": + right_point += 1 + new_round(body) + + +func _on_racket_left_body_entered(body): + if body.name == "Ball": + body.linear_velocity = body.linear_velocity * -1 + + +func _on_racket_right_body_entered(body): + if body.name == "Ball": + body.linear_velocity = body.linear_velocity * -1 diff --git a/Playground.tscn b/scenes/playground/Playground.tscn similarity index 57% rename from Playground.tscn rename to scenes/playground/Playground.tscn index cc8f87a..da60802 100644 --- a/Playground.tscn +++ b/scenes/playground/Playground.tscn @@ -1,7 +1,11 @@ -[gd_scene load_steps=5 format=3 uid="uid://catrpx1vuhvou"] +[gd_scene load_steps=6 format=3 uid="uid://catrpx1vuhvou"] -[ext_resource type="PackedScene" uid="uid://ce4mnfokb6pkn" path="res://Racket.tscn" id="1_ywul3"] -[ext_resource type="Script" path="res://Racket.gd" id="2_wrt7t"] +[ext_resource type="Script" path="res://scenes/playground/Playground.gd" id="1_86x6h"] +[ext_resource type="PackedScene" uid="uid://ce4mnfokb6pkn" path="res://scenes/playground/racket/Racket.tscn" id="1_ywul3"] + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_v2vae"] +friction = 0.0 +bounce = 1.0 [sub_resource type="RectangleShape2D" id="RectangleShape2D_u7af8"] size = Vector2(100, 600) @@ -10,11 +14,15 @@ size = Vector2(100, 600) font_size = 64 [node name="Playground" type="Node2D"] +script = ExtResource("1_86x6h") [node name="Wall" type="StaticBody2D" parent="."] +disable_mode = 1 +physics_material_override = SubResource("PhysicsMaterial_v2vae") [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Wall"] -polygon = PackedVector2Array(0, 8, 0, 600, 1200, 600, 1200, 8, 0, 0, 0, 8, 1192, 16, 1192, 16, 1192, 592, 8, 592, 8, 8) +polygon = PackedVector2Array(0, 8, 0, 600, 1200, 600, 1200, 8, 0, 0, 0, 8, 1192, 16, 1192, 16, 1192, 592, 8, 592, 8, 16) +one_way_collision_margin = 0.0 [node name="GoalAreaLeft" type="Area2D" parent="."] @@ -30,17 +38,19 @@ shape = SubResource("RectangleShape2D_u7af8") [node name="RacketLeft" parent="." instance=ExtResource("1_ywul3")] position = Vector2(128, 224) -script = ExtResource("2_wrt7t") +collision_layer = 255 +collision_mask = 255 +max_contacts_reported = 1000 +contact_monitor = true metadata/UP_KEY = "left_up" metadata/DOWN_KEY = "left_down" [node name="RacketRight" parent="." instance=ExtResource("1_ywul3")] position = Vector2(1032, 224) -script = ExtResource("2_wrt7t") metadata/UP_KEY = "right_up" metadata/DOWN_KEY = "right_down" -[node name="Label" type="Label" parent="."] +[node name="Score" type="Label" parent="."] offset_left = 544.0 offset_top = 32.0 offset_right = 673.0 @@ -49,3 +59,8 @@ text = "0 - 0" label_settings = SubResource("LabelSettings_ildlg") horizontal_alignment = 1 vertical_alignment = 1 + +[connection signal="body_entered" from="GoalAreaLeft" to="." method="_on_goal_area_left_body_entered"] +[connection signal="body_entered" from="GoalAreaRight" to="." method="_on_goal_area_right_body_entered"] +[connection signal="body_entered" from="RacketLeft" to="." method="_on_racket_left_body_entered"] +[connection signal="body_entered" from="RacketRight" to="." method="_on_racket_right_body_entered"] diff --git a/scenes/playground/ball/Ball.gd b/scenes/playground/ball/Ball.gd new file mode 100644 index 0000000..ce8cbe6 --- /dev/null +++ b/scenes/playground/ball/Ball.gd @@ -0,0 +1,21 @@ +extends RigidBody2D + + +const SPEED = 300.00 + + +func _ready(): + randomize() + + global_position = Vector2(640, 360) + + var x_direction = pow(-1, randi() % 2) + var y_direction = pow(-1, randi() % 2) + + await get_tree().create_timer(1.0).timeout + + linear_velocity = Vector2(x_direction, y_direction) * SPEED + +func _process(delta): + pass + diff --git a/scenes/playground/ball/Ball.tscn b/scenes/playground/ball/Ball.tscn new file mode 100644 index 0000000..132ee40 --- /dev/null +++ b/scenes/playground/ball/Ball.tscn @@ -0,0 +1,27 @@ +[gd_scene load_steps=4 format=3 uid="uid://dmsd30hc2ob1w"] + +[ext_resource type="Script" path="res://scenes/playground/ball/Ball.gd" id="1_vsr7q"] + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_h7mi1"] +friction = 0.0 + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_bdxrg"] +size = Vector2(32, 32) + +[node name="Ball" type="RigidBody2D"] +mass = 0.01 +physics_material_override = SubResource("PhysicsMaterial_h7mi1") +gravity_scale = 1.66533e-16 +can_sleep = false +lock_rotation = true +freeze_mode = 1 +linear_damp_mode = 1 +script = ExtResource("1_vsr7q") + +[node name="ColorRect" type="ColorRect" parent="."] +offset_right = 40.0 +offset_bottom = 40.0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(20, 20) +shape = SubResource("RectangleShape2D_bdxrg") diff --git a/Racket.gd b/scenes/playground/racket/Racket.gd similarity index 66% rename from Racket.gd rename to scenes/playground/racket/Racket.gd index 8e34167..7afd037 100644 --- a/Racket.gd +++ b/scenes/playground/racket/Racket.gd @@ -1,16 +1,14 @@ -extends CharacterBody2D +extends RigidBody2D const SPEED = 300.0 +func _ready(): + contact_monitor = true func _physics_process(delta): - - if Input.is_action_pressed(get_meta("UP_KEY")): - velocity.y = move_toward(velocity.y, -1, SPEED) move_and_collide(Vector2.UP * SPEED * delta) if Input.is_action_pressed(get_meta("DOWN_KEY")): - velocity.y = move_toward(velocity.y, 1, SPEED) move_and_collide(Vector2.DOWN * SPEED * delta) diff --git a/scenes/playground/racket/Racket.tscn b/scenes/playground/racket/Racket.tscn new file mode 100644 index 0000000..b53108e --- /dev/null +++ b/scenes/playground/racket/Racket.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=4 format=3 uid="uid://ce4mnfokb6pkn"] + +[ext_resource type="Script" path="res://scenes/playground/racket/Racket.gd" id="1_gclos"] + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_lq6ot"] +friction = 0.0 + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_ehve4"] +size = Vector2(40, 144) + +[node name="Racket" type="RigidBody2D"] +physics_material_override = SubResource("PhysicsMaterial_lq6ot") +gravity_scale = 1.66533e-16 +can_sleep = false +lock_rotation = true +script = ExtResource("1_gclos") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(20, 72) +shape = SubResource("RectangleShape2D_ehve4") + +[node name="ColorRect" type="ColorRect" parent="."] +offset_right = 40.0 +offset_bottom = 144.0 +color = Color(0.0117647, 0.0117647, 0.0117647, 1)