#zskcam
import bge
import mathutils
from mathutils import *
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner
def main():
    dict = bge.logic.globalDict 
    camempty = scene.objects['camEmpty.001']
    controlcube = scene.objects['control_cube.002']
    
    camCube = scene.objects['camCube']
    
    dropinCol = controlcube.sensors['dropinCol']
    LAST_GRIND = controlcube['LAST_GRIND']
    up = cont.sensors['up']
    ray = cont.sensors['up']
    down = cont.sensors['down']
    left = cont.sensors['left']
    right = cont.sensors['right']
    distance = 0.12
    cam = cont.actuators["Camera"]
    near = cont.sensors["Near"]
    move = own.actuators["move"]
    #match camera actuaotor
    cam_def_height = dict['cam_height'] #-.1 #-.6
    #cam_def_min = 1.6
    #cam_def_max = 2.4
    cam_def_min = dict['cam_min']
    cam_def_max = dict['cam_max']
    cam_height = cam.height 
    cam_min = cam.min   
    cam_max = cam.max
    lasty = controlcube['lasty']
    cam_moved = 0
    lastCamheight = controlcube['lastCamheight']
    raised = 0
    walk = controlcube['walk']
    cam.axis = 4
    ccheight = controlcube.worldPosition[2]
    camwpz = own.worldPosition[2]
    zdist = camwpz - ccheight
    
    cam1 = scene.objects['Camera.003']
    cam2 = scene.objects['freecam']
    cam3 = scene.objects['followcam']
    
    camList = scene.cameras
    freecam = camList["freecam"]     
    
    if 'init' not in own:
        own['init'] = 1
        own['last_move_x'] = 0
        own['last_move_y'] = 0
        own['last_move_z'] = 0
        own['last_rot_x'] = 0
        own['last_rot_y'] = 0
        own['last_rot_z'] = 0
        own['idlecampos_x'] = 0
        own['speed_mult'] = 1.00 
        controlcube['driving'] = False  
        dict['cur_ccH_min'] = dict['cam_walk_min']
        dict['cur_ccH_max'] = dict['cam_walk_max']
        cam.height = dict['cam_height']
    acam = scene.active_camera


    
    #if 1 == 1:
    #if own['playback'] == False:
    if controlcube['driving'] == False:
        if down.triggered == True and LAST_GRIND == False and walk == 0:
            hitPosition = down.hitPosition
            distance = own.getDistanceTo(hitPosition)
            cam_moved = 1       
            if distance < .2:
                camempty['hitdown'] = True
                cam.height = cam_height + .3
                cam.damping = .0 
                #print("raise cam")
            if zdist < -.2:
                cam.height = lastCamheight + .01
                cam.damping = .0
                raised = 1    
            if (distance > .4 and distance < .6) and zdist > -.2 and raised == 0:
                camheight2 = cam.height - .001
                if cam.height < .2: 
                    cam.height = camheight2
                    #print("slow lower")
            if distance >= .6 and zdist > -.2 and raised == 0:
                cam.height = cam.height - .02
                #print("slow lower2")           
        if down.triggered == False and LAST_GRIND == False and cam_moved == 0 and walk == 0:
            camempty['hitdown'] = False
            if cam_height > (cam_def_height + .06) and zdist > -.2 and raised == 0:
                cam.height = cam_height - .02
        if cam_height < -.6 and cam_moved == 0 and LAST_GRIND == False and zdist > -.2 and raised == 0 and walk == 0:
            cam_height = .1  
        if LAST_GRIND == True and walk == 0:
            if cam.height < -.5 or zdist < -.2:
                cam.height = cam.height + .013
            if cam.height >= -.5 and not down.triggered:
                pass
        
        if walk == 1:
            if dropinCol.positive == True and lasty == True:
                pass 
            else:
                pass   
        controlcube['lastCamheight'] = cam.height    
        #########
        #cam.min = dict['cam_min']
        #cam.max = dict['cam_max']
        cont.activate(own.actuators['Camera'])  
        ########
        
        if near.triggered == True and walk == 0:
            #print("near")
            if cam.min < 1:
                cam.min = cam.min + .1
                cam.max = cam.max + .1
            cam.height = cam_height + .01
            cam.damping = .001
            cont.activate(move)
            
        else:
            #print("far")
            cont.deactivate(move)
            if cam.min > cam_def_min:
                cam.min = cam.min - .05
                cam.max = cam.max - .05 
            cam.damping = .0          
        if cam.damping != 0 and down.triggered == False and near.triggered == False:
            cam.damping = 0.91             

        obj = cont.owner
        cube = controlcube
        to = cube
        from2 = obj
        distance = 0.0
        property = ""
        face = 1
        xray = 0
        poly = 0
        hit = obj.rayCast( to, from2, distance, property, face, xray, poly)
        control = "control_cube.002"
        hitobj = hit[0]
        hitobj = str(hitobj)
        if hitobj != control and walk == 0:
            cam.damping = .0 
            if cam.height < 3:
                cam.height = cam_height + .1
    
    if dict['menu_idle_timer'] > 300:
        move_len = 2048
        if own['idlecampos_x'] < move_len:
            own['idlecampos_x'] += 1
        if own['idlecampos_x'] == move_len:
           own['idlecampos_x'] = 0     
            
        if own['idlecampos_x'] < (move_len / 2):
            move = [.0001, 0, 0]
            freecam.applyMovement( move, True)
            freecam.applyRotation([.0001, 0, .0001],True)
        if own['idlecampos_x'] > (move_len / 2):
            move = [-.0001, 0, 0]
            freecam.applyMovement( move, True)
            freecam.applyRotation([-.0001, 0, -.0001],True)        
        
    if dict['npause'] == True:
        cont.deactivate(own.actuators['Camera'])
        controlcube['camera'] = 2
    if controlcube['camera'] == 2 and dict['joy_con'] == 1:
        cont.deactivate(own.actuators['Camera'])
        scene.active_camera = freecam 
        cont.activate(cube.actuators['freecam']) 
        #print('activating camera', freecam)  
#####        
    #get value 0-100    
        lLR = dict['lLR'] / .082 * 100
        lUD = dict['lUD'] / .082 * 100 - 20 / 80
        rLR = dict['rLR'] / .082 * 100 - 20 / 80
        rUD = dict['rUD'] / .082 * 100 - 20 / 80
        lTrig = dict['lTrig'] / .082 * 100 - 20 / 80
        rTrig = dict['rTrig'] / .082 * 100 - 20 / 80
#        lBump = dict['lBump']
#        rBump = dict['rBump']
        
        if lLR < -20:
            lmLR = round((lLR + 20) / 80 * 100, 0) 
        elif lLR > 20:
            lmLR = round((lLR - 20) / 80 * 100, 0)
        else: lmLR = 0  
              
        if lUD > 20:
            lmUD = round((lUD - 20) / 80 * 100, 0)
        elif lUD < -20:
            lmUD = round((lUD + 20) / 80 * 100, 0)
        else: lmUD = 0 

        if rLR < -20:
            rmLR = round((rLR + 20) / 80 * 100, 0)
        elif rLR > 20:
            rmLR = round((rLR - 20) / 80 * 100, 0)
        else: rmLR = 0         
        
        if rUD > 20:
            rmUD = round((rUD - 20) / 80 * 100, 0)
        elif rUD < -20:
            rmUD = round((rUD + 20) / 80 * 100, 0)
        else: rmUD = 0 
      
        if lTrig > 3:
            mTrig = lTrig * -1
        elif rTrig > 3:
            mTrig = rTrig    
        else: mTrig = 0

        #move camera
        damping = .95
        damping2 = 1.005
        mult = .0005 * own['speed_mult']
        move_x = lmUD * mult
        move_y = lmLR * mult
        move_z = (mTrig * -mult) / 2#4
        
        rot_mult = -.00015 * own['speed_mult']
        rot_x = rmUD * rot_mult
        rot_y = rmLR * rot_mult
        
        if move_x == 0 and own['last_move_x'] != 0:
            move_x = own['last_move_x'] * damping
        if move_y == 0 and own['last_move_y'] != 0:
            move_y = own['last_move_y'] * damping 
        if move_z == 0 and own['last_move_z'] != 0:
            move_z = own['last_move_z'] * damping         
        if rot_x == 0 and own['last_rot_x'] != 0:
            rot_x = own['last_rot_x'] * damping
        if rot_y == 0 and own['last_rot_y'] != 0:
            rot_y = own['last_rot_y'] * damping         
                             
        move = [move_y, 0, move_x]
        freecam.applyMovement( move, True)
        freecam.applyMovement([0, 0, move_z], False)    
        freecam.applyRotation([rot_x, 0, 0],True)
        freecam.applyRotation([0, 0, rot_y],False)
       
        
################    
    #print(dict['walk'], 'walk')
    multer = .02
    if dict['walk'] == 1:
        if dict['cur_ccH_targetHeight'] < dict['cam_walk_height']:
            dist = dict['cam_walk_height'] - dict['cur_ccH_targetHeight']
            
            dict['cur_ccH_targetHeight'] = dict['cur_ccH_targetHeight'] + (dist * multer)
            if dict['cur_ccH_targetHeight'] > dict['cam_walk_height']:
                dict['cur_ccH_targetHeight'] = dict['cam_walk_height']
                
        cam.min = dict['cam_walk_min'] 
        cam.max = dict['cam_walk_max']        
        #ccH_targetHeight = .9
    else:  

        if dict['cur_ccH_targetHeight'] < dict['cch_targetHeight']:
            dist = dict['cur_ccH_targetHeight'] - dict['cch_targetHeight']
            dict['cur_ccH_targetHeight'] = dict['cur_ccH_targetHeight'] - (dist * multer)
            if dict['cur_ccH_targetHeight'] > dict['cch_targetHeight'] - .001:
                dict['cur_ccH_targetHeight'] = dict['cch_targetHeight']             

        if dict['cur_ccH_targetHeight'] > dict['cch_targetHeight']:
            dist = dict['cch_targetHeight'] - dict['cur_ccH_targetHeight'] 
            dict['cur_ccH_targetHeight'] = dict['cur_ccH_targetHeight'] + (dist * multer)
            if dict['cur_ccH_targetHeight'] < dict['cch_targetHeight'] + .001:
                dict['cur_ccH_targetHeight'] = dict['cch_targetHeight']             
        
        
    ccH_targetHeight = dict['cur_ccH_targetHeight']  
    #print(dict['cur_ccH_targetHeight'])    
    #dict['cur_ccH_targetHeight'] =            
    #ccH_targetHeight = .3
    ccH = camCube.worldPosition.z
    pH = camCube.parent.worldPosition.z
    ccheight = (round((ccH - pH), 2) - .4)
    #print(ccheight, 'camera target height')
    
    localPos = camCube.localPosition.z
    #print(localPos, 'local position')
    if localPos != ccH_targetHeight:
        num = ccH_targetHeight - localPos
        camCube.localPosition.z += num
    #if camHeightSet not in own:
    if 1 == 1:    
        num = ccH_targetHeight - ccheight
    else:
        own['camHeightSet'] = True    
           
    if dict['npause'] == False:
        cont.activate(own.actuators['Camera']) 
        controlcube['camera'] = 0    
    
    cur_lens = cam1.lens
    cur_min = cam.min
    cur_max = cam.max
    #print(cur_lens)
    inc = .025
    if walk == 1:
        #lens
        if dict['walk_focal_length'] > cur_lens:
            new_lens = cur_lens + inc
        else:
            new_lens = cur_lens - inc
        if cur_lens  > (dict['walk_focal_length'] - .1) and cur_lens  < (dict['walk_focal_length'] + .1):
            new_lens = dict['walk_focal_length']  
            
        #distance      
        inc = .025   
        if cur_min > (dict['cam_walk_min'] - inc):
            new_min = cur_min - inc
        if cur_min < (dict['cam_walk_min'] + inc):
            new_min = cur_min + inc 
        if cur_max > (dict['cam_walk_max'] - inc):
            new_max = cur_min - inc
        if cur_max < (dict['cam_walk_max'] + inc):
            new_max = cur_min + inc           
    else:
        #lens
        if dict['focal_length'] > cur_lens:
            new_lens = cur_lens + inc
        else:
            new_lens = cur_lens - inc 
        if cur_lens  > (dict['focal_length'] - .1) and cur_lens  < (dict['focal_length'] + .1):
            new_lens = dict['focal_length'] 
            
        #distance  
        inc = .025   
        if cur_min > (dict['cam_min'] - inc):
            new_min = cur_min - inc
        if cur_min < (dict['cam_min'] + inc):
            new_min = cur_min + inc 
        if cur_max > (dict['cam_max'] - inc):
            new_max = cur_min - inc
        if cur_max < (dict['cam_max'] + inc):
            new_max = cur_min + inc        
            
        
            
                                         
        
    focallength = new_lens
    cam1.lens = focallength
    cam2.lens = focallength
    cam3.lens = focallength
    try:
        cam.min = new_min
        cam.max = new_max  
        #print(new_min, new_max)     
    except:
        pass    
    
                    
main()
