# -*- coding: utf-8 -*-

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from panda3d.core import *
from enemy import enemyList

#--- Globals variables------------------------------------
player = None
gap_z = [-4,4]
gap_x = [-1.5,1.5]
decal_z = 3.67
actions = {}
actions_buffer = {}
options =   {'look' : False,
            'fixe y' : True,
            'fixe z' : True}

#--- Functions--------------------------------------------

def init(p):
    global player
    player = p


    #position initial de la caméra
    base.cam.setPos(6,-20,decal_z)
    base.cam.lookAt(player.rootnp)

    #ajout de tâche
    
    taskMgr.add(updateAction, "update action")
    
    addControl('d','right',True)
    addControl('q','left',True)
    addControl('a','left',True)
    addControl('z','jump',False)
    addControl('w','jump',False)
    addControl('s','down',False)

def pushAction(key):
    actions_buffer[key] = actions[key]

def pullAction(key):
    act  = actions_buffer.pop(key)
    player.moveList =  [ml for ml in player.moveList if ml != act[0]]

def updateAction(task):

    for action in actions_buffer.values():
        if action[0] not in player.moveList: 
            player.moveList.append(action[0])

    player.move()

    if options['fixe y']:
        player.rootnp.setY(-0.1)

    if not player.planar:
        if player.rootnp.getZ() < 2:
            player.rootnp.setY(-1.0)
        elif player.rootnp.getZ() >= 2 and player.rootnp.getZ() < 3.1:
            dz = 3.1-2
            dy = 0.9
            frac = (player.rootnp.getZ()-2.0) / dz 
            player.rootnp.setY(-1 + frac * dy)
        elif player.rootnp.getZ() >= 3.1 and player.rootnp.getZ() < 4.1:
            dz = 4.1-3.1
            dy = 2.16
            frac = (player.rootnp.getZ()-3.1) / dz 
            player.rootnp.setY(-0.1 + frac * dy)
        elif player.rootnp.getZ() >= 4.1 and player.rootnp.getZ() < 6.5:
            dz = 6.5-4.1
            dy = 3.0 - 1.96
            frac = (player.rootnp.getZ()-4.1) / dz 
            player.rootnp.setY(1.96 + frac * dy)

    for e in enemyList:
        e.move()

    updateCam()

    for key,action in actions_buffer.items():
        if not action[1]:
            pullAction(key)

    return Task.cont

def updateCam():

    #--- Axe X -------------------------------------------

    px = player.rootnp.getX(base.cam)

    if px < gap_x[0]:

        base.cam.setX(player.rootnp.getX() - gap_x[0])
        if base.cam.getX() < 6:
            base.cam.setX(6)

    elif px > gap_x[1]:
        base.cam.setX(player.rootnp.getX() - gap_x[1])
        if base.cam.getX() > player.limit-6 and player.star > 0:
            base.cam.setX(player.limit-6)

    else:
        pass

    #--- Axe Y -------------------------------------------


    #--- Axe Z -------------------------------------------

    
        pz = player.rootnp.getZ()

        if not options['fixe z']:
            base.cam.setZ(player.rootnp.getZ()  + decal_z-2)
        else:
            pass

        if player.rootnp.getZ() < decal_z:
            base.cam.setZ(decal_z)

        if options['look']:
            base.cam.lookAt(player.rootnp)
        else:
            base.cam.setHpr(0,0,0)


    

def addControl(key,action,hold=False):
    
    actions[key] = [action,hold]
    base.accept(key,pushAction,[key])

    if hold:
        base.accept(key + '-up',pullAction,[key])






