Pulling the pathing code out into a separate module is better.

master
Zed A. Shaw 3 days ago
parent 7133078a1f
commit 7fa0403a28
  1. 42
      phase_13/ecs.py
  2. 41
      phase_13/pathing.py

@ -1,5 +1,6 @@
import utils
import numpy as np
import pathing
class Player:
def __init__(self, x, y):
@ -48,50 +49,11 @@ class Systems:
self.width = self.map.width
self.paths = np.full((self.height, self.width), utils.PATH_LIMIT, dtype=int)
def add_neighbors(self, neighbors, closed, near_y, near_x):
points = utils.compass(near_x, near_y)
for x,y in points:
if self.map.inbounds(x,y) and closed[y, x] == utils.SPACE:
closed[y, x] = utils.WALL
neighbors.append([x,y])
def path_enemies(self, in_grid):
self.paths.fill(utils.PATH_LIMIT)
closed = self.map.map.copy()
starting_pixels = []
open_pixels = []
counter = 0
while counter < self.height * self.width:
x = counter % self.width
y = counter // self.width
if in_grid[y, x] == 0:
self.paths[y, x] = 0
closed[y, x] = utils.WALL
starting_pixels.append([x,y])
counter += 1
for x, y in starting_pixels:
self.add_neighbors(open_pixels, closed, y, x)
counter = 1
while counter < utils.PATH_LIMIT and open_pixels:
next_open = []
for x,y in open_pixels:
self.paths[y, x] = counter
self.add_neighbors(next_open, closed, y, x)
open_pixels = next_open
counter += 1
for x, y in open_pixels:
self.paths[y, x] = counter
def move_enemies(self):
in_grid = np.full((self.map.height, self.map.width), 1, dtype=int)
in_grid[self.player.y, self.player.x] = 0
self.path_enemies(in_grid)
pathing.calculate_paths(self.map, self.paths, in_grid)
# for every enemy (actors[0] is player)
for enemy in self.actors[1:]:
nearby = utils.compass(enemy.x, enemy.y)

@ -0,0 +1,41 @@
import utils
def add_neighbors(the_map, neighbors, closed, near_y, near_x):
points = utils.compass(near_x, near_y)
for x,y in points:
if the_map.inbounds(x,y) and closed[y, x] == utils.SPACE:
closed[y, x] = utils.WALL
neighbors.append([x,y])
def calculate_paths(the_map, paths, in_grid):
height, width = paths.shape
paths.fill(utils.PATH_LIMIT)
closed = the_map.map.copy()
starting_pixels = []
open_pixels = []
counter = 0
while counter < height * width:
x = counter % width
y = counter // width
if in_grid[y, x] == 0:
paths[y, x] = 0
closed[y, x] = utils.WALL
starting_pixels.append([x,y])
counter += 1
for x, y in starting_pixels:
add_neighbors(the_map, open_pixels, closed, y, x)
counter = 1
while counter < utils.PATH_LIMIT and open_pixels:
next_open = []
for x,y in open_pixels:
paths[y, x] = counter
add_neighbors(the_map, next_open, closed, y, x)
open_pixels = next_open
counter += 1
for x, y in open_pixels:
paths[y, x] = counter
Loading…
Cancel
Save