2019-12-09 12:36:42 +01:00
|
|
|
from itertools import product
|
|
|
|
|
|
|
|
from pygame import Surface, Rect, image
|
|
|
|
|
|
|
|
from engine.entity import Entity
|
|
|
|
from engine.resources import res
|
|
|
|
from engine.servers.graphics import GraphicsServer
|
|
|
|
from engine.components.collide_rect import CollideRect
|
|
|
|
from engine.components.sprite import Sprite
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
from .common import S, Direction
|
2019-12-09 13:21:33 +01:00
|
|
|
from .pacdot import PacDot, EnsmallmentDot
|
2019-12-09 12:36:42 +01:00
|
|
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
class Node():
|
|
|
|
cpt = 0
|
2019-12-09 15:34:37 +01:00
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.id = self.__class__.cpt
|
|
|
|
self.__class__.cpt += 1
|
|
|
|
self.neighbors = [None for dir in Direction]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def children(self):
|
|
|
|
children = []
|
|
|
|
for direction in Direction:
|
|
|
|
node = self.neighbors[direction.value]
|
|
|
|
if node is not None:
|
|
|
|
children.append((node, direction))
|
|
|
|
return children
|
|
|
|
|
|
|
|
|
2019-12-09 12:36:42 +01:00
|
|
|
class Level(Entity):
|
|
|
|
def __init__(self, path):
|
|
|
|
super().__init__('level')
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
desc = image.load(res(self.path))
|
|
|
|
w, h = desc.get_size()
|
|
|
|
GraphicsServer().resize((w * S, h * S))
|
|
|
|
self.surf = Surface((w * S, h * S)).convert()
|
|
|
|
self.surf.fill((0, 0, 0))
|
|
|
|
wall = Surface((S, S)).convert()
|
|
|
|
wall.fill((0, 0, 255))
|
2019-12-09 15:29:35 +01:00
|
|
|
self.graph = [[None for x in range(w)] for y in range(h)]
|
2019-12-09 12:36:42 +01:00
|
|
|
for x, y in product(range(w), range(h)):
|
|
|
|
col = desc.get_at((x, y))
|
|
|
|
if col == (0, 0, 255):
|
|
|
|
self.add(CollideRect(Rect(x * S, y * S, S, S)))
|
|
|
|
self.surf.blit(wall, (x * S, y * S))
|
|
|
|
elif col == (139, 139, 139):
|
|
|
|
pc = PacDot()
|
|
|
|
pc.x = x * S
|
|
|
|
pc.y = y * S
|
|
|
|
self.scene.add(pc)
|
|
|
|
PacDot.tot += 1
|
2019-12-09 15:29:35 +01:00
|
|
|
self.graph[y][x] = Node()
|
|
|
|
elif col == (0, 0, 0) or col == (255, 0, 0):
|
|
|
|
self.graph[y][x] = Node()
|
2019-12-09 13:21:33 +01:00
|
|
|
elif col == (193, 193, 193):
|
|
|
|
pc = EnsmallmentDot()
|
|
|
|
pc.x = x * S
|
|
|
|
pc.y = y * S
|
|
|
|
self.scene.add(pc)
|
2019-12-09 12:36:42 +01:00
|
|
|
self.add(Sprite(self.surf, 0))
|
2019-12-09 15:29:35 +01:00
|
|
|
for y, line in enumerate(self.graph):
|
|
|
|
for x, node in enumerate(line):
|
|
|
|
if node is None:
|
|
|
|
continue
|
|
|
|
node.neighbors[Direction.LEFT.value] = \
|
2019-12-09 15:34:37 +01:00
|
|
|
self.graph[y][(x-1) % w]
|
2019-12-09 15:29:35 +01:00
|
|
|
node.neighbors[Direction.RIGHT.value] = \
|
2019-12-09 15:34:37 +01:00
|
|
|
self.graph[y][(x+1) % w]
|
2019-12-09 15:29:35 +01:00
|
|
|
node.neighbors[Direction.UP.value] = \
|
2019-12-09 15:34:37 +01:00
|
|
|
self.graph[(y-1) % h][x]
|
2019-12-09 15:29:35 +01:00
|
|
|
node.neighbors[Direction.DOWN.value] = \
|
2019-12-09 15:34:37 +01:00
|
|
|
self.graph[(y+1) % h][x]
|