SI_2020/agent.py
2020-04-07 01:18:04 +02:00

60 lines
1.8 KiB
Python

from warehouse import Coordinates, Tile
class Agent:
def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
self.x = start_x
self.y = start_y
self.direction = 'down'
self.radius = radius
self.assigned_warehouse = assigned_warehouse
self.is_loaded = False
def move(self):
next_coords = self.get_next_move_coordinates()
can_move = self.check_if_can_move(next_coords)
if can_move:
self.x = next_coords.x
self.y = next_coords.y
def get_next_move_coordinates(self):
direction_moves = {
'up': self.move_up(),
'down': self.move_down(),
'left': self.move_left(),
'right': self.move_right()
}
next_coords = direction_moves.get(self.direction, Coordinates(self.x, self.y))
return next_coords
def check_if_can_move(self, next_coords: Coordinates):
# import pdb
# pdb.set_trace()
next_tile = self.assigned_warehouse.tiles[next_coords.x][next_coords.y]
tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable
return tile_passable
def move_right(self):
pos_x = self.x + 1
if pos_x > self.assigned_warehouse.width - 1:
pos_x = self.assigned_warehouse.width - 1
return Coordinates(x=pos_x, y=self.y)
def move_left(self):
pos_x = self.x - 1
if pos_x < 0:
pos_x = 0
return Coordinates(x=pos_x, y=self.y)
def move_down(self):
pos_y = self.y + 1
if pos_y > self.assigned_warehouse.height - 1:
pos_y = self.assigned_warehouse.height - 1
return Coordinates(x=self.x, y=pos_y)
def move_up(self):
pos_y = self.y - 1
if pos_y < 0:
pos_y = 0
return Coordinates(x=self.x, y=pos_y)