AIprojekt-wozek/Moving_truck.py
2022-05-20 01:13:40 +02:00

59 lines
2.6 KiB
Python

from Empty import Empty
from Package import Package
from Shelf import Shelf
# TODO: DODAC OBSERWER ZAMIAST PRZEKAZYWANIE WSZYSTKICH BZDET
class Moving_truck:
def __init__(self, window, enviroment_2d, truck, package_spawner, a_star):
self.enviroment_2d = enviroment_2d
self.truck = truck
self.window = window
self.package_spawner = package_spawner
self.astar = a_star
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.pick_up_package(x, y)
elif isinstance(field_to_move_to, Shelf) and self.truck.has_package and field_to_move_to.type == self.truck.package_type:
self.move_package_to_shelf(x, y)
def pick_up_package(self, x, y):
truck_x = self.truck.x
truck_y = self.truck.y
package_x = truck_x+x
package_y = truck_y+y
package = self.enviroment_2d[package_x][package_y]
self.truck.has_package = True
self.truck.package_type = package.type
self.astar.reset_grid()
self.move_without_swapping(truck_x, truck_y, package_x, package_y)
def move_package_to_shelf(self, x, y):
truck_x = self.truck.x
truck_y = self.truck.y
self.enviroment_2d[truck_x+x][truck_y+y] = Package(
self.window, truck_x+x, truck_y+y, self.truck.package_type)
self.enviroment_2d[truck_x+x][truck_y +
y].is_placed = True
self.truck.has_package = False
self.package_spawner.spawn_package()
self.astar.reset_grid()
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)