-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
155 lines (107 loc) · 3.67 KB
/
utility.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
import random
from typing import Self
from BTP.BTP import Color, Vec, Win
from dataclasses import dataclass, field
from functools import wraps
import math
import time
SCALE = 6
TILE_SIZE = 16 * SCALE
class DungeonScreens:
NONE = "none"
MENU = "menu"
GAME = "game"
MAP_CREATOR = "creator"
class DungeonActionTypes:
COLLISION = "collision"
COLLISION_IN = "collision_in"
AROUND = "around"
COLLECT = "collect"
@staticmethod
def all():
return [DungeonActionTypes.COLLISION, DungeonActionTypes.COLLISION_IN, DungeonActionTypes.AROUND, DungeonActionTypes.COLLECT]
class DungeonRoleTypes:
NONE = "none"
PLAYER = "player"
MONSTER = "monster"
@dataclass
class DungeonActionData:
role: DungeonRoleTypes = field(default=DungeonRoleTypes.NONE)
class Keyboard:
LEFT = 263
RIGHT = 262
UP = 265
DOWN = 264
SPACE = 32
CTRL_R = 345
CTRL_L = 341
ENTER = 257
BLACK = Color(20, 20, 20, 255)
WHITE = Color(250, 249, 246, 255)
def draw_key_interract(btp: Win, key: str, position: Vec):
tsize = btp.text_size(key, 20)
size = tsize + Vec(10)
btp.draw_rectround(position, size, 0.1, BLACK)
center = (size-tsize)/2
btp.draw_text(key, position + center, 20, WHITE)
def center_rect(p1: Vec, s1: Vec, s2: Vec):
return p1 + (s1-s2)/2
def reduce_box(p1: Vec, s1: Vec, reduce: Vec):
return (p1 + s1 * reduce, s1 * (Vec(1) - reduce))
def rotate_around(pos: Vec, origin: Vec, deg: float) -> Vec:
theta = (deg*math.pi)/180
newx = math.cos(theta) * (pos.x-origin.x) - \
math.sin(theta) * (pos.y-origin.y) + origin.x
newy = math.sin(theta) * (pos.x-origin.x) + \
math.cos(theta) * (pos.y-origin.y) + origin.y
return Vec(newx, newy)
def rect_rect_distance(p1: Vec, s1: Vec, p2: Vec, s2: Vec):
dx = max(p1.x, p2.x) - min(p1.x + s1.x, p2.x + s2.x)
dy = max(p1.y, p2.y) - min(p1.y + s1.y, p2.y + s2.y)
return math.sqrt(dx**2 + dy**2)
def rect_rect_center(p1: Vec, s1: Vec, p2: Vec, s2: Vec):
center1 = Vec(p1.x + s1.x/2, p1.y + s1.y/2)
center2 = Vec(p2.x + s2.x/2, p2.y + s2.y/2)
return Vec(center2.x - center1.x, center2.y - center1.y)
def from_vec_str(s: str) -> Vec:
cls = s.replace('Vec(x=', '').replace(')', '').replace('y=', '').split(';')
return Vec(float(cls[0]), float(cls[1]))
def vec_floor(vec: Vec):
return Vec(math.floor(vec.x), math.floor(vec.y))
def vec_ceil(vec: Vec):
return Vec(math.ceil(vec.x), math.ceil(vec.y))
def vec_abs(vec: Vec):
return Vec(abs(vec.x), abs(vec.y))
class Stats:
def __init__(self, btp: Win) -> None:
self.btp = btp
self.data = {}
def __setitem__(self, item, value):
self.data[item] = value
def on_draw(self, position: Vec, size: float, color: Color):
text = ""
for key, val in self.data.items():
text += "{}: {}\n".format(key, val)
self.btp.draw_text(text, position, size, color)
self.data.clear()
def split_num(number, parts):
result = []
while number > 0:
if number < parts:
result.append(number)
break
result.append(parts)
number -= parts
return result
def is_in_view(btp: Win, position: Vec, size: Vec):
return btp.col_rect_rect(btp.camera_pos - btp.camera_offset, btp.get_render_size(), position, size)
def timedbg(func):
@wraps(func)
def timeit_wrapper(*args, **kwargs):
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
total_time = round((end_time - start_time) * 1000, 2)
print(f'{func.__name__} -> {total_time}ms')
return result
return timeit_wrapper