# Path finder demo
# http://www.willmcgugan.com

SCREEN_SIZE = (768, 576)
GRID_SQUARE_SIZE = (64, 64)

import pygame
from pygame.locals import *
from math import sqrt
from PathFinder import *
from time import sleep

blocked = {}

class MyNode(SquareNode):
	w = SCREEN_SIZE[0] / GRID_SQUARE_SIZE[0]
	h = SCREEN_SIZE[1] / GRID_SQUARE_SIZE[0]
	def TestValid(self, c):
		return not blocked.has_key(self.coord) or not blocked[self.coord]
		# return not (c[0] == 15 and c[1] == 15)
		# return not (c[0] == 15 and c[1] > 5 and c[1] < 30)

#p = PathFinder()
#a = MyNode((10, 10))
#b = MyNode((20, 20))
#for p in p.FindRoute(a, b):
#	print p.coord

	def render(self, surface):
	      if blocked[self.coord]:
	           render_x = self.coord[0] * GRID_SQUARE_SIZE[0]
	           render_y = self.coord[1] * GRID_SQUARE_SIZE[1]                
	           surface.fill([255, 0, 0], pygame.rect.Rect(render_x, render_y, GRID_SQUARE_SIZE[0], GRID_SQUARE_SIZE[1]))
   
def run():
    
    instructions_txt = "Draw map with left mouse button. "
    instructions_txt += "Press right mouse button (or Shift+LMB) to swim."
    title = "Path Finding Demo"
            
    fish_speed = 300.
    
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
    pygame.display.set_caption(title)
    
    default_font = pygame.font.get_default_font()
    font = pygame.font.SysFont(default_font, 26)
    instructions = font.render(instructions_txt, True, (0, 0, 0))
    
    # pygame.mouse.set_visible(False)
    
    #background = pygame.image.load(background_image_filename).convert()
    #block = pygame.image.load(block_image_filename).convert_alpha()
    #mouse = pygame.image.load(mouse_image_filename).convert_alpha()
    #fish = pygame.image.load(fish_image_filename).convert_alpha()
        
    width, height = SCREEN_SIZE
    #grid = Grid(width/64, height/64)
    
    def pos_to_coord(pos):        
        coord_x = event.pos[0] / GRID_SQUARE_SIZE[0]
        coord_y = event.pos[1] / GRID_SQUARE_SIZE[1]        
        return (coord_x, coord_y)
    
    def coord_to_pos(coord):
        pos_x = coord[0] * GRID_SQUARE_SIZE[0]
        pos_y = coord[1] * GRID_SQUARE_SIZE[1]
        return (pos_x, pos_y)
        
    fish_coord = (0, 0)
    route = []
    clock = pygame.time.Clock()
    p = PathFinder()
        
    while True:
        
        for event in pygame.event.get():
            
            if event.type == QUIT:
                return
            
            if event.type == MOUSEBUTTONDOWN and not route:
                
                mouse_coord = pos_to_coord(event.pos)
                clicked_square = MyNode(mouse_coord)
                
                if mouse_coord == fish_coord or clicked_square is None:
                    continue
                                
                shift_pressed = pygame.key.get_mods() & (KMOD_SHIFT)
                
                if event.button == 3 or (event.button == 1 and shift_pressed):
                    
                    if not blocked.has_key(mouse_coord) or not blocked[mouse_coord]:
                        route = p.FindRoute(MyNode(fish_coord), clicked_square)
                        if route is not None:                            
                            pygame.display.set_caption("Swimming to destination!")
                            #fish_coord = mouse_coord
                
                elif event.button == 1:
	            if not blocked.has_key(mouse_coord):
		    	blocked[mouse_coord] = True
		    else:
                        blocked[mouse_coord] = not blocked[mouse_coord]
            
        screen.fill((255,255,255))
        
	for i in blocked:
		MyNode(i).render(screen)
        
        time_passed = clock.tick()
        time_passed_seconds = time_passed / 1000.0

        if route:
            
            for c in route:
                x, y = coord_to_pos(c.coord)
                x += GRID_SQUARE_SIZE[0] / 2
                y += GRID_SQUARE_SIZE[1] / 2
                pygame.draw.circle(screen, (0, 255, 0), (x, y), 6)
            
            destination = route[0]
            
            if fish_coord == destination.coord:
                route = route[1:]
                if not route:
                    pygame.display.set_caption(title)
            else:
	        fish_coord = destination.coord
	
	pygame.draw.circle(screen, (0, 0, 255), coord_to_pos([f + 0.5 for f in fish_coord]), 10)
        
        screen.blit(instructions, (5, 5))
        
        pygame.display.update()
	sleep(0.1)

if __name__ == "__main__":
    run()

