Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pong demo #35

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions addons/block_code/block_code_node/block_code.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ func _ready():
_update_parent_script()


func _get_custom_or_native_class(node: Node):
if node.has_method("get_custom_class"):
return node.get_custom_class()
return node.get_class()


func _enter_tree():
if not Engine.is_editor_hint():
return

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

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


func _get_configuration_warnings():
if bsd:
if get_parent().call("get_class") != bsd.script_inherits:
return ["The parent is not a %s. Create a new BlockCode node and reattach." % bsd.script_inherits]
var warnings = []
if bsd and _get_custom_or_native_class(get_parent()) != bsd.script_inherits:
var warning = "The parent is not a %s. Create a new BlockCode node and reattach." % bsd.script_inherits
warnings.append(warning)
return warnings
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func _ready():
$Sprite2D.texture = sprite_texture


func get_class():
func get_custom_class():
return "SimpleCharacter"


Expand Down
39 changes: 39 additions & 0 deletions pong_game/ball.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@tool
class_name Ball
extends RigidBody2D

const _INITIAL_RADIUS: int = 64

## This is how fast your ball moves.
@export_range(0.0, 10000.0, 0.5, "suffix:px/s") var initial_speed: float = 500.0

## This is the initial angle of the ball.
@export_range(-180, 180, 0.5, "radians_as_degrees") var initial_direction: float = PI / 4

## How big is this ball?
@export_range(0.1, 5.0, 0.1) var size: float = 1.0:
set = _set_size

@onready var _shape: CollisionShape2D = %CollisionShape2D
@onready var _sprite: Sprite2D = %Sprite2D


func _set_size(new_size: float):
size = new_size
if not is_node_ready():
await ready
_shape.shape.radius = _INITIAL_RADIUS * size
_sprite.scale = Vector2(size, size)


func _ready():
if Engine.is_editor_hint():
set_process(false)
set_physics_process(false)
reset()


func reset():
linear_velocity = Vector2.from_angle(initial_direction) * initial_speed
angular_velocity = 0.0
_set_size(size)
Binary file added pong_game/ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions pong_game/ball.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bcgr5amsq3jfl"
path="res://.godot/imported/ball.png-207b87d36613f4458c4a15d56cdcf75a.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://pong_game/ball.png"
dest_files=["res://.godot/imported/ball.png-207b87d36613f4458c4a15d56cdcf75a.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
39 changes: 39 additions & 0 deletions pong_game/ball.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[gd_scene load_steps=7 format=3 uid="uid://c7l70grmkauij"]

[ext_resource type="Script" path="res://pong_game/ball.gd" id="1_vdrab"]
[ext_resource type="Texture2D" uid="uid://bcgr5amsq3jfl" path="res://pong_game/ball.png" id="2_xkrmm"]
[ext_resource type="AudioStream" uid="uid://bc7jd3d8m5spt" path="res://pong_game/wall_hit.ogg" id="3_mjbsd"]
[ext_resource type="AudioStream" uid="uid://jk0oapxjw354" path="res://pong_game/paddle_hit.ogg" id="4_skr8k"]

[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_c3m63"]
friction = 0.0
bounce = 1.0

[sub_resource type="CircleShape2D" id="CircleShape2D_sntrn"]
radius = 64.0

[node name="Ball" type="RigidBody2D" groups=["balls"]]
collision_layer = 2
collision_mask = 15
physics_material_override = SubResource("PhysicsMaterial_c3m63")
continuous_cd = 1
max_contacts_reported = 1
contact_monitor = true
linear_velocity = Vector2(353.553, 353.553)
script = ExtResource("1_vdrab")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
unique_name_in_owner = true
shape = SubResource("CircleShape2D_sntrn")

[node name="Sprite2D" type="Sprite2D" parent="."]
unique_name_in_owner = true
texture = ExtResource("2_xkrmm")

[node name="WallAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = ExtResource("3_mjbsd")

[node name="PaddleAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = ExtResource("4_skr8k")

[connection signal="body_entered" from="." to="." method="_on_body_entered"]
19 changes: 19 additions & 0 deletions pong_game/hud.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extends CanvasLayer

@onready var _score_labels = {
"left": %PlayerLeftScore,
"right": %PlayerRightScore,
}


## Sets the score for one player.
func set_player_score(player: String, score: int):
var text = _score_labels[player].text
if str(score) != text:
_score_labels[player].text = str(score)


## Sets the score for each player.
func set_players_scores(left_score: int, right_score: int):
set_player_score("left", left_score)
set_player_score("right", right_score)
57 changes: 57 additions & 0 deletions pong_game/hud.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[gd_scene load_steps=3 format=3 uid="uid://bis7afjjuwypq"]

[ext_resource type="Script" path="res://pong_game/hud.gd" id="1_yyb01"]
[ext_resource type="Texture2D" uid="uid://dijemw7iilr2m" path="res://pong_game/line.png" id="2_jon51"]

[node name="HUD" type="CanvasLayer" groups=["hud"]]
script = ExtResource("1_yyb01")

[node name="Control" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="HBoxContainer" type="HBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="Lines" type="Sprite2D" parent="Control/HBoxContainer"]
unique_name_in_owner = true
texture_repeat = 2
position = Vector2(960, 536)
texture = ExtResource("2_jon51")
region_enabled = true
region_rect = Rect2(0, 0, 20, 1100)

[node name="PlayerLeftScore" type="Label" parent="Control"]
unique_name_in_owner = true
layout_mode = 2
offset_left = 240.0
offset_right = 717.0
offset_bottom = 1080.0
pivot_offset = Vector2(240, 176)
size_flags_horizontal = 3
size_flags_vertical = 1
theme_override_font_sizes/font_size = 200
text = "0"
horizontal_alignment = 1

[node name="PlayerRightScore" type="Label" parent="Control"]
unique_name_in_owner = true
layout_mode = 2
offset_left = 1200.0
offset_right = 1677.0
offset_bottom = 1080.0
pivot_offset = Vector2(240, 176)
size_flags_horizontal = 3
size_flags_vertical = 1
theme_override_font_sizes/font_size = 200
text = "0"
horizontal_alignment = 1
Binary file added pong_game/line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions pong_game/line.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://dijemw7iilr2m"
path="res://.godot/imported/line.png-452af046907249a8ef12695e48447ab1.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://pong_game/line.png"
dest_files=["res://.godot/imported/line.png-452af046907249a8ef12695e48447ab1.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
33 changes: 33 additions & 0 deletions pong_game/paddle.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@tool
class_name Paddle
extends CharacterBody2D


func get_custom_class():
return "Paddle"


static func get_exposed_properties() -> Array[String]:
return ["position"]


static func get_custom_blocks() -> Array[BlockCategory]:
var b: Block

# Movement
var movement_list: Array[Block] = []
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}"
b.statement = 'velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")*{speed}\n' + "move_and_slide()"
movement_list.append(b)

b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}"
b.statement = 'velocity = Input.get_vector("player_2_left", "player_2_right", "player_2_up", "player_2_down")*{speed}\n' + "move_and_slide()"
movement_list.append(b)

var movement_category: BlockCategory = BlockCategory.new("Movement", movement_list, Color("4a86d5"))

return [movement_category]
Binary file added pong_game/paddle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions pong_game/paddle.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://tplpgtnfeda0"
path="res://.godot/imported/paddle.png-2fbcaaf37bbbddd482a4ad5b20dde0a2.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://pong_game/paddle.png"
dest_files=["res://.godot/imported/paddle.png-2fbcaaf37bbbddd482a4ad5b20dde0a2.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
19 changes: 19 additions & 0 deletions pong_game/paddle.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[gd_scene load_steps=4 format=3 uid="uid://s7enbp56f256"]

[ext_resource type="Script" path="res://pong_game/paddle.gd" id="1_74lee"]
[ext_resource type="Texture2D" uid="uid://tplpgtnfeda0" path="res://pong_game/paddle.png" id="1_eucti"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_bbbjr"]
size = Vector2(128, 384)

[node name="Paddle" type="CharacterBody2D" groups=["paddles"]]
collision_mask = 68
motion_mode = 1
script = ExtResource("1_74lee")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_bbbjr")

[node name="Sprite2D" type="Sprite2D" parent="."]
unique_name_in_owner = true
texture = ExtResource("1_eucti")
Binary file added pong_game/paddle_hit.ogg
Binary file not shown.
19 changes: 19 additions & 0 deletions pong_game/paddle_hit.ogg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[remap]

importer="oggvorbisstr"
type="AudioStreamOggVorbis"
uid="uid://jk0oapxjw354"
path="res://.godot/imported/paddle_hit.ogg-78978cea7a35465f97568c50bd227ec1.oggvorbisstr"

[deps]

source_file="res://pong_game/paddle_hit.ogg"
dest_files=["res://.godot/imported/paddle_hit.ogg-78978cea7a35465f97568c50bd227ec1.oggvorbisstr"]

[params]

loop=false
loop_offset=0
bpm=0
beat_count=0
bar_beats=4
Loading
Loading