-
Notifications
You must be signed in to change notification settings - Fork 0
/
SugarCubesDeco.py
100 lines (91 loc) · 2.82 KB
/
SugarCubesDeco.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
from SugarCubes import *
monde = None
def initMonde(pMonde):
global monde
monde = pMonde
# Objets actifs (décorateurs)
#----------------------------
def active(pClass):
class classeComposee(Par, pClass):
def __init__(self, *args, **kwargs):
lList_nomMethode = [
ls_func
for ls_func in dir(pClass)
if callable(getattr(pClass, ls_func))
]
# printErr(lList_nomMethode)
lList_nomMethode_on_ = [
ls_func
for ls_func in lList_nomMethode
if ls_func[:4] == '_on_'
]
for ls_func in lList_nomMethode_on_:
# printErr(ls_func[4:])
setattr( pClass, ls_func, on(ls_func[4:])( getattr(pClass, ls_func) ) )
# lList_nomMethodeActive = [
# ls_func
# for ls_func in dir(pClass)
# if callable(getattr(pClass, ls_func)) and hasattr(getattr(pClass, ls_func), 'toCallNTimes')
# ]
lList_nomMethodeActive = [
ls_func
for ls_func in lList_nomMethode
if hasattr(getattr(pClass, ls_func), 'toCallNTimes')
]
lList_methodeActive = [
getattr(pClass, ls_methActive) for ls_methActive in lList_nomMethodeActive
]
lList_prog = [
Repeat( lFunc_methActive.toCallNTimes, *lFunc_methActive(self) )
for lFunc_methActive in lList_methodeActive
]
Par.__init__(self, *lList_prog)
pClass.__init__(self, *args, **kwargs)
global monde
if monde != None:
monde.addProgram(self)
def diffuseInNextInstant(self, ps_typeInfo, p_valInfo=None):
global monde
if monde != None:
monde.generateEvent(ps_typeInfo, p_valInfo)
else:
raise RuntimeError('il faut un monde global, pour utiliser self.diffuse')
return classeComposee
def activeSingle(pClass):
lClass_nouvelle = active(pClass)
lInstance = lClass_nouvelle()
return lClass_nouvelle
def repeat(pn_nombreFois):
def tempAuto(pMethod):
pMethod.toCallNTimes = pn_nombreFois
return pMethod
return tempAuto
def actionForever(pMethod):
# method.__self__ == objet sur lequel est la method
# fonction.__get__(obj) == bound method
def tempo(self):
return [ Action(pMethod.__get__(self)) ]
return repeat(forever)(tempo)
def publicVar(pMethod):
def tempo(self):
return [ Diffuse( pMethod.__name__, pMethod.__get__(self) ) ]
return repeat(forever)(tempo)
def listToOr(pList_s_typeInfo):
if len(pList_s_typeInfo) == 1:
return pList_s_typeInfo[0]
else:
return Or(*pList_s_typeInfo)
def on(*pList_s_typeInfo):
def transforme(pMethod):
def tempo(self):
def pMethode_transformee(pDictList_valRecue):
return pMethod(
self,
*[
pDictList_valRecue[ls_typeInfo] if ls_typeInfo in pDictList_valRecue else []
for ls_typeInfo in pList_s_typeInfo
]
)
return [ ActionOn(listToOr(pList_s_typeInfo), pMethode_transformee) ]
return repeat(forever)(tempo)
return transforme