Skip to content

Commit

Permalink
SimpleSpawner: Add "rotate with parent" property
Browse files Browse the repository at this point in the history
Add yet another setting to the SimpleSpawner. This is to rotate the
spawned scene according to the SimpleSpawner parent node. Useful for
changing the direction of bullets spawned from a rotated object.
  • Loading branch information
manuq committed Oct 25, 2024
1 parent 2f66ee7 commit 463bb4e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions addons/block_code/simple_spawner/simple_spawner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ enum LimitBehavior { REPLACE, NO_SPAWN }
## - No Spawn: No spawn happens until any spawned scene is removed by other means.
@export var limit_behavior: LimitBehavior

## Whether the scene being spawned is rotated according to the parent node:
## - If the spawned scene is a RigidBody2D, the linear velocity and constant forces
## are rotated according to the parent node rotation.
## - If the spawned scene is a Node2D, the rotation is copied from the parent node.
@export var rotate_with_parent: bool = false

var _timer: Timer
var _spawned_scenes: Array[Node]

Expand Down Expand Up @@ -99,6 +105,12 @@ func spawn_once():
var scene: PackedScene = scenes.pick_random()
var spawned = scene.instantiate()
_spawned_scenes.push_back(spawned)
if rotate_with_parent and get_parent() and get_parent() is Node2D:
if spawned is RigidBody2D:
spawned.linear_velocity = spawned.linear_velocity.rotated(get_parent().rotation)
spawned.constant_force = spawned.constant_force.rotated(get_parent().rotation)
elif spawned is Node2D:
spawned.rotate(get_parent().rotation)
match spawn_parent:
SpawnParent.THIS:
add_child(spawned)
Expand Down

0 comments on commit 463bb4e

Please sign in to comment.