Skip to content

Commit

Permalink
Merge pull request #294 from endlessm/simple-spawner-rotation-alt
Browse files Browse the repository at this point in the history
Simple spawner rotation alt
  • Loading branch information
dylanmccall authored Nov 7, 2024
2 parents 7f30d9d + 8b35143 commit da44907
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions addons/block_code/simple_spawner/simple_spawner.gd
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
@tool
class_name SimpleSpawner
extends Node2D
## SimpleSpawner node.
##
## If multiple spawned scenes are provided, one is picked ramdomly when spawning.
##
## Spawned instances are children of the current scene.
##
## The scene being spawned is rotated according to this node's global rotation:
## - If the spawned scene is a RigidBody2D, the linear velocity and constant forces
## are rotated according to the SimpleSpawner node global rotation.
## - If the spawned scene is a Node2D, the rotation is copied from the SimpleSpawner node.

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

enum SpawnParent {
THIS, ## Spawned scenes are children of this node
SCENE, ## Spawned scenes are children of the scene
}
enum LimitBehavior { REPLACE, NO_SPAWN }

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

## The node that the spawned scenes should be a child of. If you want to move
## the SimpleSpawner without moving the scenes it has already spawned, choose
## SCENE.
@export var spawn_parent: SpawnParent

## The period of time in seconds to spawn another component. If zero, they won't spawn
## automatically. Use the "Spawn" block.
@export_range(0.0, 10.0, 0.1, "or_greater", "suffix:s") var spawn_period: float = 0.0:
Expand Down Expand Up @@ -99,12 +100,15 @@ func spawn_once():
var scene: PackedScene = scenes.pick_random()
var spawned = scene.instantiate()
_spawned_scenes.push_back(spawned)
match spawn_parent:
SpawnParent.THIS:
add_child(spawned)
SpawnParent.SCENE:
get_tree().current_scene.add_child(spawned)
spawned.position = global_position
# Rotate the spawned scene according to the SimpleSpawner:
if spawned is RigidBody2D:
spawned.linear_velocity = spawned.linear_velocity.rotated(global_rotation)
spawned.constant_force = spawned.constant_force.rotated(global_rotation)
elif spawned is Node2D:
spawned.rotate(global_rotation)
# Add the spawned instance to the current scene:
get_tree().current_scene.add_child(spawned)
spawned.position = global_position


static func setup_custom_blocks():
Expand Down

0 comments on commit da44907

Please sign in to comment.