godot-plateformer/addons/reedscene/prop/ReedPropEffect.gd

72 lines
2.0 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

''' 此Resource定义了一系列的Prop可能需要的预设的简单Effect如果有更复杂的需求应该重写State的OnEnter
'''
@tool
@icon("uid://cw1yyc4oeph85")
extends Resource
class_name ReedPropEffect
enum TargetType {
OWNER,
ChildNode,
NODE_PATH
}
enum EffectType {
SET_VALUE,
CALL_FUNC
}
##作用目标的类型默认为PropComp的拥有者
@export var target_type: TargetType = TargetType.OWNER
##如果选择了NodePath则会读取此字段默认不读取
@export var target_path: NodePath
##如果选择了ChildNode则会读取此字段默认不读取
@export var child_node_name: StringName
##如果选择了ChildNode则会读取此字段是否要递归的查找某个子节点
@export var recursive_found: bool = false
##如果选择了ChildNode则会读取此字段只检查该scene的子节点而忽略其他的外部添加的子节点
@export var owned_node_only: bool = true
##作用的具体效果类别,默认为设置某个数值
@export var effect_type: EffectType = EffectType.SET_VALUE
## SetValue 用
@export var property_name: StringName
@export var value: Variant
## CallFunc 用
@export var func_name: StringName
@export var func_args: Array[Variant] = []
## 是否在退出 state 时恢复
@export var restore_on_exit: bool = false
## 运行时缓存
var _cached_old_value: Variant
##应用效果
func apply(owner: Node, prop_comp: Node, ctx: Dictionary) -> void:
var target := _resolve_target(owner, prop_comp)
if target == null:
return
match effect_type:
EffectType.SET_VALUE:
target.set(property_name, value)
EffectType.CALL_FUNC:
if target.has_method(func_name):
target.callv(func_name, func_args)
##获取到组件或对象
func _resolve_target(owner: Node, prop_comp: Node) -> Object:
match target_type:
TargetType.OWNER:
return owner
TargetType.ChildNode:
if owner:
return owner.find_child(child_node_name,recursive_found,owned_node_only)
TargetType.NODE_PATH:
if owner:
return owner.get_node_or_null(target_path)
return null