import pygame
import main					#able to load the lib
main.init()					#
import data, color				#then load the libs
from color import white
import random

def init():
	global cube_
	global cube0
	global cube1
	global cube2
	global cube3
	global cube_rect
	cube_ = data.load_img('GRAPPLING_CUBE.png')
	cube0 = data.load_img('GRAPPLING_CUBE00.png')
	cube1 = data.load_img('GRAPPLING_CUBE01.png')
	cube2 = data.load_img('GRAPPLING_CUBE02.png')
	cube3 = data.load_img('GRAPPLING_CUBE03.png')
	cube_rect = cube_.get_rect()
	cube_.convert()
	cube0.convert()
	cube1.convert()
	cube2.convert()
	cube3.convert()
	cube_.set_colorkey(white)
	cube0.set_colorkey(white)
	cube1.set_colorkey(white)
	cube2.set_colorkey(white)
	cube3.set_colorkey(white)

class grappling_cube(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image, self.rect = None, cube_rect
		self.grap = None
		self.spr = None
		self.destx, self.desty = 0,0

	def update(self):
		if self.spr:
			self.spr.rect.center = self.rect.center
		if self.grap == True:
			if self.image == cube3:
				self.image = cube_
				self.grap = None
			elif self.image == cube2:
				self.image = cube3
			elif self.image == cube1:
				self.image = cube2
			elif self.image == cube0:
				self.image = cube1
			elif self.image == None:
				self.image = cube0
		if self.grap == False:
			if self.image == cube0:
				self.image = None
				self.grap = None
				self.rect.center = (0,0)
			elif self.image == cube1:
				self.image = cube0
			elif self.image == cube2:
				self.image = cube1
			elif self.image == cube3:
				self.image = cube2
			elif self.image == cube_:
				self.image = cube3
		#moving
		if self.destx != 0  and self.desty != 0:
			if self.destx != self.rect.centerx or self.desty != self.rect.centery:
				if self.destx < self.rect.centerx:
					self.rect.centerx -= 1
				elif self.destx > self.rect.centerx:
					self.rect.centerx += 1
				if self.desty < self.rect.centery:
					self.rect.centery -= 1
				elif self.desty > self.rect.centery:
					self.rect.centery += 1
			else:
				self.destx, self.desty = 0,0
				self.drop()

	def draw(self,screen):
		if self.image:
			screen.blit(self.image, self.rect)

	def take(self,Dsprite):
		self.rect.center = Dsprite.rect.center
		self.grap = True
		self.spr = Dsprite

	def drop(self):
		self.spr = None
		self.grap = False

	def go(self,(destx,desty)):
		self.destx = destx
		self.desty = desty

if __name__=="__main__":
	#tester
	pygame.init()					#first
	screen = pygame.display.set_mode((640,480))	#the screen
	clock = pygame.time.Clock()			#the time

	#accelerator
	exit_button = pygame.QUIT
	pygame.event.set_blocked(None)
	pygame.event.set_allowed(exit_button)
	happen = pygame.event.peek
	fps = clock.tick
	flip = pygame.display.flip
	cls = screen.fill
	white = color.white

	#cube
	init()
	cube = grappling_cube()
	taken = None

	#thing
	import kossin
	kossin.init()
	kossins = pygame.sprite.Group()
	for i in range(3):
		kossins.add(kossin.kossin(random.randint(80,540),random.randint(80,400)))

	#main loop
	end = False
	raize = False
	button = False
	while not end:
		fps(40)
		flip()
		cls(white)
		if happen(exit_button):
			end = True
		else:
			pygame.event.clear()
		kossins.update()
		kossins.draw(screen)
		if True in pygame.mouse.get_pressed():
			if not button:
				raize = True
				button = True
			else:
				raize = False
		else:
			raize = False
			button = False
		#test cube
		if raize :
			(x,y) = pygame.mouse.get_pos()
			if not taken and not cube.spr:
				for kossin in kossins:
					if kossin.rect.collidepoint(x,y):
						cube.take(kossin)
						taken = kossin
			elif taken and cube.destx == 0 and cube.desty == 0:
				cube.go((x,y))
				taken = None
		cube.update()
		cube.draw(screen)



