feat: 1. 武器自动根据最近的敌人旋转
2. 流程:player 初始化调用 add_weapon,更新weapon_container,weapon_punch在_process每一帧中监听,更新update_closest_target最近的敌人,然后rotate_to_target旋转到最近的敌人
This commit is contained in:
@@ -24,11 +24,6 @@ func _ready() -> void:
|
|||||||
dash_cooldown_timer.wait_time = dash_cooldown
|
dash_cooldown_timer.wait_time = dash_cooldown
|
||||||
|
|
||||||
add_weapon(preload("uid://murcuuks1j8l"))
|
add_weapon(preload("uid://murcuuks1j8l"))
|
||||||
add_weapon(preload("uid://murcuuks1j8l"))
|
|
||||||
add_weapon(preload("uid://murcuuks1j8l"))
|
|
||||||
add_weapon(preload("uid://murcuuks1j8l"))
|
|
||||||
add_weapon(preload("uid://murcuuks1j8l"))
|
|
||||||
add_weapon(preload("uid://murcuuks1j8l"))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -89,6 +84,9 @@ func can_dash() -> bool:
|
|||||||
Input.is_action_just_pressed("dash") and \
|
Input.is_action_just_pressed("dash") and \
|
||||||
move_dir != Vector2.ZERO
|
move_dir != Vector2.ZERO
|
||||||
|
|
||||||
|
func is_facing_right() -> bool:
|
||||||
|
return visuals.scale.x == -0.5
|
||||||
|
|
||||||
func _on_dash_timer_timeout() -> void:
|
func _on_dash_timer_timeout() -> void:
|
||||||
is_dashing = false
|
is_dashing = false
|
||||||
visuals.modulate.a = 1.0
|
visuals.modulate.a = 1.0
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ position = Vector2(21, 1)
|
|||||||
scale = Vector2(2, 2)
|
scale = Vector2(2, 2)
|
||||||
collision_layer = 16
|
collision_layer = 16
|
||||||
collision_mask = 8
|
collision_mask = 8
|
||||||
|
monitoring = false
|
||||||
|
monitorable = false
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Sprite2D/HitboxComponent" index="0" unique_id=1064980236]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Sprite2D/HitboxComponent" index="0" unique_id=1064980236]
|
||||||
z_index = 1
|
z_index = 1
|
||||||
|
|||||||
@@ -16,11 +16,83 @@ var weapon_spread: float
|
|||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
atk_start_pos = sprite_2d.position
|
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:
|
func setup_weapon(_data: ItemWeapon) -> void:
|
||||||
self.data = _data
|
self.data = _data
|
||||||
collision.shape.radius = data.stats.max_range
|
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:
|
func can_use_weapon() -> bool:
|
||||||
return cooldown_timer.is_stopped() and closest_target
|
return cooldown_timer.is_stopped() and closest_target
|
||||||
|
|||||||
Reference in New Issue
Block a user