feat: added moveto agent class, x and y path finding functions

This commit is contained in:
MlodyJ 2024-04-12 14:11:05 +02:00
parent 32fbf3bdf7
commit fee1eddb8a
2 changed files with 32 additions and 4 deletions

27
app.py
View File

@ -98,15 +98,26 @@ else:
print("Brak sciezki") print("Brak sciezki")
#oddaje tablice punktow jako sciezke agenta
def convert_to_coordinates(shortest_path):
coordinates = [(cell.X, cell.Y) for cell in shortest_path]
return coordinates
path = convert_to_coordinates(shortest_path)
#Wyjmuje pierwsze koordynaty do ruszenia agenta a potem usuwa je z listy
def pop_first_coordinates(coordinates):
if coordinates:
x, y = coordinates.pop(0)
return x, y
else:
print("Lista współrzędnych jest pusta.")
return None, None
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells) agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
running = True running = True
while running: while running:
for event in pygame.event.get(): for event in pygame.event.get():
@ -132,6 +143,14 @@ while running:
agent.current_cell.interactableItem.interact(agent) agent.current_cell.interactableItem.interact(agent)
if not agent.moved:
first_x, first_y = pop_first_coordinates(path)
if first_x is not None and first_y is not None:
agent.moveto(first_x,first_y)
window.fill((255, 0, 0)) window.fill((255, 0, 0))
draw_grid(window, cells, agent) draw_grid(window, cells, agent)
agent.update(window) agent.update(window)

View File

@ -64,3 +64,12 @@ class Agent:
if self.multiplier < 1: if self.multiplier < 1:
self.multiplier = 1 self.multiplier = 1
def moveto(self,x,y):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X < prefs.GRID_SIZE-1 and not self.cells[x][y].blocking_movement:
self.current_cell = self.cells[x][y]
self.moved=True
self.last_move_time=pygame.time.get_ticks()
print("Agent moved to x,y: ",x,y)
else:
print("Agent cannot move to this direction")