5
5
6
6
from simple_playgrounds .entities .scene_elements .element import SceneElement
7
7
from simple_playgrounds .utils .definitions import CollisionTypes
8
- from simple_playgrounds .utils .position_utils import PositionAreaSampler
8
+ from simple_playgrounds .utils .position_utils import PositionAreaSampler
9
9
from simple_playgrounds .entities .agents .parts import Part
10
10
from simple_playgrounds .playgrounds .playground import Playground
11
11
12
12
#pylint: disable=line-too-long
13
13
14
+
14
15
class InteractiveSceneElement (SceneElement , ABC ):
15
16
"""Base Class dor InteractiveSceneElements"""
16
17
interactive = True
@@ -19,6 +20,8 @@ def __init__(self, **kwargs):
19
20
SceneElement .__init__ (self , ** kwargs )
20
21
self .pm_interaction_shape .collision_type = CollisionTypes .INTERACTIVE
21
22
23
+ self .activated = False
24
+
22
25
@abstractmethod
23
26
def activate (self , activating_entity ):
24
27
"""
@@ -47,6 +50,10 @@ def reward(self):
47
50
def reward (self , rew ):
48
51
...
49
52
53
+ def pre_step (self ):
54
+ self .activated = False
55
+
56
+
50
57
51
58
class Lever (InteractiveSceneElement ):
52
59
"""Lever Entities provide a reward when activated."""
@@ -67,7 +74,7 @@ def __init__(self, initial_position, **kwargs):
67
74
self .reward_provided = False
68
75
69
76
def pre_step (self ):
70
-
77
+ super (). pre_step ()
71
78
self .reward_provided = False
72
79
73
80
@property
@@ -148,7 +155,7 @@ def activate(self, _):
148
155
list_remove = []
149
156
list_add = []
150
157
151
- if len (self .produced_entities ) < self .production_limit :
158
+ if len (self .produced_entities ) < self .production_limit and self . activated is False :
152
159
153
160
if self .local_dispenser :
154
161
initial_position = self .location_sampler .sample ([self .position [0 ], self .position [1 ]])
@@ -161,6 +168,8 @@ def activate(self, _):
161
168
self .produced_entities .append (obj )
162
169
list_add = [obj ]
163
170
171
+ self .activated = True
172
+
164
173
return list_remove , list_add
165
174
166
175
def reset (self ):
@@ -256,9 +265,10 @@ def activate(self, activating_entity):
256
265
list_add = []
257
266
list_remove = []
258
267
259
- if activating_entity in self .accepted_coins .copy ():
268
+ if activating_entity in self .accepted_coins .copy () and self . activated is False :
260
269
list_remove = [activating_entity ]
261
270
self .accepted_coins .remove (activating_entity )
271
+ self .activated = True
262
272
263
273
return list_remove , list_add
264
274
@@ -282,6 +292,10 @@ def __init__(self, initial_position, door, **kwargs):
282
292
Can be list [x,y,theta], AreaPositionSampler or Trajectory.
283
293
door: Door opened by the switch.
284
294
**kwargs: other params to configure entity. Refer to Entity class.
295
+
296
+ Notes:
297
+ It is possible to have multiple switches for a single door.
298
+ However the behavior is unstable in multiagent setting.
285
299
"""
286
300
287
301
default_config = self ._parse_configuration ('interactive' , 'switch' )
@@ -300,13 +314,16 @@ def activate(self, _):
300
314
list_add = []
301
315
list_remove = []
302
316
303
- if self .door .opened :
304
- self .door .opened = False
305
- list_add = [self .door ]
317
+ if self .activated is False :
318
+ if self .door .opened :
319
+ self .door .opened = False
320
+ list_add = [self .door ]
306
321
307
- else :
308
- self .door .opened = True
309
- list_remove = [self .door ]
322
+ else :
323
+ self .door .opened = True
324
+ list_remove = [self .door ]
325
+
326
+ self .activated = True
310
327
311
328
return list_remove , list_add
312
329
@@ -353,12 +370,13 @@ def activate(self, activating_entity):
353
370
list_remove = []
354
371
list_add = []
355
372
356
- if isinstance (activating_entity , Part ):
373
+ if isinstance (activating_entity , Part ) and self . activated is False :
357
374
if not self .door .opened :
358
375
self .door .opened = True
359
376
list_remove = [self .door ]
360
377
361
378
self ._reset_timer ()
379
+ self .activated = True
362
380
363
381
if isinstance (activating_entity , Playground ):
364
382
self .door .opened = False
@@ -373,13 +391,14 @@ def _reset_timer(self):
373
391
374
392
def pre_step (self ):
375
393
394
+ self .activated = False
376
395
if self .door .opened :
377
396
self .timer -= 1
378
397
379
398
def reset (self ):
380
399
381
400
self .timer = self .time_open
382
- self .door .close_door ()
401
+ self .door .opened = False
383
402
384
403
385
404
class Lock (InteractiveSceneElement ):
0 commit comments