extends Area2D

var Spell_Fireball = preload("res://src/Spell_Fireball.gd")
var Spell_Ice = preload("res://src/Spell_Ice.gd")
var Spell_Storm = preload("res://src/Spell_Storm.gd")
var Spell_Sacrifice = preload("res://src/Spell_Sacrifice.gd")

var UTIL = preload("res://src/Utility.gd")

var dir = Vector2()
var tex_f = preload("res://images/wizard/wizard_front.png")
var tex_b = preload("res://images/wizard/wizard_back.png")
var tex_l = preload("res://images/wizard/wizard_left.png")
var tex_r = preload("res://images/wizard/wizard_right.png")
var tex_fr = preload("res://images/wizard/wizard_fr.png")
var tex_fl = preload("res://images/wizard/wizard_fl.png")
var tex_br = preload("res://images/wizard/wizard_br.png")
var tex_bl = preload("res://images/wizard/wizard_bl.png")

var spr
var spells = [null, null, null, null]
var sacrifice
var lambs = 5
var mutton = 0
var busy = false


var overlay

var spellPlayers = []


func _init(pos):
  position.x = pos.x
  position.y = pos.y
  # APPEARANCE
  spr = Sprite.new()
  spr.set_texture(tex_f)
  spr.scale = Vector2(2.0, 2.0)
  add_child(spr)
  spr.offset = Vector2(0, -0.5 * tex_f.get_height())
  # COLLISION
  var my_shape = RectangleShape2D.new()
  my_shape.set_extents(spr.scale * Vector2(spr.texture.get_size().x / 4, spr.texture.get_size().y / 4))
  var collision = CollisionShape2D.new()
  collision.set_shape(my_shape)
  collision.set_position(spr.scale * spr.offset * Vector2(1.0, 0.5))
  add_child(collision)
  sacrifice = Spell_Sacrifice.new()
  sacrifice.setSlot(4)
  add_child(sacrifice)

func _ready():
  overlay = $"/root/World/UI/overlay"
  # SPELLS
  refreshSpells()
#  for idx in range(4):
#    var spell = spells[idx]
#    overlay.updateSpellTexture(idx, spell)
  
  for idx in range(5):
    var asp = AudioStreamPlayer.new()
    spellPlayers.append(asp)
    self.add_child(asp)


func _process(delta):
  _face_direction()
  _do_actions()
    

func _face_direction():
  dir.x = 0
  dir.y = 0
  if Input.is_action_pressed("face_down"):
    dir.y += 1
  if Input.is_action_pressed("face_up"):
    dir.y -= 1
  if Input.is_action_pressed("face_left"):
    dir.x -= 1
  if Input.is_action_pressed("face_right"):
    dir.x += 1
  if dir.x == 0 and dir.y == 0:
    dir.y = 1 # by default, look down
  # Set actual facing direction
  if dir.x == -1:
    if dir.y == -1:
#      rotation = 3*PI/4
      spr.set_texture(tex_bl)
    elif dir.y == 1:
#      rotation = PI/4
      spr.set_texture(tex_fl)
    else:
#      rotation = PI/2
      spr.set_texture(tex_l)
  elif dir.x == 1:
    if dir.y == -1:
#      rotation = -3*PI/4
      spr.set_texture(tex_br)
    elif dir.y == 1:
#      rotation = -PI/4
      spr.set_texture(tex_fr)
    else:
#      rotation = -PI/2
      spr.set_texture(tex_r)
  else:
    if dir.y == -1:
#      rotation = PI
      spr.set_texture(tex_b)
    elif dir.y == 1:
      spr.set_texture(tex_f)
      rotation = 0
    else:
      print("dir.y should be non-zero if dir.x is zero!")
      
func _do_actions():
  if not busy:
    if Input.is_action_just_pressed("spell1"):
      busy = spells[0].startCasting()
    if Input.is_action_just_pressed("spell2"):
      busy = spells[1].startCasting()
    if Input.is_action_just_pressed("spell3"):
      busy = spells[2].startCasting()
    if Input.is_action_just_pressed("spell4"):
      busy = spells[3].startCasting()
    if Input.is_action_just_pressed("sacrifice") and lambs > 0:
      busy = sacrifice.startCasting()
      
  if Input.is_action_just_released("spell1") and spells[0].casting:
      spells[0].endCasting()
      busy = false
  if Input.is_action_just_released("spell2") and spells[1].casting:
      spells[1].endCasting()
      busy = false
  if Input.is_action_just_released("spell3") and spells[2].casting:
      spells[2].endCasting()
      busy = false
  if Input.is_action_just_released("spell4") and spells[3].casting:
      spells[3].endCasting()
      busy = false
  if Input.is_action_just_released("sacrifice") and sacrifice.casting:
      sacrifice.endCasting()
      busy = false

func refreshSpells():
  for idx in range(4):
    var randSpell = randf()
    var spell
    if randSpell < 0.4:
      spell = Spell_Fireball.new()
    elif randSpell < 0.6:
      spell = Spell_Ice.new()
    else:
      spell = Spell_Storm.new()
    spell.setSlot(idx)
    add_child(spell)
  #  spells.append(spell)
    spells[idx] = spell
    overlay.updateSpellTexture(idx, spell)

func add_lamb():
  lambs += 1
  overlay.sheepLive.setSheepCount(lambs)
  
func slaughter_lamb():
  lambs -= 1
  # top up spells
  refreshSpells()
  overlay.sheepLive.setSheepCount(lambs)
  mutton += 1
  overlay.sheepDead.setSheepCount(mutton)
  $"/root/World".setBg(mutton)
  
  if mutton >= $"/root/World".winCount:
    $"/root/World".win()
  if mutton > 30: