WMICraft/logic/spawner.py

44 lines
2.0 KiB
Python
Raw Normal View History

import random
2022-03-11 19:42:17 +01:00
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)):
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":
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()
2022-03-24 12:54:22 +01:00
2022-03-24 13:09:43 +01:00
def spawn_monsters(self):
2022-03-24 12:54:22 +01:00
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" and not ((10 <=column <=14) and (8 <= row <= 12)):
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()