@@ -10,11 +10,27 @@ class _ProjectileMeta(type):
10
10
def __new__ (mcl , name , bases , odict ):
11
11
'''Create the class and create its methods dynamically'''
12
12
13
+ # Store values to use later
14
+ temp = {'_classname' : None , '_is_filters' : None , '_not_filters' : None }
15
+
16
+ # Loop through the base class attributes
17
+ for attribute in ('_classname' , '_is_filters' , '_not_filters' ):
18
+
19
+ # Try to store the odict's value of
20
+ # the attribute and delete it from odict
21
+ try :
22
+ temp [attribute ] = odict [attribute ]
23
+ del odict [attribute ]
24
+
25
+ # If the attribute is not found in odict, just leave it as None
26
+ except KeyError :
27
+ continue
28
+
13
29
# Create the object
14
30
cls = super ().__new__ (mcl , name , bases , odict )
15
31
16
32
# Is the the baseclass that uses the metaclass?
17
- if not bases :
33
+ if len ( bases ) != 1 or bases [ 0 ]. __name__ != '_ProjectileBase' :
18
34
19
35
# Do not add any methods
20
36
return cls
@@ -25,22 +41,30 @@ def __new__(mcl, name, bases, odict):
25
41
# Create the iterator <weapon>_indexes method
26
42
setattr (
27
43
cls , '{0}_indexes' .format (method_name ),
28
- lambda self : self ._projectile_indexes )
44
+ property (lambda self : cls ._projectile_indexes (
45
+ self , temp ['_classname' ],
46
+ temp ['_is_filters' ], temp ['_not_filters' ])))
29
47
30
48
# Create the get_<weapon>_indexes method
31
49
setattr (
32
50
cls , 'get_{0}_indexes' .format (method_name ),
33
- lambda self : self ._get_projectile_index_list ())
51
+ lambda self : cls ._get_projectile_index_list (
52
+ self , temp ['_classname' ],
53
+ temp ['_is_filters' ], temp ['_not_filters' ]))
34
54
35
55
# Create the get_<weapon>_count method
36
56
setattr (
37
57
cls , 'get_{0}_count' .format (method_name ),
38
- lambda self : self ._get_projectile_ammo ())
58
+ lambda self : cls ._get_projectile_ammo (
59
+ self , temp ['_classname' ],
60
+ temp ['_is_filters' ], temp ['_not_filters' ]))
39
61
40
62
# Create the set_<weapon>_count method
41
63
setattr (
42
64
cls , 'set_{0}_count' .format (method_name ),
43
- lambda self , value : self ._set_projectile_ammo (value ))
65
+ lambda self , value : cls ._set_projectile_ammo (
66
+ self , value , temp ['_classname' ],
67
+ temp ['_is_filters' ], temp ['_not_filters' ]))
44
68
45
69
# Return the new class
46
70
return cls
@@ -54,25 +78,21 @@ class _ProjectileBase(metaclass=_ProjectileMeta):
54
78
_is_filters = None
55
79
_not_filters = None
56
80
57
- def _projectile_indexes (self ):
81
+ def _projectile_indexes (self , classname , is_filters , not_filters ):
58
82
'''Iterates over all indexes the player owns for the projectile type'''
59
- return self .weapon_indexes (
60
- self ._classname , self ._is_filters , self ._not_filters )
83
+ return self .weapon_indexes (classname , is_filters , not_filters )
61
84
62
- def _get_projectile_index_list (self ):
85
+ def _get_projectile_index_list (self , classname , is_filters , not_filters ):
63
86
'''Returns a list of indexes the player owns for the projectile type'''
64
- return self .get_weapon_index_list (
65
- self ._classname , self ._is_filters , self ._not_filters )
87
+ return self .get_weapon_index_list (classname , is_filters , not_filters )
66
88
67
- def _get_projectile_ammo (self ):
89
+ def _get_projectile_ammo (self , classname , is_filters , not_filters ):
68
90
'''Returns the ammo amount the player has for the projectile type'''
69
- return self ._get_weapon_ammo (
70
- self ._classname , self ._is_filters , self ._not_filters )
91
+ return self ._get_weapon_ammo (classname , is_filters , not_filters )
71
92
72
- def _set_projectile_ammo (self , value ):
93
+ def _set_projectile_ammo (self , value , classname , is_filters , not_filters ):
73
94
'''Sets the ammo amount of the player for the projectile type'''
74
- self ._set_weapon_ammo (
75
- value , self ._classname , self ._is_filters , self ._not_filters )
95
+ self ._set_weapon_ammo (value , classname , is_filters , not_filters )
76
96
77
97
78
98
class _HEGrenade (_ProjectileBase ):
0 commit comments