Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
Add fields_around() function to object
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinus4 committed Feb 9, 2023
1 parent 293c3dd commit 6405499
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/objects/npcs/enemies/bear.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,13 @@ def tick(self) -> None:
self.machine.tick()

def slam(self):
Slam(self, [self.properties.position + Direction.NORTH.value,
self.properties.position + Direction.WEST.value,
self.properties.position + Direction.SOUTH.value,
self.properties.position + Direction.EAST.value]).execute()
Slam(self, self.fields_around()).execute()

def prepare_to_slam(self):
PrepareToSlam(self).execute()

def is_there_agent_to_slam(self):
points_around = [self.properties.position + Direction.NORTH.value,
self.properties.position + Direction.WEST.value,
self.properties.position + Direction.SOUTH.value,
self.properties.position + Direction.EAST.value]
for target_position in points_around:
for target_position in self.fields_around():
if not self.scene._objects_position_map.get(target_position):
continue
for target in self.scene.get_objects_by_position(target_position):
Expand Down
12 changes: 12 additions & 0 deletions src/objects/object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List

from src.common.point import Point
from src.common.enums import Direction
from src.common.serializable import Serializable


Expand Down Expand Up @@ -32,6 +33,17 @@ def occupied_fields(self, current_position: Point = None) -> List[Point]:
current_position = self.properties.position
return [current_position]

def fields_around(self) -> List[Point]:
fields = []
for field in self.occupied_fields():
points_around_field = [field + Direction.NORTH.value,
field + Direction.WEST.value,
field + Direction.SOUTH.value,
field + Direction.EAST.value]
# append all points around that aren't in self.occupied_fields()
fields.extend(list(filter(lambda point: point not in self.occupied_fields(), points_around_field)))
return fields

def tick(self) -> None:
"""
Called every turn of the object to perform actions
Expand Down

0 comments on commit 6405499

Please sign in to comment.