mirror of
https://gitee.com/freeyz/godot-mota.git
synced 2024-12-23 07:59:20 +08:00
54 lines
1.6 KiB
GDScript
54 lines
1.6 KiB
GDScript
tool
|
|
extends KinematicBody2D
|
|
|
|
class_name PlayerKinematicBody2D
|
|
|
|
export(NodePath) var navigation_tile_map_path : NodePath
|
|
|
|
var navigation_tile_map : NavigationTileMap
|
|
|
|
var navigation_rect : Rect2
|
|
|
|
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)
|
|
navigation_rect = navigation_tile_map.get_used_rect()
|
|
navigation_rect.position *= navigation_tile_map.cell_size
|
|
navigation_rect.size *= navigation_tile_map.cell_size
|
|
|
|
# 监听屏幕点击事件
|
|
func _input(event):
|
|
if event is InputEventMouseButton and event.is_pressed() and navigation_tile_map != null:
|
|
if not navigation_rect.has_point(event.position):
|
|
return
|
|
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
|
|
var result = move_and_collide(diff)
|
|
call_collider(result)
|
|
|
|
# 调用被碰撞对象的方法
|
|
func call_collider(collision:KinematicCollision2D):
|
|
if collision == null or collision.collider == null:
|
|
return
|
|
var collider = collision.collider
|
|
if collider is TileMap:
|
|
return
|
|
if collider.has_method("on_player_touched"):
|
|
collider.on_player_touched()
|