/
ursinastuff.py
163 lines (120 loc) · 3.84 KB
/
ursinastuff.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
import sys
import os
import math
import random
import inspect
import importlib
import subprocess
import time
from pathlib import Path
from ursina import application
from ursina.text import Text
from ursina.window import instance as window
from ursina.scene import instance as scene
from ursina.sequence import Sequence, Func, Wait
class Empty():
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key ,value)
def invoke(function, *args, **kwargs):
delay = 0
if 'delay' in kwargs:
delay = kwargs['delay']
del kwargs['delay']
if not delay:
function(*args, **kwargs)
return function
s = Sequence(
Wait(delay),
Func(function, *args, **kwargs)
)
s.start()
return s
def destroy(entity, delay=0):
if delay == 0:
_destroy(entity)
return
s = Sequence(
Wait(delay),
Func(_destroy, entity)
)
s.start()
return s
def _destroy(entity, force_destroy=False):
if not entity:
return
if entity.eternal and not force_destroy:
return
if hasattr(entity, 'stop'):
entity.stop(False)
# return
for c in entity.children:
destroy(c)
if entity in scene.entities:
scene.entities.remove(entity)
if hasattr(entity, 'on_destroy'):
entity.on_destroy()
if hasattr(entity, 'scripts'):
for s in entity.scripts:
del s
if hasattr(entity, 'animations'):
for anim in entity.animations:
anim.finish()
anim.kill()
if hasattr(entity, 'tooltip'):
destroy(entity.tooltip)
if hasattr(entity, '_on_click') and isinstance(entity._on_click, Sequence):
entity._on_click.kill()
entity.removeNode()
#unload texture
# if hasattr(entity, 'texture') and entity.texture != None:
# entity.texture.releaseAll()
del entity
def find_sequence(name, file_types, folders): # find frame_0, frame_1, frame_2 and so on
for folder in folders:
for file_type in file_types:
files = list(folder.glob(f'**/{name}*.{file_type}'))
if files:
return files
def import_all_classes(path=application.asset_folder, debug=False):
path = str(path)
sys.path.append(path)
from ursina.string_utilities import snake_to_camel
from glob import iglob
imported_successfully = list()
for file_path in iglob(path + '**/*.py', recursive=True):
if '\\build\\' in file_path or '__' in file_path:
continue
rel_path = file_path[len(path):][:-3].replace('\\', '.')
if rel_path.startswith('.'):
rel_path = rel_path[1:]
module_name = os.path.basename(file_path).split('.')[0]
class_name = snake_to_camel(module_name)
module_name = module_name
import_statement = 'from ' + rel_path + ' import *'
try:
exec(import_statement, globals())
imported_successfully.append(module_name)
if debug:
print(import_statement)
except:
if debug:
print(' x', import_statement)
pass
return imported_successfully
def print_on_screen(text, position=window.top_left, origin=(-.5,.5), scale=1, duration=1):
text_entity = Text(text=text, position=position, origin=origin, scale=scale)
destroy(text_entity, delay=duration)
if __name__ == '__main__':
from ursina import *
app = Ursina()
def test_func(item, x=None, y=None):
print(item, x, y)
test_func('test')
invoke(test_func, 'test', delay=.1)
invoke(test_func, 'test1', 1, 2, delay=.2)
invoke(test_func, 'test2', x=1, y=2, delay=.3)
def input(key):
if key == 'space':
print_on_screen('debug message', position=(0,0), origin=(0,0), scale=2)
app.run()