WMICraft/logic/spawner.py
2022-03-21 18:39:28 +01:00

29 lines
1.2 KiB
Python

import random
from common.constants import GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT
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)):
if self.grid.grid[row][column].texture_path != "water.png" and self.grid.grid[row][column].texture_path != "grass_with_tree.jpg":
coords.append([(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING + 7,
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 7])
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()