-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.py
315 lines (273 loc) · 8.29 KB
/
scripts.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import math, logging
from copy import copy
from functools import partial
from ecs.managers import EntityManager
from ecs.exceptions import NonexistentComponentTypeForEntity
from copanzers.components import (Position,
Destroyed,
Movement,
Hitbox,
Weapon,
Health,
Vision,
Mount,
Tags)
def get_logger (name):
"""
name -- str, name of the script routine this logger is for
"""
return logging.getLogger (__name__ + "." + name)
def unsure (meth):
def f (*args, **kw):
try:
return meth (*args, **kw)
except NonexistentComponentTypeForEntity:
raise AttributeError ("'{}' object has no attribute '{}'".format (
args [0].__class__.__name__, meth.__name__)) from None
f.__name__ = meth.__name__
return f
class RadarInterface:
"""
limited read only interface to the components of a entity
"""
_created = {}
def __new__ (cls, *args):
try:
return cls._created [args]
except KeyError:
ins = super ().__new__ (cls)
cls._created [args] = ins
return ins
def __init__ (self, entity, entity_manager):
"""
entity -- ecs.models.Entity, the entity this
interface represents
entity_manager -- ecs.managers.EntityManager, the manager
where we get the entities components from
"""
self.e = entity
self.eman = entity_manager
def __eq__ (self, other):
if not isinstance (other, RadarInterface):
return False
else:
return self.e == other.e
def __hash__ (self):
return int (self.e._guid)
@property
@unsure
# not exactly happy with that, maybe unsure should just raise an error
# when someone tries to access values on a destroyed entity
def destroyed (self):
return Destroyed in self.eman.database and self.e in self.eman.database [Destroyed]
@property
@unsure
def position (self):
"""
Position of the entity.
"""
# we make a shallow copy here so the script routine cannot modify the
# position of themselves or other entities
return copy (self.eman.component_for_entity (self.e, Position))
# TODO: We should maybe think about making the interfaces flyweights
class ROInterface (RadarInterface):
"""
read only interface to the components of a entity
"""
_created = {}
def __getitem__ (self, i):
try:
return self.eman.component_for_entity (self.e, Tags) [i]
except NonexistentComponentTypeForEntity:
raise KeyError (i)
@property
@unsure
def hp (self):
"""
Current hit points of entity.
"""
return self.eman.component_for_entity (self.e, Health).hp
@property
@unsure
def max_hp (self):
"""
Maximum hit points of entity.
"""
return self.eman.component_for_entity (self.e, Health).max_hp
@property
@unsure
def size (self):
"""
Size of the hit box of the entity.
"""
return self.eman.component_for_entity (self.e, Hitbox).size
@property
@unsure
def rotation (self):
"""
Rotation of the entity, in radians, rotation 0 is parallel to the
x-axis.
"""
return self.eman.component_for_entity (self.e, Movement).angle
@property
@unsure
def mounts (self):
"""
List of either interfaces of mounted entities or None if the respective
mount point is empty.
"""
# TODO: not optimal to instanstiate the interface on every call again
return [ROInterface (m, self.eman) if m else None
for m in self.eman.component_for_entity (self.e, Mount).mounts]
@property
@unsure
def root (self):
"""
Entity this entity is mounted on.
"""
return self.eman.component_for_entity (self.e, Mountable).root
class RWInterface (ROInterface):
"""
read/write interface to the components of a entity, intended to be used by
the script routines
"""
_created = {}
def __init__ (self, *args, **kw):
super ().__init__ (*args, **kw)
self.__throttle = 0
@ROInterface.rotation.setter
@unsure
def rotation (self, val):
self.eman.component_for_entity (self.e, Movement).angle = val
@property
@unsure
def speed (self):
"""
Speed of the entity as a scalar, in px/s.
"""
return self.eman.component_for_entity (self.e, Movement).length
@speed.setter
@unsure
def speed (self, val):
self.eman.component_for_entity (self.e, Movement).length = val
@property
@unsure
def max_speed (self):
"""
Maximum speed of the entity as a scalar, in px/s.
"""
return self.eman.component_for_entity (self.e,
Movement).max_speed
@property
@unsure
def velocity (self):
"""
Velocity of the entity as a Vec2d, in px/s.
"""
return self.eman.component_for_entity (self.e, Movement)
@property
@unsure
def throttle (self):
"""
speed of the entity in percent, getter clamps
value between 0 and 1
"""
return self.__throttle
@throttle.setter
@unsure
def throttle (self, val):
self.__throttle = min (1, max (0, val))
mov = self.eman.component_for_entity (self.e, Movement)
mov.length = val * mov.max_speed
@property
@unsure
def mounts (self):
"""
List of either interfaces of mounted entities or None if the respective
mount point is empty.
"""
return [RWInterface (m, self.eman) if m else None
for m in self.eman.component_for_entity (self.e, Mount).mounts]
@unsure
def shoot (self):
"""
Shoots a bullet if this entity is a weapon.
"""
self.eman.component_for_entity (self.e, Weapon).triggered = True
@property
@unsure
def till_reloaded (self):
"""
Seconds until this weapon can fire again.
"""
return self.eman.component_for_entity (self.e, Weapon).till_reloaded
@property
@unsure
def reload_time (self):
"""
Seconds between shots.
"""
return self.eman.component_for_entity (self.e, Weapon).reload_time
@property
@unsure
def bullet_damage (self):
"""
Damage done by the bullets of this weapon.
"""
return self.eman.component_for_entity (
self.e,
Weapon
).bullet_properties [0]
@property
@unsure
def bullet_speed (self):
"""
Speed of the bullets of this weapon, in px/s.
"""
return self.eman.component_for_entity (
self.e,
Weapon
).bullet_properties [1]
@property
@unsure
def bullet_hp (self):
"""
Hit points of the bullets of this weapon.
"""
return self.eman.component_for_entity (
self.e,
Weapon
).bullet_properties [2]
@property
@unsure
def bullet_size (self):
"""
Size of the bullets of this weapon.
"""
return self.eman.component_for_entity (
self.e,
Weapon
).bullet_properties [3]
@property
@unsure
def visible (self):
"""
Iterator over all living entities that are visible to this entity.
"""
return (i for i in self.eman.component_for_entity (self.e, Vision).visible
if not i.destroyed)
@property
@unsure
def vision (self):
"""
What kind of vision this entity has.
"""
return self.eman.component_for_entity (self.e, Vision).kind
@property
@unsure
def visualrange (self):
# hate this name
"""
How far this entity can see, in px.
"""
return self.eman.component_for_entity (self.e, Vision).reach