import pygame
from pygame.locals import *
from functions import *

class Fist(pygame.sprite.Sprite):
    def __init__(self,world,space):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image('fist2.bmp', -1)
        self.punching = 0
        self.body = ode.Body(world)
        M=ode.Mass()
        self.radius = 25
        M.setSphere(1500,self.radius)
        self.body.setMass(M)
        self.body.size=self.radius
        self.geom=ode.GeomSphere(space,self.radius)
        self.geom.setBody(self.body)
        self.geom.size=self.radius
        pozycja = list(pygame.mouse.get_pos())
        pozycja.append(0)
        tmp = [pozycja[0]+self.radius,pozycja[1]+self.radius,0]
        self.body.setPosition(tmp)
        self.geom.setPosition(tmp)
        pos = pygame.mouse.get_pos()
        self.rect.midtop = pos

    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.midtop = pos
        if self.punching:
            self.rect.move_ip(5, 10)

    def punch(self, target):
        if not self.punching:
            self.punching = 1
            hitbox = self.rect.inflate(-5, -5)
            return hitbox.colliderect(target.rect)

    def unpunch(self):
        self.punching = 0
        
    def saveZPos(self,elem):
        self.savedzpos = 0
        self.savedzvel = 0
        x,y,z = elem.getPosition()
        self.savedzpos = z
        x,y,z = elem.getLinearVel()
        self.savedzvel = z
        
    def loadZPos(self,elem):
        x,y,z = elem.getPosition()
        elem.setPosition((x,y,self.savedzpos))
        x,y,z = elem.getLinearVel()
        elem.setLinearVel((x,y,self.savedzvel))
        
    def Check(self,enemy):
        hitbox = self.rect
        return hitbox.colliderect(enemy.rect)