SI_2020/agent.py

60 lines
1.8 KiB
Python
Raw Normal View History

2020-04-07 01:18:04 +02:00
from warehouse import Coordinates, Tile
2020-04-02 15:25:15 +02:00
class Agent:
def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
2020-04-02 15:25:15 +02:00
self.x = start_x
self.y = start_y
2020-04-07 01:18:04 +02:00
self.direction = 'down'
self.radius = radius
self.assigned_warehouse = assigned_warehouse
self.is_loaded = False
2020-04-07 01:18:04 +02:00
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):
2020-04-07 01:18:04 +02:00
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):
2020-04-07 01:18:04 +02:00
pos_x = self.x - 1
if pos_x < 0:
pos_x = 0
return Coordinates(x=pos_x, y=self.y)
def move_down(self):
2020-04-07 01:18:04 +02:00
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):
2020-04-07 01:18:04 +02:00
pos_y = self.y - 1
if pos_y < 0:
pos_y = 0
return Coordinates(x=self.x, y=pos_y)