From 56314605ac72938e4eafc436a5f0e61f72c85a08 Mon Sep 17 00:00:00 2001 From: tonywesoly Date: Wed, 11 May 2022 13:07:26 +0200 Subject: [PATCH] =?UTF-8?q?Wykrywanie=20paczki,=20p=C3=B3=C5=82ki=20i=20sa?= =?UTF-8?q?modzielne=20poruszanie=20w=C3=B3zka?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Astar.py | 11 +++++++++-- Environment.py | 42 +++++++++++++++++------------------------- Finding_fields.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 Finding_fields.py diff --git a/Astar.py b/Astar.py index f37379b..57223cf 100644 --- a/Astar.py +++ b/Astar.py @@ -2,6 +2,7 @@ import math import pygame from Global_variables import Global_variables as G_var +from Package import Package from Shelf import Shelf @@ -90,7 +91,11 @@ class Pathfinding: fringe = [] explored = [] - + + is_target_node_walkable = True + if not target_node.walkable: + target_node.walkable = True + is_target_node_walkable = False fringe.append(start_node) while len(fringe) > 0: @@ -117,6 +122,8 @@ class Pathfinding: neighbour.parent = current_node if not neighbour in fringe: fringe.append(neighbour) + + target_node.walkable = is_target_node_walkable def get_distance(self, node_a, node_b): # funckja liczy dystans dla odległości między dwoma nodami dist_x = abs(node_a.state.x - node_b.state.x) @@ -148,4 +155,4 @@ class Pathfinding: ) pygame.draw.rect(window, color, - block) + block) \ No newline at end of file diff --git a/Environment.py b/Environment.py index f2be20f..972ffa5 100644 --- a/Environment.py +++ b/Environment.py @@ -1,4 +1,6 @@ +import time from Empty import Empty +from Finding_fields import Finding_fields from Moving_truck import Moving_truck from Package import Package from Package_types import Package_types @@ -23,14 +25,13 @@ class Environment: # TEST CREATE PACKAGE self.package_spawner = Packages_spawner(window,self.enviroment_2d) self.package_spawner.spawn_package() - # new_package = Package(self.window, 26, 7) - # self.enviroment_2d[26][7] = new_package new_truck = Truck(window, 14, 7) self.enviroment_2d[14][7] = new_truck self.truck = new_truck self.moving_truck = Moving_truck( self.window, self.enviroment_2d, self.truck, self.package_spawner) self.astar = Pathfinding(self.enviroment_2d) + self.finding_fields = Finding_fields(self.enviroment_2d) def draw_all_elements(self): for row in self.enviroment_2d: @@ -41,34 +42,25 @@ class Environment: pygame.display.flip() def update_all_elements(self,event): - # self.use_astar() - self.update_truck(event) + self.use_astar() # wywyoływanie za każdym razem astar jest bardzo zasobożerne. Lepiej raz na przejście + self.update_truck() + time.sleep(0.5) def use_astar(self): - start_state = State(1,self.truck.x,self.truck.y) - package = self.find_packate() - end_state = State(1,package.x, package.y) + start_state = State(1,self.truck.x,self.truck.y) # sprawić aby paczka i shelf były wyszukiwane raz + if self.truck.has_package: + end_position = self.finding_fields.find_closest_shelf(self.truck,self.truck.package_type) + else: + end_position = self.finding_fields.find_package() + end_state = State(1,end_position.x, end_position.y) self.astar.find_path(start_state,end_state) - def find_packate(self): #ta funkcja została zrobiona na szybko, może nie być potrzebna w przyszłości kiedy - #ktoś wpadnie na lepsze rozwiązanie - for row in self.enviroment_2d: - for field in row: - if isinstance(field,Package): - return field - return None - - def update_truck(self, event): - if event.type == KEYDOWN: - if event.key == K_LEFT: - self.moving_truck.move(-1, 0) - if event.key == K_RIGHT: - self.moving_truck.move(1, 0) - if event.key == K_UP: - self.moving_truck.move(0, -1) - if event.key == K_DOWN: - self.moving_truck.move(0, 1) + def update_truck(self): + next_field_to_move = self.astar.path[0].state + next_field_x = next_field_to_move.x - self.truck.x + next_field_y = next_field_to_move.y - self.truck.y + self.moving_truck.move(next_field_x,next_field_y) def gen_shelf_type(self): shelve_types = list(Package_types) diff --git a/Finding_fields.py b/Finding_fields.py new file mode 100644 index 0000000..3c170d4 --- /dev/null +++ b/Finding_fields.py @@ -0,0 +1,31 @@ +import math +from Empty import Empty +from Package import Package +from Shelf import Shelf + + +class Finding_fields: + def __init__(self, enviroment_2d): + self.enviroment_2d = enviroment_2d + + def find_closest_shelf(self, start_field, type): + shelves = [] + for row in self.enviroment_2d: + for field in row: + if isinstance(field,Shelf): + shelves.append(field) + min_distance = math.inf + closest_shelf = None + for shelf in shelves: + if shelf.type == type: + distance = abs(start_field.x - shelf.x) + abs(start_field.y - shelf.y) + if distance < min_distance: + min_distance = distance + closest_shelf = shelf + return closest_shelf + + def find_package(self): + for row in self.enviroment_2d: + for field in row: + if isinstance(field,Package) and not field.is_placed: + return field \ No newline at end of file