feat: 1. 武器自动根据最近的敌人旋转

2. 流程:player 初始化调用 add_weapon,更新weapon_container,weapon_punch在_process每一帧中监听,更新update_closest_target最近的敌人,然后rotate_to_target旋转到最近的敌人
This commit is contained in:
luke358
2026-05-19 16:44:31 +08:00
parent 707dadd9ed
commit cf8c5b90ff
3 changed files with 77 additions and 5 deletions
+2
View File
@@ -15,6 +15,8 @@ position = Vector2(21, 1)
scale = Vector2(2, 2)
collision_layer = 16
collision_mask = 8
monitoring = false
monitorable = false
[node name="CollisionShape2D" type="CollisionShape2D" parent="Sprite2D/HitboxComponent" index="0" unique_id=1064980236]
z_index = 1
+72
View File
@@ -16,11 +16,83 @@ var weapon_spread: float
func _ready() -> void:
atk_start_pos = sprite_2d.position
func _process(delta: float) -> void:
if not is_attacking:
if targets.size() > 0:
update_closest_target()
else:
closest_target = null
rotate_to_target()
func setup_weapon(_data: ItemWeapon) -> void:
self.data = _data
collision.shape.radius = data.stats.max_range
func use_weapon() -> void:
calculate_spread()
func rotate_to_target() -> void:
if is_attacking:
rotation = get_custom_rotation_to_target()
else:
rotation = get_rotation_target()
func get_custom_rotation_to_target() -> float:
if not closest_target or not is_instance_valid(closest_target):
return rotation
var rot := global_position.direction_to(closest_target.global_position).angle()
return rot + weapon_spread
func get_rotation_target() -> float:
if targets.size() == 0:
return get_idle_rotation()
var rot := global_position.direction_to(closest_target.global_position).angle()
return rot
func get_idle_rotation() -> float:
if Global.player.is_facing_right():
return 0
else:
return PI
func calculate_spread() -> void:
weapon_spread += randf_range(-1 + data.stats.accurary, 1- data.stats.accurary)
rotation += weapon_spread
func update_closest_target() -> void:
closest_target = get_closest_target()
func get_closest_target() -> Node2D:
# 没有敌人在攻击范围
if targets.size() == 0:
return
var closest_enemy := targets[0]
var closest_distance := global_position.distance_to(closest_enemy.global_position)
for i in range(1, targets.size()):
var target: Enemy = targets[i]
var distance := global_position.distance_to(target.global_position)
if distance < closest_distance:
closest_enemy = target
closest_distance = distance
return closest_enemy
func can_use_weapon() -> bool:
return cooldown_timer.is_stopped() and closest_target