magnetic skill

This commit is contained in:
2025-02-16 23:28:07 +01:00
parent 9b11189905
commit 57930f1c50
4 changed files with 83 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=17 format=3 uid="uid://bqi5s710xb1ju"]
[gd_scene load_steps=19 format=3 uid="uid://bqi5s710xb1ju"]
[ext_resource type="Script" path="res://scripts/player.gd" id="1_8j4h4"]
[ext_resource type="Texture2D" uid="uid://b7gp0gqvkv8j4" path="res://sprites/MrBrick_base.png" id="2_bc55y"]
@@ -14,6 +14,7 @@
[ext_resource type="Script" path="res://scripts/components/stomp_damage_component.gd" id="12_payr4"]
[ext_resource type="Script" path="res://scripts/components/flashing_component.gd" id="13_hrtyn"]
[ext_resource type="Script" path="res://scripts/components/invulnerability_component.gd" id="14_jopig"]
[ext_resource type="Script" path="res://scripts/components/magnetic_skill.gd" id="15_4df3h"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_hdsg1"]
size = Vector2(16, 31)
@@ -21,6 +22,9 @@ size = Vector2(16, 31)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_vad0t"]
size = Vector2(16, 32)
[sub_resource type="CircleShape2D" id="CircleShape2D_ps31c"]
radius = 48.0
[node name="Brick Player" type="CharacterBody2D"]
collision_layer = 4
collision_mask = 11
@@ -115,5 +119,18 @@ health_component = NodePath("../HealthComponent")
script = ExtResource("14_jopig")
flashing_component = NodePath("../FlashingComponent Base")
[node name="MagneticSkillComponent" type="Node" parent="." node_paths=PackedStringArray("magnetic_area", "root")]
script = ExtResource("15_4df3h")
magnetic_area = NodePath("../MagneticArea")
root = NodePath("..")
magnetic_move_duration = 0.9
[node name="MagneticArea" type="Area2D" parent="."]
collision_layer = 0
collision_mask = 2
[node name="CollisionShape2D" type="CollisionShape2D" parent="MagneticArea"]
shape = SubResource("CircleShape2D_ps31c")
[connection signal="on_death" from="HealthComponent" to="PlayerDeathComponent" method="_on_health_component_on_death"]
[connection signal="on_health_change" from="HealthComponent" to="KnockbackComponent" method="_on_health_component_on_health_change"]

View File

@@ -10,7 +10,7 @@ radius = 9.0
[node name="Coin" type="Area2D"]
collision_layer = 2
collision_mask = 20
collision_mask = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_3ask2")

View File

@@ -387,7 +387,7 @@ texture_region_size = Vector2i(8, 8)
tile_size = Vector2i(8, 8)
sources/1 = SubResource("TileSetAtlasSource_eoyis")
[node name="Node2D" type="Node2D"]
[node name="World" type="Node2D"]
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(331.75, -48.5)

View File

@@ -0,0 +1,63 @@
class_name MagneticSkillComponent
extends Node
@export var magnetic_area: Area2D
@export var root: Node2D
@export var magnetic_move_duration: float = 1.25
var collectables_to_pickup: Array = []
func has_component_in_children(node: Node, component_name: String) -> bool:
if not node:
return false
if node.has_node(component_name):
return true
for child in node.get_children():
if has_component_in_children(child, component_name):
return true
return false
func _ready() -> void:
if not magnetic_area:
printerr("No magnetic_area assigned!")
return
magnetic_area.body_entered.connect(on_magnetic_area_body_entered)
magnetic_area.area_entered.connect(on_magnetic_area_area_entered)
func _process(_delta: float) -> void:
for collectable in collectables_to_pickup:
if not is_instance_valid(collectable):
collectables_to_pickup.erase(collectable)
continue
move_collectable_to_root(collectable)
func on_magnetic_area_body_entered(_body: Node2D) -> void:
pass
func on_magnetic_area_area_entered(area: Area2D) -> void:
if not has_component_in_children(area, "Collectable"):
return
if not collectables_to_pickup.has(area):
collectables_to_pickup.append(area)
func move_collectable_to_root(collectable: Node2D) -> void:
if not is_instance_valid(collectable):
return
var direction: Vector2 = (root.global_position - collectable.global_position).normalized()
var speed: float = direction.length() / magnetic_move_duration
collectable.global_position += direction.normalized() * speed