From 97efc15ec3b7e2872ce91313e1b57ef9d2f30bb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= Date: Fri, 25 Oct 2024 19:45:09 -0300 Subject: [PATCH 1/2] SimpleSpawner: Add "rotate with parent" property 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. --- addons/block_code/simple_spawner/simple_spawner.gd | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/addons/block_code/simple_spawner/simple_spawner.gd b/addons/block_code/simple_spawner/simple_spawner.gd index 1bb196e3..4477b3c2 100644 --- a/addons/block_code/simple_spawner/simple_spawner.gd +++ b/addons/block_code/simple_spawner/simple_spawner.gd @@ -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 = true + var _timer: Timer var _spawned_scenes: Array[Node] @@ -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) From 9a8ba59aeb6eb6ba5c1ec5ea81c40238d385e88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= Date: Mon, 28 Oct 2024 15:55:25 -0300 Subject: [PATCH 2/2] fixup! SimpleSpawner: Add "rotate with parent" property Use global rotation instead of (possibly none) parent rotation. --- .../block_code/simple_spawner/simple_spawner.gd | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/addons/block_code/simple_spawner/simple_spawner.gd b/addons/block_code/simple_spawner/simple_spawner.gd index 4477b3c2..8e30b57b 100644 --- a/addons/block_code/simple_spawner/simple_spawner.gd +++ b/addons/block_code/simple_spawner/simple_spawner.gd @@ -34,11 +34,11 @@ 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: +## Whether the scene being spawned is rotated according to the SimpleSpawner 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 = true +## are rotated according to the SimpleSpawner node global rotation. +## - If the spawned scene is a Node2D, the rotation is copied from the SimpleSpawner node. +@export var rotate_with_spawner: bool = true var _timer: Timer var _spawned_scenes: Array[Node] @@ -105,12 +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 rotate_with_spawner: if spawned is RigidBody2D: - spawned.linear_velocity = spawned.linear_velocity.rotated(get_parent().rotation) - spawned.constant_force = spawned.constant_force.rotated(get_parent().rotation) + spawned.linear_velocity = spawned.linear_velocity.rotated(global_rotation) + spawned.constant_force = spawned.constant_force.rotated(global_rotation) elif spawned is Node2D: - spawned.rotate(get_parent().rotation) + spawned.rotate(global_rotation) match spawn_parent: SpawnParent.THIS: add_child(spawned)