2022-03-10 22:13:25 +01:00
|
|
|
import random
|
|
|
|
|
2022-03-11 19:42:17 +01:00
|
|
|
from common.constants import GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT
|
|
|
|
|
2022-03-10 22:13:25 +01:00
|
|
|
|
|
|
|
class Spawner:
|
|
|
|
def __init__(self, grid, objs_to_spawn_list: list, width, height, pos_row, pos_column):
|
|
|
|
self.objs_to_spawn_list = objs_to_spawn_list
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.pos_row = pos_row
|
|
|
|
self.pos_column = pos_column
|
|
|
|
self.grid = grid
|
|
|
|
|
|
|
|
def spawn(self):
|
|
|
|
coords = []
|
|
|
|
for row in range(self.pos_row, (self.pos_row + self.width)):
|
|
|
|
for column in range(self.pos_column, (self.pos_column + self.height)):
|
2022-03-21 18:39:28 +01:00
|
|
|
if self.grid.grid[row][column].texture_path != "water.png" and self.grid.grid[row][column].texture_path != "grass_with_tree.jpg":
|
2022-03-10 22:13:25 +01:00
|
|
|
coords.append([(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING + 7,
|
|
|
|
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 7])
|
|
|
|
|
2022-03-21 18:39:28 +01:00
|
|
|
for obj in self.objs_to_spawn_list:
|
|
|
|
random_tile = random.randint(0, len(coords) - 1)
|
|
|
|
obj.rect.x = coords[random_tile][0]
|
|
|
|
obj.rect.y = coords[random_tile][1]
|
|
|
|
coords.pop(random_tile)
|
|
|
|
obj.update()
|