import pygame
from pygame.locals import *
from functions import *
from random import randint

class Enemy(pygame.sprite.Sprite):
    def __init__(self,world,space,width,height):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image('enemy2.gif', -1)
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        #self.rect.topleft = 10, 10
        rog = randint(1,4)
        if rog == 1:
            self.rect.topleft = 10, 10+50
        elif rog == 2:
            self.rect.topleft = width-70, 10+50
        elif rog == 3:
            self.rect.topleft = width-70, height-70
        else:
            self.rect.topleft = 10, height-70
        #self.rect.topleft = randint(0,self.area.right), randint(0,self.area.bottom)
        rand1 = randint(0,1)
        rand2 = randint(0,1)
        if (rand1==0):
            rand1=-1
        if (rand2==0):
            rand2=-1
        self.movex = 3*rand1
        self.movey = 3*rand1
        self.dizzy = 0
        #~ self.body = ode.Body(world)
        #~ M=ode.Mass()
        #~ radius=30
        #~ M.setSphere(1500,radius)
        #~ self.body.setMass(M)
        #~ self.body.size=radius
        #~ self.geom=ode.GeomSphere(space,radius)
        #~ self.geom.setBody(self.body)
        #~ self.geom.size=radius

    def update(self):
        #self.movex *= 
        #self.movey *= 
        if self.dizzy:
            self._spin()
        else:
            self._walk()

    def _walk(self):
        newpos = self.rect.move((self.movex, self.movey))
        if self.rect.left < self.area.left or \
            self.rect.right > self.area.right:
            self.movex = -self.movex
            newpos = self.rect.move((self.movex, self.movey))
            self.image = pygame.transform.flip(self.image, 1, 0)
        if self.rect.top < self.area.top+50 or \
            self.rect.bottom > self.area.bottom:
            self.movey = -self.movey
            newpos = self.rect.move((self.movex, self.movey))
            self.image = pygame.transform.flip(self.image, 1, 0)
        self.rect = newpos

    def _spin(self):
        center = self.rect.center
        self.dizzy = self.dizzy + 12
        if self.dizzy >= 360:
            self.dizzy = 0
            self.image = self.original
        else:
            rotate = pygame.transform.rotate
            self.image = rotate(self.original, self.dizzy)
        self.rect = self.image.get_rect(center)

    def punched(self):
        if not self.dizzy:
            self.dizzy = 1
            self.original = self.image
            
    def die(self,world,velx,vely):
        world.choice(screams).play()
        world.choice(splats).play()
        #tempvel = world.ball.body.getLinearVel()
        #velx = tempvel[0]/opcje.krew_rozrzut
        #vely = tempvel[1]/opcje.krew_rozrzut
        world.krew.spawn((self.rect.center[0],self.rect.center[1]),(velx/opcje.krew_rozrzut,vely/opcje.krew_rozrzut))
        world.jest_krew = True
        #en.punched()
        world.punkty += 1
        world.trupy += 1
        wypisz("Kills: "+str(world.trupy),(10,10,10),(250,250,250),tlo,-400)
        wypisz("Points: "+str(world.punkty),(10,10,10),(250,250,250),tlo,-200)
        world.tlo.blit(Corpse(self.rect.topleft[0],self.rect.topleft[1]).image,(self.rect.topleft[0],self.rect.topleft[1]))
        rog = randint(1,4)
        if rog == 1:
            self.rect.topleft = 10, 10+50
        elif rog == 2:
            self.rect.topleft = width-70, 10+50
        elif rog == 3:
            self.rect.topleft = width-70, height-70
        else:
            self.rect.topleft = 10, height-70
        world.enemies.remove(self)
        world.allsprites.remove(self)
            
            
class Corpse(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image('corpse2.gif', -1)
        self.original = self.image
        rotate = pygame.transform.rotate
        self.image = rotate(self.original, randint(0,360))
        #self.rect = self.image.get_rect(center)
        self.rect.topleft = (x,y)