add grid cleanup so pathing works more than once, fix matrix reverse obstacle/clear

This commit is contained in:
Xevion
2020-04-21 18:06:53 -05:00
parent 50f56e4190
commit 02a650d72b
3 changed files with 13 additions and 9 deletions

View File

@@ -121,7 +121,11 @@ class Game(arcade.Window):
# Draw paths for all mobs
for mob in self.enemy_list:
if mob.target is not None:
self.draw_path(mob.get_path())
t1 = time.time()
path = mob.get_path()
t2 = time.time()
print(f'Path acquired in {round(t2 - t1, 4)}s')
self.draw_path(path)
self.fps.tick()
except Exception:
@@ -136,9 +140,10 @@ class Game(arcade.Window):
"""
if len(path) > 2:
path = map(lambda point: ((0.5 + point[0]) * Config.TILE_SIZE, (0.5 + point[1]) * Config.TILE_SIZE), path)
arcade.draw_lines(list(path))
# for pos1, pos2 in zip(path, path[1:])
path = map(lambda point: ((point[0]) * Config.TILE_SIZE, (point[1]) * Config.TILE_SIZE), path)
path = list(path)
for pos1, pos2 in zip(path, path[1:]):
arcade.draw_line(*pos1, *pos2, color=arcade.color.RED)
def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """

View File

@@ -45,16 +45,16 @@ class Dungeon(object):
self.levels = [
[Level.load_file(x, y, center) for y in range(size)] for x in range(size)
]
self.matrix = [[0 for yy in range(size * 10)] for xx in range(10 * size)]
self.matrix = [[1 for yy in range(size * 10)] for xx in range(10 * size)]
for column in self.levels:
for level in column:
for xx in range(10):
for yy in range(10):
if level.structure[xx][yy] == 'w':
self.matrix[(level.x * 10) + xx][(level.y * 10) + yy] = 1
self.matrix[(level.x * 10) + xx][(level.y * 10) + yy] = 0
self.grid = Grid(matrix=self.matrix)
self.finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
# pprint(self.matrix, width=1000)
pprint(self.matrix, width=1000)
def getWalls(self) -> arcade.SpriteList:
"""

View File

@@ -56,10 +56,9 @@ class Mob(arcade.Sprite):
if end is None:
end = self.target.position
start, end = self.nearestPosition(), (round(end[0] / Config.TILE_SIZE), round(end[1] / Config.TILE_SIZE))
print(start, end)
start, end = self.dungeon.grid.node(*start), self.dungeon.grid.node(*end)
paths, runs = self.dungeon.finder.find_path(start, end, self.dungeon.grid)
print(paths, runs)
self.dungeon.grid.cleanup()
return paths