import pygame, math
from game import map, ui

window = pygame.display.set_mode([800, 600])
ui.window = window
screen = "game"
s = {"fullscreen": False}
running = True
gamedata = {"level": 0, "coal": 0, "iron": 1, "copper":0}
tiles = pygame.sprite.Group()
rails = pygame.sprite.Group()
carts = pygame.sprite.Group()
interactables = pygame.sprite.Group()
listmap = []
clock = pygame.time.Clock()
selected = pygame.image.load("./resources/images/selected.png")
selected2 = pygame.image.load("./resources/images/selected2.png")
box = pygame.image.load("./resources/images/box.png")
uibox = pygame.image.load("./resources/images/ui box.png")

class Mouse(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.surface.Surface([1, 1])
        self.rect = self.image.get_rect()
        self.rect.topleft = [0, 0]
        self.clickedcart = None
        self.hoveritem = None
        self.tl = self.rect.topleft
        self.mode = "select"

    def pos(self, position):
        self.rect.topleft = position
        self.tl = self.rect.topleft


m = Mouse()


def snaptogrid(pos):
    return [int(math.floor(pos[0] / 40)), int(math.floor(pos[1] / 40))]


def loadlevel(number):
    global tiles, rails, carts, gamedata, listmap, interactables
    tiles, rails, interactables, listmap = map.loadmap(int(number))
    carts.empty()
    gamedata["level"] = number
    gamedata["coal"] = 0
    gamedata["iron"] = 1
    gamedata["copper"] = 0

loadlevel(0)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            m.pos(pygame.mouse.get_pos())
            if screen == "game":
                if pygame.sprite.spritecollide(m, carts, False) and m.mode == "select":
                    carts.update("select", m, listmap)
                    if m.clickedcart != None:
                        m.mode = "action"
                elif m.mode == "action" and m.clickedcart != None and listmap[snaptogrid(m.tl)[0]][snaptogrid(m.tl)[1]] > 0:
                    m.clickedcart.pathfind(listmap, snaptogrid(m.tl))
                    m.clickedcart = None
                    m.mode = "select"
        elif event.type == pygame.MOUSEMOTION:
            m.pos(pygame.mouse.get_pos())
            if screen == "game":
                m.hoveritem = None
                if len(pygame.sprite.spritecollide(m, carts, False)) > 0:
                    m.hoveritem = pygame.sprite.spritecollide(m, carts, False)[0]
                elif len(pygame.sprite.spritecollide(m, interactables, False)) > 0:
                    m.hoveritem = pygame.sprite.spritecollide(m, interactables, False)[0]
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                carts.add(map.Cart(snaptogrid(m.tl), "miner"))
    if screen == "game":
        window.fill([100, 100, 100])
        tiles.draw(window)
        carts.draw(window)
        carts.update("update", m, listmap)
        if not m.hoveritem == None and not m.mode == "action":
            window.blit(box, [m.rect.left+10, m.rect.top+10])
            ui.Resize(30)
            ui.Text(m.hoveritem.type.upper(), [m.rect.left+27, m.rect.top+25])
            if m.hoveritem.type.startswith("mine") and m.hoveritem not in carts:
                ui.Resize(18)
                ui.Text("Carts Inside: " + str(m.hoveritem.data["carts"]), [m.rect.left+27, m.rect.top+47])
                ui.Text("Max Carts: " + str(m.hoveritem.data["max"]), [m.rect.left+27, m.rect.top+60])
        if not m.clickedcart == None:
            window.blit(selected2, [m.clickedcart.rect.left-2, m.clickedcart.rect.top-2])
            if m.mode == "action":
                window.blit(box, [m.rect.left+10, m.rect.top+10])
                ui.Resize(30)
                try:
                    ui.Text(m.hoveritem.type.upper(), [m.rect.left+27, m.rect.top+25])
                except:
                    ui.Text(m.clickedcart.type.upper(), [m.rect.left+27, m.rect.top+25])
                if listmap[snaptogrid(m.tl)[0]][snaptogrid(m.tl)[1]] > 0:
                    ui.Resize(22)
                    ui.Text("Click to move", [m.rect.left+27, m.rect.top+45])
                    ui.Text("Cart Here", [m.rect.left+27, m.rect.top+60])
        window.blit(selected, [snaptogrid(m.tl)[0]*40-2, snaptogrid(m.tl)[1]*40-2])
        window.blit(uibox, [555, 475])
    pygame.display.flip()
    clock.tick(60)
    fps = clock.get_fps()
pygame.quit()

[((55, 90), 'pygame.display.set_mode', 'pygame.display.set_mode', (['[800, 600]'], {}), '([800, 600])\n', (78, 90), False, 'import pygame, math\n'), ((233, 254), 'pygame.sprite.Group', 'pygame.sprite.Group', ([], {}), '()\n', (252, 254), False, 'import pygame, math\n'), ((263, 284), 'pygame.sprite.Group', 'pygame.sprite.Group', ([], {}), '()\n', (282, 284), False, 'import pygame, math\n'), ((293, 314), 'pygame.sprite.Group', 'pygame.sprite.Group', ([], {}), '()\n', (312, 314), False, 'import pygame, math\n'), ((331, 352), 'pygame.sprite.Group', 'pygame.sprite.Group', ([], {}), '()\n', (350, 352), False, 'import pygame, math\n'), ((374, 393), 'pygame.time.Clock', 'pygame.time.Clock', ([], {}), '()\n', (391, 393), False, 'import pygame, math\n'), ((405, 457), 'pygame.image.load', 'pygame.image.load', (['"""./resources/images/selected.png"""'], {}), "('./resources/images/selected.png')\n", (422, 457), False, 'import pygame, math\n'), ((470, 523), 'pygame.image.load', 'pygame.image.load', (['"""./resources/images/selected2.png"""'], {}), "('./resources/images/selected2.png')\n", (487, 523), False, 'import pygame, math\n'), ((530, 577), 'pygame.image.load', 'pygame.image.load', (['"""./resources/images/box.png"""'], {}), "('./resources/images/box.png')\n", (547, 577), False, 'import pygame, math\n'), ((586, 636), 'pygame.image.load', 'pygame.image.load', (['"""./resources/images/ui box.png"""'], {}), "('./resources/images/ui box.png')\n", (603, 636), False, 'import pygame, math\n'), ((4539, 4552), 'pygame.quit', 'pygame.quit', ([], {}), '()\n', (4550, 4552), False, 'import pygame, math\n'), ((1540, 1558), 'pygame.event.get', 'pygame.event.get', ([], {}), '()\n', (1556, 1558), False, 'import pygame, math\n'), ((4472, 4493), 'pygame.display.flip', 'pygame.display.flip', ([], {}), '()\n', (4491, 4493), False, 'import pygame, math\n'), ((705, 740), 'pygame.sprite.Sprite.__init__', 'pygame.sprite.Sprite.__init__', (['self'], {}), '(self)\n', (734, 740), False, 'import pygame, math\n'), ((762, 792), 'pygame.surface.Surface', 'pygame.surface.Surface', (['[1, 1]'], {}), '([1, 1])\n', (784, 792), False, 'import pygame, math\n'), ((1153, 1176), 'math.floor', 'math.floor', (['(pos[0] / 40)'], {}), '(pos[0] / 40)\n', (1163, 1176), False, 'import pygame, math\n'), ((1183, 1206), 'math.floor', 'math.floor', (['(pos[1] / 40)'], {}), '(pos[1] / 40)\n', (1193, 1206), False, 'import pygame, math\n'), ((3190, 3203), 'game.ui.Resize', 'ui.Resize', (['(30)'], {}), '(30)\n', (3199, 3203), False, 'from game import map, ui\n'), ((3380, 3393), 'game.ui.Resize', 'ui.Resize', (['(18)'], {}), '(18)\n', (3389, 3393), False, 'from game import map, ui\n'), ((3850, 3863), 'game.ui.Resize', 'ui.Resize', (['(30)'], {}), '(30)\n', (3859, 3863), False, 'from game import map, ui\n'), ((1695, 1717), 'pygame.mouse.get_pos', 'pygame.mouse.get_pos', ([], {}), '()\n', (1715, 1717), False, 'import pygame, math\n'), ((4179, 4192), 'game.ui.Resize', 'ui.Resize', (['(22)'], {}), '(22)\n', (4188, 4192), False, 'from game import map, ui\n'), ((4213, 4274), 'game.ui.Text', 'ui.Text', (['"""Click to move"""', '[m.rect.left + 27, m.rect.top + 45]'], {}), "('Click to move', [m.rect.left + 27, m.rect.top + 45])\n", (4220, 4274), False, 'from game import map, ui\n'), ((4291, 4348), 'game.ui.Text', 'ui.Text', (['"""Cart Here"""', '[m.rect.left + 27, m.rect.top + 60]'], {}), "('Cart Here', [m.rect.left + 27, m.rect.top + 60])\n", (4298, 4348), False, 'from game import map, ui\n'), ((1771, 1815), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', (['m', 'carts', '(False)'], {}), '(m, carts, False)\n', (1798, 1815), False, 'import pygame, math\n'), ((2322, 2344), 'pygame.mouse.get_pos', 'pygame.mouse.get_pos', ([], {}), '()\n', (2342, 2344), False, 'import pygame, math\n'), ((2437, 2481), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', (['m', 'carts', '(False)'], {}), '(m, carts, False)\n', (2464, 2481), False, 'import pygame, math\n'), ((2522, 2566), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', (['m', 'carts', '(False)'], {}), '(m, carts, False)\n', (2549, 2566), False, 'import pygame, math\n'), ((2595, 2647), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', (['m', 'interactables', '(False)'], {}), '(m, interactables, False)\n', (2622, 2647), False, 'import pygame, math\n'), ((2688, 2740), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', (['m', 'interactables', '(False)'], {}), '(m, interactables, False)\n', (2715, 2740), False, 'import pygame, math\n')]