#
# Copyright (C) 2018  Sage Vaillancourt, sagev9000@gmail.com
#
# This file is part of Fronter.
# 
# Fronter 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.
# 
# Fronter 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 Fronter.  If not, see <http://www.gnu.org/licenses/>.
#

extends Area2D

# signals
signal update_display # tells parent to update points display
signal refund # will tell parent a refund is occuring, for display
signal shooting_speed_upgrade # signals that a shooting speed upgrade has occured
signal other_shooting_upgrade
signal double_laser_upgrade
signal other_ship_color_change
signal other_ship_enable_rainbow
signal shooting_speed_fully_upgraded
signal restart_game
signal multiplayer_movement

export (PackedScene) var Laser

################################
# THINGS THAT MAY NEED ADJUSTING
#################################

var ship_speed = 500

# first 5 tiers of shooting speed
const BULLET_DELAY_TIER1 = 0.8
const BULLET_DELAY_TIER2 = 0.5
const BULLET_DELAY_TIER3 = 0.2
const BULLET_DELAY_TIER4 = 0.1
const BULLET_DELAY_TIER5 = 0.05

# starting cost of shooting speed upgrades after tier 5
const SHOOTING_SPEED_UPGRADE_DEFAULT = 1500

# can't shoot faster than this
const BULLET_DELAY_MINIMUM = 0.01

# costs of shooting speed upgrades
const BULLET_DELAY_TIER2_COST = 100
const BULLET_DELAY_TIER3_COST = 200
const BULLET_DELAY_TIER4_COST = 400
const BULLET_DELAY_TIER5_COST = 1000

# double laser cost
const DOUBLE_LASER_COST = 2000

# 5 tiers of laser damage
const LASER_DAMAGE_TIER1 = 10
const LASER_DAMAGE_TIER2 = 12
const LASER_DAMAGE_TIER3 = 15
const LASER_DAMAGE_TIER4 = 20
const LASER_DAMAGE_TIER5 = 50

# costs of laser damage upgrades
const LASER_DAMAGE_TIER2_COST = 200
const LASER_DAMAGE_TIER3_COST = 500
const LASER_DAMAGE_TIER4_COST = 1000
const LASER_DAMAGE_TIER5_COST = 2000

# the ship's starting position
const STARTING_POSITION = Vector2(130, 250)

# the amount of money the player starts with
const STARTING_MONEY = 100

#################################

# set sliding shooting speed upgrade costs
var shooting_speed_upgrade = SHOOTING_SPEED_UPGRADE_DEFAULT

# used to limit player movement
var screensize

# can the player shoot at this instant
var can_shoot = false

# timer for shooting speed
var timer = null

# sets the delay between shots to the default
var bullet_delay = BULLET_DELAY_TIER1
var bullet_delay_tier = 0

# sets other shooting settings to their defaults
var laser_penetration = 0
var double_laser = false
var laser_damage = LASER_DAMAGE_TIER1

# gives the player their starting points
var money = STARTING_MONEY

# sets initial value of ship, to be added to with upgrades for refunds
var ship_value = 0

# what percentage of points should be returned on refunding
var refund_percentage = 1

# is a menu open? try to ignore input in-game
# only really relevant to touchscreens
var menu_open = false

# opens the upgrade menu
func upgradeMenu():
	menu_open = true
	var upgmenu = preload("res://UpgradeMenu.tscn").instance()
	add_child(upgmenu)
	upgmenu.connect("refund", self, "_refund_button")
	upgmenu.connect("bullet_delay_upgrade", self, "upgradeBulletDelay_button")
	upgmenu.connect("double_laser_upgrade", self, "doubleLaserUpgrade_button")
	upgmenu.connect("laser_penetration_upgrade", self, "laserPenetrationUpgrade_button")
	upgmenu.connect("menu_closed", self, "menuClosed")
	upgmenu.connect("change_color", self, "changeColor")
	upgmenu.connect("taste_the_rainbow", self, "enable_the_rainbow")
	upgmenu.bullet_delay_tier = bullet_delay_tier
	if !get_tree().has_network_peer():
		get_tree().paused = true

# signalled on close of UpgradeMenu
func menuClosed():
	menu_open = false

#######################
# REQUESTING UPGRADES #
#######################
# all will request their upgrade
# all will pause the game if not in multiplayer

func doubleLaserUpgrade_button():
	doubleLaserUpgrade()
	emit_signal("update_display")
	if !get_tree().has_network_peer():
		get_tree().paused = true

func laserPenetrationUpgrade_button():
	laserPenetrationUpgrade()
	emit_signal("update_display")
	if !get_tree().has_network_peer():
		get_tree().paused = true

func upgradeShipSpeed_button():
	upgradeShipSpeed()
	emit_signal("update_display")
	if !get_tree().has_network_peer():
		get_tree().paused = true

func upgradeBulletDelay_button():
	upgradeBulletDelay()
	emit_signal("update_display")
	if !get_tree().has_network_peer():
		get_tree().paused = true

#######################


######################
# PROVIDING UPGRADES #
######################
# checks which tier the player is already on, and if they have enough points
# upgrades and charges the player, if possible
# increases the ship value by the amount spent, for refunds
# sends an rpc message about current state for a given upgrade

# upgrades shooting speed
func upgradeBulletDelay():
	if (bullet_delay == BULLET_DELAY_TIER1 && money >= BULLET_DELAY_TIER2_COST):
		bullet_delay = BULLET_DELAY_TIER2
		money -= BULLET_DELAY_TIER2_COST
		ship_value += BULLET_DELAY_TIER2_COST
		bullet_delay_tier = 1
	elif (bullet_delay == BULLET_DELAY_TIER2 && money >= BULLET_DELAY_TIER3_COST):
		bullet_delay = BULLET_DELAY_TIER3
		money -= BULLET_DELAY_TIER3_COST
		ship_value += BULLET_DELAY_TIER3_COST
		bullet_delay_tier = 2
	elif (bullet_delay == BULLET_DELAY_TIER3 && money >= BULLET_DELAY_TIER4_COST):
		bullet_delay = BULLET_DELAY_TIER4
		money -= BULLET_DELAY_TIER4_COST
		ship_value += BULLET_DELAY_TIER4_COST
		bullet_delay_tier = 3
	elif (bullet_delay == BULLET_DELAY_TIER4 && money >= BULLET_DELAY_TIER5_COST):
		bullet_delay = BULLET_DELAY_TIER5
		money -= BULLET_DELAY_TIER5_COST
		ship_value += BULLET_DELAY_TIER5_COST
		bullet_delay_tier = 4
#	elif (bullet_delay <= BULLET_DELAY_MINIMUM):
#		emit_signal("bullet_delay_fully_upgraded")
#	elif (bullet_delay <= BULLET_DELAY_TIER5 && money >= shooting_speed_upgrade):
#		bullet_delay = bullet_delay*0.95
#		money -= shooting_speed_upgrade
#		ship_value += shooting_speed_upgrade
#		shooting_speed_upgrade *= 1.1
#		bullet_delay_tier += 1
	
	timer.set_wait_time(bullet_delay)
	rpc("other_shooting_speed_upgrade", get_tree().get_network_unique_id(), bullet_delay)

func doubleLaserUpgrade():
	if money >= DOUBLE_LASER_COST && double_laser == false:
		money -= DOUBLE_LASER_COST
		double_laser = true
	rpc("double_laser_upgrade", get_tree().get_network_unique_id())

######################

func _ready():
	# for measuring time between lasers
	timer = Timer.new()
	timer.set_one_shot(true)
	timer.set_wait_time(bullet_delay)
	timer.connect("timeout", self, "on_timeout_complete")
	add_child(timer)
	timer.start()
	
	# set player's position to the default
	position = STARTING_POSITION
	
	# sets screensize to prevent player leaving screen
	screensize = get_viewport_rect().size
	
	# starts the ship's rocketing animation
	$AnimatedSprite.play()

# resets all upgrades to the default
# refunds them with adjustable percentage returned
func _refund_button():
	bullet_delay = BULLET_DELAY_TIER1
	bullet_delay_tier = 0
	timer.set_wait_time(bullet_delay)
	laser_penetration = 0
	double_laser = false
	shooting_speed_upgrade = SHOOTING_SPEED_UPGRADE_DEFAULT
	money += ship_value*refund_percentage
	prints("Refunded ", ship_value*refund_percentage)
	ship_value = 0
	emit_signal("update_display")
	get_tree().paused = true

#Changes the ships color
func changeColor(color):
	$AnimatedSprite.modulate = color
	if get_tree().has_network_peer():
		rpc("_change_color", get_tree().get_network_unique_id(), color)

func on_timeout_complete():
	can_shoot = true

var is_shooting

func moveto(finger_position):
	if !menu_open:
		if (finger_position.x > position.x && finger_position.x < 800):
			velocity.x += 1
		if position.x > finger_position.x:
			velocity.x -= 1
		if (finger_position.y - 100 > position.y && finger_position.x < 800):
			velocity.y += 1
		if (position.y > finger_position.y - 100 && finger_position.x < 800):
			velocity.y -= 1


remote func move_player(id, position, is_shooting):
	emit_signal("multiplayer_movement", id, position, is_shooting)

remote func other_shooting_speed_upgrade(id, bullet_delay):
	emit_signal("other_shooting_upgrade", id, bullet_delay)

remote func _change_color(id, color):
	emit_signal("other_ship_color_change", id, color)

remote func _enable_rainbow(id):
	emit_signal("other_ship_enable_rainbow", id)

remote func double_laser_upgrade(id):
	emit_signal("double_laser_upgrade", id)

# the player's movement vector
var velocity = Vector2()

var rainbow = Color(0,0,0,1)
var r_up = true
var g_up = true
var b_up = true
var rainbow_speed = 30
var rainbow_is_on = false

func _process(delta):
	# move player with keyboard
	velocity = Vector2()
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1
	if (velocity.length() > 0):
		velocity = velocity.normalized() * ship_speed
	position += velocity * delta
	
	if rainbow_is_on:
		rainbow(delta)
	
	# prevents player leaving the screen
	position.x = clamp(position.x-100, 0, screensize.x-164) + 100
	position.y = clamp(position.y, 0, screensize.y)
	
	# basically exists just for rpc messages about whether or not the player is currently firing
	if can_shoot:
		is_shooting = false
	else:
		is_shooting = true
	
	# if in multiplayer mode, push position and shooting status as often as possible
	if get_tree().has_network_peer():
		rpc("move_player", get_tree().get_network_unique_id(), position, is_shooting)
	
	# if shoot button is pressed, try shooting
	if Input.is_action_pressed("ui_accept"):
		shoot()
	
	# ui_reset button opens the upgrade menu
	if Input.is_action_pressed("ui_reset"):
		upgradeMenu()

# spawn lasers to shoot
# lasers can be set to penetrate or not
# can fire two lasers at once if double_laser is on
func shoot():
	if can_shoot:
		var laser = Laser.instance()
		get_node("../").add_child(laser)
		laser.current_pen = laser_penetration
		laser.position.y = position.y - 27
		laser.position.x = position.x + 46
		laser.damage = laser_damage
		
		#MAYBE THE LASERS SHOULD BE THEIR OWN NODES
		#Would allow for more simple additions in the future
		if double_laser == true:
			var laser2 = Laser.instance()
			get_node("../").add_child(laser2)
			laser2.position.y = position.y + 28
			laser2.position.x = position.x + 46
			laser2.current_pen = laser_penetration
			
		# don't shoot again until the timer resets 
		can_shoot = false
		timer.start()

# Funky colors #
# NOT CURRENTLY REFUNDABLE #
func enable_the_rainbow():
	if money >= 2000 && not rainbow_is_on:
		money -= 2000
		rainbow_is_on = true
		rpc("_enable_rainbow", get_tree().get_network_unique_id())

func rainbow(delta):
	if rainbow.r < 1 && r_up:
		rainbow.r += rainbow_speed*delta/10
		if rainbow.r >= 1: r_up = false
	else:
		rainbow.r -= rainbow_speed*delta/10
		if rainbow.r <= 0.2: r_up = true
	
	if rainbow.g < 1 && g_up:
		rainbow.g += rainbow_speed*delta/9
		if rainbow.g >= 1: g_up = false
	else:
		rainbow.g -= rainbow_speed*delta/9
		if rainbow.g <= 0.2: g_up = true
	
	if rainbow.b < 1 && b_up:
		rainbow.b += rainbow_speed*delta/8
		if rainbow.b >= 1: b_up = false
	else:
		rainbow.b -= rainbow_speed*delta/8
		if rainbow.b <= 0.2: b_up = true
	
	$AnimatedSprite.modulate = rainbow

# displays endgame screen and pauses
func gameOver():
	var gameover = preload("res://GameOver.tscn").instance()
	add_child(gameover)
	gameover.connect("restart", self, "restart_game")
#	gameover.connect("bullet_delay_upgrade", self, "upgradeBulletDelay_button")
	get_tree().paused = true

# uses refund function to remove upgrades
# sets points back to default
func restart_game():
	_refund_button()
	get_tree().paused = false
	money = STARTING_MONEY
	emit_signal("restart_game")