Skip to content

Commit caee3ae

Browse files
authored
Merge pull request #35 from endlessm/pong-score
Add Pong demo
2 parents 80b227a + 98f8ba0 commit caee3ae

26 files changed

+693
-6
lines changed

addons/block_code/block_code_node/block_code.gd

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ func _ready():
1414
_update_parent_script()
1515

1616

17+
func _get_custom_or_native_class(node: Node):
18+
if node.has_method("get_custom_class"):
19+
return node.get_custom_class()
20+
return node.get_class()
21+
22+
1723
func _enter_tree():
1824
if not Engine.is_editor_hint():
1925
return
2026

2127
# Create script
2228
if bsd == null:
2329
var new_bsd: BlockScriptData = load("res://addons/block_code/ui/bsd_templates/default_bsd.tres").duplicate(true)
24-
new_bsd.script_inherits = get_parent().call("get_class") # For whatever reason this works instead of just .get_class :)
30+
new_bsd.script_inherits = _get_custom_or_native_class(get_parent())
2531
new_bsd.generated_script = new_bsd.generated_script.replace("INHERIT_DEFAULT", new_bsd.script_inherits)
2632
bsd = new_bsd
2733

@@ -44,6 +50,8 @@ func _update_parent_script():
4450

4551

4652
func _get_configuration_warnings():
47-
if bsd:
48-
if get_parent().call("get_class") != bsd.script_inherits:
49-
return ["The parent is not a %s. Create a new BlockCode node and reattach." % bsd.script_inherits]
53+
var warnings = []
54+
if bsd and _get_custom_or_native_class(get_parent()) != bsd.script_inherits:
55+
var warning = "The parent is not a %s. Create a new BlockCode node and reattach." % bsd.script_inherits
56+
warnings.append(warning)
57+
return warnings

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func _ready():
99
$Sprite2D.texture = sprite_texture
1010

1111

12-
func get_class():
12+
func get_custom_class():
1313
return "SimpleCharacter"
1414

1515

pong_game/ball.gd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@tool
2+
class_name Ball
3+
extends RigidBody2D
4+
5+
const _INITIAL_RADIUS: int = 64
6+
7+
## This is how fast your ball moves.
8+
@export_range(0.0, 10000.0, 0.5, "suffix:px/s") var initial_speed: float = 500.0
9+
10+
## This is the initial angle of the ball.
11+
@export_range(-180, 180, 0.5, "radians_as_degrees") var initial_direction: float = PI / 4
12+
13+
## How big is this ball?
14+
@export_range(0.1, 5.0, 0.1) var size: float = 1.0:
15+
set = _set_size
16+
17+
@onready var _shape: CollisionShape2D = %CollisionShape2D
18+
@onready var _sprite: Sprite2D = %Sprite2D
19+
20+
21+
func _set_size(new_size: float):
22+
size = new_size
23+
if not is_node_ready():
24+
await ready
25+
_shape.shape.radius = _INITIAL_RADIUS * size
26+
_sprite.scale = Vector2(size, size)
27+
28+
29+
func _ready():
30+
if Engine.is_editor_hint():
31+
set_process(false)
32+
set_physics_process(false)
33+
reset()
34+
35+
36+
func reset():
37+
linear_velocity = Vector2.from_angle(initial_direction) * initial_speed
38+
angular_velocity = 0.0
39+
_set_size(size)

pong_game/ball.png

6.74 KB
Loading

pong_game/ball.png.import

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://bcgr5amsq3jfl"
6+
path="res://.godot/imported/ball.png-207b87d36613f4458c4a15d56cdcf75a.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://pong_game/ball.png"
14+
dest_files=["res://.godot/imported/ball.png-207b87d36613f4458c4a15d56cdcf75a.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

pong_game/ball.tscn

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[gd_scene load_steps=7 format=3 uid="uid://c7l70grmkauij"]
2+
3+
[ext_resource type="Script" path="res://pong_game/ball.gd" id="1_vdrab"]
4+
[ext_resource type="Texture2D" uid="uid://bcgr5amsq3jfl" path="res://pong_game/ball.png" id="2_xkrmm"]
5+
[ext_resource type="AudioStream" uid="uid://bc7jd3d8m5spt" path="res://pong_game/wall_hit.ogg" id="3_mjbsd"]
6+
[ext_resource type="AudioStream" uid="uid://jk0oapxjw354" path="res://pong_game/paddle_hit.ogg" id="4_skr8k"]
7+
8+
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_c3m63"]
9+
friction = 0.0
10+
bounce = 1.0
11+
12+
[sub_resource type="CircleShape2D" id="CircleShape2D_sntrn"]
13+
radius = 64.0
14+
15+
[node name="Ball" type="RigidBody2D" groups=["balls"]]
16+
collision_layer = 2
17+
collision_mask = 15
18+
physics_material_override = SubResource("PhysicsMaterial_c3m63")
19+
continuous_cd = 1
20+
max_contacts_reported = 1
21+
contact_monitor = true
22+
linear_velocity = Vector2(353.553, 353.553)
23+
script = ExtResource("1_vdrab")
24+
25+
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
26+
unique_name_in_owner = true
27+
shape = SubResource("CircleShape2D_sntrn")
28+
29+
[node name="Sprite2D" type="Sprite2D" parent="."]
30+
unique_name_in_owner = true
31+
texture = ExtResource("2_xkrmm")
32+
33+
[node name="WallAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
34+
stream = ExtResource("3_mjbsd")
35+
36+
[node name="PaddleAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
37+
stream = ExtResource("4_skr8k")
38+
39+
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

pong_game/hud.gd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
extends CanvasLayer
2+
3+
@onready var _score_labels = {
4+
"left": %PlayerLeftScore,
5+
"right": %PlayerRightScore,
6+
}
7+
8+
9+
## Sets the score for one player.
10+
func set_player_score(player: String, score: int):
11+
var text = _score_labels[player].text
12+
if str(score) != text:
13+
_score_labels[player].text = str(score)
14+
15+
16+
## Sets the score for each player.
17+
func set_players_scores(left_score: int, right_score: int):
18+
set_player_score("left", left_score)
19+
set_player_score("right", right_score)

pong_game/hud.tscn

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[gd_scene load_steps=3 format=3 uid="uid://bis7afjjuwypq"]
2+
3+
[ext_resource type="Script" path="res://pong_game/hud.gd" id="1_yyb01"]
4+
[ext_resource type="Texture2D" uid="uid://dijemw7iilr2m" path="res://pong_game/line.png" id="2_jon51"]
5+
6+
[node name="HUD" type="CanvasLayer" groups=["hud"]]
7+
script = ExtResource("1_yyb01")
8+
9+
[node name="Control" type="Control" parent="."]
10+
layout_mode = 3
11+
anchors_preset = 15
12+
anchor_right = 1.0
13+
anchor_bottom = 1.0
14+
grow_horizontal = 2
15+
grow_vertical = 2
16+
17+
[node name="HBoxContainer" type="HBoxContainer" parent="Control"]
18+
layout_mode = 1
19+
anchors_preset = 15
20+
anchor_right = 1.0
21+
anchor_bottom = 1.0
22+
grow_horizontal = 2
23+
grow_vertical = 2
24+
25+
[node name="Lines" type="Sprite2D" parent="Control/HBoxContainer"]
26+
unique_name_in_owner = true
27+
texture_repeat = 2
28+
position = Vector2(960, 536)
29+
texture = ExtResource("2_jon51")
30+
region_enabled = true
31+
region_rect = Rect2(0, 0, 20, 1100)
32+
33+
[node name="PlayerLeftScore" type="Label" parent="Control"]
34+
unique_name_in_owner = true
35+
layout_mode = 2
36+
offset_left = 240.0
37+
offset_right = 717.0
38+
offset_bottom = 1080.0
39+
pivot_offset = Vector2(240, 176)
40+
size_flags_horizontal = 3
41+
size_flags_vertical = 1
42+
theme_override_font_sizes/font_size = 200
43+
text = "0"
44+
horizontal_alignment = 1
45+
46+
[node name="PlayerRightScore" type="Label" parent="Control"]
47+
unique_name_in_owner = true
48+
layout_mode = 2
49+
offset_left = 1200.0
50+
offset_right = 1677.0
51+
offset_bottom = 1080.0
52+
pivot_offset = Vector2(240, 176)
53+
size_flags_horizontal = 3
54+
size_flags_vertical = 1
55+
theme_override_font_sizes/font_size = 200
56+
text = "0"
57+
horizontal_alignment = 1

pong_game/line.png

179 Bytes
Loading

pong_game/line.png.import

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://dijemw7iilr2m"
6+
path="res://.godot/imported/line.png-452af046907249a8ef12695e48447ab1.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://pong_game/line.png"
14+
dest_files=["res://.godot/imported/line.png-452af046907249a8ef12695e48447ab1.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

0 commit comments

Comments
 (0)