WMICraft/logic/spawner.py
2022-04-10 20:28:50 +02:00

34 lines
1.2 KiB
Python

import random
from common.constants import COLUMNS, ROWS
class Spawner:
def __init__(self, map):
self.map = map
def __is_free_field(self, field):
return field == ' '
def spawn_in_area(self, objects: list, spawn_area_pos_row=0, spawn_area_pos_column=0, spawn_area_width=0,
spawn_area_height=0, size=1):
spawned_objects_count = 0
while spawned_objects_count != len(objects):
x = random.randint(0, spawn_area_height) + spawn_area_pos_row
y = random.randint(0, spawn_area_width) + spawn_area_pos_column
if x < ROWS-1 and y < COLUMNS-1 and self.__is_free_field(self.map[x][y]):
for i in range(size):
for j in range(size):
self.map[x-i][y-j] = objects[spawned_objects_count]
spawned_objects_count += 1
def spawn_where_possible(self, objects: list):
spawned_objects_count = 0
while spawned_objects_count != len(objects):
x = random.randint(0, ROWS-1)
y = random.randint(0, COLUMNS-1)
if self.__is_free_field(self.map[x][y]):
self.map[x][y] = objects[spawned_objects_count]
spawned_objects_count += 1