-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
executable file
·172 lines (146 loc) · 6.02 KB
/
world.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from bbox import *
from building_factory import *
from typing import List
from player import Player
from unit_factory import UnitFactory, Unit
class DebugObject(GameObject):
def __init__(self, screen, x, y, color):
self.s = pygame.Surface((5, 5))
self.s.fill(color)
self.x = x
self.y = y
self.screen = screen
def step(self, delta_t):
pass
def render(self):
self.screen.blit(self.s, (self.x, self.y))
class World:
objects: List[GameObject] = []
alive_objects: List[GameObject] = []
# selected_objects: List[GameObject] = []
debug_objects: List[GameObject] = []
dying_objects: List[GameObject] = []
bbox: CircleBBox
players: List[Player]
man_factory: UnitFactory
building_factory: BuildingFactory
def __init__(self, screen: pygame.Surface):
self.screen = screen
c = screen.get_size()
self.bbox = CircleBBox(c[0] / 2, c[1] / 2, c[1] / 2)
self.building_factory = BuildingFactory(screen, self)
self.man_factory = UnitFactory(screen, self)
def step(self, elapsed_time):
for obj in self.alive_objects:
# Filtering out dying objects
if obj.is_dying():
self.alive_objects.remove(obj)
self.dying_objects.append(obj)
try:
self.selected_objects.remove(obj)
except ValueError:
pass
obj.step(elapsed_time)
for obj in self.dying_objects:
if obj.is_dead():
self.dying_objects.remove(obj)
self.objects.remove(obj)
def render(self):
self.objects.sort(key=lambda x: x.get_y())
for obj in self.objects:
obj.render()
for obj in self.debug_objects:
obj.render()
def create_man(self, name, player, pos):
man = self.man_factory.create_man(name, player, pos)
self.objects.append(man)
self.alive_objects.append(man)
def create_building(self, name, player, pos):
building = self.building_factory.create_building(name, player, pos)
self.objects.append(building)
self.alive_objects.append(building)
def move_selected(self, selected_objects, coordinates):
if len(selected_objects) == 0:
return
# Creating target position rectangle
units_num = len(selected_objects)
center = (sum(obj.get_bbox().x for obj in selected_objects) / units_num,
sum(obj.get_bbox().y for obj in selected_objects) / units_num)
angle = math.atan2(coordinates[1] - center[1],
coordinates[0] - center[0])
position_distance = 30
positions = []
for j in range(int(math.ceil(math.sqrt(units_num))) - 1, -1, -1):
for i in range(int(math.ceil(math.sqrt(units_num)))):
x = position_distance * ((j - int(math.ceil(math.sqrt(units_num))) / 2) * math.cos(angle) -
(i - int(math.ceil(math.sqrt(units_num))) / 2) * math.sin(angle)) \
+ coordinates[0]
y = position_distance * ((j - int(math.ceil(math.sqrt(units_num))) / 2) * math.sin(angle) +
(i - int(math.ceil(math.sqrt(units_num))) / 2) * math.cos(angle)) \
+ coordinates[1]
positions.append([x, y])
# self.debug_objects.clear()
# self.debug_objects.append(DebugObject(self.screen, positions[0][0], positions[0][1], (255, 25, 255)))
# Move units
for i, obj in enumerate(selected_objects):
if isinstance(obj, Unit):
obj.go_to(positions[i])
def select_objects(self, rect: pygame.Rect):
selected_objects = []
if rect.w < 2 and rect.h < 2:
# Click case
mouse_bbox = CircleBBox(rect.x, rect.y, PIXEL_SCALE * 2)
min_dist = 999999
min_object = None
for obj2 in self.alive_objects:
dist = mouse_bbox.distance_to(obj2.get_bbox())
if min_dist > dist:
min_dist = dist
min_object = obj2
if min_dist <= 20:
selected_objects = [min_object]
min_object.set_is_selected(True)
else:
# Rect case
for obj in self.alive_objects:
if rect.collidepoint(obj.get_bbox().x, obj.get_bbox().y):
selected_objects.append(obj)
obj.set_is_selected(True)
return selected_objects
def request_move(self, obj, bbox1):
'''
if not bbox1.is_collision(self.bbox) and \
obj.get_bbox().is_collision(self.bbox):
return False
for i in self.alive_objects:
bbox2 = i.get_bbox()
if i != obj and \
bbox1.is_collision(bbox2) and \
not obj.get_bbox().is_collision(bbox2):
return False
'''
return True
def find_closest_enemy(self, obj: Unit):
obj_owner = obj.get_owner()
min_dist = 999999
min_object = None
for obj2 in self.alive_objects:
if obj_owner != obj2.get_owner():
dist = obj.get_bbox().distance_to(obj2.get_bbox())
if min_dist > dist:
min_dist = dist
min_object = obj2
return min_object if min_dist <= obj.get_line_of_sight() else None
def remove_selected(self):
for obj in self.selected_objects:
obj.set_health(obj.get_health() - 10)
def create_ore(self):
self.objects.append(Ore(self.screen, self, [random.randint(350, 1800), random.randint(300, 800)]))
def check_ore_collision(self, ore):
for i in self.selected_objects:
bbox1 = i.get_bbox()
if ore.get_bbox().is_collision(bbox1):
return True
def get_selected_type(self):
if self.selected_objects:
return type(self.selected_objects[0])