import pygame
from pygame.locals import *
import ode
from functions import load_image

class Rope:
	def __init__(self,world,pos):
		self.handbody = ode.Body(world)
		M = ode.Mass()
		M.setSphere(1500, 0.05)
		self.handbody.setMass(M)
		self.handbody.setPosition(pos)
		self.rope = [self.handbody]
		self.ropejoints = []
		self.length = 10
		self.val = 210
		for i in range(1,self.length):
			self.rope.append(ode.Body(world))
			M = ode.Mass()
			M.setSphere(500, 0.05)
			self.rope[i].setMass(M)
			self.rope[i].setPosition((pos[0]+(i*(self.val/self.length)),pos[1]+(i*(self.val/self.length)),0))
			#print self.rope[i].getPosition()
			self.ropejoints.append(ode.BallJoint(world))
			self.ropejoints[i-1].attach(self.rope[i-1], self.rope[i])
			self.ropejoints[i-1].setAnchor(self.rope[i-1].getPosition())
		
	def draw(self,surf,ballx,bally):
		#for i in range(len(self.rope)-1):
		for i in range(self.length-1):
			a1,b1,c1 = self.rope[i].getPosition()            
			a2,b2,c2 = self.rope[i+1].getPosition()
			#print a1,b1,a2,b2
			pygame.draw.line(surf, (55,0,200), (a1,b1), (a2,b2), 2)
		pygame.draw.line(surf, (55,0,200), (a2,b2), (ballx,bally), 2)
		
class Ball:
	def __init__(self,world,pos,space):
		self.body = ode.Body(world)
		M = ode.Mass()
		M.setSphere(2500, 0.05)
		self.body.setMass(M)
		#self.body.setPosition((pos[0]+((i+1)*(self.val/rope.length)),pos[1]+((i+1)*(self.val/rope.length)),0))
		self.radius = 30
		#bylo ballbody i geom2 i j2
		self.geom=ode.GeomSphere(space,self.radius)
		self.geom.setBody(self.body)
		self.geom.size = self.radius
		self.sprite = pygame.sprite.Sprite()
		self.sprite.image, self.sprite.rect = load_image('ball.png', -1)
		
	def connectToRope(self,rope,world,pos):
		self.joint = ode.BallJoint(world)
		self.joint.attach(rope.rope[-1], self.body)
		self.joint.setAnchor(rope.rope[-1].getPosition())
		self.body.setPosition((pos[0]+((rope.length+1)*(rope.val/rope.length)),pos[1]+((rope.length+1)*(rope.val/rope.length)),0))