95 lines
3.0 KiB
GDScript
95 lines
3.0 KiB
GDScript
class_name SpawnHookComponet extends ComponentBase
|
||
|
||
##SpawnHookComponent必须绑定一个相关的RayCast2D,用来控制射线想要检测的位置。
|
||
@export var binded_hook_ray_cast_2d : RayCast2D
|
||
##可以通過在這裏添加一個ComponentOwner默認的字段名來讀取輸入方向,默認是為空的。
|
||
@export var binded_hook_move_input_property_name: StringName
|
||
|
||
##用於Grapping Hook的基本Instance
|
||
const GRAPPING_HOOK = preload("uid://ddwoxlqluxiq5")
|
||
|
||
var _ray_direction: Vector2
|
||
var _ray_reference: RayCast2D
|
||
var _ray_length: float = 100
|
||
|
||
##當前的Graphook的實例
|
||
var _current_grap_hook_inst: Hook
|
||
|
||
##钩爪伸长达到动画位置
|
||
signal hook_reach_finished(reach_limit: float, anchor: Node2D)
|
||
|
||
##生成一个Grap Hook
|
||
func spawn_grap_hook_inst(dir: Vector2) -> Hook:
|
||
|
||
var i = GRAPPING_HOOK.instantiate() as Hook
|
||
i.init(self,true)
|
||
get_tree().current_scene.add_child(i)
|
||
i.start_stretching(dir)
|
||
_current_grap_hook_inst = i
|
||
|
||
i.stretching_finished.connect(_on_hook_stretching_end)
|
||
return _current_grap_hook_inst
|
||
|
||
##尝试关闭hook的延长
|
||
func suspend_hook_stretching(force_suspend: bool = false) -> bool:
|
||
if not _current_grap_hook_inst: return false
|
||
return _current_grap_hook_inst.end_stretching(force_suspend)
|
||
|
||
##当Hook stretching 关闭的时候,触发。
|
||
func _on_hook_stretching_end(reach_limit,anchor:Node2D) -> void:
|
||
hook_reach_finished.emit(reach_limit,anchor)
|
||
|
||
##从当前的hook获取到hook的anchor
|
||
func get_current_hook_anchor() -> Node2D:
|
||
return _current_grap_hook_inst._anchor
|
||
|
||
#func grap_reach_target(duration: float) -> bool:
|
||
#if not _ray_reference or not _current_grap_hook_inst:return false
|
||
#
|
||
###重置所有Points的坐标点位置。
|
||
#_current_grap_hook_inst.reset_line_points_to_target(component_owner.global_position)
|
||
#
|
||
#var start_pos: Vector2 = component_owner.global_position
|
||
#var target_pos: Vector2 = _ray_reference.get_collision_point()
|
||
#
|
||
#var t := get_tree().create_tween()
|
||
#t.set_trans(Tween.TRANS_SINE)
|
||
#t.set_ease(Tween.EASE_OUT)
|
||
#
|
||
#t.tween_method(
|
||
#_current_grap_hook_inst.update_line_anchor_pos,
|
||
#start_pos,
|
||
#target_pos,
|
||
#duration
|
||
#)
|
||
#
|
||
###结束时会发送广播
|
||
#t.finished.connect(_on_hook_reach_finished)
|
||
#
|
||
#return true
|
||
#
|
||
###更新GrapHook的物理
|
||
#func update_grap_physics(input: Vector2,delta: float) -> void:
|
||
#if input.is_zero_approx(): return
|
||
#if not _current_grap_hook_inst: return
|
||
#
|
||
#var i = input.normalized()
|
||
#_current_grap_hook_inst.move_hook(input,delta)
|
||
#
|
||
###此函數默認為Vector.Zeor,子類可以重寫以得到一個全新的結果,也可以通過綁定的方式獲得一個結果。
|
||
#func get_grap_move_input() -> Vector2:
|
||
#
|
||
##如果我們在組件裏綁定了Component上的一個字段,也可以直接獲取。
|
||
#if binded_hook_move_input_property_name in component_owner:
|
||
#return component_owner.get(binded_hook_move_input_property_name)
|
||
#
|
||
#return Vector2.ZERO
|
||
#
|
||
#func _on_hook_reach_finished() -> void:
|
||
#_current_grap_hook_inst.m_update_rope_with_nodes = true
|
||
#hook_reach_finished.emit()
|
||
#
|
||
|
||
func _init_component() -> void:
|
||
pass
|