59 lines
2.7 KiB
Python
59 lines
2.7 KiB
Python
from Empty import Empty
|
|
from Package import Package
|
|
from Shelf import Shelf
|
|
|
|
|
|
class Moving_truck:
|
|
def __init__(self, window, enviroment_2d, truck):
|
|
self.enviroment_2d = enviroment_2d
|
|
self.truck = truck
|
|
self.window = window
|
|
|
|
def move(self, x, y):
|
|
truck_x = self.truck.x
|
|
truck_y = self.truck.y
|
|
field_to_move_to = self.enviroment_2d[truck_x+x][truck_y+y]
|
|
if isinstance(field_to_move_to, Empty):
|
|
self.swap_fields(truck_x, truck_y, truck_x+x, truck_y+y)
|
|
elif isinstance(field_to_move_to, Package) and not field_to_move_to.is_placed:
|
|
self.move_truck_with_package(x, y)
|
|
|
|
def move_truck_with_package(self, x, y):
|
|
truck_x = self.truck.x
|
|
truck_y = self.truck.y
|
|
field_to_move_to = self.enviroment_2d[truck_x+x][truck_y+y]
|
|
field_to_move_package_to = self.enviroment_2d[truck_x+(
|
|
x*2)][truck_y+(y*2)]
|
|
if isinstance(field_to_move_package_to, Shelf) and field_to_move_to.type != field_to_move_package_to.type:
|
|
return
|
|
if isinstance(field_to_move_package_to, Shelf):
|
|
self.move_package_to_shelf(x, y)
|
|
else:
|
|
self.swap_fields(truck_x+x, truck_y+y,
|
|
truck_x+(x*2), truck_y+(y*2))
|
|
self.swap_fields(truck_x, truck_y, truck_x+x, truck_y+y)
|
|
|
|
def move_package_to_shelf(self, x, y):
|
|
truck_x = self.truck.x
|
|
truck_y = self.truck.y
|
|
package = self.enviroment_2d[truck_x+x][truck_y+y]
|
|
# self.enviroment_2d[truck_x+x][truck_y +
|
|
# y] = Placed_package(package)
|
|
self.enviroment_2d[truck_x+x][truck_y +
|
|
y].is_placed = True
|
|
self.move_without_swapping(
|
|
truck_x+x, truck_y+y, truck_x+(x*2), truck_y+(y*2))
|
|
self.move_without_swapping(truck_x, truck_y, truck_x+x, truck_y+y)
|
|
|
|
def swap_fields(self, x1, y1, x2, y2):
|
|
self.enviroment_2d[x1][y1], self.enviroment_2d[x2][y2] = self.enviroment_2d[x2][y2], self.enviroment_2d[x1][y1]
|
|
self.enviroment_2d[x1][y1].x, self.enviroment_2d[x2][y2].x = self.enviroment_2d[x2][y2].x, self.enviroment_2d[x1][y1].x
|
|
self.enviroment_2d[x1][y1].y, self.enviroment_2d[x2][y2].y = self.enviroment_2d[x2][y2].y, self.enviroment_2d[x1][y1].y
|
|
|
|
def move_without_swapping(self, initial_x, initial_y, final_x, final_y):
|
|
self.enviroment_2d[final_x][final_y] = self.enviroment_2d[initial_x][initial_y]
|
|
self.enviroment_2d[final_x][final_y].x = final_x
|
|
self.enviroment_2d[final_x][final_y].y = final_y
|
|
self.enviroment_2d[initial_x][initial_y] = Empty(
|
|
self.window, initial_x, initial_y)
|