This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathui_offscreen_units.py
92 lines (79 loc) · 3.33 KB
/
ui_offscreen_units.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
from collections import defaultdict
from PyQt5 import QtWidgets
from aoc_object_consts import GROUPS
from ui_icon_offscreen_unit import IconOffscreenUnit
class OverlayOffscreenUnits(QtWidgets.QWidget):
def __init__(self, name, parent):
super(OverlayOffscreenUnits, self).__init__(parent)
self.parent = parent
self.get_icons = lambda: {}
# Sets the widget to be on the whole gamescreen.
self.icons = {}
self.update_position()
def update_position(self):
self.setGeometry(0, 32, self.parent.width(), self.parent.height() - 32 - 175)
def set_movable(self, *arg):
# This widget is not movable!
pass
def update(self):
for icon in self.icons:
self.icons[icon].delete = True
new_icons = self.get_icons()
# Need to transform the screen into game tiles
width = (self.width()) / 90 # Transform the width to the ratio. We got squares now
height = (self.height()) / 45 # UI height contains a bar (32px) and bottom UI (175px)
# print(width, height, width/height)
c_w = width / 2
c_h = height / 2
# Some weird math. I have no theoretical idea how it works. But hey, it works!
gx, gy = self.parent.game.screen_position
c_a = gx + c_w + gy + c_w
c_b = -gx - c_h + gy - c_h + 2
c_c = gx - c_w + gy - c_w
c_d = -gx + c_h + gy + c_h + 1
for game_obj in new_icons:
x, y = game_obj.position
xy = x + y
xy_diff = y - x
if c_a > xy and c_b < xy_diff and c_c < xy and c_d > xy_diff:
# print("ON SCREEN")
pass
else:
group = defaultdict(str, GROUPS)[game_obj.group]
if game_obj not in self.icons:
self.icons[game_obj] = IconOffscreenUnit(self, game_obj)
self.icons[game_obj].bottom_text = group
self.icons[game_obj].redraw()
elif group != self.icons[game_obj].bottom_text:
self.icons[game_obj].bottom_text = group
self.icons[game_obj].redraw()
# print("NOT ON SCREEN")
tx, ty = x - gx, y - gy
k = abs(tx / ty) / 2 if abs(tx) < abs(ty) else 1 - abs(ty / tx) / 2 # might divide by zero
if tx < 0 and ty < 0:
# print("LEFT", k)
self.icons[game_obj].update_left(k)
elif tx < 0 and ty > 0:
k = 1 - k
self.icons[game_obj].update_bottom(k)
elif tx > 0 and ty > 0:
k = 1 - k
# print("RIGHT", k)
self.icons[game_obj].update_right(k)
else: # tx > 0
# print("TOP", k)
self.icons[game_obj].update_top(k)
self.icons[game_obj].delete = False
# stupid way how to get rid of not selcted units
remove_list = []
for icon in self.icons:
if self.icons[icon].delete:
remove_list += [icon]
if remove_list:
# Remove the stuff..
for i in remove_list:
self.icons[i].deleteLater()
del self.icons[i]
# print(self.icons)
if __name__ == '__main__':
pass