You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.2 KiB

def find_neighbors(self, coordinates) -> list:
"""
Find and return the cells adjacent to the cell at position (coordinates).
Adjacent cells are those directly above, below, left, right,
and all four diagonally adjacent cells.
"""
neighbors = []
x_coord, y_coord = coordinates
# Check all eight possible directions
for delta_x in [-1, 0, 1]:
for delta_y in [-1, 0, 1]:
new_x, new_y = x_coord + delta_x, y_coord + delta_y
# Skip the cell itself
if delta_x == 0 and delta_y == 0:
continue
# Check if the new coordinates are within the battlefield
if 0 <= new_x < self.width and 0 <= new_y < self.height:
if not self.battlefield[new_y][new_x]:
neighbors.append((new_x, new_y))
return neighbors
def get_closest_neighbor(self, neighbors_list, target_position):
"""
Find and return the neighbor cell in the neighbors_list closest to the target position.
"""
# If there are no neighbors, return None
if not neighbors_list:
return None
# Calculate the distance between the target position and each neighbor
distances = [((neighbor[0] - target_position[0])**2 + (neighbor[1] - target_position[1])**2)**0.5 for neighbor in neighbors_list]
# Find the index of the smallest distance
min_distance_index = distances.index(min(distances))
# Return the neighbor corresponding to the smallest distance
return neighbors_list[min_distance_index]
def iso_transform(self, x, y):
screen_x = (x - y) * self.cell_width / 2 + self.view_offset_x
screen_y = (x + y) * self.cell_height / 2 + self.view_offset_y
return screen_x, screen_y
def inv_iso_transform(self, screen_x, screen_y):
y = ((screen_x - self.view_offset_x) / (self.cell_width / 2) + (screen_y - self.view_offset_y) / (self.cell_height / 2)) / 2
x = ((screen_y - self.view_offset_y) / (self.cell_height / 2) - (screen_x - self.view_offset_x) / (self.cell_width / 2)) / 2
return int(x), int(y)
def get_distance(self, position1, position2):
x1, y1 = position1
x2, y2 = position2
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5