extends KinematicBody2D

# Variables
## Speed
export (int) var base_speed = 200 setget set_base_speed, get_base_speed # Character's speed (scalar)
export (int) var speed setget , get_speed # Character's speed (scalar)
var speed_mod = 1
var speed_ceil = 5
var speed_floor = .01
## Velocity
var can_move = true setget set_can_move, get_can_move
export (Vector2) var velocity = Vector2(0,0) setget set_velocity, get_velocity # Character's directional velocity (vector)
var internal_velocity = Vector2(0,0) setget set_internal_velocity, get_internal_velocity
var external_velocity = Vector2(0,0) setget set_external_velocity, get_external_velocity
var velocity_mod = 1

# Setters + Getters
func set_base_speed(new_speed):
	base_speed = new_speed
func get_base_speed():
	return base_speed
func set_can_move(new_bool):
	can_move = new_bool
func get_can_move():
	return can_move
func set_external_velocity(new_velocity):
	external_velocity = new_velocity
func get_external_velocity():
	return external_velocity
func get_gpos():
	return global_position
func set_internal_velocity(new_velocity):
	internal_velocity = new_velocity
func get_internal_velocity():
	if not get_can_move():
		return Vector2(0.0,0.0)
	return internal_velocity
func get_overlapping_areas():
	return $PersonalSpace.get_overlapping_areas()
func get_overlapping_bodies():
	return $PersonalSpace.get_overlapping_bodies()
func get_speed():
	if !get_can_move():
		return 0
	else:
		return base_speed * get_speed_mod()
func get_speed_mod():
	if speed_mod < speed_floor:
		return speed_floor
	elif speed_mod > speed_ceil:
		return speed_ceil
	else:
		return speed_mod
func set_velocity(new_velocity):
	velocity = new_velocity
func get_velocity():
	return get_internal_velocity().normalized()
func get_velocity_angle():
	return get_velocity().angle()
func get_velocity_length():
	return get_velocity().length()
func get_user():
	return get_parent()

# Logic

func _process(delta):
	var v = get_velocity() * get_speed() * delta * KnowledgeBase.logic_time
	move_and_collide(v)
	if v != Vector2(0,0):
		MessageBus.publish("moved", get_user())

#func _setup():
	#set_process(true)

#func _ready():
#	set_process(false)
