## freeftf
## Copyright (C) 2024  Patrick_Pluto
##
## This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along with this program.  If not, see <https://www.gnu.org/licenses/>.

extends CharacterBody3D


var jump_velocity = 4.5
var speed = 7
var zoom = 0
var player_no
var npc = false
var enabled = false
var caught = false
var captured_by
var position_pre
var got_person = false
var caught_body
var beast = false
var mouse_locked = true
var is_frozen = false
var hp = 100
var menu = preload("res://menus/game_menu.tscn")


var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready():
	player_no = Game.players
	$nametag.text = Game.player_list[Game.player_list.keys()[player_no]]
	Game.players += 1
	if Game.player_list.keys()[player_no] == multiplayer.get_unique_id():
		if Game.settings["fps_counter"] == 1:
			$fps_counter.show()
		$health.show()
		enabled = true
		$cam_y/Camera3D.current = true
		$nametag.hide()
	else:
		npc = true

func _physics_process(delta):
	if enabled:
		
		if Input.is_action_just_pressed("jump") and is_on_floor() and $jump_timeout.is_stopped():
			if beast:
				speed -= 5
				$jump_timeout.start()
			velocity.y = jump_velocity
		
		if not is_on_floor():
			velocity.y -= gravity * delta

			
		if Input.is_action_just_pressed("crouch") and !beast:
			if $collision.rotation.x == 0:
				$collision.rotation.x = deg_to_rad(-90)
				speed -= 3
				$cam_y.position.y = 0
			else :
				$collision.rotation.x = 0
				speed += 3
				$cam_y.position.y = 0.5
				position.y += 1
		
		if Input.is_action_pressed("click") and $hammer.rotation_degrees.x > 0:
			$hammer.rotation_degrees.x -= 5
			for id in Game.player_list:
				Client.rpc_id(id,"sync_hammer",name, $hammer.rotation)
		elif $hammer.rotation_degrees.x < 90 and !Input.is_action_pressed("click"):
			$hammer.rotation_degrees.x += 5
			for id in Game.player_list:
				Client.rpc_id(id,"sync_hammer",name, $hammer.rotation)
		
		var input_dir = Input.get_vector("left", "right", "forwards", "backwards")
		var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
		if direction:
			velocity.x = direction.x * speed
			velocity.z = direction.z * speed
		else:
			velocity.x = move_toward(velocity.x, 0, speed)
			velocity.z = move_toward(velocity.z, 0, speed)
		
		move_and_slide()
	
	if !npc:
		for id in Game.player_list:
				Client.rpc_id(id,"sync_player",name, position, rotation)
		if mouse_locked and !has_node("./game_menu"):
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		else:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		if Input.is_action_just_pressed("escape"):
			if has_node("./game_menu"):
				get_node("./game_menu").free()
				Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
			else:
				Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
				var instance = menu.instantiate()
				add_child(instance)
		if Input.is_action_just_pressed("mouse_lock"):
			mouse_locked = !mouse_locked
	
	
func _input(event):
	if !npc:
		
		if event is InputEventMouseMotion and (Input.is_action_pressed("cam_look") or $cam_y/Camera3D.position.z == 0) and !has_node("./game_menu"):
			var camera_rotation = event.relative * 0.01
			if $cam_y.rotation.x <= 1.6 && camera_rotation.y <= 0:
				$cam_y.rotate(Vector3.RIGHT, -camera_rotation.y)
			elif $cam_y.rotation.x >= -1.6 && camera_rotation.y >= 0:
				$cam_y.rotate(Vector3.RIGHT, -camera_rotation.y)
			if $cam_y.rotation.x >= 1.6:
				$cam_y.rotation.x = 1.6
			elif $cam_y.rotation.x <= -1.6:
				$cam_y.rotation.x = -1.6
			rotate(Vector3.DOWN, camera_rotation.x)

			
func beast_init():
	if !npc:
		Game.is_beast = true
	beast = true
	speed += 2
	position.y += 10
	$hammer/CSGBox3D/detect_hit.monitoring = true
	$hammer/CSGBox3D/detect_hit.monitorable = true
	$hammer/CSGBox3D/detect_hit/CollisionShape3D.disabled = false
	$hammer.show()
	$bag.show()

func _on_detect_hit_body_entered(body):
	if enabled and body is CharacterBody3D and !got_person:
		for id in Game.player_list:
				Client.rpc_id(id,"player_hit",body.name, name)

func captured(beast2):
	if !npc:
		$in_bag.visible = true
	$time_in_bag.start()
	visible = false
	enabled = false
	caught = true
	captured_by = beast2
	position_pre = position
	position = Vector3(10000, 10000, 10000)
	

func got_one(target):
	caught_body = target
	got_person = true
	$bag/CSGSphere3D.show()

func lost_one():
	got_person = false
	$bag/CSGSphere3D.hide()

func frozen():
	visible = true
	$in_bag.visible = false
	is_frozen = true
	enabled = false
	

func unfreeze():
	is_frozen = false
	if !npc:
		enabled = true
	caught = false

func _on_time_in_bag_timeout():
	visible = true
	if !npc:
		enabled = true
	caught = false
	captured_by.lost_one()
	captured_by = null
	position = position_pre
	$in_bag.visible = false


func _on_show_fps_timeout():
	$fps_counter.text = "FPS: "+str(Engine.get_frames_per_second())
	$health.text = "HP: "+str(hp)


func _on_jump_timeout_timeout():
	speed += 5