extends Node

var shots_node
var particles_node
var axe_scene = preload("res://scenes/weapons/axe.tscn")
var knife_scene = preload("res://scenes/weapons/knife.tscn")
var particles_scene = preload("res://scenes/particles.tscn")
var gibs_scene = preload("res://scenes/gibs.tscn")

const GIB_COUNT = 8

var spawns = []
var player_nickname
var bot_amount
var frags_limit
var time_limit
var easy_bots
var level 

onready var slowmo = preload("res://scripts/slowmotion.gd").Slowmotion.new() 

func angle_between(a, b):
	return atan2(b.y - a.y, b.x - a.x)

func add_shot(character, target, weapon):
	assert(shots_node)
	assert(character)
	
	var shot = null
	var power = 0
	
	match weapon:
		"Knife":
			shot = knife_scene.instance()
			power = 600
		"Axe":
			shot = axe_scene.instance()
			power = 450
		_:
			return
			
	var pos = character.position
	var a = angle_between(pos, target)
	var v = Vector2(cos(a), sin(a))
	
	shot.parent = character
	shot.position = pos + v
	shot.rotation = a
	shot.set_collision_mask_bit(character.id, false)
	shot.apply_impulse(Vector2(), v * power)
	shot.angular_velocity = 30
	shots_node.add_child(shot)

func add_particles(name, pos, angle, flip_h = false):
	assert(particles_node)
	var particles = particles_scene.instance()
	particles.create(name, pos, angle, flip_h)
	particles_node.add_child(particles)

func add_gibs(pos, scatter):
	for i in GIB_COUNT:
		var gib = gibs_scene.instance()
		var angle = rand_range(60, 135)
		angle = deg2rad(angle)
		gib.angular_velocity = rand_range(20, 40)
		gib.position = pos + Vector2(rand_range(-scatter.x, scatter.x), rand_range(-scatter.y, scatter.y))
		var direction = Vector2(cos(angle), sin(angle))
		gib.apply_impulse(Vector2(), -direction * rand_range(50, 150))
		particles_node.add_child(gib)

func make_spawns(spawns_node):
	assert(spawns_node)
	spawns.clear()
	
	for spawn in spawns_node.get_children():
		spawns.append(spawn.position)

func get_spawn_point():
	return spawns[round(rand_range(0, spawns.size() - 1))]

func _process(delta):
	slowmo.update(delta)
	pass

