Skip to content

Commit da44907

Browse files
authored
Merge pull request #294 from endlessm/simple-spawner-rotation-alt
Simple spawner rotation alt
2 parents 7f30d9d + 8b35143 commit da44907

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

addons/block_code/simple_spawner/simple_spawner.gd

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
@tool
22
class_name SimpleSpawner
33
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.
414

515
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
616
const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd")
717
const OptionData = preload("res://addons/block_code/code_generation/option_data.gd")
818
const Types = preload("res://addons/block_code/types/types.gd")
919

10-
enum SpawnParent {
11-
THIS, ## Spawned scenes are children of this node
12-
SCENE, ## Spawned scenes are children of the scene
13-
}
1420
enum LimitBehavior { REPLACE, NO_SPAWN }
1521

1622
## The scenes to spawn. If more than one are provided, they will be picked randomly.
1723
@export var scenes: Array[PackedScene] = []
1824

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-
2425
## The period of time in seconds to spawn another component. If zero, they won't spawn
2526
## automatically. Use the "Spawn" block.
2627
@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():
99100
var scene: PackedScene = scenes.pick_random()
100101
var spawned = scene.instantiate()
101102
_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
108112

109113

110114
static func setup_custom_blocks():

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy