Chaos_Kitchen/Scripts/PlayerControler.gd

74 lines
1.9 KiB
GDScript

extends CharacterBody2D
class_name Player
@onready var gridcontrol: TileMap = $"../NavigationMap"
signal can_interact(object : Interactible)
signal remove_interact()
var current_path : Array[Vector2i]
var object_near : Interactible
var can_interact_flag : bool
var held_item : Item
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _physics_process(delta):
if current_path.is_empty():
return
var target_position = gridcontrol.map_to_local(current_path.front())
global_position = global_position.move_toward(target_position, 5)
if global_position == target_position:
current_path.pop_front()
func _unhandled_input(event):
var click_position = get_global_mouse_position()
if event.is_action_pressed("move_to"):
if gridcontrol.is_spot_walkable(click_position):
current_path = gridcontrol.astar.get_id_path(
gridcontrol.local_to_map(global_position),
gridcontrol.local_to_map(click_position)
).slice(1)
func _shortcut_input(event):
if event.is_action_pressed("interact") && can_interact_flag:
if object_near is ItemContainer :
if not object_near.items.is_empty() && held_item == null:
held_item = object_near.items[0]
object_near.pop_item(0)
_add_item_overhead()
else :
object_near.add_item(held_item)
remove_child(held_item)
remove_child(held_item.Icon)
held_item = null
func _on_can_interact(object : Interactible):
can_interact_flag = true
object_near = object
func _on_remove_interact():
can_interact_flag = false
object_near = null
func _add_item_overhead() :
add_child(held_item)
add_child(held_item.Icon)
held_item.Icon.set_scale(Vector2(0.5,0.5))
held_item.Icon.offset = Vector2i(0,-10)
held_item.Icon.modulate = Color(1,1,1,0.4)