【Godot】让节点根据 Path2D 移动

Godot 3.5

根据 Path2D 路径移动,可以设置多个节点,类似于 Tween 节点,调用 play 方法进行移动节点

在这里插入图片描述

  • play(节点,移动到终点所需时间)
#============================================================
#	节点路径移动
#============================================================
# * 调用 set_custom_move_method 方法设置自定义移动方式
# * 默认移动方式为下面的 _default_method 方法,默认以 Control 类型
#============================================================
# @datetime: 2022-5-5 21:19:33
#============================================================
tool
class_name PathMove
extends Node


## 按照这个 Path2D 移动
export var path : NodePath setget set_path 


var _path_player : _PathPlayer = _PathPlayer.new()


#============================================================
#   Set/Get
#============================================================
func set_path(value: NodePath) -> void:
	path = value
	update_configuration_warning()


##  设置自定义移动的方法
## (这个方法必须有两个参数,第一个接收 Control 类型的节点,第二个为路径上的位置 Vector2D 类型)
## @object  
## @method  
func set_custom_move_method(object: Object, method: String):
	_path_player.set_custom_move_method(object, method)


#============================================================
#   内置
#============================================================
func _enter_tree():
	_path_player._path = get_node_or_null(path) as Path2D

func _ready():
	if Engine.editor_hint:
		set_process(false)
	set_custom_move_method(self, "_default_method")

func _process(delta):
	_path_player._process(delta)


func _get_configuration_warning():
	if path == "":
		return "没有设置 path 属性"
	var p = get_node_or_null(path)
	if not p is Path2D:
		return "选中的节点必须是 Path2D 类型的节点"
	return ""


#============================================================
#   自定义
#============================================================
## 默认移动方式
func _default_method(host, pos: Vector2):
	if host is Node2D:
		host.global_position = pos
	elif host is Control:
		host.rect_global_position = pos


##  播放节点移动
## @object  
## @time  
func play(object: Control, time: float):
	_path_player.add_task(object, time)



#============================================================
#   路径动画播放器
#============================================================
class _PathPlayer:
	
	var _tasks = {}
	var _path : Path2D
	var _func : FuncRef
	
	func set_custom_move_method(object: Object, method: String):
		_func = funcref(object, method)
	
	func _process(delta: float):
		for task in _tasks:
			if task.execute(delta):
				_tasks.erase(task)
	
	func add_task(object: Node, time: float):
		var task = Task.new(_path, time)
		task._host = object
		task._curve = _path.curve
		task._func = _func
		_tasks[task] = null
	
	class Task:
		var _host : Node
		var _curve : Curve2D
		var _func : FuncRef
		
		var _length_step : float
		var _length_max : float
		var _length : float
		
		func _init(path: Path2D, time_max: float):
			_length_max = path.curve.get_baked_length()
			_length_step = _length_max / time_max
		
		func execute(delta: float):
			_length += _length_step * delta
			var pos : Vector2 = _curve.interpolate_baked(_length)
			_func.call_func(_host, pos)
			if _length >= _length_max:
				return true

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值