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:
|
2022-04-05 10:47:45 +02:00
|
|
|
def __init__(self, grid, objs_to_spawn_list: list, spawn_area_width=0, spawn_area_height=0, spawn_area_pos_row=0,
|
|
|
|
spawn_area_pos_column=0, spawn_where_possible=False): # kolumny i wiersze liczymy od 0;
|
2022-03-10 22:13:25 +01:00
|
|
|
self.objs_to_spawn_list = objs_to_spawn_list
|
2022-04-05 10:47:45 +02:00
|
|
|
self.spawn_area_width = spawn_area_width
|
|
|
|
self.spawn_area_height = spawn_area_height
|
|
|
|
self.spawn_area_pos_row = spawn_area_pos_row
|
|
|
|
self.spawn_area_pos_column = spawn_area_pos_column
|
2022-03-10 22:13:25 +01:00
|
|
|
self.grid = grid
|
2022-04-05 10:47:45 +02:00
|
|
|
self.spawn_where_possible = spawn_where_possible
|
2022-03-10 22:13:25 +01:00
|
|
|
|
|
|
|
def spawn(self):
|
2022-04-05 10:47:45 +02:00
|
|
|
if self.spawn_where_possible:
|
|
|
|
possible_fields = self.grid.free_fields
|
|
|
|
else:
|
|
|
|
possible_fields = []
|
|
|
|
for field in self.grid.free_fields:
|
|
|
|
if (self.spawn_area_pos_column <= field.column < (self.spawn_area_width + self.spawn_area_pos_column)) \
|
|
|
|
and (self.spawn_area_pos_row <= field.row < (self.spawn_area_height + self.spawn_area_pos_row)):
|
|
|
|
possible_fields.append(field)
|
2022-03-24 12:54:22 +01:00
|
|
|
|
|
|
|
for obj in self.objs_to_spawn_list:
|
2022-04-05 10:47:45 +02:00
|
|
|
ran_index = random.randint(0, len(possible_fields)-1)
|
|
|
|
ran_field = possible_fields[ran_index]
|
|
|
|
ran_field.busy = True
|
|
|
|
obj.rect = obj.rect.clamp(ran_field)
|
|
|
|
self.grid.busy_fields.append(ran_field)
|
|
|
|
if ran_field in self.grid.free_fields:
|
|
|
|
self.grid.free_fields.remove(ran_field)
|
|
|
|
if ran_field in possible_fields:
|
|
|
|
possible_fields.remove(ran_field)
|
2022-03-24 12:54:22 +01:00
|
|
|
obj.update()
|