|
27 | 27 | """
|
28 | 28 |
|
29 | 29 | # TODO
|
30 |
| -# Implement grabbing correctly. |
31 |
| -# When an object is grabbed, does it still have a location? |
32 |
| -# What if it is released? |
33 |
| -# What if the grabbed or the grabber is deleted? |
34 |
| -# What if the grabber moves? |
35 | 30 | # Speed control in GUI does not have any effect -- fix it.
|
36 | 31 |
|
37 | 32 | from utils import distance_squared, turn_heading
|
@@ -510,14 +505,17 @@ def execute_action(self, agent, action):
|
510 | 505 | agent.direction += Direction.L
|
511 | 506 | elif action == 'Forward':
|
512 | 507 | agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
|
513 |
| - # elif action == 'Grab': |
514 |
| - # things = [thing for thing in self.list_things_at(agent.location) |
515 |
| - # if agent.can_grab(thing)] |
516 |
| - # if things: |
517 |
| - # agent.holding.append(things[0]) |
| 508 | + elif action == 'Grab': |
| 509 | + things = [thing for thing in self.list_things_at(agent.location) if agent.can_grab(thing)] |
| 510 | + if things: |
| 511 | + agent.holding.append(things[0]) |
| 512 | + print("Grabbing ", things[0].__class__.__name__) |
| 513 | + self.delete_thing(things[0]) |
518 | 514 | elif action == 'Release':
|
519 | 515 | if agent.holding:
|
520 |
| - agent.holding.pop() |
| 516 | + dropped = agent.holding.pop() |
| 517 | + print("Dropping ", dropped.__class__.__name__) |
| 518 | + self.add_thing(dropped, location=agent.location) |
521 | 519 |
|
522 | 520 | def default_location(self, thing):
|
523 | 521 | location = self.random_location_inbounds()
|
@@ -569,10 +567,7 @@ def random_location_inbounds(self, exclude=None):
|
569 | 567 | def delete_thing(self, thing):
|
570 | 568 | """Deletes thing, and everything it is holding (if thing is an agent)"""
|
571 | 569 | if isinstance(thing, Agent):
|
572 |
| - for obj in thing.holding: |
573 |
| - super().delete_thing(obj) |
574 |
| - for obs in self.observers: |
575 |
| - obs.thing_deleted(obj) |
| 570 | + del thing.holding |
576 | 571 |
|
577 | 572 | super().delete_thing(thing)
|
578 | 573 | for obs in self.observers:
|
@@ -964,24 +959,10 @@ def execute_action(self, agent, action):
|
964 | 959 |
|
965 | 960 | if isinstance(agent, Explorer) and self.in_danger(agent):
|
966 | 961 | return
|
967 |
| - |
| 962 | + |
968 | 963 | agent.bump = False
|
969 |
| - if action == 'TurnRight': |
970 |
| - agent.direction += Direction.R |
971 |
| - agent.performance -= 1 |
972 |
| - elif action == 'TurnLeft': |
973 |
| - agent.direction += Direction.L |
974 |
| - agent.performance -= 1 |
975 |
| - elif action == 'Forward': |
976 |
| - agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location)) |
977 |
| - agent.performance -= 1 |
978 |
| - elif action == 'Grab': |
979 |
| - things = [thing for thing in self.list_things_at(agent.location) |
980 |
| - if agent.can_grab(thing)] |
981 |
| - if len(things): |
982 |
| - print("Grabbing", things[0].__class__.__name__) |
983 |
| - if len(things): |
984 |
| - agent.holding.append(things[0]) |
| 964 | + if action in ['TurnRight', 'TurnLeft', 'Forward', 'Grab']: |
| 965 | + super().execute_action(agent, action) |
985 | 966 | agent.performance -= 1
|
986 | 967 | elif action == 'Climb':
|
987 | 968 | if agent.location == (1, 1): # Agent can only climb out of (1,1)
|
|
0 commit comments