|
1 | 1 | @tool
|
2 | 2 | class_name SimpleSpawner
|
3 | 3 | extends Node2D
|
| 4 | +## SimpleSpawner node. |
| 5 | +## |
| 6 | +## If multiple spawned scenes are provided, one is picked ramdomly when spawning. |
| 7 | +## |
| 8 | +## Spawned instances are children of the current scene. |
| 9 | +## |
| 10 | +## The scene being spawned is rotated according to this node's global rotation: |
| 11 | +## - If the spawned scene is a RigidBody2D, the linear velocity and constant forces |
| 12 | +## are rotated according to the SimpleSpawner node global rotation. |
| 13 | +## - If the spawned scene is a Node2D, the rotation is copied from the SimpleSpawner node. |
4 | 14 |
|
5 | 15 | const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
|
6 | 16 | const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd")
|
7 | 17 | const OptionData = preload("res://addons/block_code/code_generation/option_data.gd")
|
8 | 18 | const Types = preload("res://addons/block_code/types/types.gd")
|
9 | 19 |
|
10 |
| -enum SpawnParent { |
11 |
| - THIS, ## Spawned scenes are children of this node |
12 |
| - SCENE, ## Spawned scenes are children of the scene |
13 |
| -} |
14 | 20 | enum LimitBehavior { REPLACE, NO_SPAWN }
|
15 | 21 |
|
16 | 22 | ## The scenes to spawn. If more than one are provided, they will be picked randomly.
|
17 | 23 | @export var scenes: Array[PackedScene] = []
|
18 | 24 |
|
19 |
| -## The node that the spawned scenes should be a child of. If you want to move |
20 |
| -## the SimpleSpawner without moving the scenes it has already spawned, choose |
21 |
| -## SCENE. |
22 |
| -@export var spawn_parent: SpawnParent |
23 |
| - |
24 | 25 | ## The period of time in seconds to spawn another component. If zero, they won't spawn
|
25 | 26 | ## automatically. Use the "Spawn" block.
|
26 | 27 | @export_range(0.0, 10.0, 0.1, "or_greater", "suffix:s") var spawn_period: float = 0.0:
|
@@ -99,12 +100,15 @@ func spawn_once():
|
99 | 100 | var scene: PackedScene = scenes.pick_random()
|
100 | 101 | var spawned = scene.instantiate()
|
101 | 102 | _spawned_scenes.push_back(spawned)
|
102 |
| - match spawn_parent: |
103 |
| - SpawnParent.THIS: |
104 |
| - add_child(spawned) |
105 |
| - SpawnParent.SCENE: |
106 |
| - get_tree().current_scene.add_child(spawned) |
107 |
| - spawned.position = global_position |
| 103 | + # Rotate the spawned scene according to the SimpleSpawner: |
| 104 | + if spawned is RigidBody2D: |
| 105 | + spawned.linear_velocity = spawned.linear_velocity.rotated(global_rotation) |
| 106 | + spawned.constant_force = spawned.constant_force.rotated(global_rotation) |
| 107 | + elif spawned is Node2D: |
| 108 | + spawned.rotate(global_rotation) |
| 109 | + # Add the spawned instance to the current scene: |
| 110 | + get_tree().current_scene.add_child(spawned) |
| 111 | + spawned.position = global_position |
108 | 112 |
|
109 | 113 |
|
110 | 114 | static func setup_custom_blocks():
|
|
0 commit comments