-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAction.py
123 lines (91 loc) · 3.86 KB
/
Action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import random
from abc import ABC, abstractmethod
from PathFinder import PathFinder
from SimulationEntity import Grass, Rock, Tree, Predator, Herbivore
class Action(ABC):
def __init__(self, simulation_map):
self.map = simulation_map
self.width = simulation_map.width
self.height = simulation_map.height
@abstractmethod
def perform(self):
"""Метод выполнения действия"""
pass
class GenerationAction(Action):
"""Класс генерации сущностей"""
def __init__(self, simulation_map):
super().__init__(simulation_map)
def random_empty_coordinates(self):
"""Метод возвращает случайные координаты"""
while True:
x = random.randint(0, self.width - 1)
y = random.randint(0, self.height - 1)
if self.map.is_empty((x, y)) is True:
break
return x, y
def generation(self):
"""Метод сохраняет сгенерированные координаты в __objects_by_coordinates"""
self.map.add_object(self.random_empty_coordinates(), self.create_object())
def create_object(self):
"""Метод создания сущности"""
pass
def perform(self):
"""Метод вызова функции генерации"""
for quantity in range(self.map.width * self.map.height // 10):
self.generation()
def spawn(self):
pass
class GenerationActionRock(GenerationAction):
def __init__(self, simulation_map):
super().__init__(simulation_map)
def create_object(self):
"""Метод создания сущности"""
return Rock(self.map)
class GenerationActionTree(GenerationAction):
def __init__(self, simulation_map):
super().__init__(simulation_map)
def create_object(self):
"""Метод создания сущности"""
return Tree(self.map)
class GenerationActionGrass(GenerationAction):
def __init__(self, simulation_map):
super().__init__(simulation_map)
def create_object(self):
"""Метод создания сущности"""
return Grass(self.map)
def spawn(self):
"""Метод реинкарнации травы"""
if 0 == self.map.counter_object(Grass):
self.perform()
class GenerationActionHerbivore(GenerationAction):
def __init__(self, simulation_map):
super().__init__(simulation_map)
def create_object(self):
"""Метод создания сущности"""
return Herbivore(self.map)
class GenerationActionPredator(GenerationAction):
def __init__(self, simulation_map):
super().__init__(simulation_map)
def create_object(self):
"""Метод создания сущности"""
return Predator(self.map)
class MoveCreaturesAction(Action):
def __init__(self, simulation_map, callback):
super().__init__(simulation_map)
self.path_finder = PathFinder(simulation_map)
self.callback = callback
def perform(self):
walking_creatures = []
for row in range(self.map.height):
for column in range(self.map.width):
entity = self.map.get_object((row, column))
if entity not in walking_creatures:
walking_creatures.append(entity)
if isinstance(entity, Herbivore):
way = self.path_finder.find_path((row, column), Grass)
self.map.move_entity(way[0], way[1])
self.callback()
elif isinstance(entity, Predator):
way = self.path_finder.find_path((row, column), Herbivore)
self.map.move_entity(way[0], way[1])
self.callback()