class_name CollisionShape2D_StateReceiver
extends CollisionShape2D

## Standard vars for state receivers
export var debug_component: bool = false
export var callable_state_machine :NodePath
var request_state_change: FuncRef
onready var current_state :StateAnimatedActor

## return to default when no state function 
export var default_shape_when_no_state_function :bool = true
export var disable_shape_when_no_state_function :bool = false

# Declare member variables here. Examples:
# var a = 2
# var b = "text
## Generic parent ref node, can be kinematic2d area, etc.
var parent

var default_shape :Shape2D
var default_transform :Transform2D


# Called when the node enters the scene tree for the first time.
func _ready():
	## Standard ready routines for state receivers
	request_state_change = funcref(get_node(callable_state_machine), 'change_to_known_state')
	## Connect signal to get alerted of state change
	get_node(callable_state_machine).connect("state_changed", self,"_on_state_change")
	## A reference to the current state machine state
	current_state = get_node(callable_state_machine).current_state
	
	parent = get_parent()
	
	default_shape = shape.duplicate()
	##DONE: Are transforms primitives? Do they get passed by value?
	## Transforms are passed by value.
	default_transform = transform


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	#UiManager.debug_text = current_state.name
	## Stard state process routines
	if has_method('_state_physics_process_' + current_state.name):
		call('_state_physics_process_' + current_state.name)

## Standard function for receiving state change
func _on_state_change(old_state_name:String, new_state :State):
	###### State machine
	if new_state is StateAnimatedActor: #Testing this. Update: It works
		## Confirmed that I do neet to update the current_state with the passed state.
		current_state = new_state
		if has_method('_on_state_change_' + current_state.name):
			call('_on_state_change_' + current_state.name)
		else:
			if default_shape_when_no_state_function:
				if debug_component:
					print (get_path(), " Returning to default collision shape")
				if shape is RectangleShape2D:
					shape.extents = default_shape.extents
				transform = default_transform
			if disable_shape_when_no_state_function:
				set_deferred("disabled", true)
	else:
		push_warning("Received non animated Actor state.")
	
