diff --git a/course/course-learn-gdscript.tres b/course/course-learn-gdscript.tres index 632514b14..8b2e6540d 100644 --- a/course/course-learn-gdscript.tres +++ b/course/course-learn-gdscript.tres @@ -1,8 +1,8 @@ -[gd_resource type="Resource" load_steps=14 format=2] +[gd_resource type="Resource" load_steps=16 format=2] [ext_resource path="res://resources/Course.gd" type="Script" id=1] -[ext_resource path="res://course/lesson-4-drawing-a-rectangle/lesson.tres" type="Resource" id=2] -[ext_resource path="res://course/lesson-8-defining-variables/lesson.tres" type="Resource" id=3] +[ext_resource path="res://course/lesson-8-defining-variables/lesson.tres" type="Resource" id=2] +[ext_resource path="res://course/lesson-4-drawing-a-rectangle/lesson.tres" type="Resource" id=3] [ext_resource path="res://course/lesson-2-your-first-error/lesson.tres" type="Resource" id=4] [ext_resource path="res://course/lesson-7-member-variables/lesson.tres" type="Resource" id=5] [ext_resource path="res://course/lesson-6-multiple-function-parameters/lesson.tres" type="Resource" id=6] @@ -13,8 +13,10 @@ [ext_resource path="res://course/lesson-12-using-variables/lesson.tres" type="Resource" id=11] [ext_resource path="res://course/lesson-13-conditions/lesson.tres" type="Resource" id=12] [ext_resource path="res://course/lesson-14-multiplying/lesson.tres" type="Resource" id=13] +[ext_resource path="res://course/lesson-10-the-game-loop/lesson.tres" type="Resource" id=14] +[ext_resource path="res://course/lesson-11-time-delta/lesson.tres" type="Resource" id=15] [resource] script = ExtResource( 1 ) title = "Learn GDScript From Zero" -lessons = [ ExtResource( 9 ), ExtResource( 4 ), ExtResource( 7 ), ExtResource( 2 ), ExtResource( 10 ), ExtResource( 6 ), ExtResource( 5 ), ExtResource( 3 ), ExtResource( 8 ), ExtResource( 11 ), ExtResource( 12 ), ExtResource( 13 ) ] +lessons = [ ExtResource( 9 ), ExtResource( 4 ), ExtResource( 7 ), ExtResource( 3 ), ExtResource( 10 ), ExtResource( 6 ), ExtResource( 5 ), ExtResource( 2 ), ExtResource( 8 ), ExtResource( 14 ), ExtResource( 15 ), ExtResource( 11 ), ExtResource( 12 ), ExtResource( 13 ) ] diff --git a/course/lesson-10-the-game-loop/content-5AJTESv5.tres b/course/lesson-10-the-game-loop/content-5AJTESv5.tres new file mode 100644 index 000000000..6a12d130b --- /dev/null +++ b/course/lesson-10-the-game-loop/content-5AJTESv5.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "" +visual_element_path = "visuals/ExampleRotatingSprite.tscn" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-DizKUdOC.tres b/course/lesson-10-the-game-loop/content-DizKUdOC.tres new file mode 100644 index 000000000..a04a94861 --- /dev/null +++ b/course/lesson-10-the-game-loop/content-DizKUdOC.tres @@ -0,0 +1,18 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "As we've seen, Godot has functions that do certain actions. For example, the [code]show()[/code] and [code]hide()[/code] functions change the visibility of things. + +We can also create our own functions to make custom effects like adding or removing health to a character. + +Godot also has special functions we can customise or add to. + +Take the [code]_process()[/code] function." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-QiGjB7tK.tres b/course/lesson-10-the-game-loop/content-QiGjB7tK.tres new file mode 100644 index 000000000..91082d061 --- /dev/null +++ b/course/lesson-10-the-game-loop/content-QiGjB7tK.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "" +visual_element_path = "visuals/ExampleProcessFunction.tscn" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-RHJMQ2XN.tres b/course/lesson-10-the-game-loop/content-RHJMQ2XN.tres new file mode 100644 index 000000000..dbd599bab --- /dev/null +++ b/course/lesson-10-the-game-loop/content-RHJMQ2XN.tres @@ -0,0 +1,14 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "The [code]_process()[/code] function gets its name because it does calculations or continuous actions. + +It's like a juice factory that [b]processes[/b] juice bottles; the bottles are always moving along a conveyor belt, while different machines operate on them." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-TRhZN4rS.tres b/course/lesson-10-the-game-loop/content-TRhZN4rS.tres new file mode 100644 index 000000000..356b9ccfe --- /dev/null +++ b/course/lesson-10-the-game-loop/content-TRhZN4rS.tres @@ -0,0 +1,16 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "[font=res://ui/theme/fonts/font_title.tres]Why is [code]_process()[/code] useful?[/font] + +It's perhaps better to see the [code]_process()[/code] function in action. + +Take the following example." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-ZPxY8VUD.tres b/course/lesson-10-the-game-loop/content-ZPxY8VUD.tres new file mode 100644 index 000000000..f59be1985 --- /dev/null +++ b/course/lesson-10-the-game-loop/content-ZPxY8VUD.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 2 +text = "Other game engines might use different names like [code]_update()[/code]." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-bTxYej47.tres b/course/lesson-10-the-game-loop/content-bTxYej47.tres new file mode 100644 index 000000000..7173d193f --- /dev/null +++ b/course/lesson-10-the-game-loop/content-bTxYej47.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "In the practice, you'll learn how to use the process function to rotate and move a character yourself." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-otxF5HUx.tres b/course/lesson-10-the-game-loop/content-otxF5HUx.tres new file mode 100644 index 000000000..caf06a144 --- /dev/null +++ b/course/lesson-10-the-game-loop/content-otxF5HUx.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "It's similar in Godot, but this function can run [b]hundreds of times a second[/b]." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-pPwQDwfy.tres b/course/lesson-10-the-game-loop/content-pPwQDwfy.tres new file mode 100644 index 000000000..dcf35e828 --- /dev/null +++ b/course/lesson-10-the-game-loop/content-pPwQDwfy.tres @@ -0,0 +1,16 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "The [code]_process()[/code] function won't do anything until we add something to it. + +You might notice the underscore [code]_[/code] in front of the function name. This is a convention. It means the function is already defined by Godot. + +If the function exists, and is called exactly [code]_process[/code], then Godot will automatically run it every frame. " +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-qdYOexRj.tres b/course/lesson-10-the-game-loop/content-qdYOexRj.tres new file mode 100644 index 000000000..66941cd62 --- /dev/null +++ b/course/lesson-10-the-game-loop/content-qdYOexRj.tres @@ -0,0 +1,10 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +text = "[example \"process\" functions in other languages]" +visual_element_path = "" +reverse_blocks = false +type = 0 diff --git a/course/lesson-10-the-game-loop/content-tT6n1Txl.tres b/course/lesson-10-the-game-loop/content-tT6n1Txl.tres new file mode 100644 index 000000000..c742b2aca --- /dev/null +++ b/course/lesson-10-the-game-loop/content-tT6n1Txl.tres @@ -0,0 +1,14 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "Every frame, Godot runs the [code]_process()[/code] function. + +Since we wrote a [code]rotate()[/code] instruction, Godot is rotating the character by [code]0.05[/code] radians [b]many[/b] times a second." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-10-the-game-loop/content-wQMqAYVj.tres b/course/lesson-10-the-game-loop/content-wQMqAYVj.tres new file mode 100644 index 000000000..332215c00 --- /dev/null +++ b/course/lesson-10-the-game-loop/content-wQMqAYVj.tres @@ -0,0 +1,10 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +text = "[Conveyer belt animation?]" +visual_element_path = "" +reverse_blocks = false +type = 0 diff --git a/course/lesson-10-the-game-loop/lesson.tres b/course/lesson-10-the-game-loop/lesson.tres new file mode 100644 index 000000000..240dd8b96 --- /dev/null +++ b/course/lesson-10-the-game-loop/lesson.tres @@ -0,0 +1,23 @@ +[gd_resource type="Resource" load_steps=16 format=2] + +[ext_resource path="res://resources/Lesson.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-10-the-game-loop/practice-QiGjB7tK.tres" type="Resource" id=2] +[ext_resource path="res://course/lesson-10-the-game-loop/content-pPwQDwfy.tres" type="Resource" id=3] +[ext_resource path="res://course/lesson-10-the-game-loop/content-TRhZN4rS.tres" type="Resource" id=4] +[ext_resource path="res://course/lesson-10-the-game-loop/content-RHJMQ2XN.tres" type="Resource" id=5] +[ext_resource path="res://course/lesson-10-the-game-loop/content-5AJTESv5.tres" type="Resource" id=6] +[ext_resource path="res://course/lesson-10-the-game-loop/content-otxF5HUx.tres" type="Resource" id=7] +[ext_resource path="res://course/lesson-10-the-game-loop/content-ZPxY8VUD.tres" type="Resource" id=8] +[ext_resource path="res://course/lesson-10-the-game-loop/content-DizKUdOC.tres" type="Resource" id=9] +[ext_resource path="res://course/lesson-10-the-game-loop/content-tT6n1Txl.tres" type="Resource" id=10] +[ext_resource path="res://course/lesson-10-the-game-loop/quiz-MqE82V6j.tres" type="Resource" id=11] +[ext_resource path="res://course/lesson-10-the-game-loop/content-QiGjB7tK.tres" type="Resource" id=12] +[ext_resource path="res://course/lesson-10-the-game-loop/quiz-BnVHARbB.tres" type="Resource" id=13] +[ext_resource path="res://course/lesson-10-the-game-loop/content-bTxYej47.tres" type="Resource" id=14] +[ext_resource path="res://course/lesson-10-the-game-loop/practice-tKRHJMQ2.tres" type="Resource" id=15] + +[resource] +script = ExtResource( 1 ) +title = "The Game Loop" +content_blocks = [ ExtResource( 9 ), ExtResource( 12 ), ExtResource( 5 ), ExtResource( 7 ), ExtResource( 13 ), ExtResource( 3 ), ExtResource( 8 ), ExtResource( 4 ), ExtResource( 6 ), ExtResource( 10 ), ExtResource( 11 ), ExtResource( 14 ) ] +practices = [ ExtResource( 15 ), ExtResource( 2 ) ] diff --git a/course/lesson-10-the-game-loop/practice-QiGjB7tK.tres b/course/lesson-10-the-game-loop/practice-QiGjB7tK.tres new file mode 100644 index 000000000..b674d6608 --- /dev/null +++ b/course/lesson-10-the-game-loop/practice-QiGjB7tK.tres @@ -0,0 +1,21 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://resources/Practice.gd" type="Script" id=1] +[ext_resource path="res://course/Documentation.tres" type="Resource" id=2] + +[resource] +script = ExtResource( 1 ) +title = "Creating Circular Movement" +goal = "Make the robot move in a large circle slowly by rotating it and simultaneously moving it along its x direction. + +To do this, add the [code]rotate()[/code] and [code]move_local_x()[/code] functions to [code]_process()[/code]. + +Use a rotation speed of [code]0.05[/code] radians per frame, and move the robot [code]5[/code] pixels per frame." +starting_code = "func _process(_delta): + pass" +hints = PoolStringArray( "Replace [code]pass[/code] with the [code]rotate()[/code] and [code]move_local_x()[/code] functions" ) +validator_script_path = "rotating-and-moving/TestsRotatingAndMoving.gd" +script_slice_path = "rotating-and-moving/RotateAndMoveSprite.live-editor/slices/RotateAndMoveSprite.move_and_rotate.slice.tres" +documentation_references = PoolStringArray( "rotate", "move_local_x" ) +documentation_resource = ExtResource( 2 ) +description = "" diff --git a/course/lesson-10-the-game-loop/practice-kGx0c7DD.tres b/course/lesson-10-the-game-loop/practice-kGx0c7DD.tres new file mode 100644 index 000000000..42f8de377 --- /dev/null +++ b/course/lesson-10-the-game-loop/practice-kGx0c7DD.tres @@ -0,0 +1,15 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://resources/Practice.gd" type="Script" id=1] +[ext_resource path="res://course/Documentation.tres" type="Resource" id=2] + +[resource] +script = ExtResource( 1 ) +title = "Rotating and Moving" +goal = "" +starting_code = "" +hints = PoolStringArray( ) +validator_script_path = "rotating-and-moving/TestsRotatingAndMoving.gd" +script_slice_path = "rotating-and-moving/RotateAndMoveSprite.live-editor/slices/RotatingSprite.move_and_rotate.slice.tres" +documentation_references = PoolStringArray( ) +documentation_resource = ExtResource( 2 ) diff --git a/course/lesson-10-the-game-loop/practice-tKRHJMQ2.tres b/course/lesson-10-the-game-loop/practice-tKRHJMQ2.tres new file mode 100644 index 000000000..49dbf6232 --- /dev/null +++ b/course/lesson-10-the-game-loop/practice-tKRHJMQ2.tres @@ -0,0 +1,19 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://resources/Practice.gd" type="Script" id=1] +[ext_resource path="res://course/Documentation.tres" type="Resource" id=2] + +[resource] +script = ExtResource( 1 ) +title = "Rotating a Character Continuously" +goal = "Make the robot rotate slowly by adding to the [code]_process()[/code] function. + +A rotation speed of about [code]0.05[/code] each frame should do." +starting_code = "func _process(_delta): + pass" +hints = PoolStringArray( "Replace [code]pass[/code] with the [code]rotate()[/code] function", "The [code]rotate()[/code] function takes one parameter" ) +validator_script_path = "rotating-sprite/TestsRotating.gd" +script_slice_path = "rotating-sprite/RotatingSprite.live-editor/slices/RotatingSprite.move_and_rotate.slice.tres" +documentation_references = PoolStringArray( "rotate" ) +documentation_resource = ExtResource( 2 ) +description = "" diff --git a/course/lesson-10-the-game-loop/quiz-BnVHARbB.tres b/course/lesson-10-the-game-loop/quiz-BnVHARbB.tres new file mode 100644 index 000000000..4d38e15a2 --- /dev/null +++ b/course/lesson-10-the-game-loop/quiz-BnVHARbB.tres @@ -0,0 +1,18 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/QuizChoice.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +question = "How many parameters does this function take?" +content_bbcode = "[code] +func _process(delta): +[/code]" +hint = "" +explanation_bbcode = "The [code]_process()[/code] function takes one parameter: [code]delta[/code]. + +We'll look at what [code]delta[/code] is in the next lesson, as well as how to use it." +answer_options = [ "1", "2" ] +valid_answers = [ "1" ] +is_multiple_choice = false +do_shuffle_answers = true diff --git a/course/lesson-10-the-game-loop/quiz-MqE82V6j.tres b/course/lesson-10-the-game-loop/quiz-MqE82V6j.tres new file mode 100644 index 000000000..fb6c0f57a --- /dev/null +++ b/course/lesson-10-the-game-loop/quiz-MqE82V6j.tres @@ -0,0 +1,16 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/QuizChoice.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +question = "How often does the _process() function run?" +content_bbcode = "" +hint = "" +explanation_bbcode = "The faster your computer, the more times [code]_process()[/code] will run. + +Godot will try and run [code]_process()[/code] as quickly as it can. This makes sure any movement or animations look smooth and fluid." +answer_options = [ "Once a second.", "Multiple times a second." ] +valid_answers = [ "Multiple times a second." ] +is_multiple_choice = false +do_shuffle_answers = true diff --git a/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.gd b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.gd new file mode 100644 index 000000000..d4c9a60ae --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.gd @@ -0,0 +1,25 @@ +extends Node2D + + +func _ready() -> void: + set_process(false) + + +# EXPORT move_and_rotate +func _process(_delta): + rotate(0.05) + move_local_x(5) +# /EXPORT move_and_rotate + + +func _run() -> void: + reset() + set_process(true) + yield(get_tree().create_timer(1.0), "timeout") + Events.emit_signal("practice_run_completed") + + +func reset() -> void: + rotation = 0.0 + position = Vector2(300, 200) + set_process(false) diff --git a/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/scene/RotateAndMoveSprite.tscn.tres b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/scene/RotateAndMoveSprite.tscn.tres new file mode 100644 index 000000000..61e6ef478 --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/scene/RotateAndMoveSprite.tscn.tres @@ -0,0 +1,9 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/SceneProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.tscn" type="PackedScene" id=2] + +[resource] +script = ExtResource( 1 ) +scene = ExtResource( 2 ) +viewport_size = Vector2( 1920, 1080 ) diff --git a/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/scripts/RotateAndMoveSprite.gd.tres b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/scripts/RotateAndMoveSprite.gd.tres new file mode 100644 index 000000000..379fbf206 --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/scripts/RotateAndMoveSprite.gd.tres @@ -0,0 +1,35 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/ScriptProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.gd" type="Script" id=2] + +[resource] +script = ExtResource( 1 ) +nodes_paths = [ NodePath("RotateAndMoveSprite") ] +script_file = ExtResource( 2 ) +original_script = "extends Node2D + + +func _ready() -> void: + set_process(false) + + +# EXPORT move_and_rotate +func _process(_delta): + rotate(0.05) + move_local_x(5) +# /EXPORT move_and_rotate + + +func _run() -> void: + reset() + set_process(true) + yield(get_tree().create_timer(1.0), \"timeout\") + Events.emit_signal(\"practice_run_completed\") + + +func reset() -> void: + rotation = 0.0 + position = Vector2(300, 200) + set_process(false) +" diff --git a/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/slices/RotateAndMoveSprite.move_and_rotate.slice.tres b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/slices/RotateAndMoveSprite.move_and_rotate.slice.tres new file mode 100644 index 000000000..73429e9a9 --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/slices/RotateAndMoveSprite.move_and_rotate.slice.tres @@ -0,0 +1,20 @@ +[gd_resource type="Resource" load_steps=4 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/SliceProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/scene/RotateAndMoveSprite.tscn.tres" type="Resource" id=2] +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.live-editor/scripts/RotateAndMoveSprite.gd.tres" type="Resource" id=3] + +[resource] +script = ExtResource( 1 ) +scene_properties = ExtResource( 2 ) +script_properties = ExtResource( 3 ) +leading_spaces = 0 +keyword = "EXPORT" +closing = false +name = "move_and_rotate" +is_full_file = false +start = 7 +end = 11 +lines_before = [ "extends Node2D", "", "", "func _ready() -> void:", " set_process(false)", "", "" ] +lines_after = [ "", "", "func _run() -> void:", " reset()", " set_process(true)", " yield(get_tree().create_timer(1.0), \"timeout\")", " Events.emit_signal(\"practice_run_completed\")", "", "", "func reset() -> void:", " rotation = 0.0", " position = Vector2(300, 200)", " set_process(false)", "" ] +lines_editable = [ "func _process(_delta):", " rotate(0.05)", " move_local_x(5)" ] diff --git a/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.tscn b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.tscn new file mode 100644 index 000000000..d9e078d14 --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-and-moving/RotateAndMoveSprite.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/hand_ice.png" type="Texture" id=2] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/godot_bottom.png" type="Texture" id=3] + +[node name="Position2D" type="Position2D"] + +[node name="RotateAndMoveSprite" type="Node2D" parent="."] +position = Vector2( 300, 200 ) +script = ExtResource( 1 ) + +[node name="GodotBottom" type="Sprite" parent="RotateAndMoveSprite"] +position = Vector2( 0, -17 ) +texture = ExtResource( 3 ) + +[node name="HandIce" type="Sprite" parent="RotateAndMoveSprite"] +position = Vector2( 66, 14 ) +rotation = 0.36894 +texture = ExtResource( 2 ) + +[node name="HandIce2" type="Sprite" parent="RotateAndMoveSprite"] +position = Vector2( -65, 19 ) +rotation = -0.483649 +texture = ExtResource( 2 ) +flip_h = true diff --git a/course/lesson-10-the-game-loop/rotating-and-moving/TestsRotatingAndMoving.gd b/course/lesson-10-the-game-loop/rotating-and-moving/TestsRotatingAndMoving.gd new file mode 100644 index 000000000..168d599d4 --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-and-moving/TestsRotatingAndMoving.gd @@ -0,0 +1,13 @@ +extends PracticeTester + + +func test_character_is_moving_in_a_circle() -> String: + var node_2d = _scene_root_viewport.get_child(0).get_child(0) + var node_rotation = node_2d.rotation + var node_position = node_2d.position as Vector2 + + if node_rotation < 0.0: + return "The robot is turning in the wrong direction!" + elif is_equal_approx(node_position.x, 300.0): + return "The character didn't move as expected." + return "" diff --git a/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.gd b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.gd new file mode 100644 index 000000000..628553e70 --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.gd @@ -0,0 +1,23 @@ +extends Node2D + + +func _ready() -> void: + set_process(false) + + +# EXPORT move_and_rotate +func _process(_delta): + rotate(0.05) +# /EXPORT move_and_rotate + + +func _run() -> void: + reset() + set_process(true) + yield(get_tree().create_timer(1.0), "timeout") + Events.emit_signal("practice_run_completed") + + +func reset() -> void: + rotation = 0.0 + set_process(false) diff --git a/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/scene/RotatingSprite.tscn.tres b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/scene/RotatingSprite.tscn.tres new file mode 100644 index 000000000..777e6ed6a --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/scene/RotatingSprite.tscn.tres @@ -0,0 +1,9 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/SceneProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.tscn" type="PackedScene" id=2] + +[resource] +script = ExtResource( 1 ) +scene = ExtResource( 2 ) +viewport_size = Vector2( 1920, 1080 ) diff --git a/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/scripts/RotatingSprite.gd.tres b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/scripts/RotatingSprite.gd.tres new file mode 100644 index 000000000..3e7a492cc --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/scripts/RotatingSprite.gd.tres @@ -0,0 +1,33 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/ScriptProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.gd" type="Script" id=2] + +[resource] +script = ExtResource( 1 ) +nodes_paths = [ NodePath("RotatingSprite") ] +script_file = ExtResource( 2 ) +original_script = "extends Node2D + + +func _ready() -> void: + set_process(false) + + +# EXPORT move_and_rotate +func _process(_delta): + rotate(0.05) +# /EXPORT move_and_rotate + + +func _run() -> void: + reset() + set_process(true) + yield(get_tree().create_timer(1.0), \"timeout\") + Events.emit_signal(\"practice_run_completed\") + + +func reset() -> void: + rotation = 0.0 + set_process(false) +" diff --git a/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/slices/RotatingSprite.move_and_rotate.slice.tres b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/slices/RotatingSprite.move_and_rotate.slice.tres new file mode 100644 index 000000000..9c318b66e --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/slices/RotatingSprite.move_and_rotate.slice.tres @@ -0,0 +1,20 @@ +[gd_resource type="Resource" load_steps=4 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/SliceProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/scene/RotatingSprite.tscn.tres" type="Resource" id=2] +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.live-editor/scripts/RotatingSprite.gd.tres" type="Resource" id=3] + +[resource] +script = ExtResource( 1 ) +scene_properties = ExtResource( 2 ) +script_properties = ExtResource( 3 ) +leading_spaces = 0 +keyword = "EXPORT" +closing = false +name = "move_and_rotate" +is_full_file = false +start = 7 +end = 10 +lines_before = [ "extends Node2D", "", "", "func _ready() -> void:", " set_process(false)", "", "" ] +lines_after = [ "", "", "func _run() -> void:", " reset()", " set_process(true)", " yield(get_tree().create_timer(1.0), \"timeout\")", " Events.emit_signal(\"practice_run_completed\")", "", "", "func reset() -> void:", " rotation = 0.0", " set_process(false)", "" ] +lines_editable = [ "func _process(_delta):", " rotate(0.05)" ] diff --git a/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.tscn b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.tscn new file mode 100644 index 000000000..a808fa734 --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://course/lesson-10-the-game-loop/rotating-sprite/RotatingSprite.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/hand_ice.png" type="Texture" id=2] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/godot_bottom.png" type="Texture" id=3] + +[node name="Position2D" type="Position2D"] + +[node name="RotatingSprite" type="Node2D" parent="."] +position = Vector2( 300, 300 ) +script = ExtResource( 1 ) + +[node name="GodotBottom" type="Sprite" parent="RotatingSprite"] +position = Vector2( 0, -17 ) +texture = ExtResource( 3 ) + +[node name="HandIce" type="Sprite" parent="RotatingSprite"] +position = Vector2( 66, 14 ) +rotation = 0.36894 +texture = ExtResource( 2 ) + +[node name="HandIce2" type="Sprite" parent="RotatingSprite"] +position = Vector2( -65, 19 ) +rotation = -0.483649 +texture = ExtResource( 2 ) +flip_h = true diff --git a/course/lesson-10-the-game-loop/rotating-sprite/TestsRotating.gd b/course/lesson-10-the-game-loop/rotating-sprite/TestsRotating.gd new file mode 100644 index 000000000..3674ffbc2 --- /dev/null +++ b/course/lesson-10-the-game-loop/rotating-sprite/TestsRotating.gd @@ -0,0 +1,9 @@ +extends PracticeTester + + +func test_character_is_rotating_clockwise() -> String: + var node_2d = _scene_root_viewport.get_child(0).get_child(0) + var node_rotation = node_2d.rotation + if node_rotation < 0.0: + return "The robot is turning in the wrong direction!" + return "" diff --git a/course/lesson-10-the-game-loop/visuals/DemoRotatingSprite.gd b/course/lesson-10-the-game-loop/visuals/DemoRotatingSprite.gd new file mode 100644 index 000000000..7a9722f2e --- /dev/null +++ b/course/lesson-10-the-game-loop/visuals/DemoRotatingSprite.gd @@ -0,0 +1,13 @@ +extends Node2D + + +func _ready(): + set_process(false) + + +func run(): + set_process(true) + + +func _process(delta : float) -> void: + rotate(0.05) diff --git a/course/lesson-10-the-game-loop/visuals/DemoRotatingSprite.tscn b/course/lesson-10-the-game-loop/visuals/DemoRotatingSprite.tscn new file mode 100644 index 000000000..bf04d748f --- /dev/null +++ b/course/lesson-10-the-game-loop/visuals/DemoRotatingSprite.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://course/lesson-10-the-game-loop/visuals/DemoRotatingSprite.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/godot_bottom.png" type="Texture" id=2] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/hand_ice.png" type="Texture" id=3] + +[node name="DemoRotateSprite" type="Node2D"] +script = ExtResource( 1 ) + +[node name="GodotBottom" type="Sprite" parent="."] +position = Vector2( 0, -17 ) +texture = ExtResource( 2 ) + +[node name="HandIce" type="Sprite" parent="."] +position = Vector2( 66, 14 ) +rotation = 0.36894 +texture = ExtResource( 3 ) + +[node name="HandIce2" type="Sprite" parent="."] +position = Vector2( -65, 19 ) +rotation = -0.483649 +texture = ExtResource( 3 ) +flip_h = true diff --git a/course/lesson-10-the-game-loop/visuals/ExampleProcessFunction.tscn b/course/lesson-10-the-game-loop/visuals/ExampleProcessFunction.tscn new file mode 100644 index 000000000..a6566dcc9 --- /dev/null +++ b/course/lesson-10-the-game-loop/visuals/ExampleProcessFunction.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://course/common/GDScriptCodeExample.tscn" type="PackedScene" id=1] + +[node name="ExampleProcessFunctionBody" type="PanelContainer"] +margin_right = 288.0 +margin_bottom = 178.0 +rect_min_size = Vector2( 320, 200 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="GDScriptCodeExample" parent="." instance=ExtResource( 1 )] +anchor_right = 0.0 +anchor_bottom = 0.0 +margin_left = 7.0 +margin_top = 7.0 +margin_right = 327.0 +margin_bottom = 207.0 +rect_min_size = Vector2( 320, 200 ) +text = "func _process(delta): + rotate(0.05)" diff --git a/course/lesson-10-the-game-loop/visuals/ExampleRotatingSprite.tscn b/course/lesson-10-the-game-loop/visuals/ExampleRotatingSprite.tscn new file mode 100644 index 000000000..d796bf07b --- /dev/null +++ b/course/lesson-10-the-game-loop/visuals/ExampleRotatingSprite.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://ui/components/RunnableCodeExample.tscn" type="PackedScene" id=1] +[ext_resource path="res://course/lesson-10-the-game-loop/visuals/DemoRotatingSprite.tscn" type="PackedScene" id=2] + +[node name="ExampleRotatingSprite" type="PanelContainer"] +margin_right = 1047.0 +margin_bottom = 298.0 +size_flags_horizontal = 3 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="RunnableCodeExample" parent="." instance=ExtResource( 1 )] +margin_left = 7.0 +margin_top = 7.0 +margin_right = 1040.0 +margin_bottom = 291.0 +scene = ExtResource( 2 ) +gdscript_code = "func _process(delta): + rotate(0.05)" diff --git a/course/lesson-11-time-delta/content-1TxlTtcy.tres b/course/lesson-11-time-delta/content-1TxlTtcy.tres new file mode 100644 index 000000000..ea008eaac --- /dev/null +++ b/course/lesson-11-time-delta/content-1TxlTtcy.tres @@ -0,0 +1,16 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 2 +text = "You may have heard of frames-per-second or FPS. Games often run at 30 or 60 frames-per-second. + +This means that each second, everything in the game is updated 30 or 60 times. + +On powerful computers, it's possible to unlock the frame rate to get hundreds or thousands of frames-per-second." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-B7tKRHJM.tres b/course/lesson-11-time-delta/content-B7tKRHJM.tres new file mode 100644 index 000000000..4e75e8257 --- /dev/null +++ b/course/lesson-11-time-delta/content-B7tKRHJM.tres @@ -0,0 +1,18 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 2 +text = "Remember! [code]delta[/code] is [b]very small[/b]. In case your Maths is rusty, here are a few examples of multiplications by very small numbers: + +- [code]2 * 0.5 = 1[/code] +- [code]10 * 0.2 = 2[/code] +- [code]1 * 0.01 = 0.01[/code] + +Since delta is often something like [code]0.016667[/code], you should expect numbers to become [b]smaller[/b] when you multiply by it." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-DGt6fUqb.tres b/course/lesson-11-time-delta/content-DGt6fUqb.tres new file mode 100644 index 000000000..01d236ec6 --- /dev/null +++ b/course/lesson-11-time-delta/content-DGt6fUqb.tres @@ -0,0 +1,22 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "[font=res://ui/theme/fonts/font_title.tres]Frames Time Variance[/font] + +Your computer controls the speed at which each software runs. Depending on how busy the computer is, this can vary. + +For that reason, no game engine can guarantee a constant framerate. There will always be milliseconds variations from frame to frame. + +That is why the [code]_process()[/code] function receives a [code]delta[/code] parameter. + +In mathematics, the word \"delta\" is often used to describe a very small change between two numbers. + +The [code]delta[/code] parameter tells us how fast Godot took to complete the [b]previous frame[/b]. We can use that to ensure that the variations between frames have no visible effect." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-DwfyqdYO.tres b/course/lesson-11-time-delta/content-DwfyqdYO.tres new file mode 100644 index 000000000..8ba165b36 --- /dev/null +++ b/course/lesson-11-time-delta/content-DwfyqdYO.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "We've seen how we can use our character's [code]_process()[/code] function to make it move continuously." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-ESv5tT6n.tres b/course/lesson-11-time-delta/content-ESv5tT6n.tres new file mode 100644 index 000000000..5b3d27ccc --- /dev/null +++ b/course/lesson-11-time-delta/content-ESv5tT6n.tres @@ -0,0 +1,10 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +text = "Flip book animation?" +visual_element_path = "" +reverse_blocks = false +type = 0 diff --git a/course/lesson-11-time-delta/content-En91c9ll.tres b/course/lesson-11-time-delta/content-En91c9ll.tres new file mode 100644 index 000000000..858cf2798 --- /dev/null +++ b/course/lesson-11-time-delta/content-En91c9ll.tres @@ -0,0 +1,14 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "In this second example, we set an [b]different rotation[/b] every frame, adjusting the character's rotation based on [code]delta[/code]. + +Thanks to that, we get a [b]constant rotation[/b] per second. The character seems to rotate at a regular speed." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-Gx0c7DDi.tres b/course/lesson-11-time-delta/content-Gx0c7DDi.tres new file mode 100644 index 000000000..1f5a68429 --- /dev/null +++ b/course/lesson-11-time-delta/content-Gx0c7DDi.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "" +visual_element_path = "visuals/DemoRobotRace.tscn" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-N4rS5AJT.tres b/course/lesson-11-time-delta/content-N4rS5AJT.tres new file mode 100644 index 000000000..6e29ef073 --- /dev/null +++ b/course/lesson-11-time-delta/content-N4rS5AJT.tres @@ -0,0 +1,22 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "But it's not just our character that has a [code]_process()[/code] function; almost everything in the game has a [code]_process()[/code] function! + +Godot runs each [code]_process()[/code] function as fast as it can. + +When Godot has run every [code]_process()[/code] function in the game, it has completed one game loop, or [b]frame[/b]. + +Godot then moves on to the next frame. This runs very fast, giving us the illusion of movement. This is similar to how movies or animations work, which is why we use the word [b]frame[/b]. + +And this happens many, many times a second! + +This is how we get fluid motion in games. " +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-TLtZixQO.tres b/course/lesson-11-time-delta/content-TLtZixQO.tres new file mode 100644 index 000000000..9216d6fa7 --- /dev/null +++ b/course/lesson-11-time-delta/content-TLtZixQO.tres @@ -0,0 +1,14 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "If the frame is fast (0.1s), we only move a little. When the frame is slow (0.2s), we move more to compensate. + +Multiplying time sensitive values by [code]delta[/code] makes them [b]time-dependent[/b] rather than [b]frame-dependent[/b]." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-cSrGQgrV.tres b/course/lesson-11-time-delta/content-cSrGQgrV.tres new file mode 100644 index 000000000..6d91b24d6 --- /dev/null +++ b/course/lesson-11-time-delta/content-cSrGQgrV.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "In the next practice, we'll use delta to make rotating time-dependant." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-exRjTRhZ.tres b/course/lesson-11-time-delta/content-exRjTRhZ.tres new file mode 100644 index 000000000..620a68d0e --- /dev/null +++ b/course/lesson-11-time-delta/content-exRjTRhZ.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "" +visual_element_path = "res://course/lesson-10-the-game-loop/visuals/ExampleRotatingSprite.tscn" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-f8B67UJ8.tres b/course/lesson-11-time-delta/content-f8B67UJ8.tres new file mode 100644 index 000000000..3d1afa5e0 --- /dev/null +++ b/course/lesson-11-time-delta/content-f8B67UJ8.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "" +visual_element_path = "visuals/ExampleRotatingSpriteTime.tscn" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-hcIb5BnV.tres b/course/lesson-11-time-delta/content-hcIb5BnV.tres new file mode 100644 index 000000000..d229e0774 --- /dev/null +++ b/course/lesson-11-time-delta/content-hcIb5BnV.tres @@ -0,0 +1,12 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "" +type = 0 +text = "Let's look at the [code]_process()[/code] function in more detail." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-qE82V6jG.tres b/course/lesson-11-time-delta/content-qE82V6jG.tres new file mode 100644 index 000000000..a782ea340 --- /dev/null +++ b/course/lesson-11-time-delta/content-qE82V6jG.tres @@ -0,0 +1,22 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +title = "Why Does Frame Variance Matter?" +type = 0 +text = "What would happen if our computer slows down or has irregular frame times? + +In the examples below, we artificially made the frames last different times. Some last 0.1 seconds, some as much as 0.7 seconds. + +When we use [code]delta[/code], the robot takes into account the varying frames so aligns with the desired path; it gets the end of the line at the correct time. + +By using [code]delta[/code], we move a [b]different amount[/b] every frame, adjusting the character's position based on [code]delta[/code]. + +Thanks to that, we get [b]constant movement[/b] per second. The character seems to move at a regular speed. + +Using a constant distance each frame instead, we get [b]varying movement[/b] per second. Because of that, the character seems to jump and overshoots the target." +visual_element_path = "" +reverse_blocks = false +has_separator = false diff --git a/course/lesson-11-time-delta/content-vGm22rtQ.tres b/course/lesson-11-time-delta/content-vGm22rtQ.tres new file mode 100644 index 000000000..3e9510738 --- /dev/null +++ b/course/lesson-11-time-delta/content-vGm22rtQ.tres @@ -0,0 +1,10 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/ContentBlock.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +text = "" +visual_element_path = "visuals/ExampleRotatingSpriteFrame.tscn" +reverse_blocks = false +type = 0 diff --git a/course/lesson-11-time-delta/lesson.tres b/course/lesson-11-time-delta/lesson.tres new file mode 100644 index 000000000..3625ab4d7 --- /dev/null +++ b/course/lesson-11-time-delta/lesson.tres @@ -0,0 +1,25 @@ +[gd_resource type="Resource" load_steps=18 format=2] + +[ext_resource path="res://resources/Lesson.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-11-time-delta/quiz-HARbBWNb.tres" type="Resource" id=2] +[ext_resource path="res://course/lesson-11-time-delta/content-hcIb5BnV.tres" type="Resource" id=3] +[ext_resource path="res://course/lesson-11-time-delta/content-N4rS5AJT.tres" type="Resource" id=4] +[ext_resource path="res://course/lesson-11-time-delta/practice-UdOCQiGj.tres" type="Resource" id=5] +[ext_resource path="res://course/lesson-11-time-delta/content-DwfyqdYO.tres" type="Resource" id=6] +[ext_resource path="res://course/lesson-11-time-delta/content-exRjTRhZ.tres" type="Resource" id=7] +[ext_resource path="res://course/lesson-11-time-delta/content-1TxlTtcy.tres" type="Resource" id=8] +[ext_resource path="res://course/lesson-11-time-delta/quiz-TxYej47M.tres" type="Resource" id=10] +[ext_resource path="res://course/lesson-11-time-delta/content-DGt6fUqb.tres" type="Resource" id=12] +[ext_resource path="res://course/lesson-11-time-delta/content-qE82V6jG.tres" type="Resource" id=13] +[ext_resource path="res://course/lesson-11-time-delta/content-TLtZixQO.tres" type="Resource" id=14] +[ext_resource path="res://course/lesson-11-time-delta/content-Gx0c7DDi.tres" type="Resource" id=15] +[ext_resource path="res://course/lesson-11-time-delta/content-cSrGQgrV.tres" type="Resource" id=16] +[ext_resource path="res://course/lesson-11-time-delta/quiz-PRw9clsF.tres" type="Resource" id=17] +[ext_resource path="res://course/lesson-11-time-delta/practice-x0c7DDiz.tres" type="Resource" id=18] +[ext_resource path="res://course/lesson-11-time-delta/content-B7tKRHJM.tres" type="Resource" id=19] + +[resource] +script = ExtResource( 1 ) +title = "Time Delta" +content_blocks = [ ExtResource( 6 ), ExtResource( 7 ), ExtResource( 4 ), ExtResource( 8 ), ExtResource( 3 ), ExtResource( 2 ), ExtResource( 12 ), ExtResource( 10 ), ExtResource( 13 ), ExtResource( 15 ), ExtResource( 14 ), ExtResource( 19 ), ExtResource( 17 ), ExtResource( 16 ) ] +practices = [ ExtResource( 5 ), ExtResource( 18 ) ] diff --git a/course/lesson-11-time-delta/practice-UdOCQiGj.tres b/course/lesson-11-time-delta/practice-UdOCQiGj.tres new file mode 100644 index 000000000..106143c02 --- /dev/null +++ b/course/lesson-11-time-delta/practice-UdOCQiGj.tres @@ -0,0 +1,21 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://resources/Practice.gd" type="Script" id=1] +[ext_resource path="res://course/Documentation.tres" type="Resource" id=2] + +[resource] +script = ExtResource( 1 ) +title = "Rotating Using Delta" +goal = "At the minute, the rotation of the robot is frame-dependent. + +Add [code]delta[/code] to make the rotational speed time-dependent. + +The robot should rotate [code]2[/code] radians per second." +starting_code = "func _process(delta): + rotate(2)" +hints = PoolStringArray( "Multiply the radians by [code]delta[/code]" ) +validator_script_path = "rotating-using-delta/TestsRotatingDelta.gd" +script_slice_path = "rotating-using-delta/RotatingSpriteDelta.live-editor/slices/RotatingSpriteDelta.move_and_rotate.slice.tres" +documentation_references = PoolStringArray( "rotate" ) +documentation_resource = ExtResource( 2 ) +description = "" diff --git a/course/lesson-11-time-delta/practice-x0c7DDiz.tres b/course/lesson-11-time-delta/practice-x0c7DDiz.tres new file mode 100644 index 000000000..c807f8841 --- /dev/null +++ b/course/lesson-11-time-delta/practice-x0c7DDiz.tres @@ -0,0 +1,19 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://resources/Practice.gd" type="Script" id=1] +[ext_resource path="res://course/Documentation.tres" type="Resource" id=2] + +[resource] +script = ExtResource( 1 ) +title = "Moving in a Circle Using Delta" +goal = "In this practice, make the robot move in a smooth circle using delta. + +To get this movement, the robot should rotate [code]5[/code] radians per second and move [code]100[/code] pixels per second in the x direction." +starting_code = "func _process(delta): + pass" +hints = PoolStringArray( "Replace [code]pass[/code] with the [code]rotate()[/code] and [code]move_local_x()[/code] functions", "Make sure to multiply each parameter by [code]delta[/code]" ) +validator_script_path = "rotating-and-moving-delta/TestsRotatingMovingDelta.gd" +script_slice_path = "rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/slices/RotatingMovingSpriteDelta.move_and_rotate.slice.tres" +documentation_references = PoolStringArray( "rotate", "move_local_x" ) +documentation_resource = ExtResource( 2 ) +description = "" diff --git a/course/lesson-11-time-delta/quiz-HARbBWNb.tres b/course/lesson-11-time-delta/quiz-HARbBWNb.tres new file mode 100644 index 000000000..592c5d57b --- /dev/null +++ b/course/lesson-11-time-delta/quiz-HARbBWNb.tres @@ -0,0 +1,17 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/QuizChoice.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +question = "What parameter does the _process() function take?" +content_bbcode = "[code] +func _process(delta): + rotate(0.05) +[/code]" +hint = "" +explanation_bbcode = "The [code]_process()[/code] function has one parameter named [code]delta[/code]." +answer_options = [ "rotate", "0.05", "delta" ] +valid_answers = [ "delta" ] +is_multiple_choice = false +do_shuffle_answers = true diff --git a/course/lesson-11-time-delta/quiz-PRw9clsF.tres b/course/lesson-11-time-delta/quiz-PRw9clsF.tres new file mode 100644 index 000000000..b9272c240 --- /dev/null +++ b/course/lesson-11-time-delta/quiz-PRw9clsF.tres @@ -0,0 +1,14 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/QuizChoice.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +question = "What does this mean?" +content_bbcode = "[code] rotation_speed * delta [/code]" +hint = "" +explanation_bbcode = "The [code]*[/code] symbol means we're multiplying [code]rotation_speed[/code] by [code]delta[/code] time." +answer_options = [ "We're multiplying rotation_speed by delta.", "We're dividing delta by rotation_speed.", "We're adding rotation_speed to delta.", "We're subtracting delta from rotation_speed." ] +valid_answers = [ "We're multiplying rotation_speed by delta." ] +is_multiple_choice = false +do_shuffle_answers = true diff --git a/course/lesson-11-time-delta/quiz-TxYej47M.tres b/course/lesson-11-time-delta/quiz-TxYej47M.tres new file mode 100644 index 000000000..35b251112 --- /dev/null +++ b/course/lesson-11-time-delta/quiz-TxYej47M.tres @@ -0,0 +1,18 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://resources/QuizChoice.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +question = "What do we know about delta?" +content_bbcode = "" +hint = "" +explanation_bbcode = "[code]delta[/code] is the time it takes Godot to complete the previous frame in seconds. + +It's very small because frames happen many times a second. + +It varies each frame because Godot needs to process more or less each frame." +answer_options = [ "It's very small because frames happen many times a second.", "It varies each frame.", "It's the time it takes Godot to complete the previous frame." ] +valid_answers = [ "It's very small because frames happen many times a second.", "It varies each frame.", "It's the time it takes Godot to complete the previous frame." ] +is_multiple_choice = true +do_shuffle_answers = true diff --git a/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.gd b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.gd new file mode 100644 index 000000000..3a5f4c887 --- /dev/null +++ b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.gd @@ -0,0 +1,24 @@ +extends Node2D + + +func _ready() -> void: + set_process(false) + + +# EXPORT move_and_rotate +func _process(delta): + rotate(2 * delta) + move_local_x(100 * delta) +# /EXPORT move_and_rotate + + +func _run() -> void: + reset() + _process(0.0) + set_process(true) + + +func reset() -> void: + rotation = 0.0 + position = Vector2(300, 200) + set_process(false) diff --git a/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/scene/RotatingMovingSpriteDelta.tscn.tres b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/scene/RotatingMovingSpriteDelta.tscn.tres new file mode 100644 index 000000000..fded0ca8e --- /dev/null +++ b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/scene/RotatingMovingSpriteDelta.tscn.tres @@ -0,0 +1,9 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/SceneProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.tscn" type="PackedScene" id=2] + +[resource] +script = ExtResource( 1 ) +scene = ExtResource( 2 ) +viewport_size = Vector2( 1920, 1080 ) diff --git a/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/scripts/RotatingMovingSpriteDelta.gd.tres b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/scripts/RotatingMovingSpriteDelta.gd.tres new file mode 100644 index 000000000..25663383e --- /dev/null +++ b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/scripts/RotatingMovingSpriteDelta.gd.tres @@ -0,0 +1,34 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/ScriptProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.gd" type="Script" id=2] + +[resource] +script = ExtResource( 1 ) +nodes_paths = [ NodePath("RotatingSprite") ] +script_file = ExtResource( 2 ) +original_script = "extends Node2D + + +func _ready() -> void: + set_process(false) + + +# EXPORT move_and_rotate +func _process(delta): + rotate(2 * delta) + move_local_x(100 * delta) +# /EXPORT move_and_rotate + + +func _run() -> void: + reset() + _process(0.0) + set_process(true) + + +func reset() -> void: + rotation = 0.0 + position = Vector2(300, 200) + set_process(false) +" diff --git a/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/slices/RotatingMovingSpriteDelta.move_and_rotate.slice.tres b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/slices/RotatingMovingSpriteDelta.move_and_rotate.slice.tres new file mode 100644 index 000000000..28bafcd28 --- /dev/null +++ b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/slices/RotatingMovingSpriteDelta.move_and_rotate.slice.tres @@ -0,0 +1,20 @@ +[gd_resource type="Resource" load_steps=4 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/SliceProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/scene/RotatingMovingSpriteDelta.tscn.tres" type="Resource" id=2] +[ext_resource path="res://course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.live-editor/scripts/RotatingMovingSpriteDelta.gd.tres" type="Resource" id=3] + +[resource] +script = ExtResource( 1 ) +scene_properties = ExtResource( 2 ) +script_properties = ExtResource( 3 ) +leading_spaces = 0 +keyword = "EXPORT" +closing = false +name = "move_and_rotate" +is_full_file = false +start = 7 +end = 11 +lines_before = [ "extends Node2D", "", "", "func _ready() -> void:", " set_process(false)", "", "" ] +lines_after = [ "", "", "func _run() -> void:", " reset()", " _process(0.0)", " set_process(true)", "", "", "func reset() -> void:", " rotation = 0.0", " position = Vector2(300, 200)", " set_process(false)", "" ] +lines_editable = [ "func _process(delta):", " rotate(2 * delta)", " move_local_x(100 * delta)" ] diff --git a/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.tscn b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.tscn new file mode 100644 index 000000000..0abf79c4d --- /dev/null +++ b/course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://course/lesson-11-time-delta/rotating-and-moving-delta/RotatingMovingSpriteDelta.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/hand_ice.png" type="Texture" id=2] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/godot_bottom.png" type="Texture" id=3] + +[node name="Position2D" type="Position2D"] + +[node name="RotatingSprite" type="Node2D" parent="."] +position = Vector2( 300, 200 ) +script = ExtResource( 1 ) + +[node name="GodotBottom" type="Sprite" parent="RotatingSprite"] +position = Vector2( 0, -17 ) +texture = ExtResource( 3 ) + +[node name="HandIce" type="Sprite" parent="RotatingSprite"] +position = Vector2( 66, 14 ) +rotation = 0.36894 +texture = ExtResource( 2 ) + +[node name="HandIce2" type="Sprite" parent="RotatingSprite"] +position = Vector2( -65, 19 ) +rotation = -0.483649 +texture = ExtResource( 2 ) +flip_h = true diff --git a/course/lesson-11-time-delta/rotating-and-moving-delta/TestsRotatingMovingDelta.gd b/course/lesson-11-time-delta/rotating-and-moving-delta/TestsRotatingMovingDelta.gd new file mode 100644 index 000000000..8fc0cf6f7 --- /dev/null +++ b/course/lesson-11-time-delta/rotating-and-moving-delta/TestsRotatingMovingDelta.gd @@ -0,0 +1,21 @@ +extends PracticeTester + + +func test_moving_in_a_circle_is_time_dependent() -> String: + var has_delta := _slice.current_text.rfind("delta") > 0 + var has_spaces := _slice.current_text.rfind(" * ") > 0 + var has_two := _slice.current_text.rfind("2") > 0 + var has_hundred := _slice.current_text.rfind("100") > 0 + + if not has_delta: + return "Did you use delta to make the rotation time-dependent?" + elif not has_two: + return "Is the rotation speed correct?" + elif not has_hundred: + return "Is the movement speed correct?" + elif not has_spaces: + return "Did you use spaces between *?" + elif not _slice.current_text.match(_slice.get_slice_text()): + return "There's something wrong here." + + return "" diff --git a/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.gd b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.gd new file mode 100644 index 000000000..2d8b01ee9 --- /dev/null +++ b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.gd @@ -0,0 +1,23 @@ +extends Node2D + + +func _ready() -> void: + set_process(false) + + +# EXPORT move_and_rotate +func _process(delta): + rotate(2 * delta) +# /EXPORT move_and_rotate + + +func _run() -> void: + reset() + _process(0.0) + set_process(true) + + +func reset() -> void: + rotation = 0.0 + position = Vector2(300, 200) + set_process(false) diff --git a/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/scene/RotatingSpriteDelta.tscn.tres b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/scene/RotatingSpriteDelta.tscn.tres new file mode 100644 index 000000000..0250903da --- /dev/null +++ b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/scene/RotatingSpriteDelta.tscn.tres @@ -0,0 +1,9 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/SceneProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.tscn" type="PackedScene" id=2] + +[resource] +script = ExtResource( 1 ) +scene = ExtResource( 2 ) +viewport_size = Vector2( 1920, 1080 ) diff --git a/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/scripts/RotatingSpriteDelta.gd.tres b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/scripts/RotatingSpriteDelta.gd.tres new file mode 100644 index 000000000..1eecddd07 --- /dev/null +++ b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/scripts/RotatingSpriteDelta.gd.tres @@ -0,0 +1,33 @@ +[gd_resource type="Resource" load_steps=3 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/ScriptProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.gd" type="Script" id=2] + +[resource] +script = ExtResource( 1 ) +nodes_paths = [ NodePath("RotatingSprite") ] +script_file = ExtResource( 2 ) +original_script = "extends Node2D + + +func _ready() -> void: + set_process(false) + + +# EXPORT move_and_rotate +func _process(delta): + rotate(2 * delta) +# /EXPORT move_and_rotate + + +func _run() -> void: + reset() + _process(0.0) + set_process(true) + + +func reset() -> void: + rotation = 0.0 + position = Vector2(300, 200) + set_process(false) +" diff --git a/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/slices/RotatingSpriteDelta.move_and_rotate.slice.tres b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/slices/RotatingSpriteDelta.move_and_rotate.slice.tres new file mode 100644 index 000000000..0e0202772 --- /dev/null +++ b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/slices/RotatingSpriteDelta.move_and_rotate.slice.tres @@ -0,0 +1,20 @@ +[gd_resource type="Resource" load_steps=4 format=2] + +[ext_resource path="res://addons/gdscript-slice-exporter/collections/SliceProperties.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/scene/RotatingSpriteDelta.tscn.tres" type="Resource" id=2] +[ext_resource path="res://course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.live-editor/scripts/RotatingSpriteDelta.gd.tres" type="Resource" id=3] + +[resource] +script = ExtResource( 1 ) +scene_properties = ExtResource( 2 ) +script_properties = ExtResource( 3 ) +leading_spaces = 0 +keyword = "EXPORT" +closing = false +name = "move_and_rotate" +is_full_file = false +start = 7 +end = 10 +lines_before = [ "extends Node2D", "", "", "func _ready() -> void:", " set_process(false)", "", "" ] +lines_after = [ "", "", "func _run() -> void:", " reset()", " _process(0.0)", " set_process(true)", "", "", "func reset() -> void:", " rotation = 0.0", " position = Vector2(300, 200)", " set_process(false)", "" ] +lines_editable = [ "func _process(delta):", " rotate(2 * delta)" ] diff --git a/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.tscn b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.tscn new file mode 100644 index 000000000..8000862c6 --- /dev/null +++ b/course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://course/lesson-11-time-delta/rotating-using-delta/RotatingSpriteDelta.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/hand_ice.png" type="Texture" id=2] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/godot_bottom.png" type="Texture" id=3] + +[node name="Position2D" type="Position2D"] + +[node name="RotatingSprite" type="Node2D" parent="."] +position = Vector2( 300, 300 ) +script = ExtResource( 1 ) + +[node name="GodotBottom" type="Sprite" parent="RotatingSprite"] +position = Vector2( 0, -17 ) +texture = ExtResource( 3 ) + +[node name="HandIce" type="Sprite" parent="RotatingSprite"] +position = Vector2( 66, 14 ) +rotation = 0.36894 +texture = ExtResource( 2 ) + +[node name="HandIce2" type="Sprite" parent="RotatingSprite"] +position = Vector2( -65, 19 ) +rotation = -0.483649 +texture = ExtResource( 2 ) +flip_h = true diff --git a/course/lesson-11-time-delta/rotating-using-delta/TestsRotatingDelta.gd b/course/lesson-11-time-delta/rotating-using-delta/TestsRotatingDelta.gd new file mode 100644 index 000000000..f7e7c021f --- /dev/null +++ b/course/lesson-11-time-delta/rotating-using-delta/TestsRotatingDelta.gd @@ -0,0 +1,18 @@ +extends PracticeTester + + +func test_rotating_character_is_time_dependent() -> String: + var has_delta := _slice.current_text.rfind("delta") > 0 + var has_spaces := _slice.current_text.rfind(" * ") > 0 + var has_two := _slice.current_text.rfind("2") > 0 + + if not has_delta: + return "Did you use delta to make the rotation time-dependent?" + elif not has_two: + return "Is the rotation speed correct?" + elif not has_spaces: + return "Did you use spaces between *?" + elif not _slice.current_text.match(_slice.get_slice_text()): + return "There's something wrong here." + + return "" diff --git a/course/lesson-11-time-delta/visuals/DemoRobotRace.gd b/course/lesson-11-time-delta/visuals/DemoRobotRace.gd new file mode 100644 index 000000000..68ebb6f82 --- /dev/null +++ b/course/lesson-11-time-delta/visuals/DemoRobotRace.gd @@ -0,0 +1,9 @@ +tool +extends RunnableCodeExample + +func _ready(): + create_slider_for("computer_speed", 0.1, 1.0, 0.1) + +func _set_instance_value(value: float, property_name: String, value_label: Label) -> void: + ._set_instance_value(value, property_name, value_label) + _gdscript_text_edit.text = gdscript_code.replace(property_name, "%s [=%s]"%[property_name, value]) diff --git a/course/lesson-11-time-delta/visuals/DemoRobotRace.tscn b/course/lesson-11-time-delta/visuals/DemoRobotRace.tscn new file mode 100644 index 000000000..f6a835135 --- /dev/null +++ b/course/lesson-11-time-delta/visuals/DemoRobotRace.tscn @@ -0,0 +1,39 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://course/lesson-11-time-delta/visuals/RobotRace.tscn" type="PackedScene" id=1] +[ext_resource path="res://ui/components/RunnableCodeExample.tscn" type="PackedScene" id=2] +[ext_resource path="res://course/lesson-11-time-delta/visuals/DemoRobotRace.gd" type="Script" id=3] + +[node name="Control" type="PanelContainer"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -1220.0 +margin_bottom = -560.0 +rect_min_size = Vector2( 700, 560 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="RunnableCodeExample" parent="." instance=ExtResource( 2 )] +margin_left = 7.0 +margin_top = 7.0 +margin_right = 693.0 +margin_bottom = 553.0 +script = ExtResource( 3 ) +scene = ExtResource( 1 ) + +[node name="Frame" parent="RunnableCodeExample" index="0"] +margin_right = 686.0 +margin_bottom = 546.0 + +[node name="ResetButton" parent="RunnableCodeExample/Frame/HBoxContainer" index="0"] +visible = false + +[node name="GDScriptCode" parent="RunnableCodeExample" index="1"] +visible = false +margin_left = 463.0 +margin_right = 923.0 +margin_bottom = 460.0 +draw_spaces = true + +[editable path="RunnableCodeExample"] diff --git a/course/lesson-11-time-delta/visuals/DemoRotatingSpriteFrame.gd b/course/lesson-11-time-delta/visuals/DemoRotatingSpriteFrame.gd new file mode 100644 index 000000000..d9e897894 --- /dev/null +++ b/course/lesson-11-time-delta/visuals/DemoRotatingSpriteFrame.gd @@ -0,0 +1,23 @@ +extends Node2D + +const _times := preload("./DemoRotatingTime.gd")._times; +var _index := 0 + +onready var _timer := $Timer as Timer +onready var _sprite := $DemoRotateSprite as Node2D +onready var _label_rotation := $LabelRotation as Label +onready var _label_frame := $LabelFrame as Label + +func _ready(): + _on_Timer_timeout() + +func _on_Timer_timeout(): + var rotation_amount := 0.2; + var frame_speed:float = _times[_index] + _sprite.rotate(rotation_amount) + _timer.start(frame_speed) + + _label_rotation.text = "rotation: %s"%[rotation_amount] + _label_frame.text = "frame speed: %s"%[frame_speed] + + _index = wrapi(_index + 1, 0, _times.size() - 1) diff --git a/course/lesson-11-time-delta/visuals/DemoRotatingSpriteFrame.tscn b/course/lesson-11-time-delta/visuals/DemoRotatingSpriteFrame.tscn new file mode 100644 index 000000000..0be12167b --- /dev/null +++ b/course/lesson-11-time-delta/visuals/DemoRotatingSpriteFrame.tscn @@ -0,0 +1,52 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://course/lesson-11-time-delta/visuals/DemoRotatingSpriteFrame.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/godot_bottom.png" type="Texture" id=2] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/hand_ice.png" type="Texture" id=3] +[ext_resource path="res://ui/theme/gdscript_app_theme.tres" type="Theme" id=4] + +[node name="Node2D" type="Node2D"] +script = ExtResource( 1 ) + +[node name="DemoRotateSprite" type="Node2D" parent="."] + +[node name="GodotBottom" type="Sprite" parent="DemoRotateSprite"] +position = Vector2( 0, -17 ) +texture = ExtResource( 2 ) + +[node name="HandIce" type="Sprite" parent="DemoRotateSprite"] +position = Vector2( 66, 14 ) +rotation = 0.36894 +texture = ExtResource( 3 ) + +[node name="HandIce2" type="Sprite" parent="DemoRotateSprite"] +position = Vector2( -65, 19 ) +rotation = -0.483649 +texture = ExtResource( 3 ) +flip_h = true + +[node name="Timer" type="Timer" parent="."] + +[node name="LabelRotation" type="Label" parent="."] +margin_left = -127.0 +margin_top = -94.0 +margin_right = 55.0 +margin_bottom = -66.0 +theme = ExtResource( 4 ) +text = "rotation: 0" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="LabelFrame" type="Label" parent="."] +margin_left = -127.0 +margin_top = -118.0 +margin_right = 55.0 +margin_bottom = -90.0 +theme = ExtResource( 4 ) +text = "frame speed: 0" +__meta__ = { +"_edit_use_anchors_": false +} + +[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"] diff --git a/course/lesson-11-time-delta/visuals/DemoRotatingTime.gd b/course/lesson-11-time-delta/visuals/DemoRotatingTime.gd new file mode 100644 index 000000000..ee0792eed --- /dev/null +++ b/course/lesson-11-time-delta/visuals/DemoRotatingTime.gd @@ -0,0 +1,23 @@ +extends Node2D + +const _times := [0.1, 0.1, 0.2, 0.1, 0.2] +var _index := 0 + +onready var _timer := $Timer as Timer +onready var _sprite := $DemoRotateSprite as Node2D +onready var _label_rotation := $LabelRotation as Label +onready var _label_frame := $LabelFrame as Label + +func _ready(): + _on_Timer_timeout() + +func _on_Timer_timeout(): + var rotation_amount := 1 * _timer.wait_time; + var frame_speed:float = _times[_index] + _sprite.rotate(rotation_amount) + _timer.start(frame_speed) + + _label_rotation.text = "rotation: %s"%[rotation_amount] + _label_frame.text = "frame speed: %s"%[frame_speed] + + _index = wrapi(_index + 1, 0, _times.size() - 1) diff --git a/course/lesson-11-time-delta/visuals/DemoRotatingTime.tscn b/course/lesson-11-time-delta/visuals/DemoRotatingTime.tscn new file mode 100644 index 000000000..7d2219b93 --- /dev/null +++ b/course/lesson-11-time-delta/visuals/DemoRotatingTime.tscn @@ -0,0 +1,52 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://course/lesson-11-time-delta/visuals/DemoRotatingTime.gd" type="Script" id=1] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/godot_bottom.png" type="Texture" id=2] +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/hand_ice.png" type="Texture" id=3] +[ext_resource path="res://ui/theme/gdscript_app_theme.tres" type="Theme" id=4] + +[node name="Node2D" type="Node2D"] +script = ExtResource( 1 ) + +[node name="DemoRotateSprite" type="Node2D" parent="."] + +[node name="GodotBottom" type="Sprite" parent="DemoRotateSprite"] +position = Vector2( 0, -17 ) +texture = ExtResource( 2 ) + +[node name="HandIce" type="Sprite" parent="DemoRotateSprite"] +position = Vector2( 66, 14 ) +rotation = 0.36894 +texture = ExtResource( 3 ) + +[node name="HandIce2" type="Sprite" parent="DemoRotateSprite"] +position = Vector2( -65, 19 ) +rotation = -0.483649 +texture = ExtResource( 3 ) +flip_h = true + +[node name="Timer" type="Timer" parent="."] + +[node name="LabelRotation" type="Label" parent="."] +margin_left = -135.0 +margin_top = -94.0 +margin_right = 47.0 +margin_bottom = -66.0 +theme = ExtResource( 4 ) +text = "rotation: 0" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="LabelFrame" type="Label" parent="."] +margin_left = -135.0 +margin_top = -118.0 +margin_right = 47.0 +margin_bottom = -90.0 +theme = ExtResource( 4 ) +text = "frame speed: 0" +__meta__ = { +"_edit_use_anchors_": false +} + +[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"] diff --git a/course/lesson-11-time-delta/visuals/ExampleRotatingSpriteFrame.tscn b/course/lesson-11-time-delta/visuals/ExampleRotatingSpriteFrame.tscn new file mode 100644 index 000000000..0a2ea0ff0 --- /dev/null +++ b/course/lesson-11-time-delta/visuals/ExampleRotatingSpriteFrame.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://ui/components/RunnableCodeExample.tscn" type="PackedScene" id=1] +[ext_resource path="res://course/lesson-11-time-delta/visuals/DemoRotatingSpriteFrame.tscn" type="PackedScene" id=2] + +[node name="ExampleRotatingSpriteFrame" type="PanelContainer"] +margin_right = 1047.0 +margin_bottom = 298.0 +size_flags_horizontal = 3 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="RunnableCodeExample" parent="." instance=ExtResource( 1 )] +margin_left = 7.0 +margin_top = 7.0 +margin_right = 1040.0 +margin_bottom = 291.0 +scene = ExtResource( 2 ) +gdscript_code = "func _process(delta): + rotate(0.2)" diff --git a/course/lesson-11-time-delta/visuals/ExampleRotatingSpriteTime.tscn b/course/lesson-11-time-delta/visuals/ExampleRotatingSpriteTime.tscn new file mode 100644 index 000000000..04e9f1fea --- /dev/null +++ b/course/lesson-11-time-delta/visuals/ExampleRotatingSpriteTime.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://ui/components/RunnableCodeExample.tscn" type="PackedScene" id=1] +[ext_resource path="res://course/lesson-11-time-delta/visuals/DemoRotatingTime.tscn" type="PackedScene" id=2] + +[node name="ExampleRotatingSpriteTime" type="PanelContainer"] +margin_right = 1047.0 +margin_bottom = 298.0 +size_flags_horizontal = 3 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="RunnableCodeExample" parent="." instance=ExtResource( 1 )] +margin_left = 7.0 +margin_top = 7.0 +margin_right = 1040.0 +margin_bottom = 291.0 +scene = ExtResource( 2 ) +gdscript_code = "func _process(delta): + rotate(1 * delta)" diff --git a/course/lesson-11-time-delta/visuals/RobotRace.gd b/course/lesson-11-time-delta/visuals/RobotRace.gd new file mode 100644 index 000000000..fd801ca50 --- /dev/null +++ b/course/lesson-11-time-delta/visuals/RobotRace.gd @@ -0,0 +1,59 @@ +extends Node2D + +const DELTAS := [0.1, 0.3, 0.5, 0.1, 0.2, 0.2, 0.6, 0.1, 0.1, 0.1, 0.3, 0.2, 0.1, 0.1, 0.1, 0.1] + +var computer_speed := 1.0 setget _set_computer_speed, get_computer_speed +var _index := 0 + +onready var _robots := $Robots +onready var _timer := $Timer +onready var _robot_normal := $Robots/RunningRobotNormal +onready var _robot_frame_delta := $Robots/RunningRobotFrameDelta +onready var _robot_frame := $Robots/RunningRobotFrame + + +func run() -> void: + if not _robot_normal.is_connected("animation_complete", _robot_frame_delta, "reset"): + _robot_normal.connect("animation_complete", _robot_frame_delta, "reset") + if not _robot_normal.is_connected("animation_complete", _robot_frame, "reset"): + _robot_normal.connect("animation_complete", _robot_frame, "reset") + + reset() + for child in _robots.get_children(): + child.run() + + _on_Timer_timeout() + + +func _set_computer_speed(new_computer_speed: float) -> void: + computer_speed = new_computer_speed * 10 + + if not _robots: + return + + for child in _robots.get_children(): + child.computer_speed = computer_speed + + reset() + + +func get_computer_speed() -> float: + return computer_speed + + +func reset() -> void: + _index = 0 + _timer.stop() + _robot_normal.reset() + + +func _on_Timer_timeout() -> void: + if _index > DELTAS.size() - 1: + _index = 0 + return + + _robot_frame_delta.set_sprite_position(_robot_normal.get_sprite_position()) + _robot_frame.move(Vector2(30, 0)) + + _timer.start(DELTAS[_index] / computer_speed) + _index += 1 diff --git a/course/lesson-11-time-delta/visuals/RobotRace.tscn b/course/lesson-11-time-delta/visuals/RobotRace.tscn new file mode 100644 index 000000000..99a9951ef --- /dev/null +++ b/course/lesson-11-time-delta/visuals/RobotRace.tscn @@ -0,0 +1,120 @@ +[gd_scene load_steps=6 format=2] + +[ext_resource path="res://course/lesson-11-time-delta/visuals/RunningRobot.tscn" type="PackedScene" id=1] +[ext_resource path="res://course/lesson-11-time-delta/visuals/RobotRace.gd" type="Script" id=2] + +[sub_resource type="GDScript" id=2] +script/source = "extends \"res://course/lesson-11-time-delta/visuals/RunningRobot.gd\" + +signal animation_complete + + +func _ready() -> void: + set_process(false) + update() + + +func _process(delta) -> void: + _sprite.position.x += speed * delta * computer_speed + + if _sprite.position.x > distance/2: + set_process(false) + + +func _draw() -> void: + draw_line(Vector2(distance/2, 0), Vector2(distance/2, distance), Color.white * 0.5, 4, true) + + draw_line(Vector2(-distance/2, 0), Vector2(distance/2, 0), Color.white, 4, true) + draw_circle(Vector2(-distance/2, 0), 6, Color.white) + draw_circle(Vector2(distance/2, 0), 6, Color.white) + + +func run() -> void: + reset() + set_process(true) + + +func reset() -> void: + _sprite.position.x = -distance/2 + set_process(false) + emit_signal(\"animation_complete\") +" + +[sub_resource type="GDScript" id=1] +script/source = "extends \"res://course/lesson-11-time-delta/visuals/RunningRobot.gd\" + + +func reset() -> void: + _sprite.position.x = -distance/2 +" + +[sub_resource type="GDScript" id=3] +script/source = "extends \"res://course/lesson-11-time-delta/visuals/RunningRobot.gd\" + + +func _ready() -> void: + speed /= 3 + + +func reset() -> void: + _sprite.position.x = -distance/2 + + +func move(vector: Vector2) -> void: + _sprite.position += vector +" + +[node name="RobotRace" type="Node2D"] +script = ExtResource( 2 ) + +[node name="Robots" type="Node2D" parent="."] + +[node name="RunningRobotNormal" parent="Robots" instance=ExtResource( 1 )] +position = Vector2( 0, -120 ) +script = SubResource( 2 ) + +[node name="Label" type="Label" parent="Robots/RunningRobotNormal"] +margin_left = -264.0 +margin_top = 8.0 +margin_right = -124.0 +margin_bottom = 22.0 +rect_min_size = Vector2( 140, 0 ) +text = "Desired Path" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="RunningRobotFrameDelta" parent="Robots" instance=ExtResource( 1 )] +position = Vector2( 0, 70 ) +script = SubResource( 1 ) + +[node name="Label" type="Label" parent="Robots/RunningRobotFrameDelta"] +margin_left = -264.0 +margin_top = 8.0 +margin_right = -124.0 +margin_bottom = 22.0 +rect_min_size = Vector2( 140, 0 ) +text = "Using Delta" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="RunningRobotFrame" parent="Robots" instance=ExtResource( 1 )] +position = Vector2( 0, 220 ) +script = SubResource( 3 ) + +[node name="Label" type="Label" parent="Robots/RunningRobotFrame"] +margin_left = -264.0 +margin_top = 8.0 +margin_right = -124.0 +margin_bottom = 22.0 +rect_min_size = Vector2( 140, 0 ) +text = "Constant Distance" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Timer" type="Timer" parent="."] +one_shot = true + +[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"] diff --git a/course/lesson-11-time-delta/visuals/RunningRobot.gd b/course/lesson-11-time-delta/visuals/RunningRobot.gd new file mode 100644 index 000000000..fca41034b --- /dev/null +++ b/course/lesson-11-time-delta/visuals/RunningRobot.gd @@ -0,0 +1,22 @@ +extends Node2D + +var computer_speed := 1.0 +var distance := 400 +var speed := distance/3 + +onready var _sprite := $Robot + +func _ready() -> void: + _sprite.position.x = -distance/2 + +func run() -> void: + pass + +func reset() -> void: + pass + +func set_sprite_position(new_position: Vector2) -> void: + _sprite.position = new_position + +func get_sprite_position() -> Vector2: + return _sprite.position diff --git a/course/lesson-11-time-delta/visuals/RunningRobot.tscn b/course/lesson-11-time-delta/visuals/RunningRobot.tscn new file mode 100644 index 000000000..c4ba3daef --- /dev/null +++ b/course/lesson-11-time-delta/visuals/RunningRobot.tscn @@ -0,0 +1,11 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://course/lesson-1-what-code-is-like/first_practice/godot_bottom.png" type="Texture" id=1] +[ext_resource path="res://course/lesson-11-time-delta/visuals/RunningRobot.gd" type="Script" id=2] + +[node name="RunningRobot" type="Node2D"] +script = ExtResource( 2 ) + +[node name="Robot" type="Sprite" parent="."] +position = Vector2( 0, -60 ) +texture = ExtResource( 1 ) diff --git a/course/lesson-3-standing-on-shoulders-of-giants/ExampleRotateSprite.tscn b/course/lesson-3-standing-on-shoulders-of-giants/ExampleRotateSprite.tscn index 2cf6dd198..6636dc186 100644 --- a/course/lesson-3-standing-on-shoulders-of-giants/ExampleRotateSprite.tscn +++ b/course/lesson-3-standing-on-shoulders-of-giants/ExampleRotateSprite.tscn @@ -24,7 +24,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="RunnableCodeExample" parent="." instance=ExtResource( 1 )] +[node name="RunnableCodeExample" parent="." instance=ExtResource( 2 )] margin_left = 7.0 margin_top = 7.0 margin_right = 1040.0 diff --git a/lesson-10.md b/lesson-10.md new file mode 100644 index 000000000..f70e15c58 --- /dev/null +++ b/lesson-10.md @@ -0,0 +1,39 @@ +As we've seen, Godot has functions that do certain actions. For example, the `show()` and `hide()` functions change the visibility of things. + +We can also create our own functions to make custom effects like adding or removing health to a character. + +Godot also has special functions we can customise or add to. + +Take the `_process()` function. + +- Show process() code example + +The `_process()` function gets its name because it does calculations or continuous actions. + +It's like a factory that **processes** food; the food is always moving along a conveyer belt. + +- Animation of a conveyer belt + +It's similar in Godot, but this function can run **hundreds of times a second**. + +- Note: Other game engines might use different names for this like update. + +Godot runs this function on its own; we don't have to keep telling Godot to run it. + +The `_process()` function won't do anything until we add something to it. + +You might notice the underscore in front of the function name. This means the function is built-in to Godot and we can add to it. + +Using an underscore in this way is common in lots of languages to show developers they can add to the function. + +- example "process" functions in other languages + +But how and why is the process useful? + +It's perhaps better to see the process function in action. Take the following example. + +- Example of a sprite rotating all of the time continuously. + +We've added to the character's process function so that it continuously rotate. + +In the practice, you'll learn how to use the process function to rotate continuously yourself. \ No newline at end of file