-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnodes.py
164 lines (135 loc) · 6.38 KB
/
nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pygame
from vector import Vector2
from constants import *
import numpy as np
class Node(object):
def __init__(self, x, y):
self.position = Vector2(x, y)
self.neighbors = {UP:None, DOWN:None, LEFT:None, RIGHT:None, PORTAL:None}
self.access = {UP:[PACMAN, BLINKY, PINKY, INKY, CLYDE, FRUIT],
DOWN:[PACMAN, BLINKY, PINKY, INKY, CLYDE, FRUIT],
LEFT:[PACMAN, BLINKY, PINKY, INKY, CLYDE, FRUIT],
RIGHT:[PACMAN, BLINKY, PINKY, INKY, CLYDE, FRUIT]}
def denyAccess(self, direction, entity):
if entity.name in self.access[direction]:
self.access[direction].remove(entity.name)
def allowAccess(self, direction, entity):
if entity.name not in self.access[direction]:
self.access[direction].append(entity.name)
def render(self, screen):
for n in self.neighbors.keys():
if self.neighbors[n] is not None:
line_start = self.position.asTuple()
line_end = self.neighbors[n].position.asTuple()
pygame.draw.line(screen, WHITE, line_start, line_end, 4)
pygame.draw.circle(screen, RED, self.position.asInt(), 12)
class NodeGroup(object):
def __init__(self, level):
self.level = level
self.nodesLUT = {}
self.nodeSymbols = ['+', 'P', 'n']
self.pathSymbols = ['.', '-', '|', 'p']
data = self.readMazeFile(level)
self.createNodeTable(data)
self.connectHorizontally(data)
self.connectVertically(data)
self.homekey = None
def readMazeFile(self, textfile):
return np.loadtxt(textfile, dtype='<U1')
def createNodeTable(self, data, xoffset=0, yoffset=0):
for row in list(range(data.shape[0])):
for col in list(range(data.shape[1])):
if data[row][col] in self.nodeSymbols:
x, y = self.constructKey(col+xoffset, row+yoffset)
self.nodesLUT[(x, y)] = Node(x, y)
def constructKey(self, x, y):
return x * TILEWIDTH, y * TILEHEIGHT
def connectHorizontally(self, data, xoffset=0, yoffset=0):
for row in list(range(data.shape[0])):
key = None
for col in list(range(data.shape[1])):
if data[row][col] in self.nodeSymbols:
if key is None:
key = self.constructKey(col+xoffset, row+yoffset)
else:
otherkey = self.constructKey(col+xoffset, row+yoffset)
self.nodesLUT[key].neighbors[RIGHT] = self.nodesLUT[otherkey]
self.nodesLUT[otherkey].neighbors[LEFT] = self.nodesLUT[key]
key = otherkey
elif data[row][col] not in self.pathSymbols:
key = None
def connectVertically(self, data, xoffset=0, yoffset=0):
dataT = data.transpose()
for col in list(range(dataT.shape[0])):
key = None
for row in list(range(dataT.shape[1])):
if dataT[col][row] in self.nodeSymbols:
if key is None:
key = self.constructKey(col+xoffset, row+yoffset)
else:
otherkey = self.constructKey(col+xoffset, row+yoffset)
self.nodesLUT[key].neighbors[DOWN] = self.nodesLUT[otherkey]
self.nodesLUT[otherkey].neighbors[UP] = self.nodesLUT[key]
key = otherkey
elif dataT[col][row] not in self.pathSymbols:
key = None
def getStartTempNode(self):
nodes = list(self.nodesLUT.values())
return nodes[0]
def setPortalPair(self, pair1, pair2):
key1 = self.constructKey(*pair1)
key2 = self.constructKey(*pair2)
if key1 in self.nodesLUT.keys() and key2 in self.nodesLUT.keys():
self.nodesLUT[key1].neighbors[PORTAL] = self.nodesLUT[key2]
self.nodesLUT[key2].neighbors[PORTAL] = self.nodesLUT[key1]
def createHomeNodes(self, xoffset, yoffset):
homedata = np.array([['X','X','+','X','X'],
['X','X','.','X','X'],
['+','X','.','X','+'],
['+','.','+','.','+'],
['+','X','X','X','+']])
self.createNodeTable(homedata, xoffset, yoffset)
self.connectHorizontally(homedata, xoffset, yoffset)
self.connectVertically(homedata, xoffset, yoffset)
self.homekey = self.constructKey(xoffset+2, yoffset)
return self.homekey
def connectHomeNodes(self, homekey, otherkey, direction):
key = self.constructKey(*otherkey)
self.nodesLUT[homekey].neighbors[direction] = self.nodesLUT[key]
self.nodesLUT[key].neighbors[direction*-1] = self.nodesLUT[homekey]
def getNodeFromPixels(self, xpixel, ypixel):
if (xpixel, ypixel) in self.nodesLUT.keys():
return self.nodesLUT[(xpixel, ypixel)]
return None
def getNodeFromTiles(self, col, row):
x, y = self.constructKey(col, row)
if (x, y) in self.nodesLUT.keys():
return self.nodesLUT[(x, y)]
return None
def denyAccess(self, col, row, direction, entity):
node = self.getNodeFromTiles(col, row)
if node is not None:
node.denyAccess(direction, entity)
def allowAccess(self, col, row, direction, entity):
node = self.getNodeFromTiles(col, row)
if node is not None:
node.allowAccess(direction, entity)
def denyAccessList(self, col, row, direction, entities):
for entity in entities:
self.denyAccess(col, row, direction, entity)
def allowAccessList(self, col, row, direction, entities):
for entity in entities:
self.allowAccess(col, row, direction, entity)
def denyHomeAccess(self, entity):
self.nodesLUT[self.homekey].denyAccess(DOWN, entity)
def allowHomeAccess(self, entity):
self.nodesLUT[self.homekey].allowAccess(DOWN, entity)
def denyHomeAccessList(self, entities):
for entity in entities:
self.denyHomeAccess(entity)
def allowHomeAccessList(self, entities):
for entity in entities:
self.allowHomeAccess(entity)
def render(self, screen):
for node in self.nodesLUT.values():
node.render(screen)