Add SpinComponent to handle rotation of a Node2D with adjustable speed and direction
This commit is contained in:
30
objects/entities/spike_wheel.tscn
Normal file
30
objects/entities/spike_wheel.tscn
Normal file
@@ -0,0 +1,30 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://buff711j0f2ph"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://ddlwjg81ro1kl" path="res://sprites/spike_trap.png" id="1_xg6e1"]
|
||||
[ext_resource type="Script" uid="uid://cq26pfxn17s7s" path="res://scripts/components/spin_component.gd" id="2_3d4lc"]
|
||||
[ext_resource type="Script" uid="uid://dkmxhjtmu5xlb" path="res://scripts/components/damage_component.gd" id="3_ytlu4"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_xg6e1"]
|
||||
radius = 8.0
|
||||
|
||||
[node name="Spike Wheel" type="Area2D"]
|
||||
collision_layer = 8
|
||||
collision_mask = 4
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
texture = ExtResource("1_xg6e1")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("CircleShape2D_xg6e1")
|
||||
|
||||
[node name="SpinComponent" type="Node2D" parent="." node_paths=PackedStringArray("root")]
|
||||
script = ExtResource("2_3d4lc")
|
||||
root = NodePath("../Sprite2D")
|
||||
speed = 1.0
|
||||
metadata/_custom_type_script = "uid://cq26pfxn17s7s"
|
||||
|
||||
[node name="DamageComponent" type="Node" parent="." node_paths=PackedStringArray("area2d")]
|
||||
script = ExtResource("3_ytlu4")
|
||||
damage = 1.0
|
||||
area2d = NodePath("..")
|
||||
metadata/_custom_type_script = "uid://dkmxhjtmu5xlb"
|
18
scripts/components/spin_component.gd
Normal file
18
scripts/components/spin_component.gd
Normal file
@@ -0,0 +1,18 @@
|
||||
class_name SpinComponent
|
||||
extends Node2D
|
||||
|
||||
@export var root: Node2D
|
||||
@export var speed: float = 8.0
|
||||
@export var rotation_direction: Vector2 = Vector2.RIGHT
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
spin(delta)
|
||||
|
||||
|
||||
func spin(delta: float) -> void:
|
||||
if not root:
|
||||
return
|
||||
|
||||
var rotation_speed: float = speed * rotation_direction.x
|
||||
root.rotation += rotation_speed * delta
|
1
scripts/components/spin_component.gd.uid
Normal file
1
scripts/components/spin_component.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cq26pfxn17s7s
|
Reference in New Issue
Block a user