2020-07-21 20:34:34 +08:00
|
|
|
tool
|
|
|
|
extends KinematicBody2D
|
|
|
|
|
|
|
|
class_name PlayerKinematicBody2D
|
|
|
|
|
|
|
|
export(NodePath) var navigation_tile_map_path : NodePath
|
|
|
|
|
|
|
|
var navigation_tile_map : NavigationTileMap
|
|
|
|
|
2020-07-23 13:20:45 +08:00
|
|
|
var navigation_rect : Rect2
|
|
|
|
|
2020-07-21 20:34:34 +08:00
|
|
|
onready var array_tween : ArrayTween = $ArrayTween
|
|
|
|
|
|
|
|
onready var animation_tree : AnimationTree = $AnimationTree
|
|
|
|
|
|
|
|
# 初始化导航相关数据
|
|
|
|
func _ready():
|
|
|
|
if not navigation_tile_map_path.is_empty():
|
|
|
|
navigation_tile_map = get_node(navigation_tile_map_path)
|
2020-07-23 13:20:45 +08:00
|
|
|
navigation_rect = navigation_tile_map.get_used_rect()
|
|
|
|
navigation_rect.position *= navigation_tile_map.cell_size
|
|
|
|
navigation_rect.size *= navigation_tile_map.cell_size
|
2020-07-21 20:34:34 +08:00
|
|
|
|
|
|
|
# 监听屏幕点击事件
|
|
|
|
func _input(event):
|
|
|
|
if event is InputEventMouseButton and event.is_pressed() and navigation_tile_map != null:
|
2020-07-23 13:20:45 +08:00
|
|
|
if not navigation_rect.has_point(event.position):
|
|
|
|
return
|
2020-07-21 20:34:34 +08:00
|
|
|
var astar = navigation_tile_map.astar
|
|
|
|
var start_id = astar.get_closest_point(position)
|
|
|
|
var end_id = astar.get_closest_point(event.position)
|
|
|
|
var paths = astar.get_point_path(start_id,end_id)
|
|
|
|
animation_tree.active = true
|
|
|
|
array_tween.interpolate_array(paths)
|
|
|
|
|
|
|
|
func _on_ArrayTween_array_completed():
|
|
|
|
animation_tree.active = false
|
|
|
|
|
|
|
|
func _on_ArrayTween_array_value_step(step):
|
|
|
|
var diff = step - position
|
|
|
|
animation_tree["parameters/player/blend_position"] = diff
|
|
|
|
move_and_collide(diff)
|
|
|
|
get_viewport().get_texture()
|