from Texture import *
from Thing import *
#import pygame
#from pygame.locals import *
from Light import *
from Shot import *
import globalVars

class Player(Thing):
    def __init__(self):
        Thing.__init__(self)
        self.texture = Texture("marine2")
        self.shots = []
        self.move_speed = 1.5
        self.flashx = 8
        self.flashy = 14
        self.gunx = 26
        self.guny = 16
        self.light = Light((self.x+self.flashx,self.y+self.flashy), 90, fov = 100, base_width = 3, length=600)
        globalVars.lightList.append(self.light)
        self.dead = 0
        self.maxHealth = 500
        self.health = self.maxHealth
        
    def draw(self):
        self.texture.draw(pos=(self.x, self.y))
        
    def update(self, delta):
        mouseX, mouseY = globalVars.mouseX, globalVars.mouseY
        Thing.update(self, delta)
        self.light.setPos(self.x+self.flashx,self.y+self.flashy)
        
        adj = mouseX - (self.x+self.flashx)
        op = mouseY - (self.y+self.flashy)
        
        if adj < 0:
            offset = 90
        else:
            offset = -90

        adj *= 1.0;op *= 1.0 # convert to floats for atan()

        if adj != 0:
          flashrotation = degrees(atan(op/adj)) + offset
        else:
          flashrotation = 90
        
        self.light.setDirection(flashrotation)
                
        
        self.light.draw()
        self.draw()
        
        # start by going through the shots and finding the ones that have landed
	dead = []
	# go through once to find all the ones we need to kick out
	for i in range(len(self.shots)):
	    if self.shots[i].dead:
	        dead.append(i)
	        
        for i in dead:
            self.shots.pop(i)              
            break # can only do 1 in a frame
        
        for shot in self.shots:
            shot.update(delta)
        
        for guy in globalVars.alienList:
	    if circleCollide(self, guy, 24):
	        self.hit(guy.damage)
	        
	for switch in globalVars.switches:
	    if circleCollide(self, switch):
	        switch.flip()
	
	for health in globalVars.healthList:
	    if circleCollide(self, health):
	        health.dead = 1
	        self.addHealth(health.value)

    def addHealth(self, value):
        self.health += value
        if self.health > self.maxHealth:
            self.health = self.maxHealth
            
    def hit(self, damage):
        self.health -= damage
        if self.health <= 0:
            self.dead = 1
            
    def startShoot(self):
        shot = Shot([self.x+self.gunx, self.y+self.guny], [globalVars.mouseX, globalVars.mouseY])

        self.shots.append(shot)


    def stopShoot(self):
        None

    def move(self, direction):
        if direction == 'up':
            self.setSpeed(y = -self.move_speed)
        elif direction == 'down':
            self.setSpeed(y = self.move_speed)
        elif direction == 'right':    
            self.setSpeed(x = self.move_speed)
        elif direction == 'left':
            self.setSpeed(x = -self.move_speed)

    def stop(self, direction):
        if direction == 'up' or direction == 'down':
            self.setSpeed(y = 0)
        elif direction == 'right' or direction == 'left':    
            self.setSpeed(x = 0)
            