""" Turret.py
"""

def updateShot( shotVel, shotPos ):
    newVel = [ shotVel[0], shotVel[1] + 0.2 ]
    newPos = [shotPos[0] + newVel[0] * 0.5, shotPos[1] + newVel[1] * 0.5]

    return newVel, newPos


def rotateImage( currentCenter, masterImage, degrees ):
    newImage = pygame.transform.rotate(masterImage, degrees)
    imageRect = newImage.get_rect()
    imageRect.center = currentCenter

    return imageRect, newImage

#Initialize
import pygame,sys,random,math
pygame.init()

#Display
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Turret")

#Entities
#Gray background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((155, 155, 155))

#cloud background
cloud = pygame.image.load("Cloud2.gif")
cloud = cloud.convert()
cloudRect = cloud.get_rect();

cloudPos = 0

#ground background
ground = pygame.image.load("Ground.png")
ground = ground.convert()
ground.set_colorkey( (255,255,255) )
groundRect = ground.get_rect();
groundMask = pygame.mask.from_surface( ground )

groundPos = 0


groundLevel = 500
#groundRect = pygame.Rect((0,groundLevel), (800, 100) )

#Load the explosion
allExplosions = pygame.image.load("SkybusterExplosion.gif")
allExplosions = allExplosions.convert()
explosionsRect = allExplosions.get_rect();

explosion = []
for j in range(5):
    for i in range(4):
        image = allExplosions.subsurface( pygame.Rect((320 * i, 240 * j),
                                                      (320,240)))
        image.set_colorkey( (0,1,0) )
        explosion.insert(0,image)
explosionRect = explosion[0].get_rect()


#make a turret model
turretMaster = pygame.image.load("turret.gif")
turretMaster = turretMaster.convert()
turretRect = turretMaster.get_rect();
turretRect.center = (200,groundLevel-10)
turretDir = 0

turret2Rect = turretMaster.get_rect();
turret2Rect.center = (600, groundLevel-10)
turret2Dir = 180

turnRate = 2

#make the shot mask for later offsetting
shotMaskSurface = pygame.Surface((7,7))
shotMaskSurface.fill((0,0,0))
pygame.draw.circle( shotMaskSurface, (255, 255, 255), (3,3), 3)
shotMaskSurface.set_colorkey((0,0,0))
shotMask = pygame.mask.from_surface( shotMaskSurface )

for i in range(0,6):
    for j in range(0,6):
        print shotMask.get_at((i,j)),
    print
    
print shotMask

#ACTION

#Assign 
clock = pygame.time.Clock()
keepGoing = True
shotFired = False
shot2Fired = False

turretHit = 0
turret2Hit = 0

turretPower = 15
turret2Power = 15

    #Loop
while keepGoing:

    #Time
    clock.tick(30)

    #Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepGoing = False


    keys = pygame.key.get_pressed()
    if turretHit == 0:
        if keys[pygame.K_a]:
            turretDir += turnRate
            if turretDir > 360:
                turretDir -= 360
            
        if keys[pygame.K_d]:
            turretDir -= turnRate
            if turretDir < 0:
                turretDir += 360                

        if keys[pygame.K_q]:
            turretRect.move_ip([-1,0])
            if turretRect.left < 0:
                turretRect.left = 0

        if keys[pygame.K_e]:
            turretRect.move_ip([1,0])
            if turretRect.right > 800/2:
                turretRect.right = 800/2
    
        if keys[pygame.K_w]:
            turretPower += 1
            if turretPower > 20:
                turretPower = 10

        if  not shotFired and keys[pygame.K_s]:
            shotVel = [turretPower * math.cos(math.radians(turretDir)), -turretPower * math.sin(math.radians(turretDir))]
            shotPos = [float(turretRect.center[0]), float(turretRect.center[1])]
            shotFired = True

    if turret2Hit == 0:
        if keys[pygame.K_j]:
            turret2Dir += turnRate
            if turret2Dir > 360:
                turret2Dir -= 360
                
        if keys[pygame.K_l]:
            turret2Dir -= turnRate
            if turret2Dir < 0:
                turret2Dir += 360                
        
        if keys[pygame.K_u]:
            turret2Rect.move_ip([-1,0])
            if turret2Rect.left < 800/2:
                turret2Rect.left = 800/2

        if keys[pygame.K_o]:
            turret2Rect.move_ip([1,0])
            if turret2Rect.right > 800:
                turret2Rect.right = 800

        if keys[pygame.K_i]:
            turret2Power += 1
            if turret2Power > 20:
                turret2Power = 10

        if not shot2Fired and keys[pygame.K_k]:
            shot2Vel = [turret2Power * math.cos(math.radians(turret2Dir)), -turret2Power * math.sin(math.radians(turret2Dir))]
            shot2Pos = [float(turret2Rect.center[0]), float(turret2Rect.center[1])]
            shot2Fired = True

    #Rotate the turret        
    turretRect, turret = rotateImage( turretRect.center, turretMaster, turretDir )
    turret2Rect, turret2 = rotateImage( turret2Rect.center, turretMaster, turret2Dir )

    turret2Mask = pygame.mask.from_surface( turret2 )
    if groundMask.overlap( turret2Mask, ( turret2Rect.left - groundPos,
                                          turret2Rect.top)):
        turret2Rect.move_ip(0,-1)
    else:
        turret2Rect.move_ip(0,1)

    turretMask = pygame.mask.from_surface( turret )
    if groundMask.overlap( turretMask, ( turretRect.left - groundPos,
                                          turretRect.top)):
        turretRect.move_ip(0,-1)
    else:
        turretRect.move_ip(0,1)

    
    if shotFired:
        shotVel, shotPos = updateShot(shotVel, shotPos)
        if groundMask.overlap( shotMask, ( int(shotPos[0]) - groundPos,
                                           int(shotPos[1]))):
            shotFired = False
        if turret2Rect.collidepoint( shotPos ):
            if turret2Mask.overlap( shotMask, (turret2Rect.width/2 - 3 - (turret2Rect.centerx - int(shotPos[0])),
                                             turret2Rect.height/2 - 3 - (turret2Rect.centery - int(shotPos[1])))):
                print("Turret2 Boom")
                turret2Hit = 100
                shotFired = False
                
    if shot2Fired:
        shot2Vel, shot2Pos = updateShot(shot2Vel, shot2Pos)
        if groundMask.overlap( shotMask, ( int(shot2Pos[0]) - groundPos,
                                           int(shot2Pos[1]))):
            shot2Fired = False
        if turretRect.collidepoint( shot2Pos ):
            if turretMask.overlap( shotMask, (turretRect.width/2 - 3 - (turretRect.centerx - int(shot2Pos[0])),
                                             turretRect.height/2 - 3 - (turretRect.centery - int(shot2Pos[1])))):
                print("Turret Boom")
                turretHit = 100
                shot2Fired = False;
        
    if turretHit > 0:
        turretHit -= 1;

    if turret2Hit > 0:
        turret2Hit -= 1;
            
    #scroll the cloud
    cloudPos -= 1
    if cloudPos < -1600:
        cloudPos = 0
    cloudRect.left = cloudPos

    #scroll the ground
    groundPos -= 2
    if groundPos < -1600:
        groundPos = 0
    groundRect.left = groundPos
    
    #Refresh screen
#    screen.blit(background, (0, 0))
    screen.blit(cloud, cloudRect)
    screen.blit(ground, groundRect)
#    pygame.draw.rect(screen, (30,200,30), groundRect)
    screen.blit(turret, turretRect)
    screen.blit(turret2, turret2Rect)
    if shotFired:
        pygame.draw.circle( screen, (255, 155, 55), (int(shotPos[0]), int(shotPos[1])), 3 )
    if shot2Fired:
        pygame.draw.circle( screen, (255, 155, 55), (int(shot2Pos[0]), int(shot2Pos[1])), 3 )
    if turretHit >= 80:
        explosionRect.center = turretRect.center
        screen.blit(explosion[turretHit-80], explosionRect )

    if turret2Hit >= 80:
        explosionRect.center = turret2Rect.center
        screen.blit(explosion[turret2Hit-80], explosionRect )
    pygame.draw.rect(screen, (100,200,255), pygame.Rect((10,10),(turretPower*2,20)))
    pygame.draw.rect(screen, (100,200,255), pygame.Rect((550,10),(turret2Power*2,20)))
    pygame.draw.circle( screen, (255,0,0), turretRect.center,2 )
    pygame.display.flip()
                
